use shadow_engine_2d::prelude::*;
use winit::keyboard::KeyCode;
#[derive(Clone, Copy)]
struct Velocity {
x: f32,
y: f32,
}
impl Velocity {
fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}
impl Component for Velocity {}
fn main() {
env_logger::init();
let mut engine = Engine::builder()
.title("Shadow Engine 2D - Basic Game")
.size(1280, 720)
.build();
let player = engine.world_mut().spawn();
if let Some(entity) = engine.world_mut().entity_mut(player) {
entity.add(Transform::new(0.0, 0.0));
entity.add(Sprite::new(50.0, 50.0).with_color(Color::CYAN));
entity.add(Velocity::new(0.0, 0.0));
}
for i in 0..5 {
let obstacle = engine.world_mut().spawn();
if let Some(entity) = engine.world_mut().entity_mut(obstacle) {
entity.add(Transform::new(-300.0 + i as f32 * 150.0, 200.0));
entity.add(Sprite::new(40.0, 40.0).with_color(Color::RED));
}
}
engine.run(move |world, input, time| {
let speed = 300.0;
let dt = time.delta();
for entity in world.entities_mut() {
if entity.has::<Velocity>() {
if let Some(velocity) = entity.get_mut::<Velocity>() {
velocity.x = 0.0;
velocity.y = 0.0;
if input.key_pressed(KeyCode::KeyW) || input.key_pressed(KeyCode::ArrowUp) {
velocity.y = -speed;
}
if input.key_pressed(KeyCode::KeyS) || input.key_pressed(KeyCode::ArrowDown) {
velocity.y = speed;
}
if input.key_pressed(KeyCode::KeyA) || input.key_pressed(KeyCode::ArrowLeft) {
velocity.x = -speed;
}
if input.key_pressed(KeyCode::KeyD) || input.key_pressed(KeyCode::ArrowRight) {
velocity.x = speed;
}
}
let vel = entity.get::<Velocity>().copied();
if let (Some(transform), Some(velocity)) =
(entity.get_mut::<Transform>(), vel) {
transform.position.x += velocity.x * dt;
transform.position.y += velocity.y * dt;
}
}
}
});
}