diff --git a/Cargo.lock b/Cargo.lock index eab89df..fc79b1b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -877,6 +877,7 @@ dependencies = [ "galactica-content", "galactica-playeragent", "galactica-util", + "log", "nalgebra", "rand", "rapier2d", diff --git a/crates/system/Cargo.toml b/crates/system/Cargo.toml index 34a4bde..de5c455 100644 --- a/crates/system/Cargo.toml +++ b/crates/system/Cargo.toml @@ -25,3 +25,4 @@ rapier2d = { workspace = true } nalgebra = { workspace = true } crossbeam = { workspace = true } rand = { workspace = true } +log = { workspace = true } diff --git a/crates/system/src/data/ship/ship.rs b/crates/system/src/data/ship/ship.rs index c16f673..31fc37f 100644 --- a/crates/system/src/data/ship/ship.rs +++ b/crates/system/src/data/ship/ship.rs @@ -78,11 +78,11 @@ impl ShipData { /// That is the simulation's responsiblity. /// /// Will panic if we're not flying. - pub fn start_land_on(&mut self, target_handle: Arc) { + pub fn start_land_on(&mut self, target: Arc) { match self.state { ShipState::Flying { .. } => { self.state = ShipState::Landing { - target: target_handle, + target, current_z: 1.0, }; } @@ -285,13 +285,12 @@ impl ShipData { &self.state } - /// Get a handle to this ship's content + /// Get this ship's content pub fn get_content(&self) -> &Arc { &self.ship } - /// Get this ship's current hull. - /// Use content handle to get maximum hull + /// Get this ship's current hull pub fn get_hull(&self) -> f32 { self.hull } @@ -316,9 +315,4 @@ impl ShipData { pub fn get_faction(&self) -> &Arc { &self.faction } - - /// Get this ship's content handle - pub fn get_ship(&self) -> &Arc { - &self.ship - } } diff --git a/crates/system/src/lib.rs b/crates/system/src/lib.rs index 443c370..61bbdb8 100644 --- a/crates/system/src/lib.rs +++ b/crates/system/src/lib.rs @@ -6,3 +6,6 @@ pub mod data; pub mod phys; +mod playerdirective; + +pub use playerdirective::PlayerDirective; diff --git a/crates/system/src/phys/physsim.rs b/crates/system/src/phys/physsim.rs index 1315e0e..8fc4fcd 100644 --- a/crates/system/src/phys/physsim.rs +++ b/crates/system/src/phys/physsim.rs @@ -2,6 +2,7 @@ use galactica_content::Faction; use galactica_content::Relationship; use galactica_content::Ship; use galactica_playeragent::PlayerAgent; +use log::error; use nalgebra::{Isometry2, Point2, Rotation2, Vector2}; use rand::Rng; use rapier2d::{ @@ -17,6 +18,7 @@ use super::{ PhysImage, PhysProjectileImage, PhysShipImage, PhysStepResources, PhysWrapper, }; use crate::data::{ShipAutoPilot, ShipPersonality, ShipState}; +use crate::PlayerDirective; // TODO: replace with a more generic handle /// A handle for a ship in this simulation @@ -190,7 +192,7 @@ impl PhysSim { } /// Update a player ship's controls - pub fn update_player_controls(&mut self, player: &PlayerAgent) { + pub fn apply_directive(&mut self, directive: PlayerDirective, player: &PlayerAgent) { if player.ship.is_none() { return; } @@ -204,37 +206,49 @@ impl PhysSim { ShipState::Flying { autopilot: ShipAutoPilot::None, - } => { - ship_object.controls.guns = player.input.pressed_guns(); - ship_object.controls.left = player.input.pressed_left(); - ship_object.controls.right = player.input.pressed_right(); - ship_object.controls.thrust = player.input.pressed_thrust(); - - if player.input.pressed_land() { + } => match directive { + PlayerDirective::None => {} + PlayerDirective::Engine(x) => ship_object.controls.thrust = x, + PlayerDirective::TurnLeft(x) => ship_object.controls.left = x, + PlayerDirective::TurnRight(x) => ship_object.controls.right = x, + PlayerDirective::Guns(x) => ship_object.controls.guns = x, + PlayerDirective::Land => { if let Some(target) = player.selection.get_planet() { ship_object.data.set_autopilot(ShipAutoPilot::Landing { target: target.clone(), }) } } - } - - ShipState::Flying { .. } => { - // Any input automatically releases autopilot - if player.input.pressed_left() - || player.input.pressed_right() - || player.input.pressed_thrust() - || player.input.pressed_guns() - { - ship_object.data.set_autopilot(ShipAutoPilot::None); + _ => { + error!("Got an invalid directive {directive:?} in shipstate `Flying`"); } - } + }, - ShipState::Landed { .. } => { - if player.input.pressed_land() { + ShipState::Flying { .. } => match directive { + PlayerDirective::None => {} + PlayerDirective::Engine(_) + | PlayerDirective::TurnLeft(_) + | PlayerDirective::TurnRight(_) + | PlayerDirective::Land + | PlayerDirective::Guns(_) => { + // Disable autopilot and apply action + ship_object.data.set_autopilot(ShipAutoPilot::None); + self.apply_directive(directive, player); + } + _ => { + error!("Got an invalid directive {directive:?} in shipstate `Flying`"); + } + }, + + ShipState::Landed { .. } => match directive { + PlayerDirective::None => {} + PlayerDirective::UnLand => { self.start_unland_ship(player.ship.unwrap()); } - } + _ => { + error!("Got an invalid directive {directive:?} in shipstate `Landed`"); + } + }, }; } } diff --git a/crates/system/src/playerdirective.rs b/crates/system/src/playerdirective.rs new file mode 100644 index 0000000..4f93521 --- /dev/null +++ b/crates/system/src/playerdirective.rs @@ -0,0 +1,24 @@ +/// An action the player wants to take in the game. +#[derive(Debug, Clone)] +pub enum PlayerDirective { + /// Do nothing + None, + + /// Set main engine state + Engine(bool), + + /// Set left turn thruster state + TurnLeft(bool), + + /// Set right turn thruster state + TurnRight(bool), + + /// Set main gun state + Guns(bool), + + /// Land on the currently selected planet + Land, + + /// Take off from the planet we're landed on + UnLand, +}