X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcore%2Fcontroller.rs;h=de91928e0fdf2a1a19ad2a1963789198780d3d74;hb=bb3eb700e040846ca793832da4adc133218f2954;hp=9d23257f5a0a567e3f9651c719ed7aaeb588579b;hpb=eca2559123ae3c7ef184bf42ec60680fcddb38f6;p=kaka%2Frust-sdl-test.git diff --git a/src/core/controller.rs b/src/core/controller.rs index 9d23257..de91928 100644 --- a/src/core/controller.rs +++ b/src/core/controller.rs @@ -1,6 +1,5 @@ -use common::Point2D; +use geometry::{Angle, Point}; use {hashmap, point}; -use common::Radians; use sdl2::HapticSubsystem; use sdl2::JoystickSubsystem; use sdl2::event::Event; @@ -67,7 +66,7 @@ pub struct Stick { idy: u8, pub x: f32, pub y: f32, - pub a: Radians, + pub a: Angle, } impl Stick { @@ -80,29 +79,29 @@ impl Stick { Ok(val) => val as f32 / 32768.0, Err(_) => panic!("invalid y axis {}", self.idy), }; - self.a = Radians(self.y.atan2(self.x) as f64); + self.a = point!(self.x as f64, self.y as f64).to_angle(); } - #[inline(always)] #[allow(dead_code)] fn up(&self) -> bool { self.y > 0.99 } - #[inline(always)] #[allow(dead_code)] fn down(&self) -> bool { self.y < -0.99 } - #[inline(always)] #[allow(dead_code)] fn left(&self) -> bool { self.x < -0.99 } - #[inline(always)] #[allow(dead_code)] fn right(&self) -> bool { self.x > 0.99 } + #[inline(always)] #[allow(dead_code)] pub fn up(&self) -> bool { self.y < -0.99 } + #[inline(always)] #[allow(dead_code)] pub fn down(&self) -> bool { self.y > 0.99 } + #[inline(always)] #[allow(dead_code)] pub fn left(&self) -> bool { self.x < -0.99 } + #[inline(always)] #[allow(dead_code)] pub fn right(&self) -> bool { self.x > 0.99 } - pub fn to_axis_point(&self) -> Point2D { + pub fn to_axis_point(&self) -> Point { point!(self.x as f64, self.y as f64) } - pub fn to_point(&self) -> Point2D { + pub fn to_point(&self) -> Point { let p = point!(self.x as f64, self.y as f64); if p.length() > 1.0 { - p.normalize() + p.normalized() } else { p } } } -impl From<&Stick> for Point2D { +impl From<&Stick> for Point { fn from(item: &Stick) -> Self { Self { x: item.x as f64, @@ -143,6 +142,7 @@ enum DeviceControls { ButtonLeft, ButtonRight, } +use self::DeviceControls::*; #[derive(Eq, PartialEq, Hash)] enum ActionControls { @@ -154,6 +154,7 @@ enum ActionControls { Shoot, Start, } +use self::ActionControls::*; //#[derive(Debug)] pub struct Controller { @@ -185,13 +186,13 @@ impl Controller { } fn set_mapping(&mut self, action: &HashMap, device: &HashMap) { - self.mov.idx = *action.get(&ActionControls::MovementX).map(|i| device.get(i)).flatten().unwrap(); - self.mov.idy = *action.get(&ActionControls::MovementY).map(|i| device.get(i)).flatten().unwrap(); - self.aim.idx = *action.get(&ActionControls::AimX).map(|i| device.get(i)).flatten().unwrap(); - self.aim.idy = *action.get(&ActionControls::AimY).map(|i| device.get(i)).flatten().unwrap(); - self.jump.id = *action.get(&ActionControls::Jump).map(|i| device.get(i)).flatten().unwrap(); - self.shoot.id = *action.get(&ActionControls::Shoot).map(|i| device.get(i)).flatten().unwrap(); - self.start.id = *action.get(&ActionControls::Start).map(|i| device.get(i)).flatten().unwrap(); + self.mov.idx = *action.get(&MovementX).map(|i| device.get(i)).flatten().unwrap(); + self.mov.idy = *action.get(&MovementY).map(|i| device.get(i)).flatten().unwrap(); + self.aim.idx = *action.get(&AimX).map(|i| device.get(i)).flatten().unwrap(); + self.aim.idy = *action.get(&AimY).map(|i| device.get(i)).flatten().unwrap(); + self.jump.id = *action.get(&Jump).map(|i| device.get(i)).flatten().unwrap(); + self.shoot.id = *action.get(&Shoot).map(|i| device.get(i)).flatten().unwrap(); + self.start.id = *action.get(&Start).map(|i| device.get(i)).flatten().unwrap(); } pub fn update(&mut self, dt: Duration) { @@ -212,42 +213,42 @@ impl Controller { fn get_action_mapping() -> HashMap { hashmap!( - ActionControls::MovementX => DeviceControls::AxisLX, - ActionControls::MovementY => DeviceControls::AxisLY, - ActionControls::AimX => DeviceControls::AxisRX, - ActionControls::AimY => DeviceControls::AxisRY, - ActionControls::Jump => DeviceControls::ButtonL1, - ActionControls::Shoot => DeviceControls::ButtonR1, - ActionControls::Start => DeviceControls::ButtonStart + MovementX => AxisLX, + MovementY => AxisLY, + AimX => AxisRX, + AimY => AxisRY, + Jump => ButtonA, + Shoot => ButtonR1, + Start => ButtonStart ) } fn get_device_mapping(device_name: &str) -> HashMap { match device_name { "Sony PLAYSTATION(R)3 Controller" => hashmap!( - DeviceControls::AxisLX => 0, - DeviceControls::AxisLY => 1, - DeviceControls::AxisRX => 3, - DeviceControls::AxisRY => 4, - DeviceControls::AxisL2 => 2, - DeviceControls::AxisR2 => 5, - DeviceControls::ButtonA => 0, - DeviceControls::ButtonB => 1, - DeviceControls::ButtonY => 3, - DeviceControls::ButtonX => 2, - DeviceControls::ButtonSelect => 8, - DeviceControls::ButtonStart => 9, - DeviceControls::ButtonHome => 10, - DeviceControls::ButtonL3 => 11, - DeviceControls::ButtonR3 => 12, - DeviceControls::ButtonL1 => 4, - DeviceControls::ButtonR1 => 5, - DeviceControls::ButtonL2 => 6, - DeviceControls::ButtonR2 => 7, - DeviceControls::ButtonUp => 13, - DeviceControls::ButtonDown => 14, - DeviceControls::ButtonLeft => 15, - DeviceControls::ButtonRight => 16 + AxisLX => 0, + AxisLY => 1, + AxisRX => 3, + AxisRY => 4, + AxisL2 => 2, + AxisR2 => 5, + ButtonA => 0, + ButtonB => 1, + ButtonY => 3, + ButtonX => 2, + ButtonSelect => 8, + ButtonStart => 9, + ButtonHome => 10, + ButtonL3 => 11, + ButtonR3 => 12, + ButtonL1 => 4, + ButtonR1 => 5, + ButtonL2 => 6, + ButtonR2 => 7, + ButtonUp => 13, + ButtonDown => 14, + ButtonLeft => 15, + ButtonRight => 16 ), _ => panic!("No controller mapping for device '{}'", device_name) }