summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-04-16 23:02:01 +0200
committerHampusM <hampus@hampusmat.com>2024-04-16 23:02:01 +0200
commit7da31da573f6bbc044d28bd961f67d0db90f8d0e (patch)
tree35076d3ea9305ab629d6d39cad125daf5586751e
parent188c413d6693b3e649820e3f6d9f0842ebba0ce3 (diff)
refactor: use fly camera extension
-rw-r--r--src/main.rs165
1 files changed, 25 insertions, 140 deletions
diff --git a/src/main.rs b/src/main.rs
index 03fb441..f91e0c7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,13 +1,13 @@
use std::error::Error;
use std::path::Path;
+use engine::camera::fly::{Extension as FlyCameraExtension, Options as FlyCameraOptions};
use engine::camera::Camera;
-use engine::delta_time::DeltaTime;
+use engine::data_types::dimens::Dimens;
use engine::ecs::sole::Single;
-use engine::ecs::Query;
-use engine::event::Update as UpdateEvent;
-use engine::input::{Extension as InputExtension, Keys};
-use engine::lighting::{LightSource, LightSourceBuilder};
+use engine::event::Start as StartEvent;
+use engine::input::Extension as InputExtension;
+use engine::lighting::LightSourceBuilder;
use engine::material::Builder as MaterialBuilder;
use engine::renderer::Extension as RendererExtension;
use engine::shader::Program as ShaderProgram;
@@ -15,7 +15,7 @@ use engine::texture::{List as TextureList, Texture};
use engine::transform::Transform;
use engine::vector::{Vec2, Vec3};
use engine::vertex::Builder as VertexBuilder;
-use engine::window::{Extension as WindowExtension, Key, KeyState};
+use engine::window::{CursorMode, Extension as WindowExtension, Window};
use engine::Engine;
use tracing::Level;
use tracing_subscriber::FmtSubscriber;
@@ -24,93 +24,7 @@ use crate::cube::{create_cube_mesh, Corner as CubeCorner, Side as CubeSide};
mod cube;
-const CAM_SPEED: f32 = 3.0;
-
-const LIGHT_SOURCE_SPEED: f32 = 1.2;
-
-fn lmao(
- camera_query: Query<(Camera,)>,
- light_source_query: Query<(LightSource,)>,
- keys: Single<Keys>,
- delta_time: Single<DeltaTime>,
-)
-{
- let (mut camera,) = camera_query.iter().next().expect("No camera");
-
- let (mut light_source,) = light_source_query.iter().next().expect("No light source");
-
- let delta_time = delta_time.duration;
-
- if matches!(keys.get_key_state(Key::W), KeyState::Pressed) {
- let cam_target_direction = camera.target_direction().clone();
-
- camera.position += cam_target_direction * CAM_SPEED * delta_time.as_secs_f32();
- }
-
- if matches!(keys.get_key_state(Key::S), KeyState::Pressed) {
- let rev_cam_target_direction = -camera.target_direction().clone();
-
- camera.position +=
- rev_cam_target_direction * CAM_SPEED * delta_time.as_secs_f32();
- }
-
- if matches!(keys.get_key_state(Key::A), KeyState::Pressed) {
- let cam_left = camera.left().clone();
-
- // Camera speed adjusted to be same no matter how far the distance is to the
- // camera target
- let cam_speed_dist_adj =
- CAM_SPEED * (camera.position.clone() - camera.target.clone()).length();
-
- camera.position += cam_left * cam_speed_dist_adj * delta_time.as_secs_f32();
- }
-
- if matches!(keys.get_key_state(Key::D), KeyState::Pressed) {
- let cam_right = camera.right().clone();
-
- // Camera speed adjusted to be same no matter how far the distance is to the
- // camera target
- let cam_speed_dist_adj =
- CAM_SPEED * (camera.position.clone() - camera.target.clone()).length();
-
- camera.position += cam_right * cam_speed_dist_adj * delta_time.as_secs_f32();
- }
-
- if matches!(keys.get_key_state(Key::K), KeyState::Pressed) {
- let cam_up = camera.up().clone();
-
- // Camera speed adjusted to be same no matter how far the distance is to the
- // camera target
- let cam_speed_dist_adj =
- CAM_SPEED * (camera.position.clone() - camera.target.clone()).length();
-
- camera.position += cam_up * cam_speed_dist_adj * delta_time.as_secs_f32();
- }
-
- if matches!(keys.get_key_state(Key::J), KeyState::Pressed) {
- let cam_up = camera.down().clone();
-
- // Camera speed adjusted to be same no matter how far the distance is to the
- // camera target
- let cam_speed_dist_adj =
- CAM_SPEED * (camera.position.clone() - camera.target.clone()).length();
-
- camera.position += cam_up * cam_speed_dist_adj * delta_time.as_secs_f32();
- }
-
- if matches!(keys.get_key_state(Key::O), KeyState::Pressed) {
- let front_right = Vec3 { x: 1.0, y: 0.0, z: 1.0 };
-
- light_source
- .translate(front_right * LIGHT_SOURCE_SPEED * delta_time.as_secs_f32());
- }
-
- if matches!(keys.get_key_state(Key::L), KeyState::Pressed) {
- let back_left = Vec3 { x: -1.0, y: 0.0, z: -1.0 };
-
- light_source.translate(back_left * LIGHT_SOURCE_SPEED * delta_time.as_secs_f32());
- }
-}
+const WINDOW_SIZE: Dimens<u32> = Dimens { width: 1920, height: 1080 };
fn main() -> Result<(), Box<dyn Error>>
{
@@ -150,10 +64,21 @@ fn main() -> Result<(), Box<dyn Error>>
engine.spawn((Camera { current: true, ..Default::default() },));
- engine.register_system(UpdateEvent, lmao);
+ engine.register_system(StartEvent, prepare_window);
engine.add_extension(RendererExtension::default());
- engine.add_extension(WindowExtension::default().window_title("Game"));
+
+ engine.add_extension(
+ WindowExtension::default()
+ .window_title("Game")
+ .window_size(WINDOW_SIZE),
+ );
+
+ engine.add_extension(FlyCameraExtension(FlyCameraOptions {
+ camera_speed: 3.0,
+ mouse_sensitivity: 0.2,
+ }));
+
engine.add_extension(InputExtension::default());
engine.start();
@@ -161,6 +86,11 @@ fn main() -> Result<(), Box<dyn Error>>
Ok(())
}
+fn prepare_window(window: Single<Window>)
+{
+ window.set_cursor_mode(CursorMode::Disabled).unwrap();
+}
+
fn cube_vertex_builder_cb(
vertex_builder: VertexBuilder,
_side: CubeSide,
@@ -174,48 +104,3 @@ fn cube_vertex_builder_cb(
CubeCorner::BottomLeft => Vec2 { x: 0.0, y: 0.0 },
})
}
-
-trait CameraExt
-{
- fn target_direction(&self) -> Vec3<f32>;
-
- fn right(&self) -> Vec3<f32>;
-
- fn left(&self) -> Vec3<f32>;
-
- fn up(&self) -> Vec3<f32>;
-
- fn down(&self) -> Vec3<f32>;
-}
-
-impl CameraExt for Camera
-{
- fn target_direction(&self) -> Vec3<f32>
- {
- -(&self.position - &self.target).normalize()
- }
-
- fn right(&self) -> Vec3<f32>
- {
- let rev_target_direction = (&self.position - &self.target).normalize();
-
- Vec3::UP.cross(&rev_target_direction).normalize()
- }
-
- fn left(&self) -> Vec3<f32>
- {
- -self.right()
- }
-
- fn up(&self) -> Vec3<f32>
- {
- let rev_target_direction = (&self.position - &self.target).normalize();
-
- rev_target_direction.cross(&self.right())
- }
-
- fn down(&self) -> Vec3<f32>
- {
- -self.up()
- }
-}