diff --git a/Cargo.lock b/Cargo.lock index d9ab5bc..5740129 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -643,10 +643,9 @@ dependencies = [ "anyhow", "cgmath", "galactica-content", - "galactica-galaxy", "galactica-playeragent", "galactica-render", - "galactica-systemsim", + "galactica-system", "galactica-util", "pollster", "rand", @@ -668,15 +667,6 @@ dependencies = [ "walkdir", ] -[[package]] -name = "galactica-galaxy" -version = "0.0.0" -dependencies = [ - "cgmath", - "galactica-content", - "rand", -] - [[package]] name = "galactica-packer" version = "0.0.0" @@ -696,7 +686,6 @@ dependencies = [ "anyhow", "cgmath", "galactica-content", - "galactica-galaxy", "galactica-util", "pollster", "rapier2d", @@ -712,9 +701,8 @@ dependencies = [ "bytemuck", "cgmath", "galactica-content", - "galactica-galaxy", "galactica-packer", - "galactica-systemsim", + "galactica-system", "galactica-util", "glyphon", "image", @@ -724,13 +712,12 @@ dependencies = [ ] [[package]] -name = "galactica-systemsim" +name = "galactica-system" version = "0.0.0" dependencies = [ "cgmath", "crossbeam", "galactica-content", - "galactica-galaxy", "galactica-playeragent", "galactica-util", "nalgebra", diff --git a/Cargo.toml b/Cargo.toml index 61c46dd..a263a3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,8 +45,7 @@ readme = "" galactica-util = { path = "crates/util" } galactica-content = { path = "crates/content" } galactica-render = { path = "crates/render" } -galactica-systemsim = { path = "crates/systemsim" } -galactica-galaxy = { path = "crates/galaxy" } +galactica-system = { path = "crates/system" } galactica-packer = { path = "crates/packer" } galactica-playeragent = { path = "crates/playeragent" } galactica = { path = "crates/galactica" } diff --git a/crates/galactica/Cargo.toml b/crates/galactica/Cargo.toml index 9bd73a8..48b93fa 100644 --- a/crates/galactica/Cargo.toml +++ b/crates/galactica/Cargo.toml @@ -24,8 +24,7 @@ workspace = true galactica-content = { workspace = true } galactica-render = { workspace = true } galactica-util = { workspace = true } -galactica-systemsim = { workspace = true } -galactica-galaxy = { workspace = true } +galactica-system = { workspace = true } galactica-playeragent = { workspace = true } rand = { workspace = true } diff --git a/crates/galactica/src/game.rs b/crates/galactica/src/game.rs index 8563e8f..df560c4 100644 --- a/crates/galactica/src/game.rs +++ b/crates/galactica/src/game.rs @@ -1,15 +1,17 @@ use cgmath::Point2; use galactica_content::{Content, FactionHandle, OutfitHandle, ShipHandle, SystemHandle}; -use galactica_galaxy::ship::ShipPersonality; use galactica_playeragent::PlayerAgent; -use galactica_systemsim::{ParticleBuilder, StepResources, SySimShipHandle, SystemSim, Wrapper}; +use galactica_system::data::ShipPersonality; +use galactica_system::phys::{ + ParticleBuilder, PhysSim, PhysSimShipHandle, PhysStepResources, Wrapper, +}; use galactica_util::timing::Timing; use rand::seq::SliceRandom; use std::time::Instant; #[derive(Clone)] pub struct GameState { - pub systemsim: SystemSim, + pub systemsim: PhysSim, pub timing: Timing, pub start_instant: Instant, } @@ -36,7 +38,7 @@ pub struct Game { unsafe impl<'a> Send for Game {} impl<'a> Game { - pub fn make_player(&mut self) -> SySimShipHandle { + pub fn make_player(&mut self) -> PhysSimShipHandle { let player = self.state.systemsim.add_ship( &self.ct, ShipHandle { index: 0 }, @@ -57,7 +59,7 @@ impl<'a> Game { } pub fn new(ct: Content) -> Self { - let mut systemsim = SystemSim::new(&ct, SystemHandle { index: 0 }); + let mut systemsim = PhysSim::new(&ct, SystemHandle { index: 0 }); let a = systemsim.add_ship( &ct, @@ -119,7 +121,7 @@ impl<'a> Game { let t: f32 = self.last_update.elapsed().as_secs_f32() * self.time_scale; self.new_particles.clear(); - self.state.systemsim.step(StepResources { + self.state.systemsim.step(PhysStepResources { ct: &self.ct, particles: &mut self.new_particles, timing: &mut self.state.timing, diff --git a/crates/galactica/src/main.rs b/crates/galactica/src/main.rs index 6c71644..51cf66d 100644 --- a/crates/galactica/src/main.rs +++ b/crates/galactica/src/main.rs @@ -4,7 +4,7 @@ use anyhow::{bail, Result}; use galactica_content::{Content, SystemHandle}; use galactica_playeragent::{PlayerAgent, PlayerStatus}; use galactica_render::RenderInput; -use galactica_systemsim::{util, SySimShipHandle}; +use galactica_system::phys::{util, PhysSimShipHandle}; use galactica_util::constants::ASSET_CACHE; use std::{ fs, @@ -56,7 +56,7 @@ fn main() -> Result<()> { ct: &content, systemsim: &game.get_state().systemsim, particles: game.get_particles(), - player_ship: SySimShipHandle(player.ship.unwrap()), + player_ship: PhysSimShipHandle(player.ship.unwrap()), current_system: SystemHandle { index: 0 }, timing: game.get_state().timing.clone(), }; @@ -79,7 +79,7 @@ fn main() -> Result<()> { let o = &game .get_state() .systemsim - .get_ship(&SySimShipHandle(player.ship.unwrap())); + .get_ship(&PhysSimShipHandle(player.ship.unwrap())); if let Some(o) = o { let r = &game.get_state().systemsim.get_rigid_body(o.rigid_body); if let Some(r) = r { diff --git a/crates/galaxy/Cargo.toml b/crates/galaxy/Cargo.toml deleted file mode 100644 index 2f8f817..0000000 --- a/crates/galaxy/Cargo.toml +++ /dev/null @@ -1,23 +0,0 @@ -[package] -name = "galactica-galaxy" -description = "Galactica's game data manager" -categories = { workspace = true } -keywords = { workspace = true } -version = { workspace = true } -rust-version = { workspace = true } -authors = { workspace = true } -edition = { workspace = true } -homepage = { workspace = true } -repository = { workspace = true } -license = { workspace = true } -documentation = { workspace = true } -readme = { workspace = true } - -[lints] -workspace = true - -[dependencies] -galactica-content = { workspace = true } - -cgmath = { workspace = true } -rand = { workspace = true } diff --git a/crates/galaxy/src/lib.rs b/crates/galaxy/src/lib.rs deleted file mode 100644 index 05271e4..0000000 --- a/crates/galaxy/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod ship; diff --git a/crates/playeragent/Cargo.toml b/crates/playeragent/Cargo.toml index 240bee9..70cf870 100644 --- a/crates/playeragent/Cargo.toml +++ b/crates/playeragent/Cargo.toml @@ -19,7 +19,6 @@ workspace = true [dependencies] galactica-content = { workspace = true } galactica-util = { workspace = true } -galactica-galaxy = { workspace = true } winit = { workspace = true } wgpu = { workspace = true } diff --git a/crates/render/Cargo.toml b/crates/render/Cargo.toml index fe0d241..68bae95 100644 --- a/crates/render/Cargo.toml +++ b/crates/render/Cargo.toml @@ -20,8 +20,7 @@ workspace = true galactica-content = { workspace = true } galactica-util = { workspace = true } galactica-packer = { workspace = true } -galactica-systemsim = { workspace = true } -galactica-galaxy = { workspace = true } +galactica-system = { workspace = true } anyhow = { workspace = true } cgmath = { workspace = true } diff --git a/crates/render/src/datastructs.rs b/crates/render/src/datastructs.rs index 00920ac..4ce7b28 100644 --- a/crates/render/src/datastructs.rs +++ b/crates/render/src/datastructs.rs @@ -1,6 +1,6 @@ use cgmath::Point2; use galactica_content::{Content, SystemHandle}; -use galactica_systemsim::{ParticleBuilder, SySimShipHandle, SystemSim}; +use galactica_system::phys::{ParticleBuilder, PhysSim, PhysSimShipHandle}; use galactica_util::timing::Timing; use glyphon::{FontSystem, SwashCache, TextAtlas, TextRenderer}; use std::rc::Rc; @@ -15,7 +15,7 @@ pub struct RenderInput<'a> { pub camera_pos: Point2, /// Player ship data - pub player_ship: SySimShipHandle, + pub player_ship: PhysSimShipHandle, /// The system we're currently in pub current_system: SystemHandle, @@ -24,7 +24,7 @@ pub struct RenderInput<'a> { pub camera_zoom: f32, /// The world state to render - pub systemsim: &'a SystemSim, + pub systemsim: &'a PhysSim, // TODO: handle overflow. is it a problem? /// The current time, in seconds diff --git a/crates/render/src/gpustate/mod.rs b/crates/render/src/gpustate/mod.rs index 0d15940..230423a 100644 --- a/crates/render/src/gpustate/mod.rs +++ b/crates/render/src/gpustate/mod.rs @@ -10,8 +10,8 @@ use crate::{ /// GPUState is very big, so its methods have been split /// among the following files. mod new; +mod phys; mod render; -mod systemsim; /// A high-level GPU wrapper. Reads game state (via RenderInput), /// produces pretty pictures. diff --git a/crates/render/src/gpustate/systemsim.rs b/crates/render/src/gpustate/phys.rs similarity index 98% rename from crates/render/src/gpustate/systemsim.rs rename to crates/render/src/gpustate/phys.rs index fb08700..09a49cd 100644 --- a/crates/render/src/gpustate/systemsim.rs +++ b/crates/render/src/gpustate/phys.rs @@ -2,7 +2,7 @@ use bytemuck; use cgmath::{EuclideanSpace, InnerSpace, Point2, Vector2}; -use galactica_systemsim::util; +use galactica_system::phys::util; use galactica_util::constants::OBJECT_SPRITE_INSTANCE_LIMIT; use crate::{ @@ -12,7 +12,7 @@ use crate::{ }; impl GPUState { - pub(super) fn sysim_push_ship( + pub(super) fn phys_push_ship( &mut self, state: &RenderInput, // NE and SW corners of screen @@ -120,7 +120,7 @@ impl GPUState { } } - pub(super) fn sysim_push_projectile( + pub(super) fn phys_push_projectile( &mut self, state: &RenderInput, // NE and SW corners of screen @@ -190,7 +190,7 @@ impl GPUState { } } - pub(super) fn sysim_push_system( + pub(super) fn phys_push_system( &mut self, state: &RenderInput, // NE and SW corners of screen diff --git a/crates/render/src/gpustate/render.rs b/crates/render/src/gpustate/render.rs index 8e5a649..e71dbc7 100644 --- a/crates/render/src/gpustate/render.rs +++ b/crates/render/src/gpustate/render.rs @@ -116,9 +116,9 @@ impl super::GPUState { // Order matters, it determines what is drawn on top. // The order inside ships and projectiles doesn't matter, // but ships should always be under projectiles. - self.sysim_push_system(&input, (clip_ne, clip_sw)); - self.sysim_push_ship(&input, (clip_ne, clip_sw)); - self.sysim_push_projectile(&input, (clip_ne, clip_sw)); + self.phys_push_system(&input, (clip_ne, clip_sw)); + self.phys_push_ship(&input, (clip_ne, clip_sw)); + self.phys_push_projectile(&input, (clip_ne, clip_sw)); self.ui.draw(&input, &mut self.state); // These should match the indices in each shader, diff --git a/crates/render/src/ui/radar.rs b/crates/render/src/ui/radar.rs index 616fd27..927af2d 100644 --- a/crates/render/src/ui/radar.rs +++ b/crates/render/src/ui/radar.rs @@ -1,6 +1,5 @@ use cgmath::{Deg, InnerSpace, Point2, Rad, Vector2}; -use galactica_galaxy::ship::ShipState; -use galactica_systemsim::util; +use galactica_system::{data::ShipState, phys::util}; use galactica_util::constants::UI_SPRITE_INSTANCE_LIMIT; use crate::{ diff --git a/crates/render/src/ui/status.rs b/crates/render/src/ui/status.rs index 5ea1c79..4a09eb8 100644 --- a/crates/render/src/ui/status.rs +++ b/crates/render/src/ui/status.rs @@ -1,5 +1,6 @@ use std::f32::consts::TAU; +use galactica_system::data::ShipState; use galactica_util::constants::{RADIALBAR_SPRITE_INSTANCE_LIMIT, UI_SPRITE_INSTANCE_LIMIT}; use crate::{ @@ -32,8 +33,7 @@ impl Status { let player_ship = input.systemsim.get_ship(&input.player_ship).unwrap(); match player_ship.data.get_state() { - galactica_galaxy::ship::ShipState::Collapsing { .. } - | galactica_galaxy::ship::ShipState::Flying => { + ShipState::Collapsing { .. } | ShipState::Flying => { max_shields = player_ship.data.get_outfits().get_shield_strength(); current_shields = player_ship.data.get_shields(); current_hull = player_ship.data.get_hull(); diff --git a/crates/systemsim/Cargo.toml b/crates/system/Cargo.toml similarity index 85% rename from crates/systemsim/Cargo.toml rename to crates/system/Cargo.toml index a54d3f3..5761a2f 100644 --- a/crates/systemsim/Cargo.toml +++ b/crates/system/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "galactica-systemsim" -description = "Physics interactions for Galactica" +name = "galactica-system" +description = "Galactica's star system simulations" categories = { workspace = true } keywords = { workspace = true } version = { workspace = true } @@ -18,7 +18,6 @@ workspace = true [dependencies] galactica-content = { workspace = true } -galactica-galaxy = { workspace = true } galactica-util = { workspace = true } galactica-playeragent = { workspace = true } diff --git a/crates/system/src/data/mod.rs b/crates/system/src/data/mod.rs new file mode 100644 index 0000000..f1dcb80 --- /dev/null +++ b/crates/system/src/data/mod.rs @@ -0,0 +1,4 @@ +//! This module contains data structs shared between all simulations + +mod ship; +pub use ship::*; diff --git a/crates/galaxy/src/ship/mod.rs b/crates/system/src/data/ship/mod.rs similarity index 100% rename from crates/galaxy/src/ship/mod.rs rename to crates/system/src/data/ship/mod.rs diff --git a/crates/galaxy/src/ship/outfitset.rs b/crates/system/src/data/ship/outfitset.rs similarity index 98% rename from crates/galaxy/src/ship/outfitset.rs rename to crates/system/src/data/ship/outfitset.rs index 5314faf..2977f1a 100644 --- a/crates/galaxy/src/ship/outfitset.rs +++ b/crates/system/src/data/ship/outfitset.rs @@ -160,6 +160,8 @@ impl OutfitSet { } // TODO: pick these better + /// Returns the flare sprite that should be shown when this + /// ship is using its thrusters pub fn get_flare_sprite(&self, ct: &Content) -> Option { for i in self.outfits.keys() { let c = ct.get_outfit(*i); diff --git a/crates/galaxy/src/ship/personality.rs b/crates/system/src/data/ship/personality.rs similarity index 100% rename from crates/galaxy/src/ship/personality.rs rename to crates/system/src/data/ship/personality.rs diff --git a/crates/galaxy/src/ship/ship.rs b/crates/system/src/data/ship/ship.rs similarity index 91% rename from crates/galaxy/src/ship/ship.rs rename to crates/system/src/data/ship/ship.rs index 4dddee6..f6360c7 100644 --- a/crates/galaxy/src/ship/ship.rs +++ b/crates/system/src/data/ship/ship.rs @@ -14,8 +14,13 @@ pub enum ShipState { Flying, // TODO: system, position (also in collapse)? /// This ship has been destroyed, and is playing its collapse sequence. - /// Parameters are total collapse sequence length and remaining sequence length, in seconds. - Collapsing { total: f32, elapsed: f32 }, + Collapsing { + /// Total collapse sequence length, in seconds + total: f32, + + /// How many seconds of the collapse sequence we've played + elapsed: f32, + }, } impl ShipState { @@ -57,8 +62,9 @@ impl ShipState { } } +/// Represents all attributes of a single ship #[derive(Debug, Clone)] -pub struct GxShip { +pub struct ShipData { // Metadata values ct_handle: ShipHandle, faction: FactionHandle, @@ -82,15 +88,16 @@ pub struct GxShip { last_hit: Instant, } -impl GxShip { - pub fn new( +impl ShipData { + /// Create a new ShipData + pub(crate) fn new( ct: &Content, ct_handle: ShipHandle, faction: FactionHandle, personality: ShipPersonality, ) -> Self { let s = ct.get_ship(ct_handle); - GxShip { + ShipData { ct_handle, faction, outfits: OutfitSet::new(s.space, &s.guns), @@ -124,7 +131,7 @@ impl GxShip { /// Will panic if `which` isn't a point on this ship. /// Returns `true` if this gun was fired, /// and `false` if it is on cooldown or empty. - pub fn fire_gun(&mut self, ct: &Content, which: &GunPoint) -> bool { + pub(crate) fn fire_gun(&mut self, ct: &Content, which: &GunPoint) -> bool { let c = self.gun_cooldowns.get_mut(which).unwrap(); if *c > 0.0 { @@ -143,7 +150,7 @@ impl GxShip { } /// Hit this ship with the given amount of damage - pub fn apply_damage(&mut self, ct: &Content, mut d: f32) { + pub(crate) fn apply_damage(&mut self, ct: &Content, mut d: f32) { if self.state.is_collapsing() { return; } @@ -166,7 +173,7 @@ impl GxShip { } /// Update this ship's state by `t` seconds - pub fn step(&mut self, t: f32) { + pub(crate) fn step(&mut self, t: f32) { match self.state { ShipState::Collapsing { ref mut elapsed, .. @@ -201,7 +208,7 @@ impl GxShip { } // Misc getters, so internal state is untouchable -impl GxShip { +impl ShipData { /// Get this ship's state pub fn get_state(&self) -> &ShipState { &self.state diff --git a/crates/system/src/lib.rs b/crates/system/src/lib.rs new file mode 100644 index 0000000..443c370 --- /dev/null +++ b/crates/system/src/lib.rs @@ -0,0 +1,8 @@ +#![warn(missing_docs)] + +//! Galactica's system simulation. +//! data contains shared object data, +//! phys contains the physics system simulation (the one you see) + +pub mod data; +pub mod phys; diff --git a/crates/systemsim/src/controller/mod.rs b/crates/system/src/phys/controller/mod.rs similarity index 86% rename from crates/systemsim/src/controller/mod.rs rename to crates/system/src/phys/controller/mod.rs index 7b8a9fa..8faa44e 100644 --- a/crates/systemsim/src/controller/mod.rs +++ b/crates/system/src/phys/controller/mod.rs @@ -9,9 +9,9 @@ use point::PointShipController; use rapier2d::{dynamics::RigidBodySet, geometry::ColliderHandle}; use std::{collections::HashMap, fmt::Debug}; -use crate::{ - objects::{ShipControls, SySimShip}, - StepResources, +use super::{ + objects::{PhysSimShip, ShipControls}, + PhysStepResources, }; /// Represents a ship controller @@ -38,9 +38,9 @@ impl ShipController { /// Compute new ship controls from world state pub fn update_controls( &mut self, - res: &StepResources, + res: &PhysStepResources, rigid_bodies: &RigidBodySet, - ships: &HashMap, + ships: &HashMap, this_ship: ColliderHandle, ) -> Option { match self { @@ -61,9 +61,9 @@ where /// or None if no change is to be made. fn update_controls( &mut self, - res: &StepResources, + res: &PhysStepResources, rigid_bodies: &RigidBodySet, - ships: &HashMap, + ships: &HashMap, this_ship: ColliderHandle, ) -> Option; } diff --git a/crates/systemsim/src/controller/null.rs b/crates/system/src/phys/controller/null.rs similarity index 76% rename from crates/systemsim/src/controller/null.rs rename to crates/system/src/phys/controller/null.rs index eba28fd..67e9504 100644 --- a/crates/systemsim/src/controller/null.rs +++ b/crates/system/src/phys/controller/null.rs @@ -1,10 +1,12 @@ use rapier2d::{dynamics::RigidBodySet, geometry::ColliderHandle}; use std::collections::HashMap; -use super::ShipControllerStruct; -use crate::{ - objects::{ShipControls, SySimShip}, - StepResources, +use super::{ + super::{ + objects::{PhysSimShip, ShipControls}, + PhysStepResources, + }, + ShipControllerStruct, }; /// The Null controller is assigned to objects that are static or not controlled by the computer. @@ -22,9 +24,9 @@ impl NullShipController { impl ShipControllerStruct for NullShipController { fn update_controls( &mut self, - _res: &StepResources, + _res: &PhysStepResources, _rigid_bodies: &RigidBodySet, - _ships: &HashMap, + _ships: &HashMap, _this_ship: ColliderHandle, ) -> Option { None diff --git a/crates/systemsim/src/controller/point.rs b/crates/system/src/phys/controller/point.rs similarity index 91% rename from crates/systemsim/src/controller/point.rs rename to crates/system/src/phys/controller/point.rs index 2a4828f..53df5db 100644 --- a/crates/systemsim/src/controller/point.rs +++ b/crates/system/src/phys/controller/point.rs @@ -3,10 +3,12 @@ use galactica_content::Relationship; use rapier2d::{dynamics::RigidBodySet, geometry::ColliderHandle}; use std::collections::HashMap; -use super::ShipControllerStruct; -use crate::{ - objects::{ShipControls, SySimShip}, - util, StepResources, +use super::{ + super::{ + objects::{PhysSimShip, ShipControls}, + util, PhysStepResources, + }, + ShipControllerStruct, }; /// "Point" ship controller. @@ -24,9 +26,9 @@ impl PointShipController { impl ShipControllerStruct for PointShipController { fn update_controls( &mut self, - res: &StepResources, + res: &PhysStepResources, rigid_bodies: &RigidBodySet, - ships: &HashMap, + ships: &HashMap, this_ship: ColliderHandle, ) -> Option { let mut controls = ShipControls::new(); diff --git a/crates/systemsim/src/lib.rs b/crates/system/src/phys/mod.rs similarity index 57% rename from crates/systemsim/src/lib.rs rename to crates/system/src/phys/mod.rs index d759dd5..fcfaac9 100644 --- a/crates/systemsim/src/lib.rs +++ b/crates/system/src/phys/mod.rs @@ -1,6 +1,4 @@ -#![warn(missing_docs)] - -//! This module provides a physics-based simulation of one galaxy system. +//! This module provides a physics-based simulation of one system. pub mod controller; pub mod objects; @@ -12,5 +10,5 @@ mod wrapper; pub use particlebuilder::*; pub use stepresources::*; -pub use systemsim::{SySimShipHandle, SystemSim}; +pub use systemsim::{PhysSim, PhysSimShipHandle}; pub use wrapper::Wrapper; diff --git a/crates/systemsim/src/objects/collapse.rs b/crates/system/src/phys/objects/collapse.rs similarity index 95% rename from crates/systemsim/src/objects/collapse.rs rename to crates/system/src/phys/objects/collapse.rs index ee63226..934efe3 100644 --- a/crates/systemsim/src/objects/collapse.rs +++ b/crates/system/src/phys/objects/collapse.rs @@ -1,11 +1,11 @@ use cgmath::{Deg, EuclideanSpace, InnerSpace, Matrix2, Rad, Vector2, Zero}; use galactica_content::{CollapseEvent, Ship}; -use galactica_galaxy::ship::GxShip; use nalgebra::point; use rand::{rngs::ThreadRng, Rng}; use rapier2d::{dynamics::RigidBody, geometry::Collider}; -use crate::{util, ParticleBuilder, StepResources}; +use super::super::{util, ParticleBuilder, PhysStepResources}; +use crate::data::ShipData; #[derive(Debug, Clone)] pub(super) struct ShipCollapseSequence { @@ -40,8 +40,8 @@ impl ShipCollapseSequence { /// Step this sequence `t` seconds pub(super) fn step( &mut self, - res: &mut StepResources, - ship_data: &GxShip, + res: &mut PhysStepResources, + ship_data: &ShipData, rigid_body: &mut RigidBody, collider: &mut Collider, ) { @@ -57,6 +57,8 @@ impl ShipCollapseSequence { // The fraction of this collapse sequence that has been played let frac_done = elapsed / total; + // TODO: slight random offset for event particles + // Trigger collapse events for event in &ship_content.collapse.events { match event { diff --git a/crates/systemsim/src/objects/mod.rs b/crates/system/src/phys/objects/mod.rs similarity index 60% rename from crates/systemsim/src/objects/mod.rs rename to crates/system/src/phys/objects/mod.rs index d73def0..1b5e903 100644 --- a/crates/systemsim/src/objects/mod.rs +++ b/crates/system/src/phys/objects/mod.rs @@ -3,5 +3,5 @@ mod collapse; mod projectile; mod ship; -pub use projectile::SySimProjectile; -pub use ship::{ShipControls, SySimShip}; +pub use projectile::PhysProjectile; +pub use ship::{PhysSimShip, ShipControls}; diff --git a/crates/systemsim/src/objects/projectile.rs b/crates/system/src/phys/objects/projectile.rs similarity index 91% rename from crates/systemsim/src/objects/projectile.rs rename to crates/system/src/phys/objects/projectile.rs index 62770b5..68b8c85 100644 --- a/crates/systemsim/src/objects/projectile.rs +++ b/crates/system/src/phys/objects/projectile.rs @@ -2,9 +2,9 @@ use galactica_content::{FactionHandle, Projectile}; use rand::Rng; use rapier2d::{dynamics::RigidBodyHandle, geometry::ColliderHandle}; -/// A single projectile in this sim +/// A single projectile in this simulation #[derive(Debug, Clone)] -pub struct SySimProjectile { +pub struct PhysProjectile { /// This projectile's game data pub content: Projectile, @@ -24,7 +24,7 @@ pub struct SySimProjectile { pub size_rng: f32, } -impl SySimProjectile { +impl PhysProjectile { /// Create a new projectile pub fn new( content: Projectile, // TODO: use a handle @@ -35,7 +35,7 @@ impl SySimProjectile { let mut rng = rand::thread_rng(); let size_rng = content.size_rng; let lifetime = content.lifetime; - SySimProjectile { + PhysProjectile { rigid_body, collider, content, diff --git a/crates/systemsim/src/objects/ship.rs b/crates/system/src/phys/objects/ship.rs similarity index 91% rename from crates/systemsim/src/objects/ship.rs rename to crates/system/src/phys/objects/ship.rs index 53e1656..0e03ad8 100644 --- a/crates/systemsim/src/objects/ship.rs +++ b/crates/system/src/phys/objects/ship.rs @@ -1,6 +1,5 @@ use cgmath::{Deg, EuclideanSpace, InnerSpace, Matrix2, Rad, Vector2, Zero}; use galactica_content::{Content, FactionHandle, ShipHandle}; -use galactica_galaxy::ship::{GxShip, ShipPersonality, ShipState}; use nalgebra::{point, vector}; use rand::Rng; use rapier2d::{ @@ -8,9 +7,12 @@ use rapier2d::{ geometry::{Collider, ColliderHandle}, }; -use crate::{util, ParticleBuilder, StepResources}; +use crate::data::{ShipData, ShipPersonality, ShipState}; -use super::collapse::ShipCollapseSequence; +use super::{ + super::{util, ParticleBuilder, PhysStepResources}, + collapse::ShipCollapseSequence, +}; /// A ship's controls #[derive(Debug, Clone)] @@ -42,7 +44,7 @@ impl ShipControls { /// A ship instance in the physics system #[derive(Debug, Clone)] -pub struct SySimShip { +pub struct PhysSimShip { /// This ship's physics handle pub rigid_body: RigidBodyHandle, @@ -50,7 +52,7 @@ pub struct SySimShip { pub collider: ColliderHandle, /// This ship's game data - pub data: GxShip, + pub data: ShipData, /// This ship's controls pub(crate) controls: ShipControls, @@ -59,7 +61,7 @@ pub struct SySimShip { collapse_sequence: Option, } -impl SySimShip { +impl PhysSimShip { /// Make a new ship pub(crate) fn new( ct: &Content, @@ -69,10 +71,10 @@ impl SySimShip { rigid_body: RigidBodyHandle, collider: ColliderHandle, ) -> Self { - SySimShip { + PhysSimShip { rigid_body, collider, - data: GxShip::new(ct, handle, faction, personality), + data: ShipData::new(ct, handle, faction, personality), controls: ShipControls::new(), collapse_sequence: Some(ShipCollapseSequence::new()), } @@ -81,7 +83,7 @@ impl SySimShip { /// Step this ship's state by t seconds pub fn step( &mut self, - res: &mut StepResources, + res: &mut PhysStepResources, rigid_body: &mut RigidBody, collider: &mut Collider, ) { @@ -103,7 +105,7 @@ impl SySimShip { /// Step this ship's state by t seconds (called when alive) fn step_live( &mut self, - res: &mut StepResources, + res: &mut PhysStepResources, rigid_body: &mut RigidBody, collider: &mut Collider, ) { @@ -180,7 +182,7 @@ impl SySimShip { } } -impl SySimShip { +impl PhysSimShip { /// Get this ship's control state pub fn get_controls(&self) -> &ShipControls { &self.controls diff --git a/crates/systemsim/src/particlebuilder.rs b/crates/system/src/phys/particlebuilder.rs similarity index 100% rename from crates/systemsim/src/particlebuilder.rs rename to crates/system/src/phys/particlebuilder.rs diff --git a/crates/systemsim/src/stepresources.rs b/crates/system/src/phys/stepresources.rs similarity index 82% rename from crates/systemsim/src/stepresources.rs rename to crates/system/src/phys/stepresources.rs index 32a342d..5518152 100644 --- a/crates/systemsim/src/stepresources.rs +++ b/crates/system/src/phys/stepresources.rs @@ -1,9 +1,9 @@ -use crate::{wrapper::Wrapper, ParticleBuilder}; +use super::{wrapper::Wrapper, ParticleBuilder}; use galactica_content::Content; use galactica_util::timing::Timing; /// External resources we need to compute time steps -pub struct StepResources<'a> { +pub struct PhysStepResources<'a> { /// Game content pub ct: &'a Content, diff --git a/crates/systemsim/src/systemsim.rs b/crates/system/src/phys/systemsim.rs similarity index 90% rename from crates/systemsim/src/systemsim.rs rename to crates/system/src/phys/systemsim.rs index 529fbdd..dd4f8d9 100644 --- a/crates/systemsim/src/systemsim.rs +++ b/crates/system/src/phys/systemsim.rs @@ -3,7 +3,6 @@ use galactica_content::{ Content, FactionHandle, GunPoint, OutfitHandle, ProjectileCollider, Relationship, ShipHandle, SystemHandle, }; -use galactica_galaxy::ship::ShipPersonality; use galactica_playeragent::PlayerAgent; use nalgebra::{point, vector}; use rand::Rng; @@ -14,39 +13,41 @@ use rapier2d::{ }; use std::{collections::HashMap, f32::consts::PI}; -use crate::{ +use crate::data::ShipPersonality; + +use super::{ controller::ShipController, - objects::{SySimProjectile, SySimShip}, - util, ParticleBuilder, StepResources, + objects::{PhysProjectile, PhysSimShip}, + util, ParticleBuilder, PhysStepResources, }; // TODO: replace with a more generic handle /// A handle for a ship in this simulation /// This lets other crates reference ships /// without including `rapier2d`. -pub struct SySimShipHandle(pub ColliderHandle); +pub struct PhysSimShipHandle(pub ColliderHandle); /// Manages the physics state of one system #[derive(Clone)] -pub struct SystemSim { +pub struct PhysSim { /// The system this sim is attached to _system: SystemHandle, rigid_body_set: RigidBodySet, collider_set: ColliderSet, - projectiles: HashMap, - ships: HashMap, + projectiles: HashMap, + ships: HashMap, ship_behaviors: HashMap, } // Private methods -impl<'a> SystemSim { +impl<'a> PhysSim { fn remove_projectile( &mut self, - res: &mut StepResources, + res: &mut PhysStepResources, c: ColliderHandle, - ) -> Option<(RigidBody, SySimProjectile)> { + ) -> Option<(RigidBody, PhysProjectile)> { let p = match self.projectiles.remove(&c) { Some(p) => p, None => return None, @@ -66,7 +67,7 @@ impl<'a> SystemSim { return Some((r, p)); } - fn remove_ship(&mut self, res: &mut StepResources, colliderhandle: ColliderHandle) { + fn remove_ship(&mut self, res: &mut PhysStepResources, colliderhandle: ColliderHandle) { let ship = match self.ships.get(&colliderhandle) { None => return, Some(s) => s, @@ -86,7 +87,7 @@ impl<'a> SystemSim { fn collide_projectile_ship( &mut self, - res: &mut StepResources, + res: &mut PhysStepResources, projectile_h: ColliderHandle, ship_h: ColliderHandle, ) { @@ -151,7 +152,7 @@ impl<'a> SystemSim { } // Public methods -impl SystemSim { +impl PhysSim { /// Create a new physics system pub fn new(_ct: &Content, system: SystemHandle) -> Self { Self { @@ -172,7 +173,7 @@ impl SystemSim { faction: FactionHandle, personality: ShipPersonality, position: Point2, - ) -> SySimShipHandle { + ) -> PhysSimShipHandle { let ship_content = ct.get_ship(handle); let cl = ColliderBuilder::convex_decomposition( &ship_content.collision.points[..], @@ -205,10 +206,10 @@ impl SystemSim { self.ships.insert( collider, - SySimShip::new(ct, handle, personality, faction, ridid_body, collider), + PhysSimShip::new(ct, handle, personality, faction, ridid_body, collider), ); - return SySimShipHandle(collider); + return PhysSimShipHandle(collider); } /// Update a player ship's controls @@ -229,7 +230,7 @@ impl SystemSim { /// - updates ship controls (runs behaviors) /// - applies ship controls /// - creates projectiles - fn step_ships(&mut self, res: &mut StepResources) { + fn step_ships(&mut self, res: &mut PhysStepResources) { // We can't apply these right away since self is borrowed // by the iterator // TODO: don't allocate! @@ -350,7 +351,7 @@ impl SystemSim { self.projectiles.insert( collider.clone(), - SySimProjectile::new( + PhysProjectile::new( outfit.projectile.clone(), rigid_body, ship.data.get_faction(), @@ -361,7 +362,7 @@ impl SystemSim { } /// Step this physics system by `t` seconds - pub fn step(&mut self, mut res: StepResources) { + pub fn step(&mut self, mut res: PhysStepResources) { res.timing.start_physics_ships(); self.step_ships(&mut res); res.timing.mark_physics_ships(); @@ -441,14 +442,14 @@ impl SystemSim { } // Public getters -impl SystemSim { +impl PhysSim { /// Get a ship physics object - pub fn get_ship(&self, ship: &SySimShipHandle) -> Option<&SySimShip> { + pub fn get_ship(&self, ship: &PhysSimShipHandle) -> Option<&PhysSimShip> { self.ships.get(&ship.0) } /// Get a ship physics object - pub fn get_ship_mut(&mut self, ship: &SySimShipHandle) -> Option<&mut SySimShip> { + pub fn get_ship_mut(&mut self, ship: &PhysSimShipHandle) -> Option<&mut PhysSimShip> { self.ships.get_mut(&ship.0) } @@ -463,19 +464,19 @@ impl SystemSim { } /// Iterate over all ships in this physics system - pub fn iter_ship_body(&self) -> impl Iterator + '_ { + pub fn iter_ship_body(&self) -> impl Iterator + '_ { self.ships .values() .map(|x| (x, self.rigid_body_set.get(x.rigid_body).unwrap())) } /// Iterate over all ships in this physics system - pub fn iter_ships(&self) -> impl Iterator + '_ { + pub fn iter_ships(&self) -> impl Iterator + '_ { self.ships.values() } /// Iterate over all ships in this physics system - pub fn iter_projectiles(&self) -> impl Iterator + '_ { + pub fn iter_projectiles(&self) -> impl Iterator + '_ { self.projectiles.values() } } diff --git a/crates/systemsim/src/util.rs b/crates/system/src/phys/util.rs similarity index 100% rename from crates/systemsim/src/util.rs rename to crates/system/src/phys/util.rs diff --git a/crates/systemsim/src/wrapper.rs b/crates/system/src/phys/wrapper.rs similarity index 100% rename from crates/systemsim/src/wrapper.rs rename to crates/system/src/phys/wrapper.rs diff --git a/crates/util/src/timing.rs b/crates/util/src/timing.rs index 06c9812..16dbdda 100644 --- a/crates/util/src/timing.rs +++ b/crates/util/src/timing.rs @@ -48,7 +48,7 @@ impl Timing { self.physics_ship_timer = Instant::now(); } - /// Record galaxy simulation time. + /// Record total frame compute time pub fn mark_frame(&mut self) { self.frame = self.frame_timer.elapsed(); }