diff --git a/Cargo.lock b/Cargo.lock index 8822da9..49edf44 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -649,6 +649,7 @@ dependencies = [ "galactica-systemsim", "galactica-util", "pollster", + "rand", "wgpu", "winit", ] diff --git a/crates/galactica/Cargo.toml b/crates/galactica/Cargo.toml index 0729a85..9bd73a8 100644 --- a/crates/galactica/Cargo.toml +++ b/crates/galactica/Cargo.toml @@ -28,6 +28,7 @@ galactica-systemsim = { workspace = true } galactica-galaxy = { workspace = true } galactica-playeragent = { workspace = true } +rand = { workspace = true } winit = { workspace = true } wgpu = { workspace = true } pollster = { workspace = true } diff --git a/crates/galactica/src/game.rs b/crates/galactica/src/game.rs index 5637ec0..e790f26 100644 --- a/crates/galactica/src/game.rs +++ b/crates/galactica/src/game.rs @@ -4,6 +4,7 @@ use galactica_galaxy::{ship::ShipPersonality, Galaxy, GxShipHandle}; use galactica_playeragent::PlayerAgent; use galactica_systemsim::{ParticleBuilder, StepResources, SystemSim, Wrapper}; use galactica_util::timing::Timing; +use rand::seq::SliceRandom; use std::time::Instant; #[derive(Clone)] @@ -26,6 +27,10 @@ pub struct Game { wrapper: Wrapper, // Physics computer time_scale: f32, last_update: Instant, + + /// Particles to create this frame. + /// Must be cleared at the start of every frame + /// TODO: better way to handle this? new_particles: Vec, } @@ -107,9 +112,14 @@ impl<'a> Game { &self.state } + pub fn get_particles(&self) -> &Vec { + &self.new_particles + } + pub fn update(&mut self) { self.state.timing.start_frame(); 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); @@ -125,6 +135,7 @@ impl<'a> Game { }); self.last_update = Instant::now(); + self.new_particles.shuffle(&mut rand::thread_rng()); self.state.timing.mark_frame(); } } diff --git a/crates/galactica/src/main.rs b/crates/galactica/src/main.rs index f0d974f..a8d078e 100644 --- a/crates/galactica/src/main.rs +++ b/crates/galactica/src/main.rs @@ -55,8 +55,7 @@ fn main() -> Result<()> { current_time: game.get_state().start_instant.elapsed().as_secs_f32(), ct: &content, systemsim: &game.get_state().systemsim, - particles: &mut Vec::new(), - //particles: &mut self.new_particles, + particles: game.get_particles(), player_data: player.ship.unwrap(), gx: &game.get_state().gx, current_system: SystemHandle { index: 0 }, diff --git a/crates/render/src/datastructs.rs b/crates/render/src/datastructs.rs index 1df65dd..184cce7 100644 --- a/crates/render/src/datastructs.rs +++ b/crates/render/src/datastructs.rs @@ -38,7 +38,7 @@ pub struct RenderInput<'a> { pub gx: &'a Galaxy, /// Particles to spawn during this frame - pub particles: &'a mut Vec, + pub particles: &'a Vec, /// Time we spent in each part of the game loop pub timing: Timing, diff --git a/crates/render/src/gpustate/render.rs b/crates/render/src/gpustate/render.rs index 7f3ae6e..8e5a649 100644 --- a/crates/render/src/gpustate/render.rs +++ b/crates/render/src/gpustate/render.rs @@ -3,7 +3,6 @@ use bytemuck; use cgmath::Point2; use galactica_util::constants::PARTICLE_SPRITE_INSTANCE_LIMIT; use glyphon::Resolution; -use rand::seq::SliceRandom; use std::iter; use wgpu; @@ -81,7 +80,6 @@ impl super::GPUState { ); // Write all new particles to GPU buffer - input.particles.shuffle(&mut rand::thread_rng()); for i in input.particles.iter() { self.state.queue.write_buffer( &self.state.vertex_buffers.particle.instances, @@ -103,7 +101,6 @@ impl super::GPUState { self.state.vertex_buffers.particle_counter = 0; } } - input.particles.clear(); // Create sprite instances