use std::collections::HashMap;
use anyhow::Result;
use ecs::*;
use engine::prelude::{
cgmath::{Vector3, Vector4, vec3},
*,
};
use crate::{FREE_CAMERA_CONTROL, celestial_object::*};
#[derive(Clone, Copy, Debug, Resource)]
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, Resource)]
struct InputSettings {
mappings: HashMap<(String, u32, Input), (Control, bool)>,
}
impl Default for InputSettings {
fn default() -> Self {
Self {
mappings: [
(
("Thrustmaster T.16000M".to_string(), 1, Input::Axis(0)),
(Control::Pitch, false),
),
(
("Thrustmaster T.16000M".to_string(), 1, Input::Axis(1)),
(Control::Throttle, true),
),
(
("Thrustmaster T.16000M".to_string(), 1, Input::Axis(2)),
(Control::StrafeHorizontal, false),
),
(
("Thrustmaster T.16000M".to_string(), 1, Input::Axis(3)),
(Control::StrafeVertical, false),
),
(
(
"Thrustmaster T.16000M".to_string(),
1,
Input::Button(Button::One),
),
(Control::PrimaryWeapon, false),
),
(
("Thrustmaster T.16000M".to_string(), 0, Input::Axis(0)),
(Control::Yaw, true),
),
(
("Thrustmaster T.16000M".to_string(), 0, Input::Axis(1)),
(Control::Roll, false),
),
(
(
"Thrustmaster T.16000M".to_string(),
0,
Input::Button(Button::One),
),
(Control::SecondaryWeapon, false),
),
]
.into_iter()
.collect(),
}
}
}
impl InputSettings {
pub fn map_axis(
&self,
device_name: impl ToString,
device_id: u32,
axis: u8,
) -> Option<(Control, bool)> {
self.mappings
.get(&(device_name.to_string(), device_id, Input::Axis(axis)))
.map(|control| *control)
}
}
pub struct Game;
impl Game {
pub fn update(&mut self, world: &mut World) -> Result<()> {
if FREE_CAMERA_CONTROL {
let now = world.now();
let (scene, camera_control): (&mut Scene, &mut FreeCameraControl) =
world.resources.get_mut()?;
camera_control.update(now, scene.view_mut())?;
}
Ok(())
}
pub fn event(&mut self, world: &mut World, event: EngineEvent<'_>) -> Result<()> {
if let Some(event) = Self::motion_concepts(world, event)? {
match event {
EngineEvent::JoystickAdded(joystick) => {
println!("joystick {} added", joystick.name());
}
EngineEvent::JoystickRemoved(joystick) => {
println!("joystick {} removed", joystick.name());
}
_ => (),
}
}
Ok(())
}
fn motion_concepts<'a>(
world: &mut World,
event: EngineEvent<'a>,
) -> Result