Added a few derives

master
Mark 2024-01-11 11:28:11 -08:00
parent 876a95e546
commit 083f76332f
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
8 changed files with 67 additions and 25 deletions

View File

@ -5,7 +5,7 @@ use galactica_content::{Content, FactionHandle, ShipHandle, SystemHandle};
/// Keeps track of all objects in the galaxy. /// Keeps track of all objects in the galaxy.
/// This struct does NO physics, it keeps track of data exclusively. /// This struct does NO physics, it keeps track of data exclusively.
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Galaxy { pub struct Galaxy {
/// Universal counter. /// Universal counter.
/// Used to create unique handles for game objects. /// Used to create unique handles for game objects.

View File

@ -6,7 +6,7 @@ use super::{OutfitSet, ShipPersonality};
use galactica_content::{Content, FactionHandle, GunPoint, Outfit, ShipHandle}; use galactica_content::{Content, FactionHandle, GunPoint, Outfit, ShipHandle};
use rand::{rngs::ThreadRng, Rng}; use rand::{rngs::ThreadRng, Rng};
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct GxShip { pub struct GxShip {
// Metadata values // Metadata values
handle: GxShipHandle, handle: GxShipHandle,

View File

@ -3,24 +3,64 @@
mod null; mod null;
mod point; mod point;
pub use null::*; use null::*;
pub use point::Point; use point::PointShipController;
use galactica_galaxy::GxShipHandle; use galactica_galaxy::GxShipHandle;
use rapier2d::dynamics::{RigidBodyHandle, RigidBodySet}; use rapier2d::dynamics::{RigidBodyHandle, RigidBodySet};
use std::collections::HashMap; use std::{collections::HashMap, fmt::Debug};
use crate::{ use crate::{
objects::{ShipControls, SySimShip}, objects::{ShipControls, SySimShip},
StepResources, 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<GxShipHandle, SySimShip>,
this_ship: RigidBodyHandle,
this_data: GxShipHandle,
) -> Option<ShipControls> {
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 /// Ship controller trait. Any struct that implements this
/// may be used to control a ship. /// 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. /// Update a ship's controls based on system state.
/// This method does not return anything, it modifies /// This method returns the ship's new control values,
/// the ship's controls in-place. /// or None if no change is to be made.
fn update_controls( fn update_controls(
&mut self, &mut self,
res: &StepResources, res: &StepResources,
@ -28,5 +68,5 @@ pub trait ShipController {
ships: &HashMap<GxShipHandle, SySimShip>, ships: &HashMap<GxShipHandle, SySimShip>,
this_ship: RigidBodyHandle, this_ship: RigidBodyHandle,
this_data: GxShipHandle, this_data: GxShipHandle,
) -> ShipControls; ) -> Option<ShipControls>;
} }

View File

@ -2,7 +2,7 @@ use galactica_galaxy::GxShipHandle;
use rapier2d::dynamics::{RigidBodyHandle, RigidBodySet}; use rapier2d::dynamics::{RigidBodyHandle, RigidBodySet};
use std::collections::HashMap; use std::collections::HashMap;
use super::ShipController; use super::ShipControllerStruct;
use crate::{ use crate::{
objects::{ShipControls, SySimShip}, objects::{ShipControls, SySimShip},
StepResources, StepResources,
@ -10,16 +10,17 @@ use crate::{
/// The Null controller is assigned to objects that are static or not controlled by the computer. /// 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. /// 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 /// Create a new ship controller
pub fn new() -> Self { pub fn new() -> Self {
Self {} Self {}
} }
} }
impl ShipController for Null { impl ShipControllerStruct for NullShipController {
fn update_controls( fn update_controls(
&mut self, &mut self,
_res: &StepResources, _res: &StepResources,
@ -27,7 +28,7 @@ impl ShipController for Null {
_ships: &HashMap<GxShipHandle, SySimShip>, _ships: &HashMap<GxShipHandle, SySimShip>,
_this_ship: RigidBodyHandle, _this_ship: RigidBodyHandle,
_this_data: GxShipHandle, _this_data: GxShipHandle,
) -> ShipControls { ) -> Option<ShipControls> {
ShipControls::new() None
} }
} }

View File

@ -4,7 +4,7 @@ use galactica_galaxy::GxShipHandle;
use rapier2d::dynamics::{RigidBodyHandle, RigidBodySet}; use rapier2d::dynamics::{RigidBodyHandle, RigidBodySet};
use std::collections::HashMap; use std::collections::HashMap;
use super::ShipController; use super::ShipControllerStruct;
use crate::{ use crate::{
objects::{ShipControls, SySimShip}, objects::{ShipControls, SySimShip},
util, StepResources, util, StepResources,
@ -12,16 +12,17 @@ use crate::{
/// "Point" ship controller. /// "Point" ship controller.
/// Point and shoot towards the nearest enemy. /// 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 /// Create a new ship controller
pub fn new() -> Self { pub fn new() -> Self {
Self {} Self {}
} }
} }
impl ShipController for Point { impl ShipControllerStruct for PointShipController {
fn update_controls( fn update_controls(
&mut self, &mut self,
res: &StepResources, res: &StepResources,
@ -29,7 +30,7 @@ impl ShipController for Point {
ships: &HashMap<GxShipHandle, SySimShip>, ships: &HashMap<GxShipHandle, SySimShip>,
this_ship: RigidBodyHandle, this_ship: RigidBodyHandle,
this_data: GxShipHandle, this_data: GxShipHandle,
) -> ShipControls { ) -> Option<ShipControls> {
let mut controls = ShipControls::new(); let mut controls = ShipControls::new();
let this_rigidbody = rigid_bodies.get(this_ship).unwrap(); let this_rigidbody = rigid_bodies.get(this_ship).unwrap();
@ -60,7 +61,7 @@ impl ShipController for Point {
// Find the closest target // Find the closest target
let mut closest_enemy_position = match hostile_ships.next() { let mut closest_enemy_position = match hostile_ships.next() {
Some(c) => util::rigidbody_position(c), 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(); let mut d = (my_position - closest_enemy_position).magnitude();
@ -84,6 +85,6 @@ impl ShipController for Point {
} }
controls.guns = true; controls.guns = true;
return controls; return Some(controls);
} }
} }

View File

@ -6,7 +6,7 @@ use rapier2d::{dynamics::RigidBody, geometry::Collider};
use crate::{util, ParticleBuilder, StepResources}; use crate::{util, ParticleBuilder, StepResources};
#[derive(Debug)] #[derive(Debug, Clone)]
pub(super) struct ShipCollapseSequence { pub(super) struct ShipCollapseSequence {
total_length: f32, total_length: f32,
time_elapsed: f32, time_elapsed: f32,

View File

@ -3,7 +3,7 @@ use rand::Rng;
use rapier2d::{dynamics::RigidBodyHandle, geometry::ColliderHandle}; use rapier2d::{dynamics::RigidBodyHandle, geometry::ColliderHandle};
/// A single projectile in this sim /// A single projectile in this sim
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct SySimProjectile { pub struct SySimProjectile {
/// This projectile's game data /// This projectile's game data
pub content: Projectile, pub content: Projectile,

View File

@ -41,7 +41,7 @@ impl ShipControls {
} }
/// A ship instance in the physics system /// A ship instance in the physics system
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct SySimShip { pub struct SySimShip {
/// This ship's physics handle /// This ship's physics handle
pub rigid_body: RigidBodyHandle, pub rigid_body: RigidBodyHandle,