Added Directives to system sim

master
Mark 2024-02-07 15:41:43 -08:00
parent f56fd7ea49
commit 5dab73ec24
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
6 changed files with 69 additions and 32 deletions

1
Cargo.lock generated
View File

@ -877,6 +877,7 @@ dependencies = [
"galactica-content",
"galactica-playeragent",
"galactica-util",
"log",
"nalgebra",
"rand",
"rapier2d",

View File

@ -25,3 +25,4 @@ rapier2d = { workspace = true }
nalgebra = { workspace = true }
crossbeam = { workspace = true }
rand = { workspace = true }
log = { workspace = true }

View File

@ -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<SystemObject>) {
pub fn start_land_on(&mut self, target: Arc<SystemObject>) {
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<Ship> {
&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<Faction> {
&self.faction
}
/// Get this ship's content handle
pub fn get_ship(&self) -> &Arc<Ship> {
&self.ship
}
}

View File

@ -6,3 +6,6 @@
pub mod data;
pub mod phys;
mod playerdirective;
pub use playerdirective::PlayerDirective;

View File

@ -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(),
})
}
}
_ => {
error!("Got an invalid directive {directive:?} in shipstate `Flying`");
}
},
ShipState::Flying { .. } => {
// Any input automatically releases autopilot
if player.input.pressed_left()
|| player.input.pressed_right()
|| player.input.pressed_thrust()
|| player.input.pressed_guns()
{
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 { .. } => {
if player.input.pressed_land() {
ShipState::Landed { .. } => match directive {
PlayerDirective::None => {}
PlayerDirective::UnLand => {
self.start_unland_ship(player.ship.unwrap());
}
_ => {
error!("Got an invalid directive {directive:?} in shipstate `Landed`");
}
},
};
}
}

View File

@ -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,
}