feat: added example simple_move_2d

This commit is contained in:
lisk77 2025-03-11 10:57:07 +01:00
parent ab73b145b1
commit ae50603703
3 changed files with 79 additions and 6 deletions

View file

@ -7,8 +7,8 @@ Simply run
cargo run --example <example_name>
```
| 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. |

View file

@ -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::<Transform2D>(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::<Renderer2D>(setup, update);
}

View file

@ -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::*;