Compare commits
2 Commits
5015040226
...
9f1db1c7b6
Author | SHA1 | Date |
---|---|---|
Mark | 9f1db1c7b6 | |
Mark | e60670c189 |
|
@ -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,9 +686,9 @@ dependencies = [
|
|||
"anyhow",
|
||||
"cgmath",
|
||||
"galactica-content",
|
||||
"galactica-galaxy",
|
||||
"galactica-util",
|
||||
"pollster",
|
||||
"rapier2d",
|
||||
"wgpu",
|
||||
"winit",
|
||||
]
|
||||
|
@ -711,9 +701,8 @@ dependencies = [
|
|||
"bytemuck",
|
||||
"cgmath",
|
||||
"galactica-content",
|
||||
"galactica-galaxy",
|
||||
"galactica-packer",
|
||||
"galactica-systemsim",
|
||||
"galactica-system",
|
||||
"galactica-util",
|
||||
"glyphon",
|
||||
"image",
|
||||
|
@ -723,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",
|
||||
|
|
|
@ -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" }
|
||||
|
|
|
@ -207,7 +207,7 @@ impl crate::Build for System {
|
|||
});
|
||||
}
|
||||
|
||||
objects.sort_by(|a, b| a.pos.z.total_cmp(&b.pos.z));
|
||||
objects.sort_by(|a, b| b.pos.z.total_cmp(&a.pos.z));
|
||||
content.systems.push(Self {
|
||||
name: system_name,
|
||||
objects,
|
||||
|
|
|
@ -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 }
|
||||
|
|
|
@ -1,16 +1,17 @@
|
|||
use cgmath::Point2;
|
||||
use galactica_content::{Content, FactionHandle, OutfitHandle, ShipHandle, SystemHandle};
|
||||
use galactica_galaxy::{ship::ShipPersonality, Galaxy, GxShipHandle};
|
||||
use galactica_playeragent::PlayerAgent;
|
||||
use galactica_systemsim::{ParticleBuilder, StepResources, 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 gx: Galaxy,
|
||||
pub systemsim: SystemSim,
|
||||
pub systemsim: PhysSim,
|
||||
pub timing: Timing,
|
||||
pub start_instant: Instant,
|
||||
}
|
||||
|
@ -37,57 +38,56 @@ pub struct Game {
|
|||
unsafe impl<'a> Send for Game {}
|
||||
|
||||
impl<'a> Game {
|
||||
pub fn make_player(&mut self) -> GxShipHandle {
|
||||
let player = self.state.gx.create_ship(
|
||||
pub fn make_player(&mut self) -> PhysSimShipHandle {
|
||||
let player = self.state.systemsim.add_ship(
|
||||
&self.ct,
|
||||
ShipHandle { index: 0 },
|
||||
FactionHandle { index: 0 },
|
||||
ShipPersonality::Player,
|
||||
&SystemHandle { index: 0 },
|
||||
Point2 { x: 0.0, y: 0.0 },
|
||||
);
|
||||
let s = self.state.gx.get_ship_mut(player).unwrap();
|
||||
s.add_outfit(&self.ct.get_outfit(OutfitHandle { index: 0 }));
|
||||
s.add_outfit(&self.ct.get_outfit(OutfitHandle { index: 1 }));
|
||||
s.add_outfit(&self.ct.get_outfit(OutfitHandle { index: 2 }));
|
||||
|
||||
self.state
|
||||
.systemsim
|
||||
.add_ship(&self.ct, &s, Point2 { x: 0.0, y: 0.0 });
|
||||
let s = self.state.systemsim.get_ship_mut(&player).unwrap();
|
||||
s.data
|
||||
.add_outfit(&self.ct.get_outfit(OutfitHandle { index: 0 }));
|
||||
s.data
|
||||
.add_outfit(&self.ct.get_outfit(OutfitHandle { index: 1 }));
|
||||
s.data
|
||||
.add_outfit(&self.ct.get_outfit(OutfitHandle { index: 2 }));
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
pub fn new(ct: Content) -> Self {
|
||||
let mut gx = Galaxy::new(&ct);
|
||||
let mut systemsim = PhysSim::new(&ct, SystemHandle { index: 0 });
|
||||
|
||||
let a = gx.create_ship(
|
||||
let a = systemsim.add_ship(
|
||||
&ct,
|
||||
ShipHandle { index: 0 },
|
||||
FactionHandle { index: 1 },
|
||||
ShipPersonality::Dummy,
|
||||
&SystemHandle { index: 0 },
|
||||
ShipPersonality::Point,
|
||||
Point2 { x: 100.0, y: 0.0 },
|
||||
);
|
||||
let s = gx.get_ship_mut(a).unwrap();
|
||||
s.add_outfit(&ct.get_outfit(OutfitHandle { index: 0 }));
|
||||
s.add_outfit(&ct.get_outfit(OutfitHandle { index: 1 }));
|
||||
s.add_outfit(&ct.get_outfit(OutfitHandle { index: 2 }));
|
||||
|
||||
let a = gx.create_ship(
|
||||
let s = systemsim.get_ship_mut(&a).unwrap();
|
||||
s.data.add_outfit(ct.get_outfit(OutfitHandle { index: 0 }));
|
||||
s.data.add_outfit(&ct.get_outfit(OutfitHandle { index: 1 }));
|
||||
s.data.add_outfit(&ct.get_outfit(OutfitHandle { index: 2 }));
|
||||
|
||||
let a = systemsim.add_ship(
|
||||
&ct,
|
||||
ShipHandle { index: 0 },
|
||||
FactionHandle { index: 0 },
|
||||
ShipPersonality::Point,
|
||||
&SystemHandle { index: 0 },
|
||||
Point2 { x: 0.0, y: 120.0 },
|
||||
);
|
||||
let s = gx.get_ship_mut(a).unwrap();
|
||||
s.add_outfit(&ct.get_outfit(OutfitHandle { index: 0 }));
|
||||
s.add_outfit(&ct.get_outfit(OutfitHandle { index: 1 }));
|
||||
s.add_outfit(&ct.get_outfit(OutfitHandle { index: 2 }));
|
||||
|
||||
let systemsim = SystemSim::new(&ct, &gx, SystemHandle { index: 0 });
|
||||
let s = systemsim.get_ship_mut(&a).unwrap();
|
||||
s.data.add_outfit(ct.get_outfit(OutfitHandle { index: 0 }));
|
||||
s.data.add_outfit(&ct.get_outfit(OutfitHandle { index: 1 }));
|
||||
s.data.add_outfit(&ct.get_outfit(OutfitHandle { index: 2 }));
|
||||
|
||||
let state = GameState {
|
||||
gx,
|
||||
systemsim,
|
||||
timing: Timing::new(),
|
||||
start_instant: Instant::now(),
|
||||
|
@ -121,13 +121,8 @@ impl<'a> Game {
|
|||
let t: f32 = self.last_update.elapsed().as_secs_f32() * self.time_scale;
|
||||
self.new_particles.clear();
|
||||
|
||||
self.state.timing.start_galaxy();
|
||||
self.state.gx.step(t);
|
||||
self.state.timing.mark_galaxy();
|
||||
|
||||
self.state.systemsim.step(StepResources {
|
||||
self.state.systemsim.step(PhysStepResources {
|
||||
ct: &self.ct,
|
||||
gx: &mut self.state.gx,
|
||||
particles: &mut self.new_particles,
|
||||
timing: &mut self.state.timing,
|
||||
wrapper: &mut self.wrapper,
|
||||
|
|
|
@ -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;
|
||||
use galactica_system::phys::{util, PhysSimShipHandle};
|
||||
use galactica_util::constants::ASSET_CACHE;
|
||||
use std::{
|
||||
fs,
|
||||
|
@ -41,7 +41,7 @@ fn main() -> Result<()> {
|
|||
let mut game = game::Game::new(content.clone());
|
||||
let p = game.make_player();
|
||||
|
||||
let mut player = PlayerAgent::new(p);
|
||||
let mut player = PlayerAgent::new(p.0);
|
||||
player.set_camera_aspect(
|
||||
gpu.window().inner_size().width as f32 / gpu.window().inner_size().height as f32,
|
||||
);
|
||||
|
@ -56,8 +56,7 @@ fn main() -> Result<()> {
|
|||
ct: &content,
|
||||
systemsim: &game.get_state().systemsim,
|
||||
particles: game.get_particles(),
|
||||
player_data: player.ship.unwrap(),
|
||||
gx: &game.get_state().gx,
|
||||
player_ship: PhysSimShipHandle(player.ship.unwrap()),
|
||||
current_system: SystemHandle { index: 0 },
|
||||
timing: game.get_state().timing.clone(),
|
||||
};
|
||||
|
@ -77,7 +76,10 @@ fn main() -> Result<()> {
|
|||
|
||||
let player_status = {
|
||||
let pos = {
|
||||
let o = &game.get_state().systemsim.get_ship(player.ship.unwrap());
|
||||
let o = &game
|
||||
.get_state()
|
||||
.systemsim
|
||||
.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 {
|
||||
|
|
|
@ -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 }
|
|
@ -1,98 +0,0 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use crate::{handles::GxShipHandle, ship::GxShip, ship::ShipPersonality};
|
||||
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, Clone)]
|
||||
pub struct Galaxy {
|
||||
/// Universal counter.
|
||||
/// Used to create unique handles for game objects.
|
||||
index: u64,
|
||||
|
||||
/// All ships in the galaxy
|
||||
ships: HashMap<GxShipHandle, GxShip>,
|
||||
|
||||
/// Ships indexed by the system they're in.
|
||||
/// A ship must always be in exactly one system.
|
||||
system_ship_table: HashMap<SystemHandle, Vec<GxShipHandle>>,
|
||||
|
||||
/// Systems indexed by which ships they contain.
|
||||
/// A ship must always be in exactly one system.
|
||||
ship_system_table: HashMap<GxShipHandle, SystemHandle>,
|
||||
}
|
||||
|
||||
impl Galaxy {
|
||||
pub fn new(ct: &Content) -> Self {
|
||||
Self {
|
||||
system_ship_table: ct.iter_systems().map(|s| (s, Vec::new())).collect(),
|
||||
ship_system_table: HashMap::new(),
|
||||
index: 0,
|
||||
ships: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawn a ship
|
||||
pub fn create_ship(
|
||||
&mut self,
|
||||
ct: &Content,
|
||||
ship: ShipHandle,
|
||||
faction: FactionHandle,
|
||||
personality: ShipPersonality,
|
||||
system: &SystemHandle,
|
||||
) -> GxShipHandle {
|
||||
let handle = GxShipHandle {
|
||||
index: self.index,
|
||||
content: ship,
|
||||
};
|
||||
self.index += 1;
|
||||
|
||||
self.ships
|
||||
.insert(handle, GxShip::new(ct, handle, ship, faction, personality));
|
||||
self.system_ship_table.get_mut(system).unwrap().push(handle);
|
||||
self.ship_system_table.insert(handle, *system);
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
pub fn step(&mut self, t: f32) {
|
||||
// TODO: don't allocate on step, need a better
|
||||
// way to satisfy the borrow checker.
|
||||
// Same needs to be done in the `systemsim` crate.
|
||||
let mut to_remove = Vec::new();
|
||||
for (_, s) in &mut self.ships {
|
||||
s.step(t);
|
||||
|
||||
if s.is_dead() {
|
||||
to_remove.push(s.get_handle());
|
||||
}
|
||||
}
|
||||
|
||||
// Remove dead ships
|
||||
// No fancy animation here, that's handled by the physics system.
|
||||
for i in to_remove {
|
||||
let system = self.ship_system_table.remove(&i).unwrap();
|
||||
self.system_ship_table
|
||||
.get_mut(&system)
|
||||
.unwrap()
|
||||
.retain(|x| *x != i);
|
||||
self.ships.remove(&i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Public getters
|
||||
impl Galaxy {
|
||||
pub fn get_ship(&self, handle: GxShipHandle) -> Option<&GxShip> {
|
||||
self.ships.get(&handle)
|
||||
}
|
||||
|
||||
pub fn get_ship_mut(&mut self, handle: GxShipHandle) -> Option<&mut GxShip> {
|
||||
self.ships.get_mut(&handle)
|
||||
}
|
||||
|
||||
pub fn iter_ships(&self) -> impl Iterator<Item = &GxShip> {
|
||||
self.ships.values()
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
use std::hash::Hash;
|
||||
|
||||
use galactica_content::ShipHandle;
|
||||
|
||||
/// A lightweight representation of a ship in the galaxy
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct GxShipHandle {
|
||||
/// This ship's unique index
|
||||
pub(crate) index: u64,
|
||||
|
||||
/// This ship's content handle
|
||||
/// (technically redundant, but this greatly simplifies code)
|
||||
pub(crate) content: ShipHandle,
|
||||
}
|
||||
|
||||
impl GxShipHandle {
|
||||
pub fn content_handle(&self) -> ShipHandle {
|
||||
self.content
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for GxShipHandle {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
self.index.hash(state)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for GxShipHandle {}
|
||||
impl PartialEq for GxShipHandle {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.index.eq(&other.index)
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
//! This module keeps track of the galaxy state.
|
||||
//! It is also responsible for ship stats, outfits, etc.
|
||||
//!
|
||||
//! `galaxy` does not provide any simulation logic---that is done in seperate crates.
|
||||
|
||||
mod galaxy;
|
||||
mod handles;
|
||||
pub mod ship;
|
||||
|
||||
pub use galaxy::*;
|
||||
pub use handles::*;
|
|
@ -19,10 +19,10 @@ workspace = true
|
|||
[dependencies]
|
||||
galactica-content = { workspace = true }
|
||||
galactica-util = { workspace = true }
|
||||
galactica-galaxy = { workspace = true }
|
||||
|
||||
winit = { workspace = true }
|
||||
wgpu = { workspace = true }
|
||||
pollster = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
cgmath = { workspace = true }
|
||||
rapier2d = { workspace = true }
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
use galactica_content::Content;
|
||||
use galactica_galaxy::GxShipHandle;
|
||||
use rapier2d::geometry::ColliderHandle;
|
||||
use winit::event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode};
|
||||
|
||||
use crate::{camera::Camera, inputstatus::InputStatus, PlayerStatus};
|
||||
|
||||
pub struct PlayerAgent {
|
||||
/// Which ship this player is controlling
|
||||
pub ship: Option<GxShipHandle>,
|
||||
pub ship: Option<ColliderHandle>,
|
||||
|
||||
/// This player's camera
|
||||
pub camera: Camera,
|
||||
|
@ -19,7 +19,7 @@ pub struct PlayerAgent {
|
|||
}
|
||||
|
||||
impl PlayerAgent {
|
||||
pub fn new(ship: GxShipHandle) -> Self {
|
||||
pub fn new(ship: ColliderHandle) -> Self {
|
||||
Self {
|
||||
input: InputStatus::new(),
|
||||
camera: Camera::new(),
|
||||
|
|
|
@ -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 }
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use cgmath::Point2;
|
||||
use galactica_content::{Content, SystemHandle};
|
||||
use galactica_galaxy::{Galaxy, GxShipHandle};
|
||||
use galactica_systemsim::{ParticleBuilder, SystemSim};
|
||||
use galactica_system::phys::{ParticleBuilder, PhysSim, PhysSimShipHandle};
|
||||
use galactica_util::timing::Timing;
|
||||
use glyphon::{FontSystem, SwashCache, TextAtlas, TextRenderer};
|
||||
use std::rc::Rc;
|
||||
|
@ -16,7 +15,7 @@ pub struct RenderInput<'a> {
|
|||
pub camera_pos: Point2<f32>,
|
||||
|
||||
/// Player ship data
|
||||
pub player_data: GxShipHandle,
|
||||
pub player_ship: PhysSimShipHandle,
|
||||
|
||||
/// The system we're currently in
|
||||
pub current_system: SystemHandle,
|
||||
|
@ -25,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
|
||||
|
@ -34,9 +33,6 @@ pub struct RenderInput<'a> {
|
|||
/// Game content
|
||||
pub ct: &'a Content,
|
||||
|
||||
/// Game data
|
||||
pub gx: &'a Galaxy,
|
||||
|
||||
/// Particles to spawn during this frame
|
||||
pub particles: &'a Vec<ParticleBuilder>,
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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,18 +12,18 @@ 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
|
||||
screen_clip: (Point2<f32>, Point2<f32>),
|
||||
) {
|
||||
for s in state.systemsim.iter_ships() {
|
||||
let r = state.systemsim.get_rigid_body(s.rigid_body).unwrap();
|
||||
for ship in state.systemsim.iter_ships() {
|
||||
let r = state.systemsim.get_rigid_body(ship.rigid_body).unwrap();
|
||||
let ship_pos = util::rigidbody_position(&r);
|
||||
let ship_rot = util::rigidbody_rotation(r);
|
||||
let ship_ang = -ship_rot.angle(Vector2 { x: 0.0, y: 1.0 }); // TODO: inconsistent angles. Fix!
|
||||
let ship_cnt = state.ct.get_ship(s.data_handle.content_handle());
|
||||
let ship_cnt = state.ct.get_ship(ship.data.get_content());
|
||||
|
||||
// Position adjusted for parallax
|
||||
// TODO: adjust parallax for zoom?
|
||||
|
@ -80,16 +80,8 @@ impl GPUState {
|
|||
);
|
||||
self.state.vertex_buffers.object_counter += 1;
|
||||
|
||||
// This will be None if this ship is dead.
|
||||
// (physics object stays around to complete the death animation)
|
||||
// If that is the case, we're done, no flares to draw anyway!
|
||||
let ship = match state.gx.get_ship(s.data_handle) {
|
||||
None => continue,
|
||||
Some(s) => s,
|
||||
};
|
||||
|
||||
let flare = ship.get_outfits().get_flare_sprite(state.ct);
|
||||
if s.get_controls().thrust && flare.is_some() {
|
||||
let flare = ship.data.get_outfits().get_flare_sprite(state.ct);
|
||||
if ship.get_controls().thrust && flare.is_some() && ship.data.get_state().is_flying() {
|
||||
for engine_point in &ship_cnt.engines {
|
||||
self.state.queue.write_buffer(
|
||||
&self.state.global_uniform.object_buffer,
|
||||
|
@ -128,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
|
||||
|
@ -198,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
|
|
@ -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,
|
||||
|
|
|
@ -36,9 +36,9 @@ impl FpsIndicator {
|
|||
self.buffer.set_text(
|
||||
&mut state.text_font_system,
|
||||
&format!(
|
||||
"Frame: {:#?} ({:05.00})\nGame: {:05.02}%\nShips: {:05.02}%\nPhys: {:05.02}%\n",
|
||||
input.timing.frame, 1.0 /input.timing.frame.as_secs_f32(),
|
||||
100.0 * (input.timing.galaxy.as_secs_f32() / input.timing.frame.as_secs_f32()),
|
||||
"Frame: {:#?} ({:05.00})\nShips: {:05.02}%\nPhys: {:05.02}%\n",
|
||||
input.timing.frame,
|
||||
1.0 / input.timing.frame.as_secs_f32(),
|
||||
100.0 * (input.timing.physics_sim.as_secs_f32() / input.timing.frame.as_secs_f32()),
|
||||
100.0
|
||||
* (input.timing.physics_ship.as_secs_f32() / input.timing.frame.as_secs_f32()),
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use cgmath::{Deg, InnerSpace, Point2, Rad, Vector2};
|
||||
use galactica_systemsim::util;
|
||||
use galactica_system::{data::ShipState, phys::util};
|
||||
use galactica_util::constants::UI_SPRITE_INSTANCE_LIMIT;
|
||||
|
||||
use crate::{
|
||||
|
@ -31,13 +31,17 @@ impl Radar {
|
|||
|
||||
// TODO: maybe a cleaner solution for last posititon?
|
||||
// This is necessary because the player may be dead or landed
|
||||
if let Some(player_world_object) = input.systemsim.get_ship(input.player_data) {
|
||||
let player_ship = input.systemsim.get_ship(&input.player_ship).unwrap();
|
||||
|
||||
match player_ship.data.get_state() {
|
||||
ShipState::Flying | ShipState::Collapsing { .. } => {
|
||||
let player_body = input
|
||||
.systemsim
|
||||
.get_rigid_body(player_world_object.rigid_body)
|
||||
.get_rigid_body(player_ship.rigid_body)
|
||||
.unwrap();
|
||||
|
||||
self.last_player_position = util::rigidbody_position(player_body)
|
||||
}
|
||||
};
|
||||
|
||||
let planet_sprite = input.ct.get_sprite_handle("ui::planetblip");
|
||||
|
@ -115,17 +119,17 @@ impl Radar {
|
|||
for (s, r) in input.systemsim.iter_ship_body() {
|
||||
// This will be None if this ship is dead.
|
||||
// Stays around while the physics system runs a collapse sequence
|
||||
let color = match input.gx.get_ship(s.data_handle) {
|
||||
None => {
|
||||
let color = match s.data.get_state() {
|
||||
ShipState::Collapsing { .. } => {
|
||||
// TODO: configurable
|
||||
[0.2, 0.2, 0.2, 1.0]
|
||||
}
|
||||
Some(data) => {
|
||||
let c = input.ct.get_faction(data.get_faction()).color;
|
||||
ShipState::Flying => {
|
||||
let c = input.ct.get_faction(s.data.get_faction()).color;
|
||||
[c[0], c[1], c[2], 1.0]
|
||||
}
|
||||
};
|
||||
let ship = input.ct.get_ship(s.data_handle.content_handle());
|
||||
let ship = input.ct.get_ship(s.data.get_content());
|
||||
let size = (ship.size * ship.sprite.aspect) * ship_scale;
|
||||
let p = util::rigidbody_position(r);
|
||||
let d = (p - self.last_player_position) / radar_range;
|
||||
|
|
|
@ -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::{
|
||||
|
@ -29,19 +30,16 @@ impl Status {
|
|||
let current_shields;
|
||||
let current_hull;
|
||||
let max_hull;
|
||||
if let Some(player_world_object) = input.systemsim.get_ship(input.player_data) {
|
||||
let data = input.gx.get_ship(player_world_object.data_handle).unwrap();
|
||||
max_shields = data.get_outfits().get_shield_strength();
|
||||
current_shields = data.get_shields();
|
||||
current_hull = data.get_hull();
|
||||
max_hull = input.ct.get_ship(data.get_content()).hull;
|
||||
} else {
|
||||
// Sensible defaults if ship doesn't exist (landed or dead)
|
||||
max_shields = 1.0;
|
||||
current_shields = 0.0;
|
||||
current_hull = 0.0;
|
||||
max_hull = 1.0;
|
||||
};
|
||||
let player_ship = input.systemsim.get_ship(&input.player_ship).unwrap();
|
||||
|
||||
match player_ship.data.get_state() {
|
||||
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();
|
||||
max_hull = input.ct.get_ship(player_ship.data.get_content()).hull;
|
||||
}
|
||||
}
|
||||
|
||||
state.queue.write_buffer(
|
||||
&state.vertex_buffers.ui.instances,
|
||||
|
|
|
@ -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 }
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
//! This module contains data structs shared between all simulations
|
||||
|
||||
mod ship;
|
||||
pub use ship::*;
|
|
@ -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<SpriteHandle> {
|
||||
for i in self.outfits.keys() {
|
||||
let c = ct.get_outfit(*i);
|
|
@ -1,21 +1,81 @@
|
|||
use std::{collections::HashMap, time::Instant};
|
||||
|
||||
use crate::GxShipHandle;
|
||||
|
||||
use super::{OutfitSet, ShipPersonality};
|
||||
use galactica_content::{Content, FactionHandle, GunPoint, Outfit, ShipHandle};
|
||||
use rand::{rngs::ThreadRng, Rng};
|
||||
|
||||
/// Ship state machine.
|
||||
/// Any ship we keep track of is in one of these states.
|
||||
/// Dead ships don't exist---they removed once their collapse
|
||||
/// sequence fully plays out.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct GxShip {
|
||||
pub enum ShipState {
|
||||
/// This ship is alive and well in open space
|
||||
Flying, // TODO: system, position (also in collapse)?
|
||||
|
||||
/// This ship has been destroyed, and is playing its collapse sequence.
|
||||
Collapsing {
|
||||
/// Total collapse sequence length, in seconds
|
||||
total: f32,
|
||||
|
||||
/// How many seconds of the collapse sequence we've played
|
||||
elapsed: f32,
|
||||
},
|
||||
}
|
||||
|
||||
impl ShipState {
|
||||
/// True if this ship has been destroyed and has finished it's collapse sequence.
|
||||
/// Ships are deleted once this is true.
|
||||
pub fn should_be_removed(&self) -> bool {
|
||||
match self {
|
||||
Self::Collapsing { elapsed, total } => elapsed >= total,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Is this ship playing its collapse sequence?
|
||||
pub fn is_collapsing(&self) -> bool {
|
||||
match self {
|
||||
Self::Collapsing { .. } => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// If this ship is collapsing, return total collapse time and remaining collapse time.
|
||||
/// Otherwise, return None
|
||||
pub fn collapse_state(&self) -> Option<(f32, f32)> {
|
||||
match self {
|
||||
Self::Collapsing {
|
||||
total,
|
||||
elapsed: remaining,
|
||||
} => Some((*total, *remaining)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Is this ship flying in open space?
|
||||
pub fn is_flying(&self) -> bool {
|
||||
match self {
|
||||
Self::Flying => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents all attributes of a single ship
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ShipData {
|
||||
// Metadata values
|
||||
handle: GxShipHandle,
|
||||
ct_handle: ShipHandle,
|
||||
faction: FactionHandle,
|
||||
outfits: OutfitSet,
|
||||
|
||||
personality: ShipPersonality,
|
||||
|
||||
/// Ship state machine. Keeps track of all possible ship state.
|
||||
/// TODO: document this, draw a graph
|
||||
state: ShipState,
|
||||
|
||||
// State values
|
||||
// TODO: unified ship stats struct, like outfit space
|
||||
hull: f32,
|
||||
|
@ -28,17 +88,16 @@ pub struct GxShip {
|
|||
last_hit: Instant,
|
||||
}
|
||||
|
||||
impl GxShip {
|
||||
impl ShipData {
|
||||
/// Create a new ShipData
|
||||
pub(crate) fn new(
|
||||
ct: &Content,
|
||||
handle: GxShipHandle,
|
||||
ct_handle: ShipHandle,
|
||||
faction: FactionHandle,
|
||||
personality: ShipPersonality,
|
||||
) -> Self {
|
||||
let s = ct.get_ship(ct_handle);
|
||||
GxShip {
|
||||
handle,
|
||||
ShipData {
|
||||
ct_handle,
|
||||
faction,
|
||||
outfits: OutfitSet::new(s.space, &s.guns),
|
||||
|
@ -46,6 +105,9 @@ impl GxShip {
|
|||
last_hit: Instant::now(),
|
||||
rng: rand::thread_rng(),
|
||||
|
||||
// TODO: ships must always start landed on planets
|
||||
state: ShipState::Flying,
|
||||
|
||||
// Initial stats
|
||||
hull: s.hull,
|
||||
shields: 0.0,
|
||||
|
@ -65,16 +127,11 @@ impl GxShip {
|
|||
self.outfits.remove(o)
|
||||
}
|
||||
|
||||
/// If this ship is dead, it will be removed from the game.
|
||||
pub fn is_dead(&self) -> bool {
|
||||
self.hull <= 0.0
|
||||
}
|
||||
|
||||
/// Try to fire a gun.
|
||||
/// 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 {
|
||||
|
@ -93,8 +150,8 @@ impl GxShip {
|
|||
}
|
||||
|
||||
/// Hit this ship with the given amount of damage
|
||||
pub fn apply_damage(&mut self, mut d: f32) {
|
||||
if self.is_dead() {
|
||||
pub(crate) fn apply_damage(&mut self, ct: &Content, mut d: f32) {
|
||||
if self.state.is_collapsing() {
|
||||
return;
|
||||
}
|
||||
if self.shields >= d {
|
||||
|
@ -105,10 +162,26 @@ impl GxShip {
|
|||
self.hull = 0f32.max(self.hull - d);
|
||||
}
|
||||
self.last_hit = Instant::now();
|
||||
|
||||
if self.hull <= 0.0 {
|
||||
// This ship has been destroyed, update state
|
||||
self.state = ShipState::Collapsing {
|
||||
total: ct.get_ship(self.ct_handle).collapse.length,
|
||||
elapsed: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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, ..
|
||||
} => {
|
||||
*elapsed += t;
|
||||
}
|
||||
|
||||
ShipState::Flying => {
|
||||
// Cooldown guns
|
||||
for (_, c) in &mut self.gun_cooldowns {
|
||||
if *c > 0.0 {
|
||||
|
@ -130,13 +203,15 @@ impl GxShip {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Misc getters, so internal state is untouchable
|
||||
impl GxShip {
|
||||
/// Get a handle to this ship game object
|
||||
pub fn get_handle(&self) -> GxShipHandle {
|
||||
self.handle
|
||||
impl ShipData {
|
||||
/// Get this ship's state
|
||||
pub fn get_state(&self) -> &ShipState {
|
||||
&self.state
|
||||
}
|
||||
|
||||
/// Get a handle to this ship's content
|
|
@ -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;
|
|
@ -6,13 +6,12 @@ mod point;
|
|||
use null::*;
|
||||
use point::PointShipController;
|
||||
|
||||
use galactica_galaxy::GxShipHandle;
|
||||
use rapier2d::dynamics::{RigidBodyHandle, RigidBodySet};
|
||||
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
|
||||
|
@ -39,15 +38,14 @@ impl ShipController {
|
|||
/// Compute new ship controls from world state
|
||||
pub fn update_controls(
|
||||
&mut self,
|
||||
res: &StepResources,
|
||||
res: &PhysStepResources,
|
||||
rigid_bodies: &RigidBodySet,
|
||||
ships: &HashMap<GxShipHandle, SySimShip>,
|
||||
this_ship: RigidBodyHandle,
|
||||
this_data: GxShipHandle,
|
||||
ships: &HashMap<ColliderHandle, PhysSimShip>,
|
||||
this_ship: ColliderHandle,
|
||||
) -> 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),
|
||||
Self::Null(n) => n.update_controls(res, rigid_bodies, ships, this_ship),
|
||||
Self::Point(p) => p.update_controls(res, rigid_bodies, ships, this_ship),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -63,10 +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<GxShipHandle, SySimShip>,
|
||||
this_ship: RigidBodyHandle,
|
||||
this_data: GxShipHandle,
|
||||
ships: &HashMap<ColliderHandle, PhysSimShip>,
|
||||
this_ship: ColliderHandle,
|
||||
) -> Option<ShipControls>;
|
||||
}
|
|
@ -1,11 +1,12 @@
|
|||
use galactica_galaxy::GxShipHandle;
|
||||
use rapier2d::dynamics::{RigidBodyHandle, RigidBodySet};
|
||||
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.
|
||||
|
@ -23,11 +24,10 @@ impl NullShipController {
|
|||
impl ShipControllerStruct for NullShipController {
|
||||
fn update_controls(
|
||||
&mut self,
|
||||
_res: &StepResources,
|
||||
_res: &PhysStepResources,
|
||||
_rigid_bodies: &RigidBodySet,
|
||||
_ships: &HashMap<GxShipHandle, SySimShip>,
|
||||
_this_ship: RigidBodyHandle,
|
||||
_this_data: GxShipHandle,
|
||||
_ships: &HashMap<ColliderHandle, PhysSimShip>,
|
||||
_this_ship: ColliderHandle,
|
||||
) -> Option<ShipControls> {
|
||||
None
|
||||
}
|
|
@ -1,13 +1,14 @@
|
|||
use cgmath::{Deg, InnerSpace};
|
||||
use galactica_content::Relationship;
|
||||
use galactica_galaxy::GxShipHandle;
|
||||
use rapier2d::dynamics::{RigidBodyHandle, RigidBodySet};
|
||||
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.
|
||||
|
@ -25,37 +26,30 @@ impl PointShipController {
|
|||
impl ShipControllerStruct for PointShipController {
|
||||
fn update_controls(
|
||||
&mut self,
|
||||
res: &StepResources,
|
||||
res: &PhysStepResources,
|
||||
rigid_bodies: &RigidBodySet,
|
||||
ships: &HashMap<GxShipHandle, SySimShip>,
|
||||
this_ship: RigidBodyHandle,
|
||||
this_data: GxShipHandle,
|
||||
ships: &HashMap<ColliderHandle, PhysSimShip>,
|
||||
this_ship: ColliderHandle,
|
||||
) -> Option<ShipControls> {
|
||||
let mut controls = ShipControls::new();
|
||||
|
||||
let this_rigidbody = rigid_bodies.get(this_ship).unwrap();
|
||||
let my_data = res.gx.get_ship(this_data).unwrap();
|
||||
let my_ship = ships.get(&this_ship).unwrap();
|
||||
let this_rigidbody = rigid_bodies.get(my_ship.rigid_body).unwrap();
|
||||
let my_position = util::rigidbody_position(this_rigidbody);
|
||||
let my_rotation = util::rigidbody_rotation(this_rigidbody);
|
||||
let my_angvel = this_rigidbody.angvel();
|
||||
let my_faction = res.ct.get_faction(my_data.get_faction());
|
||||
let my_faction = res.ct.get_faction(my_ship.data.get_faction());
|
||||
|
||||
// Iterate all possible targets
|
||||
let mut hostile_ships = ships
|
||||
.values()
|
||||
.filter(|s| {
|
||||
let data = res.gx.get_ship(s.data_handle);
|
||||
if let Some(data) = data {
|
||||
match my_faction.relationships.get(&data.get_faction()).unwrap() {
|
||||
Relationship::Hostile => true,
|
||||
.filter(
|
||||
// Target only flying ships we're hostile towards
|
||||
|s| match my_faction.relationships.get(&s.data.get_faction()).unwrap() {
|
||||
Relationship::Hostile => s.data.get_state().is_flying(),
|
||||
_ => false,
|
||||
}
|
||||
} else {
|
||||
// This check is necessary---we don't want to target (or panic on)
|
||||
// ships that don't have data (and are thus playing their collapse animation)
|
||||
false
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
.map(|s| rigid_bodies.get(s.rigid_body).unwrap());
|
||||
|
||||
// Find the closest target
|
|
@ -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::SystemSim;
|
||||
pub use systemsim::{PhysSim, PhysSimShipHandle};
|
||||
pub use wrapper::Wrapper;
|
|
@ -1,41 +1,37 @@
|
|||
use cgmath::{Deg, EuclideanSpace, InnerSpace, Matrix2, Rad, Vector2, Zero};
|
||||
use galactica_content::{CollapseEvent, Ship, ShipHandle};
|
||||
use galactica_content::{CollapseEvent, Ship};
|
||||
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 {
|
||||
total_length: f32,
|
||||
time_elapsed: f32,
|
||||
rng: ThreadRng,
|
||||
|
||||
/// The elapsed collapse duration when step()
|
||||
/// was last called
|
||||
last_call: f32,
|
||||
}
|
||||
|
||||
impl ShipCollapseSequence {
|
||||
pub(super) fn new(total_length: f32) -> Self {
|
||||
pub(super) fn new() -> Self {
|
||||
Self {
|
||||
total_length,
|
||||
time_elapsed: 0.0,
|
||||
rng: rand::thread_rng(),
|
||||
last_call: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Has this sequence been fully played out?
|
||||
pub(super) fn is_done(&self) -> bool {
|
||||
self.time_elapsed >= self.total_length
|
||||
}
|
||||
|
||||
/// Pick a random points inside a ship's collider
|
||||
fn random_in_ship(&mut self, ship_content: &Ship, collider: &Collider) -> Vector2<f32> {
|
||||
fn random_in_ship(&mut self, ship: &Ship, collider: &Collider) -> Vector2<f32> {
|
||||
let mut y = 0.0;
|
||||
let mut x = 0.0;
|
||||
let mut a = false;
|
||||
while !a {
|
||||
y = self.rng.gen_range(-1.0..=1.0) * ship_content.size / 2.0;
|
||||
x = self.rng.gen_range(-1.0..=1.0) * ship_content.size * ship_content.sprite.aspect
|
||||
/ 2.0;
|
||||
y = self.rng.gen_range(-1.0..=1.0) * ship.size / 2.0;
|
||||
x = self.rng.gen_range(-1.0..=1.0) * ship.size * ship.sprite.aspect / 2.0;
|
||||
a = collider.shape().contains_local_point(&point![x, y]);
|
||||
}
|
||||
Vector2 { x, y }
|
||||
|
@ -44,25 +40,32 @@ impl ShipCollapseSequence {
|
|||
/// Step this sequence `t` seconds
|
||||
pub(super) fn step(
|
||||
&mut self,
|
||||
res: &mut StepResources,
|
||||
ship_handle: ShipHandle,
|
||||
res: &mut PhysStepResources,
|
||||
ship_data: &ShipData,
|
||||
rigid_body: &mut RigidBody,
|
||||
collider: &mut Collider,
|
||||
) {
|
||||
let ship_content = res.ct.get_ship(ship_handle);
|
||||
let ship_content = res.ct.get_ship(ship_data.get_content());
|
||||
let ship_pos = util::rigidbody_position(rigid_body);
|
||||
let ship_rot = util::rigidbody_rotation(rigid_body);
|
||||
let ship_ang = ship_rot.angle(Vector2 { x: 1.0, y: 0.0 });
|
||||
|
||||
let (total, elapsed) = ship_data.get_state().collapse_state().unwrap();
|
||||
// How much time has passed since step() was last called
|
||||
let delta = elapsed - self.last_call;
|
||||
|
||||
// The fraction of this collapse sequence that has been played
|
||||
let frac_done = self.time_elapsed / self.total_length;
|
||||
let frac_done = elapsed / total;
|
||||
|
||||
// TODO: slight random offset for event particles
|
||||
|
||||
// Trigger collapse events
|
||||
for event in &ship_content.collapse.events {
|
||||
match event {
|
||||
CollapseEvent::Effect(event) => {
|
||||
if (event.time > self.time_elapsed && event.time <= self.time_elapsed + res.t)
|
||||
|| (event.time == 0.0 && self.time_elapsed == 0.0)
|
||||
if (event.time > self.last_call && event.time <= elapsed)
|
||||
|| (event.time == 0.0 && self.last_call == 0.0)
|
||||
// ^^ Don't miss events scheduled at the very start of the sequence!
|
||||
{
|
||||
for spawner in &event.effects {
|
||||
let effect = res.ct.get_effect(spawner.effect);
|
||||
|
@ -112,7 +115,9 @@ impl ShipCollapseSequence {
|
|||
return y;
|
||||
};
|
||||
|
||||
let p_add = (res.t / self.total_length) * pdf(frac_done) * spawner.count;
|
||||
// Notice that we don't use res.t here, since ship state is updated earlier
|
||||
// ( through self.data.step() )
|
||||
let p_add = (delta / total) * pdf(frac_done) * spawner.count;
|
||||
|
||||
if self.rng.gen_range(0.0..=1.0) <= p_add {
|
||||
let pos = if let Some(pos) = spawner.pos {
|
||||
|
@ -138,6 +143,6 @@ impl ShipCollapseSequence {
|
|||
}
|
||||
}
|
||||
|
||||
self.time_elapsed += res.t;
|
||||
self.last_call = elapsed;
|
||||
}
|
||||
}
|
|
@ -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};
|
|
@ -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,
|
|
@ -1,6 +1,5 @@
|
|||
use cgmath::{Deg, EuclideanSpace, InnerSpace, Matrix2, Rad, Vector2, Zero};
|
||||
use galactica_content::{Content, FactionHandle};
|
||||
use galactica_galaxy::GxShipHandle;
|
||||
use galactica_content::{Content, FactionHandle, ShipHandle};
|
||||
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,83 +52,70 @@ pub struct SySimShip {
|
|||
pub collider: ColliderHandle,
|
||||
|
||||
/// This ship's game data
|
||||
pub data_handle: GxShipHandle,
|
||||
pub data: ShipData,
|
||||
|
||||
/// This ship's controls
|
||||
pub(crate) controls: ShipControls,
|
||||
|
||||
/// This ship's collapse sequence
|
||||
collapse_sequence: ShipCollapseSequence,
|
||||
|
||||
/// This ship's faction.
|
||||
/// This is technically redundant, faction is also stored in
|
||||
/// game data, but that's destroyed once the ship dies.
|
||||
/// We need the faction for the collapse sequence!
|
||||
faction: FactionHandle,
|
||||
collapse_sequence: Option<ShipCollapseSequence>,
|
||||
}
|
||||
|
||||
impl SySimShip {
|
||||
impl PhysSimShip {
|
||||
/// Make a new ship
|
||||
pub(crate) fn new(
|
||||
ct: &Content,
|
||||
data_handle: GxShipHandle,
|
||||
handle: ShipHandle,
|
||||
personality: ShipPersonality,
|
||||
faction: FactionHandle,
|
||||
rigid_body: RigidBodyHandle,
|
||||
collider: ColliderHandle,
|
||||
) -> Self {
|
||||
let ship_content = ct.get_ship(data_handle.content_handle());
|
||||
SySimShip {
|
||||
PhysSimShip {
|
||||
rigid_body,
|
||||
collider,
|
||||
data_handle,
|
||||
data: ShipData::new(ct, handle, faction, personality),
|
||||
controls: ShipControls::new(),
|
||||
faction,
|
||||
collapse_sequence: ShipCollapseSequence::new(ship_content.collapse.length),
|
||||
collapse_sequence: Some(ShipCollapseSequence::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// If this is true, remove this ship from the physics system.
|
||||
pub fn should_be_removed(&self) -> bool {
|
||||
self.collapse_sequence.is_done()
|
||||
}
|
||||
|
||||
/// 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,
|
||||
) {
|
||||
let ship_data = res.gx.get_ship(self.data_handle);
|
||||
if ship_data.is_none() {
|
||||
// If ship data is none, it has been removed because the ship has been destroyed.
|
||||
// play collapse sequence.
|
||||
self.collapse_sequence.step(
|
||||
res,
|
||||
self.data_handle.content_handle(),
|
||||
rigid_body,
|
||||
collider,
|
||||
);
|
||||
} else {
|
||||
self.data.step(res.t);
|
||||
match self.data.get_state() {
|
||||
ShipState::Collapsing { .. } => {
|
||||
// Borrow checker hack, so we may pass self.data
|
||||
// to the collapse sequence
|
||||
let mut seq = self.collapse_sequence.take().unwrap();
|
||||
seq.step(res, &self.data, rigid_body, collider);
|
||||
self.collapse_sequence = Some(seq);
|
||||
}
|
||||
ShipState::Flying => {
|
||||
return self.step_live(res, rigid_body, collider);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
) {
|
||||
let ship = res.gx.get_ship(self.data_handle).unwrap();
|
||||
let ship_content = res.ct.get_ship(self.data_handle.content_handle());
|
||||
let ship_content = res.ct.get_ship(self.data.get_content());
|
||||
let ship_pos = util::rigidbody_position(&rigid_body);
|
||||
let ship_rot = util::rigidbody_rotation(rigid_body);
|
||||
let ship_ang = ship_rot.angle(Vector2 { x: 1.0, y: 0.0 });
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
if ship.get_hull() <= ship_content.damage.hull {
|
||||
if self.data.get_hull() <= ship_content.damage.hull {
|
||||
for e in &ship_content.damage.effects {
|
||||
if rng.gen_range(0.0..=1.0) <= res.t / e.frequency {
|
||||
let effect = res.ct.get_effect(e.effect);
|
||||
|
@ -171,31 +160,31 @@ impl SySimShip {
|
|||
|
||||
if self.controls.thrust {
|
||||
rigid_body.apply_impulse(
|
||||
vector![engine_force.x, engine_force.y] * ship.get_outfits().get_engine_thrust(),
|
||||
vector![engine_force.x, engine_force.y]
|
||||
* self.data.get_outfits().get_engine_thrust(),
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
if self.controls.right {
|
||||
rigid_body
|
||||
.apply_torque_impulse(ship.get_outfits().get_steer_power() * -100.0 * res.t, true);
|
||||
rigid_body.apply_torque_impulse(
|
||||
self.data.get_outfits().get_steer_power() * -100.0 * res.t,
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
if self.controls.left {
|
||||
rigid_body
|
||||
.apply_torque_impulse(ship.get_outfits().get_steer_power() * 100.0 * res.t, true);
|
||||
rigid_body.apply_torque_impulse(
|
||||
self.data.get_outfits().get_steer_power() * 100.0 * res.t,
|
||||
true,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SySimShip {
|
||||
impl PhysSimShip {
|
||||
/// Get this ship's control state
|
||||
pub fn get_controls(&self) -> &ShipControls {
|
||||
&self.controls
|
||||
}
|
||||
|
||||
/// Get this ship's faction
|
||||
pub fn get_faction(&self) -> FactionHandle {
|
||||
self.faction
|
||||
}
|
||||
}
|
|
@ -1,16 +1,12 @@
|
|||
use crate::{wrapper::Wrapper, ParticleBuilder};
|
||||
use super::{wrapper::Wrapper, ParticleBuilder};
|
||||
use galactica_content::Content;
|
||||
use galactica_galaxy::Galaxy;
|
||||
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,
|
||||
|
||||
/// Game data
|
||||
pub gx: &'a mut Galaxy,
|
||||
|
||||
/// Length of time step
|
||||
pub t: f32,
|
||||
|
|
@ -1,10 +1,7 @@
|
|||
use cgmath::{Deg, EuclideanSpace, InnerSpace, Matrix2, Point2, Rad, Vector2, Zero};
|
||||
use galactica_content::{
|
||||
Content, GunPoint, OutfitHandle, ProjectileCollider, Relationship, SystemHandle,
|
||||
};
|
||||
use galactica_galaxy::{
|
||||
ship::{GxShip, ShipPersonality},
|
||||
Galaxy, GxShipHandle,
|
||||
Content, FactionHandle, GunPoint, OutfitHandle, ProjectileCollider, Relationship, ShipHandle,
|
||||
SystemHandle,
|
||||
};
|
||||
use galactica_playeragent::PlayerAgent;
|
||||
use nalgebra::{point, vector};
|
||||
|
@ -16,36 +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 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<ColliderHandle, SySimProjectile>,
|
||||
ships: HashMap<GxShipHandle, SySimShip>,
|
||||
|
||||
// TODO: these don't need to be cloned each frame
|
||||
ship_behaviors: HashMap<GxShipHandle, ShipController>,
|
||||
collider_ship_table: HashMap<ColliderHandle, GxShipHandle>,
|
||||
projectiles: HashMap<ColliderHandle, PhysProjectile>,
|
||||
ships: HashMap<ColliderHandle, PhysSimShip>,
|
||||
ship_behaviors: HashMap<ColliderHandle, ShipController>,
|
||||
}
|
||||
|
||||
// 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,
|
||||
|
@ -65,38 +67,32 @@ impl<'a> SystemSim {
|
|||
return Some((r, p));
|
||||
}
|
||||
|
||||
fn remove_ship(&mut self, res: &mut StepResources, s: ColliderHandle) {
|
||||
let s = match self.collider_ship_table.get(&s) {
|
||||
None => return,
|
||||
Some(s) => match self.ships.get(s) {
|
||||
fn remove_ship(&mut self, res: &mut PhysStepResources, colliderhandle: ColliderHandle) {
|
||||
let ship = match self.ships.get(&colliderhandle) {
|
||||
None => return,
|
||||
Some(s) => s,
|
||||
},
|
||||
};
|
||||
|
||||
self.rigid_body_set.remove(
|
||||
s.rigid_body,
|
||||
ship.rigid_body,
|
||||
&mut res.wrapper.im,
|
||||
&mut self.collider_set,
|
||||
&mut res.wrapper.ij,
|
||||
&mut res.wrapper.mj,
|
||||
true,
|
||||
);
|
||||
let h = self.collider_ship_table.remove(&s.collider).unwrap();
|
||||
self.ship_behaviors.remove(&h);
|
||||
self.ships.remove(&h);
|
||||
self.ships.remove(&colliderhandle).unwrap();
|
||||
self.ship_behaviors.remove(&colliderhandle);
|
||||
}
|
||||
|
||||
fn collide_projectile_ship(
|
||||
&mut self,
|
||||
res: &mut StepResources,
|
||||
res: &mut PhysStepResources,
|
||||
projectile_h: ColliderHandle,
|
||||
ship_h: ColliderHandle,
|
||||
) {
|
||||
let projectile = self.projectiles.get(&projectile_h);
|
||||
let ship = self
|
||||
.ships
|
||||
.get_mut(self.collider_ship_table.get(&ship_h).unwrap());
|
||||
let ship = self.ships.get_mut(&ship_h);
|
||||
if projectile.is_none() || ship.is_none() {
|
||||
return;
|
||||
}
|
||||
|
@ -104,13 +100,10 @@ impl<'a> SystemSim {
|
|||
let ship = ship.unwrap();
|
||||
|
||||
let f = res.ct.get_faction(projectile.faction);
|
||||
let r = f.relationships.get(&ship.get_faction()).unwrap();
|
||||
let r = f.relationships.get(&ship.data.get_faction()).unwrap();
|
||||
let destory_projectile = match r {
|
||||
Relationship::Hostile => {
|
||||
// We only apply damage if the target ship is alive
|
||||
if let Some(ship_d) = res.gx.get_ship_mut(ship.data_handle) {
|
||||
ship_d.apply_damage(projectile.content.damage);
|
||||
}
|
||||
ship.data.apply_damage(res.ct, projectile.content.damage);
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
|
@ -159,40 +152,29 @@ impl<'a> SystemSim {
|
|||
}
|
||||
|
||||
// Public methods
|
||||
impl SystemSim {
|
||||
impl PhysSim {
|
||||
/// Create a new physics system
|
||||
pub fn new(ct: &Content, gx: &Galaxy, system: SystemHandle) -> Self {
|
||||
let mut w = Self {
|
||||
pub fn new(_ct: &Content, system: SystemHandle) -> Self {
|
||||
Self {
|
||||
_system: system,
|
||||
rigid_body_set: RigidBodySet::new(),
|
||||
collider_set: ColliderSet::new(),
|
||||
projectiles: HashMap::new(),
|
||||
ships: HashMap::new(),
|
||||
ship_behaviors: HashMap::new(),
|
||||
collider_ship_table: HashMap::new(),
|
||||
};
|
||||
|
||||
// TODO: guarantee not touching
|
||||
// TODO: add, remove ships each tick
|
||||
// Maybe store position in galaxy crate?
|
||||
let mut rng = rand::thread_rng();
|
||||
for s in gx.iter_ships() {
|
||||
w.add_ship(
|
||||
ct,
|
||||
s,
|
||||
Point2 {
|
||||
x: rng.gen_range(-500.0..=500.0),
|
||||
y: rng.gen_range(-500.0..=500.0),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
/// Add a ship to this physics system
|
||||
pub fn add_ship(&mut self, ct: &Content, ship: &GxShip, position: Point2<f32>) {
|
||||
let ship_content = ct.get_ship(ship.get_content());
|
||||
pub fn add_ship(
|
||||
&mut self,
|
||||
ct: &Content,
|
||||
handle: ShipHandle,
|
||||
faction: FactionHandle,
|
||||
personality: ShipPersonality,
|
||||
position: Point2<f32>,
|
||||
) -> PhysSimShipHandle {
|
||||
let ship_content = ct.get_ship(handle);
|
||||
let cl = ColliderBuilder::convex_decomposition(
|
||||
&ship_content.collision.points[..],
|
||||
&ship_content.collision.indices[..],
|
||||
|
@ -209,25 +191,25 @@ impl SystemSim {
|
|||
.translation(vector![position.x, position.y])
|
||||
.can_sleep(false);
|
||||
|
||||
let r = self.rigid_body_set.insert(rb.build());
|
||||
let c = self
|
||||
.collider_set
|
||||
.insert_with_parent(cl.build(), r, &mut self.rigid_body_set);
|
||||
|
||||
self.collider_ship_table.insert(c, ship.get_handle());
|
||||
let ridid_body = self.rigid_body_set.insert(rb.build());
|
||||
let collider =
|
||||
self.collider_set
|
||||
.insert_with_parent(cl.build(), ridid_body, &mut self.rigid_body_set);
|
||||
|
||||
self.ship_behaviors.insert(
|
||||
ship.get_handle(),
|
||||
match ship.get_personality() {
|
||||
collider,
|
||||
match personality {
|
||||
ShipPersonality::Dummy | ShipPersonality::Player => ShipController::new_null(),
|
||||
ShipPersonality::Point => ShipController::new_point(),
|
||||
},
|
||||
);
|
||||
|
||||
self.ships.insert(
|
||||
ship.get_handle(),
|
||||
SySimShip::new(ct, ship.get_handle(), ship.get_faction(), r, c),
|
||||
collider,
|
||||
PhysSimShip::new(ct, handle, personality, faction, ridid_body, collider),
|
||||
);
|
||||
|
||||
return PhysSimShipHandle(collider);
|
||||
}
|
||||
|
||||
/// Update a player ship's controls
|
||||
|
@ -248,75 +230,65 @@ 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!
|
||||
let mut projectiles = Vec::new();
|
||||
let mut to_remove = Vec::new();
|
||||
for (_, handle) in &self.collider_ship_table {
|
||||
// Again, borrow checker hack. TODO: fix
|
||||
let keys: Vec<_> = self.ships.keys().map(|c| *c).collect();
|
||||
for collider in keys {
|
||||
// Borrow immutably for now...
|
||||
// (required to compute controls)
|
||||
let ship_object = self.ships.get(handle).unwrap();
|
||||
if ship_object.should_be_removed() {
|
||||
to_remove.push(ship_object.collider);
|
||||
let ship = self.ships.get(&collider).unwrap();
|
||||
if ship.data.get_state().should_be_removed() {
|
||||
to_remove.push(collider);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Short-circuit continue if this ship isn't in game data
|
||||
// (which means it's playing a collapse sequence)
|
||||
if res.gx.get_ship(*handle).is_none() {
|
||||
let ship_object = self.ships.get_mut(handle).unwrap();
|
||||
ship_object.step(
|
||||
// This ship is playing a collapse sequence
|
||||
// and needs no additional logic
|
||||
if ship.data.get_state().is_collapsing() {
|
||||
let ship = self.ships.get_mut(&collider).unwrap();
|
||||
ship.step(
|
||||
res,
|
||||
&mut self.rigid_body_set[ship_object.rigid_body],
|
||||
&mut self.collider_set[ship_object.collider],
|
||||
&mut self.rigid_body_set[ship.rigid_body],
|
||||
&mut self.collider_set[ship.collider],
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Compute new controls
|
||||
let controls;
|
||||
let b = self.ship_behaviors.get_mut(handle).unwrap();
|
||||
controls = b.update_controls(
|
||||
&res,
|
||||
&self.rigid_body_set,
|
||||
&self.ships,
|
||||
ship_object.rigid_body,
|
||||
*handle,
|
||||
);
|
||||
let b = self.ship_behaviors.get_mut(&collider).unwrap();
|
||||
controls = b.update_controls(&res, &self.rigid_body_set, &self.ships, ship.collider);
|
||||
|
||||
let ship = self.ships.get_mut(&collider).unwrap();
|
||||
|
||||
// Now re-borrow mutably to apply changes
|
||||
let ship_object = self.ships.get_mut(handle).unwrap();
|
||||
if let Some(controls) = controls {
|
||||
ship_object.controls = controls;
|
||||
ship.controls = controls;
|
||||
}
|
||||
ship_object.step(
|
||||
ship.step(
|
||||
res,
|
||||
&mut self.rigid_body_set[ship_object.rigid_body],
|
||||
&mut self.collider_set[ship_object.collider],
|
||||
&mut self.rigid_body_set[ship.rigid_body],
|
||||
&mut self.collider_set[ship.collider],
|
||||
);
|
||||
|
||||
// If we're firing, try to fire each gun
|
||||
if ship_object.controls.guns {
|
||||
let ship_data = res.gx.get_ship_mut(ship_object.data_handle).unwrap();
|
||||
|
||||
if ship.controls.guns {
|
||||
// TODO: don't allocate here. This is a hack to satisfy the borrow checker,
|
||||
// convert this to a refcell or do the replace dance.
|
||||
let pairs: Vec<(GunPoint, Option<OutfitHandle>)> = ship_data
|
||||
let pairs: Vec<(GunPoint, Option<OutfitHandle>)> = ship
|
||||
.data
|
||||
.get_outfits()
|
||||
.iter_gun_points()
|
||||
.map(|(p, o)| (p.clone(), o.clone()))
|
||||
.collect();
|
||||
|
||||
for (gun, outfit) in pairs {
|
||||
if ship_data.fire_gun(res.ct, &gun) {
|
||||
projectiles.push((
|
||||
ship_object.data_handle,
|
||||
ship_object.rigid_body,
|
||||
gun.clone(),
|
||||
outfit.unwrap(),
|
||||
));
|
||||
if ship.data.fire_gun(res.ct, &gun) {
|
||||
projectiles.push((ship.collider, gun.clone(), outfit.unwrap()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -328,11 +300,11 @@ impl SystemSim {
|
|||
}
|
||||
|
||||
// Create projectiles
|
||||
for (ship_dat, rigid_body, gun_point, outfit) in projectiles {
|
||||
for (collider, gun_point, outfit) in projectiles {
|
||||
let mut rng = rand::thread_rng();
|
||||
let rigid_body = self.get_rigid_body(rigid_body).unwrap();
|
||||
|
||||
let ship_dat = res.gx.get_ship(ship_dat).unwrap();
|
||||
let ship = self.ships.get(&collider).unwrap();
|
||||
let rigid_body = self.get_rigid_body(ship.rigid_body).unwrap();
|
||||
let ship_pos = util::rigidbody_position(rigid_body);
|
||||
let ship_rot = util::rigidbody_rotation(rigid_body);
|
||||
let ship_vel = util::rigidbody_velocity(rigid_body);
|
||||
|
@ -379,10 +351,10 @@ impl SystemSim {
|
|||
|
||||
self.projectiles.insert(
|
||||
collider.clone(),
|
||||
SySimProjectile::new(
|
||||
PhysProjectile::new(
|
||||
outfit.projectile.clone(),
|
||||
rigid_body,
|
||||
ship_dat.get_faction(),
|
||||
ship.data.get_faction(),
|
||||
collider,
|
||||
),
|
||||
);
|
||||
|
@ -390,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();
|
||||
|
@ -416,11 +388,7 @@ impl SystemSim {
|
|||
};
|
||||
|
||||
let p = self.projectiles.get(&a);
|
||||
let s = self.ships.get_mut(match self.collider_ship_table.get(&b) {
|
||||
// If none, this ship is dead.
|
||||
Some(x) => x,
|
||||
None => continue,
|
||||
});
|
||||
let s = self.ships.get_mut(&b);
|
||||
if p.is_none() || s.is_none() {
|
||||
continue;
|
||||
}
|
||||
|
@ -474,10 +442,15 @@ impl SystemSim {
|
|||
}
|
||||
|
||||
// Public getters
|
||||
impl SystemSim {
|
||||
impl PhysSim {
|
||||
/// Get a ship physics object
|
||||
pub fn get_ship(&self, ship: GxShipHandle) -> Option<&SySimShip> {
|
||||
self.ships.get(&ship)
|
||||
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: &PhysSimShipHandle) -> Option<&mut PhysSimShip> {
|
||||
self.ships.get_mut(&ship.0)
|
||||
}
|
||||
|
||||
/// Get a rigid body from a handle
|
||||
|
@ -491,19 +464,19 @@ impl SystemSim {
|
|||
}
|
||||
|
||||
/// Iterate over all ships in this physics system
|
||||
pub fn iter_ship_body(&self) -> impl Iterator<Item = (&SySimShip, &RigidBody)> + '_ {
|
||||
pub fn iter_ship_body(&self) -> impl Iterator<Item = (&PhysSimShip, &RigidBody)> + '_ {
|
||||
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<Item = &SySimShip> + '_ {
|
||||
pub fn iter_ships(&self) -> impl Iterator<Item = &PhysSimShip> + '_ {
|
||||
self.ships.values()
|
||||
}
|
||||
|
||||
/// Iterate over all ships in this physics system
|
||||
pub fn iter_projectiles(&self) -> impl Iterator<Item = &SySimProjectile> + '_ {
|
||||
pub fn iter_projectiles(&self) -> impl Iterator<Item = &PhysProjectile> + '_ {
|
||||
self.projectiles.values()
|
||||
}
|
||||
}
|
|
@ -9,10 +9,6 @@ pub struct Timing {
|
|||
pub frame: Duration,
|
||||
frame_timer: Instant,
|
||||
|
||||
/// The time we spent simulating game state
|
||||
pub galaxy: Duration,
|
||||
galaxy_timer: Instant,
|
||||
|
||||
/// The time we spent simulating physics
|
||||
pub physics_sim: Duration,
|
||||
physics_sim_timer: Instant,
|
||||
|
@ -29,10 +25,8 @@ impl Timing {
|
|||
pub fn new() -> Self {
|
||||
Self {
|
||||
frame: Duration::ZERO,
|
||||
galaxy: Duration::ZERO,
|
||||
physics_sim: Duration::ZERO,
|
||||
physics_ship: Duration::ZERO,
|
||||
galaxy_timer: Instant::now(),
|
||||
physics_sim_timer: Instant::now(),
|
||||
physics_ship_timer: Instant::now(),
|
||||
frame_timer: Instant::now(),
|
||||
|
@ -44,11 +38,6 @@ impl Timing {
|
|||
self.frame_timer = Instant::now();
|
||||
}
|
||||
|
||||
/// Start galaxy timer
|
||||
pub fn start_galaxy(&mut self) {
|
||||
self.galaxy_timer = Instant::now();
|
||||
}
|
||||
|
||||
/// Start physics sim timer
|
||||
pub fn start_physics_sim(&mut self) {
|
||||
self.physics_sim_timer = Instant::now();
|
||||
|
@ -59,16 +48,11 @@ 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();
|
||||
}
|
||||
|
||||
/// Record galaxy simulation time.
|
||||
pub fn mark_galaxy(&mut self) {
|
||||
self.galaxy = self.galaxy_timer.elapsed();
|
||||
}
|
||||
|
||||
/// Record physics simulation time
|
||||
pub fn mark_physics_sim(&mut self) {
|
||||
self.physics_sim = self.physics_sim_timer.elapsed();
|
||||
|
|
Loading…
Reference in New Issue