Rc'd content struct

master
Mark 2024-02-02 16:46:57 -08:00
parent 40fb31ad7e
commit 1343c01379
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
8 changed files with 29 additions and 25 deletions

View File

@ -4,11 +4,12 @@ use galactica_system::data::ShipPersonality;
use galactica_system::phys::{PhysImage, PhysSim, PhysSimShipHandle, PhysStepResources}; use galactica_system::phys::{PhysImage, PhysSim, PhysSimShipHandle, PhysStepResources};
use galactica_util::timing::Timing; use galactica_util::timing::Timing;
use nalgebra::Point2; use nalgebra::Point2;
use std::rc::Rc;
use std::time::Instant; use std::time::Instant;
pub struct Game { pub struct Game {
// Core game data // Core game data
ct: Content, ct: Rc<Content>,
phys_sim: PhysSim, phys_sim: PhysSim,
timing: Timing, timing: Timing,
start_instant: Instant, start_instant: Instant,
@ -43,7 +44,7 @@ impl<'a> Game {
return player; return player;
} }
pub fn new(ct: Content) -> Self { pub fn new(ct: Rc<Content>) -> Self {
let mut phys_sim = PhysSim::new(&ct, SystemHandle { index: 0 }); let mut phys_sim = PhysSim::new(&ct, SystemHandle { index: 0 });
let a = phys_sim.add_ship( let a = phys_sim.add_ship(
@ -118,7 +119,7 @@ impl<'a> Game {
self.timing.start_frame(); self.timing.start_frame();
self.phys_sim.step( self.phys_sim.step(
PhysStepResources { PhysStepResources {
ct: &self.ct, ct: self.ct.clone(),
t: self.last_update.elapsed().as_secs_f32() * self.time_scale, t: self.last_update.elapsed().as_secs_f32() * self.time_scale,
timing: &mut self.timing, timing: &mut self.timing,
}, },

View File

@ -21,6 +21,7 @@ use nalgebra::Vector2;
use std::{ use std::{
fs, fs,
path::{Path, PathBuf}, path::{Path, PathBuf},
rc::Rc,
time::Instant, time::Instant,
}; };
use winit::{ use winit::{
@ -112,17 +113,17 @@ fn try_main() -> Result<()> {
} }
// TODO: pretty error if missing (also in cli) // 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("./content"),
PathBuf::from("./assets"), PathBuf::from("./assets"),
atlas_index, atlas_index,
)?; )?);
let event_loop = EventLoop::new(); let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap(); let window = WindowBuilder::new().build(&event_loop).unwrap();
let mut gpu = pollster::block_on(galactica_render::GPUState::new( let mut gpu = pollster::block_on(galactica_render::GPUState::new(
window, window,
&content, content.clone(),
RenderScenes::System, RenderScenes::System,
))?; ))?;
gpu.init(&content); gpu.init(&content);
@ -147,7 +148,7 @@ fn try_main() -> Result<()> {
camera_pos: player.camera.pos, camera_pos: player.camera.pos,
camera_zoom: player.camera.zoom, camera_zoom: player.camera.zoom,
current_time: game.get_current_time(), current_time: game.get_current_time(),
ct: &content, ct: content.clone(),
phys_img: &phys_img, phys_img: &phys_img,
player: &player, player: &player,
time_since_last_run: last_run.elapsed().as_secs_f32(), time_since_last_run: last_run.elapsed().as_secs_f32(),

View File

@ -174,7 +174,7 @@ impl PhysEffect {
return; return;
} }
self.anim.step(res.ct, res.t); self.anim.step(&res.ct, res.t);
self.lifetime -= res.t; self.lifetime -= res.t;
if self.lifetime <= 0.0 { if self.lifetime <= 0.0 {

View File

@ -67,7 +67,7 @@ impl PhysProjectile {
wrapper: &mut PhysWrapper, wrapper: &mut PhysWrapper,
) { ) {
self.lifetime -= res.t; self.lifetime -= res.t;
self.anim.step(res.ct, res.t); self.anim.step(&res.ct, res.t);
if self.lifetime <= 0.0 { if self.lifetime <= 0.0 {
self.destroy(res, new, wrapper, true); self.destroy(res, new, wrapper, true);
@ -102,7 +102,7 @@ impl PhysProjectile {
None => {} None => {}
Some(handle) => { Some(handle) => {
new.effects.push(PhysEffect::new( new.effects.push(PhysEffect::new(
res.ct, &res.ct,
wrapper, wrapper,
*handle, *handle,
*rb.translation(), *rb.translation(),

View File

@ -84,12 +84,12 @@ impl ShipCollapseSequence {
let pos: Vector2<f32> = if let Some(pos) = spawner.pos { let pos: Vector2<f32> = if let Some(pos) = spawner.pos {
Vector2::new(pos.x, pos.y) Vector2::new(pos.x, pos.y)
} else { } 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); let pos = ship_pos + (ship_rot * pos);
new.effects.push(PhysEffect::new( new.effects.push(PhysEffect::new(
res.ct, &res.ct,
wrapper, wrapper,
spawner.effect, spawner.effect,
pos, pos,
@ -124,13 +124,13 @@ impl ShipCollapseSequence {
let pos = if let Some(pos) = spawner.pos { let pos = if let Some(pos) = spawner.pos {
Vector2::new(pos.x, pos.y) Vector2::new(pos.x, pos.y)
} else { } else {
self.random_in_ship(res.ct, ship_content, &collider) self.random_in_ship(&res.ct, ship_content, &collider)
}; };
// Position, adjusted for ship rotation // Position, adjusted for ship rotation
let pos = ship_pos + (ship_rot * pos); let pos = ship_pos + (ship_rot * pos);
new.effects.push(PhysEffect::new( new.effects.push(PhysEffect::new(
res.ct, &res.ct,
wrapper, wrapper,
spawner.effect, spawner.effect,
pos, pos,

View File

@ -100,32 +100,32 @@ impl PhysShip {
} }
self.data.step(res.t); 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 { for (_, e) in &mut self.engine_anim {
e.step(res.ct, res.t); e.step(&res.ct, res.t);
} }
// Flare animations // Flare animations
if !self.controls.thrust && self.last_controls.thrust { 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() { if flare.is_some() {
let flare_outfit = flare.unwrap(); let flare_outfit = flare.unwrap();
let flare = res.ct.get_outfit(flare_outfit); let flare = res.ct.get_outfit(flare_outfit);
if flare.engine_flare_on_stop.is_some() { if flare.engine_flare_on_stop.is_some() {
for (_, e) in &mut self.engine_anim { 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 { } 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() { if flare.is_some() {
let flare_outfit = flare.unwrap(); let flare_outfit = flare.unwrap();
let flare = res.ct.get_outfit(flare_outfit); let flare = res.ct.get_outfit(flare_outfit);
if flare.engine_flare_on_start.is_some() { if flare.engine_flare_on_start.is_some() {
for (_, e) in &mut self.engine_anim { 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(); .collect();
for (gun_point, outfit) in pairs { 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 outfit = outfit.unwrap();
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
@ -285,7 +285,7 @@ impl PhysShip {
let collider = wrapper.insert_collider(collider, rigid_body); let collider = wrapper.insert_collider(collider, rigid_body);
new.projectiles.push(PhysProjectile::new( new.projectiles.push(PhysProjectile::new(
res.ct, &res.ct,
outfit.projectile.clone(), outfit.projectile.clone(),
rigid_body, rigid_body,
self.data.get_faction(), self.data.get_faction(),
@ -456,7 +456,7 @@ impl PhysShip {
let pos = ship_pos + (Rotation2::new(ship_ang) * pos); let pos = ship_pos + (Rotation2::new(ship_ang) * pos);
new.effects.push(PhysEffect::new( new.effects.push(PhysEffect::new(
res.ct, &res.ct,
wrapper, wrapper,
e.effect, e.effect,
pos, pos,

View File

@ -131,7 +131,7 @@ impl PhysSim {
None => {} None => {}
Some(x) => { Some(x) => {
self.effects.push(PhysEffect::new( self.effects.push(PhysEffect::new(
res.ct, &res.ct,
&mut self.wrapper, &mut self.wrapper,
*x, *x,
pos, pos,

View File

@ -1,10 +1,12 @@
use std::rc::Rc;
use galactica_content::Content; use galactica_content::Content;
use galactica_util::timing::Timing; use galactica_util::timing::Timing;
/// External resources we need to compute time steps /// External resources we need to compute time steps
pub struct PhysStepResources<'a> { pub struct PhysStepResources<'a> {
/// Game content /// Game content
pub ct: &'a Content, pub ct: Rc<Content>,
/// Length of time step /// Length of time step
pub t: f32, pub t: f32,