From 1343c01379adda244d0130a5154578fa29312f94 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 2 Feb 2024 16:46:57 -0800 Subject: [PATCH] Rc'd content struct --- crates/galactica/src/game.rs | 7 ++++--- crates/galactica/src/main.rs | 9 +++++---- crates/system/src/phys/objects/effect.rs | 2 +- crates/system/src/phys/objects/projectile.rs | 4 ++-- .../system/src/phys/objects/ship/collapse.rs | 8 ++++---- crates/system/src/phys/objects/ship/ship.rs | 18 +++++++++--------- crates/system/src/phys/physsim.rs | 2 +- crates/system/src/phys/stepresources.rs | 4 +++- 8 files changed, 29 insertions(+), 25 deletions(-) diff --git a/crates/galactica/src/game.rs b/crates/galactica/src/game.rs index 8eb8f6d..a7c1e7a 100644 --- a/crates/galactica/src/game.rs +++ b/crates/galactica/src/game.rs @@ -4,11 +4,12 @@ use galactica_system::data::ShipPersonality; use galactica_system::phys::{PhysImage, PhysSim, PhysSimShipHandle, PhysStepResources}; use galactica_util::timing::Timing; use nalgebra::Point2; +use std::rc::Rc; use std::time::Instant; pub struct Game { // Core game data - ct: Content, + ct: Rc, phys_sim: PhysSim, timing: Timing, start_instant: Instant, @@ -43,7 +44,7 @@ impl<'a> Game { return player; } - pub fn new(ct: Content) -> Self { + pub fn new(ct: Rc) -> Self { let mut phys_sim = PhysSim::new(&ct, SystemHandle { index: 0 }); let a = phys_sim.add_ship( @@ -118,7 +119,7 @@ impl<'a> Game { self.timing.start_frame(); self.phys_sim.step( PhysStepResources { - ct: &self.ct, + ct: self.ct.clone(), t: self.last_update.elapsed().as_secs_f32() * self.time_scale, timing: &mut self.timing, }, diff --git a/crates/galactica/src/main.rs b/crates/galactica/src/main.rs index 575c3e5..c2fef48 100644 --- a/crates/galactica/src/main.rs +++ b/crates/galactica/src/main.rs @@ -21,6 +21,7 @@ use nalgebra::Vector2; use std::{ fs, path::{Path, PathBuf}, + rc::Rc, time::Instant, }; use winit::{ @@ -112,17 +113,17 @@ fn try_main() -> Result<()> { } // TODO: pretty error if missing (also in cli) - let content = Content::load_dir( + let content = Rc::new(Content::load_dir( PathBuf::from("./content"), PathBuf::from("./assets"), atlas_index, - )?; + )?); let event_loop = EventLoop::new(); let window = WindowBuilder::new().build(&event_loop).unwrap(); let mut gpu = pollster::block_on(galactica_render::GPUState::new( window, - &content, + content.clone(), RenderScenes::System, ))?; gpu.init(&content); @@ -147,7 +148,7 @@ fn try_main() -> Result<()> { camera_pos: player.camera.pos, camera_zoom: player.camera.zoom, current_time: game.get_current_time(), - ct: &content, + ct: content.clone(), phys_img: &phys_img, player: &player, time_since_last_run: last_run.elapsed().as_secs_f32(), diff --git a/crates/system/src/phys/objects/effect.rs b/crates/system/src/phys/objects/effect.rs index cc157b4..98d0dbc 100644 --- a/crates/system/src/phys/objects/effect.rs +++ b/crates/system/src/phys/objects/effect.rs @@ -174,7 +174,7 @@ impl PhysEffect { return; } - self.anim.step(res.ct, res.t); + self.anim.step(&res.ct, res.t); self.lifetime -= res.t; if self.lifetime <= 0.0 { diff --git a/crates/system/src/phys/objects/projectile.rs b/crates/system/src/phys/objects/projectile.rs index 0e257ae..ffbd6fd 100644 --- a/crates/system/src/phys/objects/projectile.rs +++ b/crates/system/src/phys/objects/projectile.rs @@ -67,7 +67,7 @@ impl PhysProjectile { wrapper: &mut PhysWrapper, ) { self.lifetime -= res.t; - self.anim.step(res.ct, res.t); + self.anim.step(&res.ct, res.t); if self.lifetime <= 0.0 { self.destroy(res, new, wrapper, true); @@ -102,7 +102,7 @@ impl PhysProjectile { None => {} Some(handle) => { new.effects.push(PhysEffect::new( - res.ct, + &res.ct, wrapper, *handle, *rb.translation(), diff --git a/crates/system/src/phys/objects/ship/collapse.rs b/crates/system/src/phys/objects/ship/collapse.rs index e909026..affb6ee 100644 --- a/crates/system/src/phys/objects/ship/collapse.rs +++ b/crates/system/src/phys/objects/ship/collapse.rs @@ -84,12 +84,12 @@ impl ShipCollapseSequence { let pos: Vector2 = if let Some(pos) = spawner.pos { Vector2::new(pos.x, pos.y) } else { - self.random_in_ship(res.ct, ship_content, &collider) + self.random_in_ship(&res.ct, ship_content, &collider) }; let pos = ship_pos + (ship_rot * pos); new.effects.push(PhysEffect::new( - res.ct, + &res.ct, wrapper, spawner.effect, pos, @@ -124,13 +124,13 @@ impl ShipCollapseSequence { let pos = if let Some(pos) = spawner.pos { Vector2::new(pos.x, pos.y) } else { - self.random_in_ship(res.ct, ship_content, &collider) + self.random_in_ship(&res.ct, ship_content, &collider) }; // Position, adjusted for ship rotation let pos = ship_pos + (ship_rot * pos); new.effects.push(PhysEffect::new( - res.ct, + &res.ct, wrapper, spawner.effect, pos, diff --git a/crates/system/src/phys/objects/ship/ship.rs b/crates/system/src/phys/objects/ship/ship.rs index e6a47d8..c9d9bfb 100644 --- a/crates/system/src/phys/objects/ship/ship.rs +++ b/crates/system/src/phys/objects/ship/ship.rs @@ -100,32 +100,32 @@ impl PhysShip { } self.data.step(res.t); - self.anim.step(res.ct, res.t); + self.anim.step(&res.ct, res.t); for (_, e) in &mut self.engine_anim { - e.step(res.ct, res.t); + e.step(&res.ct, res.t); } // Flare animations if !self.controls.thrust && self.last_controls.thrust { - let flare = self.get_flare(res.ct); + let flare = self.get_flare(&res.ct); if flare.is_some() { let flare_outfit = flare.unwrap(); let flare = res.ct.get_outfit(flare_outfit); if flare.engine_flare_on_stop.is_some() { for (_, e) in &mut self.engine_anim { - e.jump_to(res.ct, flare.engine_flare_on_stop.unwrap()); + e.jump_to(&res.ct, flare.engine_flare_on_stop.unwrap()); } } }; } else if self.controls.thrust && !self.last_controls.thrust { - let flare = self.get_flare(res.ct); + let flare = self.get_flare(&res.ct); if flare.is_some() { let flare_outfit = flare.unwrap(); let flare = res.ct.get_outfit(flare_outfit); if flare.engine_flare_on_start.is_some() { for (_, e) in &mut self.engine_anim { - e.jump_to(res.ct, flare.engine_flare_on_start.unwrap()); + e.jump_to(&res.ct, flare.engine_flare_on_start.unwrap()); } } }; @@ -232,7 +232,7 @@ impl PhysShip { .collect(); for (gun_point, outfit) in pairs { - if self.data.fire_gun(res.ct, &gun_point) { + if self.data.fire_gun(&res.ct, &gun_point) { let outfit = outfit.unwrap(); let mut rng = rand::thread_rng(); @@ -285,7 +285,7 @@ impl PhysShip { let collider = wrapper.insert_collider(collider, rigid_body); new.projectiles.push(PhysProjectile::new( - res.ct, + &res.ct, outfit.projectile.clone(), rigid_body, self.data.get_faction(), @@ -456,7 +456,7 @@ impl PhysShip { let pos = ship_pos + (Rotation2::new(ship_ang) * pos); new.effects.push(PhysEffect::new( - res.ct, + &res.ct, wrapper, e.effect, pos, diff --git a/crates/system/src/phys/physsim.rs b/crates/system/src/phys/physsim.rs index fe68751..f4a5599 100644 --- a/crates/system/src/phys/physsim.rs +++ b/crates/system/src/phys/physsim.rs @@ -131,7 +131,7 @@ impl PhysSim { None => {} Some(x) => { self.effects.push(PhysEffect::new( - res.ct, + &res.ct, &mut self.wrapper, *x, pos, diff --git a/crates/system/src/phys/stepresources.rs b/crates/system/src/phys/stepresources.rs index 0d028ef..abf3e7e 100644 --- a/crates/system/src/phys/stepresources.rs +++ b/crates/system/src/phys/stepresources.rs @@ -1,10 +1,12 @@ +use std::rc::Rc; + use galactica_content::Content; use galactica_util::timing::Timing; /// External resources we need to compute time steps pub struct PhysStepResources<'a> { /// Game content - pub ct: &'a Content, + pub ct: Rc, /// Length of time step pub t: f32,