use std::collections::HashMap; use anyhow::Result; use ecs::*; use engine::prelude::*; #[derive(Clone, Copy, Debug)] struct PlayerEntity(Entity); #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] enum Input { Axis(u8), Button(Button), } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] enum Button { One, } #[derive(Clone, Copy, Debug)] enum Control { Throttle, StrafeHorizontal, StrafeVertical, Yaw, Pitch, Roll, PrimaryWeapon, SecondaryWeapon, } #[derive(Clone, Debug)] struct ControllerSettings { mappings: HashMap<(String, Input), Control>, } impl Default for ControllerSettings { fn default() -> Self { Self { mappings: [ ( ("Controller 1".to_string(), Input::Axis(0)), Control::Throttle, ), ( ("Controller 1".to_string(), Input::Axis(1)), Control::StrafeHorizontal, ), ( ("Controller 1".to_string(), Input::Axis(2)), Control::StrafeVertical, ), (("Controller 1".to_string(), Input::Axis(3)), Control::Pitch), ( ("Controller 1".to_string(), Input::Button(Button::One)), Control::PrimaryWeapon, ), (("Controller 2".to_string(), Input::Axis(1)), Control::Yaw), (("Controller 2".to_string(), Input::Axis(0)), Control::Roll), ( ("Controller 2".to_string(), Input::Button(Button::One)), Control::SecondaryWeapon, ), ] .into_iter() .collect(), } } } impl ControllerSettings { pub fn map_axis( &self, controller_name: impl ToString, axes: ControllerAxis, ) -> Vec<(Control, f32)> { self.mappings .get(&controller_name.to_string()) .map(|control| *control) } } pub struct Game; impl Game { pub fn update(&mut self, _world: &mut World) -> Result<()> { Ok(()) } pub fn event(&mut self, world: &mut World, event: EngineEvent<'_>) -> Result<()> { let player = world.resources.get::(); let fighter_object = world.entity_mut(player.0)?; match event { EngineEvent::MouseButtonDown(MouseButton::Left) => { let camera_control = world.resources.get_mut::(); camera_control.mouse_down(); } EngineEvent::MouseButtonUp(MouseButton::Left) => { let camera_control = world.resources.get_mut::(); camera_control.mouse_release(); } EngineEvent::MouseMotion(x, y) => { let mut resources = world.resources.multi_mut(); let scene = resources.get::(); let camera_control = resources.get::(); camera_control.mouse_move(x, y, scene.view_mut())?; } EngineEvent::ControllerAxis(controller, axis) => {} _ => (), } Ok(()) } } impl Game { pub fn setup_updates(world_builder: &mut WorldBuilder) -> Result<()> { world_builder.add_update( "player_rotation", 200, Self::player_orientation, EmptyFilter, )?; Ok(()) } pub fn setup_scene(world: &mut World) -> Result<()> { let mut fighter = AssetHandler::create(world).create_entity("fighter")?; fighter.insert_component(FreeSpaceControl::new(FreeSpaceControlSettings::default())); let player = PlayerEntity(world.add_entity(fighter)?); world.resources.insert(player); world.commit_entity_changes() } } // updates impl Game { fn player_orientation( world: &mut World, _entity: Entity, draw: &mut Draw, control: &mut FreeSpaceControl, ) -> Result<()> { if let Some(transform) = control.update(world.now()) { draw.set_transform(transform)?; } Ok(()) } }