2025-03-11 17:58:40 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2025-03-11 12:49:23 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
|
|
|
|
use ecs::*;
|
|
|
|
use engine::prelude::*;
|
|
|
|
|
2025-03-11 17:58:40 +00:00
|
|
|
#[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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-11 12:49:23 +00:00
|
|
|
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<()> {
|
2025-03-11 17:58:40 +00:00
|
|
|
let player = world.resources.get::<PlayerEntity>();
|
|
|
|
let fighter_object = world.entity_mut(player.0)?;
|
|
|
|
|
2025-03-11 12:49:23 +00:00
|
|
|
match event {
|
|
|
|
EngineEvent::MouseButtonDown(MouseButton::Left) => {
|
|
|
|
let camera_control = world.resources.get_mut::<FreeCameraControl>();
|
|
|
|
camera_control.mouse_down();
|
|
|
|
}
|
|
|
|
EngineEvent::MouseButtonUp(MouseButton::Left) => {
|
|
|
|
let camera_control = world.resources.get_mut::<FreeCameraControl>();
|
|
|
|
camera_control.mouse_release();
|
|
|
|
}
|
|
|
|
EngineEvent::MouseMotion(x, y) => {
|
|
|
|
let mut resources = world.resources.multi_mut();
|
|
|
|
let scene = resources.get::<Scene>();
|
|
|
|
let camera_control = resources.get::<FreeCameraControl>();
|
|
|
|
|
|
|
|
camera_control.mouse_move(x, y, scene.view_mut())?;
|
|
|
|
}
|
|
|
|
|
2025-03-11 17:58:40 +00:00
|
|
|
EngineEvent::ControllerAxis(controller, axis) => {}
|
|
|
|
|
2025-03-11 12:49:23 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Game {
|
|
|
|
pub fn setup_updates(world_builder: &mut WorldBuilder) -> Result<()> {
|
2025-03-11 17:58:40 +00:00
|
|
|
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)?;
|
|
|
|
}
|
2025-03-11 12:49:23 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|