From a6bc1a021db4b14f6c7f35e394621959ba86898c Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 16 Feb 2024 18:25:59 -0800 Subject: [PATCH] Minor edits --- crates/galactica/src/main.rs | 11 ++-- crates/playeragent/src/playeragent.rs | 6 +- crates/render/src/gpustate.rs | 2 +- crates/render/src/renderinput.rs | 4 +- crates/render/src/ui/executor.rs | 2 +- crates/system/src/data/ship/outfitset.rs | 71 +++++++++++++++------ crates/system/src/phys/objects/ship/ship.rs | 13 +++- crates/system/src/phys/physsim.rs | 2 +- 8 files changed, 77 insertions(+), 34 deletions(-) diff --git a/crates/galactica/src/main.rs b/crates/galactica/src/main.rs index f4ff5c9..927e454 100644 --- a/crates/galactica/src/main.rs +++ b/crates/galactica/src/main.rs @@ -15,6 +15,7 @@ use log4rs::{ Config, }; use std::{ + cell::RefCell, fs, path::{Path, PathBuf}, sync::Arc, @@ -127,7 +128,7 @@ fn try_main() -> Result<()> { current_time: game.get_current_time(), ct: content.clone(), phys_img: PhysImage::new(), - player: PlayerAgent::new(&content, p.0), + player: RefCell::new(PlayerAgent::new(&content, p.0)), // TODO: this is a hack for testing. current_system: content.systems.values().next().unwrap().clone(), timing: game.get_timing().clone(), @@ -184,13 +185,13 @@ fn try_main() -> Result<()> { }, ) .unwrap(); - game.apply_directive(directive, &input.player); + game.apply_directive(directive, &mut input.player.borrow_mut()); } WindowEvent::CursorMoved { position, .. } => { let directive = gpu .process_input(&input, InputEvent::MouseMove(position.cast())) .unwrap(); - game.apply_directive(directive, &input.player); + game.apply_directive(directive, &mut input.player.borrow_mut()); } WindowEvent::MouseInput { state, button, .. } => { let down = state == &ElementState::Pressed; @@ -201,7 +202,7 @@ fn try_main() -> Result<()> { }; if let Some(event) = event { let directive = gpu.process_input(&input, event).unwrap(); - game.apply_directive(directive, &input.player); + game.apply_directive(directive, &mut input.player.borrow_mut()); } } WindowEvent::MouseWheel { delta, .. } => { @@ -214,7 +215,7 @@ fn try_main() -> Result<()> { }), ) .unwrap(); - game.apply_directive(directive, &input.player); + game.apply_directive(directive, &mut input.player.borrow_mut()); } WindowEvent::Resized(_) => { gpu.resize(&content); diff --git a/crates/playeragent/src/playeragent.rs b/crates/playeragent/src/playeragent.rs index a4436a9..a642506 100644 --- a/crates/playeragent/src/playeragent.rs +++ b/crates/playeragent/src/playeragent.rs @@ -29,13 +29,17 @@ pub struct PlayerAgent { /// Which ship this player is controlling pub ship: Option, - /// What the player has selected + /// What this player has selected pub selection: PlayerSelection, + + /// The amount of money this player has + pub credits: u32, } impl PlayerAgent { pub fn new(ct: &Content, ship: ColliderHandle) -> Self { Self { + credits: 1000, ship: Some(ship), selection: PlayerSelection::OrbitingBody( ct.systems diff --git a/crates/render/src/gpustate.rs b/crates/render/src/gpustate.rs index f7cbc90..d8d309e 100644 --- a/crates/render/src/gpustate.rs +++ b/crates/render/src/gpustate.rs @@ -287,7 +287,7 @@ impl GPUState { /// Main render function. Draws sprites on a window. pub fn render(&mut self, input: &Arc) -> Result<(), wgpu::SurfaceError> { - if let Some(ship) = input.player.ship { + if let Some(ship) = input.player.borrow().ship { let o = input.phys_img.get_ship(&PhysSimShipHandle(ship)); if let Some(o) = o { match o.ship.get_data().get_state() { diff --git a/crates/render/src/renderinput.rs b/crates/render/src/renderinput.rs index 6456f5b..9724697 100644 --- a/crates/render/src/renderinput.rs +++ b/crates/render/src/renderinput.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{cell::RefCell, sync::Arc}; use galactica_content::{Content, System}; use galactica_playeragent::PlayerAgent; @@ -9,7 +9,7 @@ use galactica_util::timing::Timing; #[derive(Debug)] pub struct RenderInput { /// Player ship data - pub player: PlayerAgent, + pub player: RefCell, /// The system we're currently in pub current_system: Arc, diff --git a/crates/render/src/ui/executor.rs b/crates/render/src/ui/executor.rs index 74a5ebd..ba0a5dd 100644 --- a/crates/render/src/ui/executor.rs +++ b/crates/render/src/ui/executor.rs @@ -301,7 +301,7 @@ impl UiScriptExecutor { // Send player state change events if { - let player = input.player.ship; + let player = input.player.borrow().ship; if let Some(player) = player { let ship = input.phys_img.get_ship(&PhysSimShipHandle(player)).unwrap(); if self.last_player_state == 0 diff --git a/crates/system/src/data/ship/outfitset.rs b/crates/system/src/data/ship/outfitset.rs index e042841..a4ecc88 100644 --- a/crates/system/src/data/ship/outfitset.rs +++ b/crates/system/src/data/ship/outfitset.rs @@ -104,6 +104,7 @@ impl OutfitSet { if outfit.is_none() { *outfit = Some(o.clone()); added = true; + break; } } if !added { @@ -112,14 +113,15 @@ impl OutfitSet { } self.used_space += o.space; - self.stats.add(&o.stats); - self.shield_generators.push(ShieldGenerator { - outfit: o.clone(), - delay: o.stats.shield_delay, - generation: o.stats.shield_generation, - }); + if o.stats.shield_generation != 0.0 { + self.shield_generators.push(ShieldGenerator { + outfit: o.clone(), + delay: o.stats.shield_delay, + generation: o.stats.shield_generation, + }); + } if self.outfits.contains_key(&o.index) { self.outfits.get_mut(&o.index).unwrap().1 += 1; @@ -133,26 +135,32 @@ impl OutfitSet { pub(super) fn remove(&mut self, o: &Arc) -> OutfitRemoveResult { if !self.outfits.contains_key(&o.index) { return OutfitRemoveResult::NotExist; + } + + let n = self.outfits.get_mut(&o.index).unwrap(); + if n.1 == 1u32 { + self.outfits.remove(&o.index); } else { - let n = self.outfits.get_mut(&o.index).unwrap(); - if n.1 == 1u32 { - self.outfits.remove(&o.index); - } else { - self.outfits.get_mut(&o.index).unwrap().1 -= 1; - } + self.outfits.get_mut(&o.index).unwrap().1 -= 1; + } + + if o.gun.is_some() { + let (_, x) = self + .gun_points + .iter_mut() + .find(|(_, x)| x.is_some() && x.as_ref().unwrap().index == o.index) + .unwrap(); + *x = None; } self.used_space -= o.space; - self.stats.subtract(&o.stats); - { - // This index will exist, since we checked the hashmap - let index = self - .shield_generators - .iter() - .position(|g| g.outfit.index == o.index) - .unwrap(); + let index = self + .shield_generators + .iter() + .position(|g| g.outfit.index == o.index); + if let Some(index) = index { self.shield_generators.remove(index); } @@ -198,6 +206,29 @@ impl OutfitSet { &self.used_space } + /// Number of available (used & free) gun points + pub fn total_gun_points(&self) -> usize { + self.gun_points.len() + } + + /// Number of free gun points + pub fn free_gun_points(&self) -> usize { + self.iter_gun_points().filter(|(_, o)| o.is_none()).count() + } + + /// Does this set contain `count` of `outfit`? + pub fn has_outfit(&self, outfit: &Arc, mut count: u32) -> bool { + for i in self.iter_outfits() { + if count <= 0 { + return true; + } + if i.0.index == outfit.index { + count -= 1; + } + } + return count <= 0; + } + /// Get the combined stats of all outfits in this set. /// There are two things to note here: /// First, shield_delay is always zero. That is handled diff --git a/crates/system/src/phys/objects/ship/ship.rs b/crates/system/src/phys/objects/ship/ship.rs index d3d9ea6..b4607e1 100644 --- a/crates/system/src/phys/objects/ship/ship.rs +++ b/crates/system/src/phys/objects/ship/ship.rs @@ -14,7 +14,7 @@ use rapier2d::{ use super::{autopilot, collapse::ShipCollapseSequence, controller::ShipController, ShipControls}; use crate::{ - data::{ShipAutoPilot, ShipData, ShipPersonality, ShipState}, + data::{OutfitRemoveResult, ShipAutoPilot, ShipData, ShipPersonality, ShipState}, phys::{ get_phys_id, objects::{PhysEffect, PhysProjectile}, @@ -505,8 +505,8 @@ impl PhysShip { } /// Add one outfit to this ship - pub fn add_outfit(&mut self, o: Arc) { - self.data.add_outfit(&o); + pub fn add_outfit(&mut self, o: &Arc) { + self.data.add_outfit(o); self.update_flares(); } @@ -517,6 +517,13 @@ impl PhysShip { } self.update_flares(); } + + /// Remove one outfit from this ship + pub fn remove_outfit(&mut self, o: &Arc) -> OutfitRemoveResult { + let r = self.data.remove_outfit(o); + self.update_flares(); + return r; + } } /// Public immutable diff --git a/crates/system/src/phys/physsim.rs b/crates/system/src/phys/physsim.rs index 8fc4fcd..6fc73e4 100644 --- a/crates/system/src/phys/physsim.rs +++ b/crates/system/src/phys/physsim.rs @@ -192,7 +192,7 @@ impl PhysSim { } /// Update a player ship's controls - pub fn apply_directive(&mut self, directive: PlayerDirective, player: &PlayerAgent) { + pub fn apply_directive(&mut self, directive: PlayerDirective, player: &mut PlayerAgent) { if player.ship.is_none() { return; }