X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcore%2Fcontroller.rs;h=de91928e0fdf2a1a19ad2a1963789198780d3d74;hb=bb3eb700e040846ca793832da4adc133218f2954;hp=832480d6830686956003345f53c0fd0d63f0fa66;hpb=05ba09764d251b4dbf3d6572e1fe54bcbc154dda;p=kaka%2Frust-sdl-test.git diff --git a/src/core/controller.rs b/src/core/controller.rs index 832480d..de91928 100644 --- a/src/core/controller.rs +++ b/src/core/controller.rs @@ -1,6 +1,5 @@ -use common::Point2D; -use hashmap; -use common::Radians; +use geometry::{Angle, Point}; +use {hashmap, point}; use sdl2::HapticSubsystem; use sdl2::JoystickSubsystem; use sdl2::event::Event; @@ -67,8 +66,7 @@ pub struct Stick { idy: u8, pub x: f32, pub y: f32, - pub a: Radians, - pub len: f32, + pub a: Angle, } impl Stick { @@ -81,32 +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.len = { - let x = (self.x / self.y).abs().min(1.0); - let y = (self.y / self.x).abs().min(1.0); - (self.x.powi(2) + self.y.powi(2)).sqrt() / (x.powi(2) + y.powi(2)).sqrt() - } + 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_point(&self) -> Point2D { - Point2D { - x: self.x as f64, - y: self.y as f64, - } + pub fn to_axis_point(&self) -> Point { + point!(self.x as f64, self.y as f64) } - pub fn to_adjusted_point(&self) -> Point2D { - Point2D::from(self.a) * self.len as f64 + pub fn to_point(&self) -> Point { + let p = point!(self.x as f64, self.y as f64); + if p.length() > 1.0 { + 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, @@ -147,6 +142,7 @@ enum DeviceControls { ButtonLeft, ButtonRight, } +use self::DeviceControls::*; #[derive(Eq, PartialEq, Hash)] enum ActionControls { @@ -158,6 +154,7 @@ enum ActionControls { Shoot, Start, } +use self::ActionControls::*; //#[derive(Debug)] pub struct Controller { @@ -189,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) { @@ -216,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) }