Fixed player controller
parent
fef4e5ed23
commit
3784aa3e08
|
@ -10,8 +10,8 @@ pub struct Dummy {
|
||||||
|
|
||||||
impl Dummy {
|
impl Dummy {
|
||||||
/// Create a new ship controller
|
/// Create a new ship controller
|
||||||
pub fn new(handle: ShipPhysicsHandle) -> Box<Self> {
|
pub fn new(handle: ShipPhysicsHandle) -> Self {
|
||||||
Box::new(Self { _handle: handle })
|
Self { _handle: handle }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,22 +6,30 @@ use galactica_world::{ShipPhysicsHandle, World};
|
||||||
/// Controls a ship using controller input
|
/// Controls a ship using controller input
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
handle: ShipPhysicsHandle,
|
handle: ShipPhysicsHandle,
|
||||||
key_left: bool,
|
|
||||||
key_right: bool,
|
/// Turn left
|
||||||
key_guns: bool,
|
pub key_left: bool,
|
||||||
key_thrust: bool,
|
|
||||||
|
/// Turn right
|
||||||
|
pub key_right: bool,
|
||||||
|
|
||||||
|
/// Fire guns
|
||||||
|
pub key_guns: bool,
|
||||||
|
|
||||||
|
/// Foward thrust
|
||||||
|
pub key_thrust: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Player {
|
impl Player {
|
||||||
/// Make a new ship controller
|
/// Make a new ship controller
|
||||||
pub fn new(handle: ShipPhysicsHandle) -> Box<Self> {
|
pub fn new(handle: ShipPhysicsHandle) -> Self {
|
||||||
Box::new(Self {
|
Self {
|
||||||
handle,
|
handle,
|
||||||
key_left: false,
|
key_left: false,
|
||||||
key_right: false,
|
key_right: false,
|
||||||
key_guns: false,
|
key_guns: false,
|
||||||
key_thrust: false,
|
key_thrust: false,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,8 +12,8 @@ pub struct Point {
|
||||||
|
|
||||||
impl Point {
|
impl Point {
|
||||||
/// Create a new ship controller
|
/// Create a new ship controller
|
||||||
pub fn new(handle: ShipPhysicsHandle) -> Box<Self> {
|
pub fn new(handle: ShipPhysicsHandle) -> Self {
|
||||||
Box::new(Self { handle })
|
Self { handle }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -79,6 +79,7 @@ pub struct Game {
|
||||||
ui: Ui,
|
ui: Ui,
|
||||||
physics: World,
|
physics: World,
|
||||||
shipbehaviors: Vec<Box<dyn ShipBehavior>>,
|
shipbehaviors: Vec<Box<dyn ShipBehavior>>,
|
||||||
|
playerbehavior: behavior::Player,
|
||||||
content: content::Content,
|
content: content::Content,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,9 +131,8 @@ impl Game {
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut shipbehaviors: Vec<Box<dyn ShipBehavior>> = Vec::new();
|
let mut shipbehaviors: Vec<Box<dyn ShipBehavior>> = Vec::new();
|
||||||
shipbehaviors.push(behavior::Player::new(h1));
|
shipbehaviors.push(Box::new(behavior::Dummy::new(h2)));
|
||||||
shipbehaviors.push(behavior::Dummy::new(h2));
|
shipbehaviors.push(Box::new(behavior::Point::new(h3)));
|
||||||
shipbehaviors.push(behavior::Point::new(h3));
|
|
||||||
|
|
||||||
Game {
|
Game {
|
||||||
last_update: Instant::now(),
|
last_update: Instant::now(),
|
||||||
|
@ -150,6 +150,7 @@ impl Game {
|
||||||
physics,
|
physics,
|
||||||
shipbehaviors,
|
shipbehaviors,
|
||||||
content: ct,
|
content: ct,
|
||||||
|
playerbehavior: behavior::Player::new(h1),
|
||||||
ui: Ui::new(),
|
ui: Ui::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -178,7 +179,15 @@ impl Game {
|
||||||
pub fn update(&mut self) {
|
pub fn update(&mut self) {
|
||||||
let t: f32 = self.last_update.elapsed().as_secs_f32() * self.time_scale;
|
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| {
|
self.shipbehaviors.retain_mut(|b| {
|
||||||
|
// Remove shipbehaviors of destroyed ships
|
||||||
if self.physics.get_ship_mut(&b.get_handle()).is_none() {
|
if self.physics.get_ship_mut(&b.get_handle()).is_none() {
|
||||||
false
|
false
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue