use bevy::window::{PresentMode, WindowResolution}; use bevy::{prelude::*, winit::WinitSettings}; #[derive(Component)] enum Direction { Up, Down, Left, Right, None, } fn main() { App::new() .add_plugins(DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { title: "I am a window!".into(), resolution: WindowResolution::new(500., 300.).with_scale_factor_override(1.0), present_mode: PresentMode::AutoVsync, // Tells wasm to resize the window according to the available canvas fit_canvas_to_parent: true, // Tells wasm not to override default event handling, like F5, Ctrl+R etc. prevent_default_event_handling: false, ..default() }), ..default() })) .add_plugins(EnemyPlugin) .insert_resource(ClearColor(Color::srgb(0.5, 0.5, 0.9))) .insert_resource(WinitSettings::game()) .add_systems(Startup, setup_player) .add_systems(Update, player_movements) .run(); } fn offset_to_velocity(abs_v: f32, tr: Vec3, tr_target: Vec3) -> Vec2 { let dx = tr_target.x - tr.x; let dy = tr_target.y - tr.y; let v = Vec2 { x: dx, y: dy }.normalize_or_zero(); Vec2 { x: v.x * abs_v, y: v.y * abs_v, } } fn map_key_to_movement(key: &KeyCode) -> Direction { match key { KeyCode::KeyW => Direction::Up, KeyCode::KeyS => Direction::Down, KeyCode::KeyA => Direction::Left, KeyCode::KeyD => Direction::Right, _ => Direction::None, } } pub struct EnemyPlugin; impl Plugin for EnemyPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, setup_enemy) .add_systems(Update, enemy_follow); } fn name(&self) -> &str { "Enemy plugin" } } #[derive(Debug)] enum EntityType { Player, Enemy, } #[derive(Component)] pub struct Player { name: String, } #[derive(Component)] pub struct Enemy { name: String, } fn enemy_follow( time: Res