From 56c2f0de888e7f8b1aebda2423c41a93d2004f6e Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 26 Jan 2023 03:05:29 +0100 Subject: [PATCH] fuck --- src/camera.rs | 12 +++++-- src/fly_controller.rs | 23 +++++++++++--- src/prefabs.rs | 6 ++-- src/world/block.rs | 73 +++++++++++++++++++++++++++++++++++++++++++ src/world/worldgen.rs | 1 + 5 files changed, 105 insertions(+), 10 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index f05935b..6405e04 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,4 +1,4 @@ -use glam::{Mat4, Vec3}; +use glam::{Mat4, Vec3, EulerRot}; use shipyard::{Component, ViewMut, View, IntoIter, Workload, IntoWorkload}; use std::f32::consts::PI; use crate::{transform::Transform, events::WindowResizedEvent}; @@ -40,8 +40,14 @@ fn update_view_matrix( ) { for (camera, transform) in (&mut vm_camera, v_transform.inserted_or_modified()).iter() { let (_, rotation, translation) = transform.0.to_scale_rotation_translation(); - let dir = rotation * Vec3::Z; //this may be incorrect! - camera.view_matrix = Mat4::look_to_rh(translation, dir, camera.up); + //let direction = rotation * Vec3::Z; //this may be incorrect! + let (yaw, pitch, _) = rotation.to_euler(EulerRot::YXZ); + let direction= Vec3::new( + yaw.cos() * pitch.cos(), + pitch.sin(), + yaw.sin() * pitch.cos() + ); + camera.view_matrix = Mat4::look_to_rh(translation, direction, camera.up); } } diff --git a/src/fly_controller.rs b/src/fly_controller.rs index 51f4285..b6f35f4 100644 --- a/src/fly_controller.rs +++ b/src/fly_controller.rs @@ -1,4 +1,4 @@ -use glam::{Vec3, Mat4, EulerRot, Quat}; +use glam::{Vec3, Mat4, Quat, EulerRot}; use shipyard::{Component, View, ViewMut, IntoIter, UniqueView}; use crate::{transform::Transform, input::Inputs, settings::GameSettings}; @@ -12,9 +12,24 @@ pub fn update_controllers( settings: UniqueView, ) { for (_, mut transform) in (&controllers, &mut transforms).iter() { - let (scale, mut rotation, translation) = transform.0.to_scale_rotation_translation(); - let look = inputs.look * settings.mouse_sensitivity * -1.; - rotation *= Quat::from_euler(EulerRot::YXZ, look.x, look.y, 0.); + let (scale, mut rotation, mut translation) = transform.0.to_scale_rotation_translation(); + let look = inputs.look * settings.mouse_sensitivity; + + //rotation *= Quat::from_axis_angle(Vec3::Y, look.x); + + //old way + // rotation = rotation.normalize(); + // rotation *= Quat::from_euler(EulerRot::ZYX, 0., look.x, look.y).normalize(); + // rotation = rotation.normalize(); + + // let direction = (rotation * Vec3::Z).normalize(); + // let camera_right = Vec3::Y.cross(direction).normalize(); + // let camera_up = direction.cross(camera_right); + // rotation *= Quat::from_axis_angle(Vec3::Y, look.x); + // rotation *= Quat::from_axis_angle(camera_right, look.y); + + //translation += (rotation * Vec3::X) / 4.; + transform.0 = Mat4::from_scale_rotation_translation(scale, rotation, translation); } } diff --git a/src/prefabs.rs b/src/prefabs.rs index 6043bcf..fc1e075 100644 --- a/src/prefabs.rs +++ b/src/prefabs.rs @@ -15,7 +15,7 @@ pub trait AssetPaths { #[derive(Clone, Copy, Debug, EnumIter)] #[repr(u8)] -pub enum BlockTextures { +pub enum BlockTexture { Stone = 0, Dirt = 1, GrassTop = 2, @@ -30,7 +30,7 @@ pub enum BlockTextures { Snow = 11, GrassSideSnow = 12, } -impl AssetPaths for BlockTextures { +impl AssetPaths for BlockTexture { fn file_name(self) -> &'static str { match self { Self::Stone => "stone.png", @@ -61,7 +61,7 @@ pub fn load_prefabs( renderer: NonSendSync> ) { storages.add_unique_non_send_sync(BlockTexturesPrefab( - load_texture2darray_prefab::( + load_texture2darray_prefab::( "./assets/blocks/".into(), &renderer.display, MipmapsOption::AutoGeneratedMipmaps diff --git a/src/world/block.rs b/src/world/block.rs index ced61a8..dc3de5d 100644 --- a/src/world/block.rs +++ b/src/world/block.rs @@ -1,4 +1,5 @@ use strum::EnumIter; +use crate::prefabs::BlockTexture; #[derive(Clone, Copy, Debug, PartialEq, Eq, EnumIter)] #[repr(u8)] @@ -9,3 +10,75 @@ pub enum Block { Grass, Sand, } +impl Block { + pub const fn descriptor(self) -> BlockDescriptor { + match self { + Self::Air => BlockDescriptor { + name: "air", + render: RenderType::None, + collision: CollisionType::None, + raycast_collision: false, + }, + Self::Stone => BlockDescriptor { + name: "stone", + render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Stone)), + collision: CollisionType::Solid, + raycast_collision: true, + }, + _ => todo!() + } + } +} + +#[derive(Clone, Copy, Debug)] +pub struct BlockDescriptor { + pub name: &'static str, + pub render: RenderType, + pub collision: CollisionType, + pub raycast_collision: bool, +} +impl BlockDescriptor { + pub fn of(block: Block) -> Self { + block.descriptor() + } +} + +#[derive(Clone, Copy, Debug)] +pub struct CubeTexture { + pub top: BlockTexture, + pub bottom: BlockTexture, + pub left: BlockTexture, + pub right: BlockTexture, + pub front: BlockTexture, + pub back: BlockTexture, +} +impl CubeTexture { + pub const fn top_sides_bottom(top: BlockTexture, sides: BlockTexture, bottom: BlockTexture) -> Self { + Self { + top, + bottom, + left: sides, + right: sides, + front: sides, + back: sides, + } + } + pub const fn horizontal_vertical(horizontal: BlockTexture, vertical: BlockTexture) -> Self { + Self::top_sides_bottom(vertical, horizontal, vertical) + } + pub const fn all(texture: BlockTexture) -> Self { + Self::horizontal_vertical(texture, texture) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum CollisionType { + None, + Solid, +} + +#[derive(Clone, Copy, Debug)] +pub enum RenderType { + None, + SolidBlock(CubeTexture) +} diff --git a/src/world/worldgen.rs b/src/world/worldgen.rs index a3b17c5..ba6c4e0 100644 --- a/src/world/worldgen.rs +++ b/src/world/worldgen.rs @@ -9,6 +9,7 @@ pub fn generate_world(_position: IVec3, _seed: u32) -> BlockData { blocks[0][0][0] = Block::Stone; blocks[1][0][0] = Block::Stone; blocks[0][1][0] = Block::Stone; + blocks[0][2][0] = Block::Stone; blocks[0][0][1] = Block::Stone; //TODO actual world generation blocks