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.
/// 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.

View File

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

View File

@ -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<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
/// 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<GxShipHandle, SySimShip>,
this_ship: RigidBodyHandle,
this_data: GxShipHandle,
) -> ShipControls;
) -> Option<ShipControls>;
}

View File

@ -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<GxShipHandle, SySimShip>,
_this_ship: RigidBodyHandle,
_this_data: GxShipHandle,
) -> ShipControls {
ShipControls::new()
) -> Option<ShipControls> {
None
}
}

View File

@ -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<GxShipHandle, SySimShip>,
this_ship: RigidBodyHandle,
this_data: GxShipHandle,
) -> ShipControls {
) -> Option<ShipControls> {
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);
}
}

View File

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

View File

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

View File

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