From 083f76332f162829591e2876e9d5c792c79ecb86 Mon Sep 17 00:00:00 2001 From: Mark Date: Thu, 11 Jan 2024 11:28:11 -0800 Subject: [PATCH] Added a few derives --- crates/galaxy/src/galaxy.rs | 2 +- crates/galaxy/src/ship/ship.rs | 2 +- crates/systemsim/src/controller/mod.rs | 54 +++++++++++++++++++--- crates/systemsim/src/controller/null.rs | 13 +++--- crates/systemsim/src/controller/point.rs | 15 +++--- crates/systemsim/src/objects/collapse.rs | 2 +- crates/systemsim/src/objects/projectile.rs | 2 +- crates/systemsim/src/objects/ship.rs | 2 +- 8 files changed, 67 insertions(+), 25 deletions(-) diff --git a/crates/galaxy/src/galaxy.rs b/crates/galaxy/src/galaxy.rs index 5176956..1a4cf64 100644 --- a/crates/galaxy/src/galaxy.rs +++ b/crates/galaxy/src/galaxy.rs @@ -5,7 +5,7 @@ use galactica_content::{Content, FactionHandle, ShipHandle, SystemHandle}; /// Keeps track of all objects in the galaxy. /// This struct does NO physics, it keeps track of data exclusively. -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Galaxy { /// Universal counter. /// Used to create unique handles for game objects. diff --git a/crates/galaxy/src/ship/ship.rs b/crates/galaxy/src/ship/ship.rs index 446f790..d25e5ea 100644 --- a/crates/galaxy/src/ship/ship.rs +++ b/crates/galaxy/src/ship/ship.rs @@ -6,7 +6,7 @@ use super::{OutfitSet, ShipPersonality}; use galactica_content::{Content, FactionHandle, GunPoint, Outfit, ShipHandle}; use rand::{rngs::ThreadRng, Rng}; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct GxShip { // Metadata values handle: GxShipHandle, diff --git a/crates/systemsim/src/controller/mod.rs b/crates/systemsim/src/controller/mod.rs index 27ab057..61b6abd 100644 --- a/crates/systemsim/src/controller/mod.rs +++ b/crates/systemsim/src/controller/mod.rs @@ -3,24 +3,64 @@ mod null; mod point; -pub use null::*; -pub use point::Point; +use null::*; +use point::PointShipController; use galactica_galaxy::GxShipHandle; use rapier2d::dynamics::{RigidBodyHandle, RigidBodySet}; -use std::collections::HashMap; +use std::{collections::HashMap, fmt::Debug}; use crate::{ objects::{ShipControls, SySimShip}, StepResources, }; +/// Represents a ship controller +#[derive(Debug, Clone)] +pub enum ShipController { + /// Null controller + Null(NullShipController), + + /// Point controller + Point(PointShipController), +} + +impl ShipController { + /// Make a new null controller + pub fn new_null() -> Self { + Self::Null(NullShipController::new()) + } + + /// Make a new point controller + pub fn new_point() -> Self { + Self::Point(PointShipController::new()) + } + + /// Compute new ship controls from world state + pub fn update_controls( + &mut self, + res: &StepResources, + rigid_bodies: &RigidBodySet, + ships: &HashMap, + this_ship: RigidBodyHandle, + this_data: GxShipHandle, + ) -> Option { + match self { + Self::Null(n) => n.update_controls(res, rigid_bodies, ships, this_ship, this_data), + Self::Point(p) => p.update_controls(res, rigid_bodies, ships, this_ship, this_data), + } + } +} + /// Ship controller trait. Any struct that implements this /// may be used to control a ship. -pub trait ShipController { +pub trait ShipControllerStruct +where + Self: Debug + Clone, +{ /// Update a ship's controls based on system state. - /// This method does not return anything, it modifies - /// the ship's controls in-place. + /// This method returns the ship's new control values, + /// or None if no change is to be made. fn update_controls( &mut self, res: &StepResources, @@ -28,5 +68,5 @@ pub trait ShipController { ships: &HashMap, this_ship: RigidBodyHandle, this_data: GxShipHandle, - ) -> ShipControls; + ) -> Option; } diff --git a/crates/systemsim/src/controller/null.rs b/crates/systemsim/src/controller/null.rs index 2996d5d..54da163 100644 --- a/crates/systemsim/src/controller/null.rs +++ b/crates/systemsim/src/controller/null.rs @@ -2,7 +2,7 @@ use galactica_galaxy::GxShipHandle; use rapier2d::dynamics::{RigidBodyHandle, RigidBodySet}; use std::collections::HashMap; -use super::ShipController; +use super::ShipControllerStruct; use crate::{ objects::{ShipControls, SySimShip}, StepResources, @@ -10,16 +10,17 @@ use crate::{ /// The Null controller is assigned to objects that are static or not controlled by the computer. /// Most notably, the player's ship has a Null controller. -pub struct Null {} +#[derive(Debug, Clone)] +pub struct NullShipController {} -impl Null { +impl NullShipController { /// Create a new ship controller pub fn new() -> Self { Self {} } } -impl ShipController for Null { +impl ShipControllerStruct for NullShipController { fn update_controls( &mut self, _res: &StepResources, @@ -27,7 +28,7 @@ impl ShipController for Null { _ships: &HashMap, _this_ship: RigidBodyHandle, _this_data: GxShipHandle, - ) -> ShipControls { - ShipControls::new() + ) -> Option { + None } } diff --git a/crates/systemsim/src/controller/point.rs b/crates/systemsim/src/controller/point.rs index 30d4397..00ffc97 100644 --- a/crates/systemsim/src/controller/point.rs +++ b/crates/systemsim/src/controller/point.rs @@ -4,7 +4,7 @@ use galactica_galaxy::GxShipHandle; use rapier2d::dynamics::{RigidBodyHandle, RigidBodySet}; use std::collections::HashMap; -use super::ShipController; +use super::ShipControllerStruct; use crate::{ objects::{ShipControls, SySimShip}, util, StepResources, @@ -12,16 +12,17 @@ use crate::{ /// "Point" ship controller. /// Point and shoot towards the nearest enemy. -pub struct Point {} +#[derive(Debug, Clone)] +pub struct PointShipController {} -impl Point { +impl PointShipController { /// Create a new ship controller pub fn new() -> Self { Self {} } } -impl ShipController for Point { +impl ShipControllerStruct for PointShipController { fn update_controls( &mut self, res: &StepResources, @@ -29,7 +30,7 @@ impl ShipController for Point { ships: &HashMap, this_ship: RigidBodyHandle, this_data: GxShipHandle, - ) -> ShipControls { + ) -> Option { let mut controls = ShipControls::new(); let this_rigidbody = rigid_bodies.get(this_ship).unwrap(); @@ -60,7 +61,7 @@ impl ShipController for Point { // Find the closest target let mut closest_enemy_position = match hostile_ships.next() { Some(c) => util::rigidbody_position(c), - None => return controls, // Do nothing if no targets are available + None => return Some(controls), // Do nothing if no targets are available }; let mut d = (my_position - closest_enemy_position).magnitude(); @@ -84,6 +85,6 @@ impl ShipController for Point { } controls.guns = true; - return controls; + return Some(controls); } } diff --git a/crates/systemsim/src/objects/collapse.rs b/crates/systemsim/src/objects/collapse.rs index 28c3d78..c8be78e 100644 --- a/crates/systemsim/src/objects/collapse.rs +++ b/crates/systemsim/src/objects/collapse.rs @@ -6,7 +6,7 @@ use rapier2d::{dynamics::RigidBody, geometry::Collider}; use crate::{util, ParticleBuilder, StepResources}; -#[derive(Debug)] +#[derive(Debug, Clone)] pub(super) struct ShipCollapseSequence { total_length: f32, time_elapsed: f32, diff --git a/crates/systemsim/src/objects/projectile.rs b/crates/systemsim/src/objects/projectile.rs index f315158..62770b5 100644 --- a/crates/systemsim/src/objects/projectile.rs +++ b/crates/systemsim/src/objects/projectile.rs @@ -3,7 +3,7 @@ use rand::Rng; use rapier2d::{dynamics::RigidBodyHandle, geometry::ColliderHandle}; /// A single projectile in this sim -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct SySimProjectile { /// This projectile's game data pub content: Projectile, diff --git a/crates/systemsim/src/objects/ship.rs b/crates/systemsim/src/objects/ship.rs index 48c3802..70c304d 100644 --- a/crates/systemsim/src/objects/ship.rs +++ b/crates/systemsim/src/objects/ship.rs @@ -41,7 +41,7 @@ impl ShipControls { } /// A ship instance in the physics system -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct SySimShip { /// This ship's physics handle pub rigid_body: RigidBodyHandle,