From ae5060370347a4b8523b66ca7e8568c7b0afd7f5 Mon Sep 17 00:00:00 2001 From: lisk77 Date: Tue, 11 Mar 2025 10:57:07 +0100 Subject: [PATCH] feat: added example `simple_move_2d` --- examples/README.md | 10 +++--- examples/simple_move_2d.rs | 73 ++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- 3 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 examples/simple_move_2d.rs diff --git a/examples/README.md b/examples/README.md index 31f7f8d..7c353da 100644 --- a/examples/README.md +++ b/examples/README.md @@ -7,8 +7,8 @@ Simply run cargo run --example ``` -| Example | Description | -|---------------------------------------------------|-----------------------------------------------------------------------------------------| -| [hello_world](comet/examples/hello_world) | A simple boilerplate example to show how to properly start creating a Comet App. | -| [textured_entity](comet/examples/textured_entity) | This covers the basics on how to create a camera and your first entity with a texture | - +| Example | Description | +|------------------------------------|----------------------------------------------------------------------------------------| +| [hello_world](hello_world) | A simple boilerplate example to show how to properly start creating a Comet App. | +| [textured_entity](textured_entity) | This covers the basics on how to create a camera and your first entity with a texture. | +| [simple_move_2d](simple_move_2d) | Simple demonstration of a hypothetical movement system in 2D. | \ No newline at end of file diff --git a/examples/simple_move_2d.rs b/examples/simple_move_2d.rs new file mode 100644 index 0000000..10e4251 --- /dev/null +++ b/examples/simple_move_2d.rs @@ -0,0 +1,73 @@ +use comet::prelude::*; +use winit_input_helper::WinitInputHelper; +use comet_input::keyboard::Key; + +fn setup(app: &mut App, renderer: &mut Renderer2D) { + // Takes all the textures from resources/textures and puts them into a texture atlas + renderer.initialize_atlas(); + + let camera = app.new_entity(); + app.add_component(camera, Transform2D::new()); + app.add_component(camera, Camera2D::new(Vec2::new(2.0, 2.0), 1.0, 1)); + + let e1 = app.new_entity(); + + app.add_component(e1, Transform2D::new()); + + let mut renderer2d = Render2D::new(); + renderer2d.set_texture(r"resources/textures/comet_icon.png"); + renderer2d.set_visibility(true); + + app.add_component(e1, renderer2d); +} + +fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) { + handle_input(app, dt); + + renderer.render_scene_2d(app.scene()); +} + +fn handle_input(app: &mut App, dt: f32) { + if app.key_held(Key::KeyW) + || app.key_held(Key::KeyA) + || app.key_held(Key::KeyS) + || app.key_held(Key::KeyD) + { + update_position( + app.input_manager().clone(), + app.get_component_mut::(1).unwrap(), + dt + ); + } +} + +fn update_position(input: WinitInputHelper, transform: &mut Transform2D, dt: f32) { + let mut direction = Vec2::ZERO; + + if input.key_held(Key::KeyW) { + direction += Vec2::Y; + } + if input.key_held(Key::KeyA) { + direction -= Vec2::X; + } + if input.key_held(Key::KeyS) { + direction -= Vec2::Y; + } + if input.key_held(Key::KeyD) { + direction += Vec2::X; + } + + // If check to prevent division by zero and the comet to fly off into infinity... + if direction != Vec2::ZERO { + let normalized_dir = direction.normalize(); + let displacement = normalized_dir * 777.7 * dt; + transform.translate(displacement); + } +} + +fn main() { + App::new() + .with_title("Hello world") + .with_preset(App2D) + .run::(setup, update); +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index aa8afee..6dd0f2b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ pub mod prelude { pub use comet_app::App; pub use comet_app::ApplicationType::App2D; pub use comet_renderer::renderer2d::Renderer2D; - pub use comet_input::input_handler; + pub use comet_input::*; pub use comet_log::*; pub use comet_colors::*; pub use comet_ecs::*;