diff --git a/crates/shipbehavior/src/behavior/dummy.rs b/crates/shipbehavior/src/behavior/dummy.rs index 1f8ec4b..c684f09 100644 --- a/crates/shipbehavior/src/behavior/dummy.rs +++ b/crates/shipbehavior/src/behavior/dummy.rs @@ -10,8 +10,8 @@ pub struct Dummy { impl Dummy { /// Create a new ship controller - pub fn new(handle: ShipPhysicsHandle) -> Box { - Box::new(Self { _handle: handle }) + pub fn new(handle: ShipPhysicsHandle) -> Self { + Self { _handle: handle } } } diff --git a/crates/shipbehavior/src/behavior/player.rs b/crates/shipbehavior/src/behavior/player.rs index 26a1639..1983096 100644 --- a/crates/shipbehavior/src/behavior/player.rs +++ b/crates/shipbehavior/src/behavior/player.rs @@ -6,22 +6,30 @@ use galactica_world::{ShipPhysicsHandle, World}; /// Controls a ship using controller input pub struct Player { handle: ShipPhysicsHandle, - key_left: bool, - key_right: bool, - key_guns: bool, - key_thrust: bool, + + /// Turn left + pub key_left: bool, + + /// Turn right + pub key_right: bool, + + /// Fire guns + pub key_guns: bool, + + /// Foward thrust + pub key_thrust: bool, } impl Player { /// Make a new ship controller - pub fn new(handle: ShipPhysicsHandle) -> Box { - Box::new(Self { + pub fn new(handle: ShipPhysicsHandle) -> Self { + Self { handle, key_left: false, key_right: false, key_guns: false, key_thrust: false, - }) + } } } diff --git a/crates/shipbehavior/src/behavior/point.rs b/crates/shipbehavior/src/behavior/point.rs index 4888107..5039535 100644 --- a/crates/shipbehavior/src/behavior/point.rs +++ b/crates/shipbehavior/src/behavior/point.rs @@ -12,8 +12,8 @@ pub struct Point { impl Point { /// Create a new ship controller - pub fn new(handle: ShipPhysicsHandle) -> Box { - Box::new(Self { handle }) + pub fn new(handle: ShipPhysicsHandle) -> Self { + Self { handle } } } diff --git a/src/game/game.rs b/src/game/game.rs index 89200c4..e14f78e 100644 --- a/src/game/game.rs +++ b/src/game/game.rs @@ -79,6 +79,7 @@ pub struct Game { ui: Ui, physics: World, shipbehaviors: Vec>, + playerbehavior: behavior::Player, content: content::Content, } @@ -130,9 +131,8 @@ impl Game { ); let mut shipbehaviors: Vec> = Vec::new(); - shipbehaviors.push(behavior::Player::new(h1)); - shipbehaviors.push(behavior::Dummy::new(h2)); - shipbehaviors.push(behavior::Point::new(h3)); + shipbehaviors.push(Box::new(behavior::Dummy::new(h2))); + shipbehaviors.push(Box::new(behavior::Point::new(h3))); Game { last_update: Instant::now(), @@ -150,6 +150,7 @@ impl Game { physics, shipbehaviors, content: ct, + playerbehavior: behavior::Player::new(h1), ui: Ui::new(), } } @@ -178,7 +179,15 @@ impl Game { pub fn update(&mut self) { let t: f32 = self.last_update.elapsed().as_secs_f32() * self.time_scale; + self.playerbehavior.key_guns = self.input.key_guns; + self.playerbehavior.key_thrust = self.input.key_thrust; + self.playerbehavior.key_right = self.input.key_right; + self.playerbehavior.key_left = self.input.key_left; + self.playerbehavior + .update_controls(&mut self.physics, &self.content); + self.shipbehaviors.retain_mut(|b| { + // Remove shipbehaviors of destroyed ships if self.physics.get_ship_mut(&b.get_handle()).is_none() { false } else {