Fixed player controller

master
Mark 2024-01-01 16:20:01 -08:00
parent fef4e5ed23
commit 3784aa3e08
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
4 changed files with 31 additions and 14 deletions

View File

@ -10,8 +10,8 @@ pub struct Dummy {
impl Dummy {
/// Create a new ship controller
pub fn new(handle: ShipPhysicsHandle) -> Box<Self> {
Box::new(Self { _handle: handle })
pub fn new(handle: ShipPhysicsHandle) -> Self {
Self { _handle: handle }
}
}

View File

@ -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<Self> {
Box::new(Self {
pub fn new(handle: ShipPhysicsHandle) -> Self {
Self {
handle,
key_left: false,
key_right: false,
key_guns: false,
key_thrust: false,
})
}
}
}

View File

@ -12,8 +12,8 @@ pub struct Point {
impl Point {
/// Create a new ship controller
pub fn new(handle: ShipPhysicsHandle) -> Box<Self> {
Box::new(Self { handle })
pub fn new(handle: ShipPhysicsHandle) -> Self {
Self { handle }
}
}

View File

@ -79,6 +79,7 @@ pub struct Game {
ui: Ui,
physics: World,
shipbehaviors: Vec<Box<dyn ShipBehavior>>,
playerbehavior: behavior::Player,
content: content::Content,
}
@ -130,9 +131,8 @@ impl Game {
);
let mut shipbehaviors: Vec<Box<dyn ShipBehavior>> = 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 {