Minor edits

master
Mark 2024-02-16 18:25:59 -08:00
parent f5c9540fb7
commit a6bc1a021d
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
8 changed files with 77 additions and 34 deletions

View File

@ -15,6 +15,7 @@ use log4rs::{
Config, Config,
}; };
use std::{ use std::{
cell::RefCell,
fs, fs,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::Arc, sync::Arc,
@ -127,7 +128,7 @@ fn try_main() -> Result<()> {
current_time: game.get_current_time(), current_time: game.get_current_time(),
ct: content.clone(), ct: content.clone(),
phys_img: PhysImage::new(), 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. // TODO: this is a hack for testing.
current_system: content.systems.values().next().unwrap().clone(), current_system: content.systems.values().next().unwrap().clone(),
timing: game.get_timing().clone(), timing: game.get_timing().clone(),
@ -184,13 +185,13 @@ fn try_main() -> Result<()> {
}, },
) )
.unwrap(); .unwrap();
game.apply_directive(directive, &input.player); game.apply_directive(directive, &mut input.player.borrow_mut());
} }
WindowEvent::CursorMoved { position, .. } => { WindowEvent::CursorMoved { position, .. } => {
let directive = gpu let directive = gpu
.process_input(&input, InputEvent::MouseMove(position.cast())) .process_input(&input, InputEvent::MouseMove(position.cast()))
.unwrap(); .unwrap();
game.apply_directive(directive, &input.player); game.apply_directive(directive, &mut input.player.borrow_mut());
} }
WindowEvent::MouseInput { state, button, .. } => { WindowEvent::MouseInput { state, button, .. } => {
let down = state == &ElementState::Pressed; let down = state == &ElementState::Pressed;
@ -201,7 +202,7 @@ fn try_main() -> Result<()> {
}; };
if let Some(event) = event { if let Some(event) = event {
let directive = gpu.process_input(&input, event).unwrap(); 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, .. } => { WindowEvent::MouseWheel { delta, .. } => {
@ -214,7 +215,7 @@ fn try_main() -> Result<()> {
}), }),
) )
.unwrap(); .unwrap();
game.apply_directive(directive, &input.player); game.apply_directive(directive, &mut input.player.borrow_mut());
} }
WindowEvent::Resized(_) => { WindowEvent::Resized(_) => {
gpu.resize(&content); gpu.resize(&content);

View File

@ -29,13 +29,17 @@ pub struct PlayerAgent {
/// Which ship this player is controlling /// Which ship this player is controlling
pub ship: Option<ColliderHandle>, pub ship: Option<ColliderHandle>,
/// What the player has selected /// What this player has selected
pub selection: PlayerSelection, pub selection: PlayerSelection,
/// The amount of money this player has
pub credits: u32,
} }
impl PlayerAgent { impl PlayerAgent {
pub fn new(ct: &Content, ship: ColliderHandle) -> Self { pub fn new(ct: &Content, ship: ColliderHandle) -> Self {
Self { Self {
credits: 1000,
ship: Some(ship), ship: Some(ship),
selection: PlayerSelection::OrbitingBody( selection: PlayerSelection::OrbitingBody(
ct.systems ct.systems

View File

@ -287,7 +287,7 @@ impl GPUState {
/// Main render function. Draws sprites on a window. /// Main render function. Draws sprites on a window.
pub fn render(&mut self, input: &Arc<RenderInput>) -> Result<(), wgpu::SurfaceError> { pub fn render(&mut self, input: &Arc<RenderInput>) -> 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)); let o = input.phys_img.get_ship(&PhysSimShipHandle(ship));
if let Some(o) = o { if let Some(o) = o {
match o.ship.get_data().get_state() { match o.ship.get_data().get_state() {

View File

@ -1,4 +1,4 @@
use std::sync::Arc; use std::{cell::RefCell, sync::Arc};
use galactica_content::{Content, System}; use galactica_content::{Content, System};
use galactica_playeragent::PlayerAgent; use galactica_playeragent::PlayerAgent;
@ -9,7 +9,7 @@ use galactica_util::timing::Timing;
#[derive(Debug)] #[derive(Debug)]
pub struct RenderInput { pub struct RenderInput {
/// Player ship data /// Player ship data
pub player: PlayerAgent, pub player: RefCell<PlayerAgent>,
/// The system we're currently in /// The system we're currently in
pub current_system: Arc<System>, pub current_system: Arc<System>,

View File

@ -301,7 +301,7 @@ impl UiScriptExecutor {
// Send player state change events // Send player state change events
if { if {
let player = input.player.ship; let player = input.player.borrow().ship;
if let Some(player) = player { if let Some(player) = player {
let ship = input.phys_img.get_ship(&PhysSimShipHandle(player)).unwrap(); let ship = input.phys_img.get_ship(&PhysSimShipHandle(player)).unwrap();
if self.last_player_state == 0 if self.last_player_state == 0

View File

@ -104,6 +104,7 @@ impl OutfitSet {
if outfit.is_none() { if outfit.is_none() {
*outfit = Some(o.clone()); *outfit = Some(o.clone());
added = true; added = true;
break;
} }
} }
if !added { if !added {
@ -112,14 +113,15 @@ impl OutfitSet {
} }
self.used_space += o.space; self.used_space += o.space;
self.stats.add(&o.stats); self.stats.add(&o.stats);
if o.stats.shield_generation != 0.0 {
self.shield_generators.push(ShieldGenerator { self.shield_generators.push(ShieldGenerator {
outfit: o.clone(), outfit: o.clone(),
delay: o.stats.shield_delay, delay: o.stats.shield_delay,
generation: o.stats.shield_generation, generation: o.stats.shield_generation,
}); });
}
if self.outfits.contains_key(&o.index) { if self.outfits.contains_key(&o.index) {
self.outfits.get_mut(&o.index).unwrap().1 += 1; self.outfits.get_mut(&o.index).unwrap().1 += 1;
@ -133,26 +135,32 @@ impl OutfitSet {
pub(super) fn remove(&mut self, o: &Arc<Outfit>) -> OutfitRemoveResult { pub(super) fn remove(&mut self, o: &Arc<Outfit>) -> OutfitRemoveResult {
if !self.outfits.contains_key(&o.index) { if !self.outfits.contains_key(&o.index) {
return OutfitRemoveResult::NotExist; return OutfitRemoveResult::NotExist;
} else { }
let n = self.outfits.get_mut(&o.index).unwrap(); let n = self.outfits.get_mut(&o.index).unwrap();
if n.1 == 1u32 { if n.1 == 1u32 {
self.outfits.remove(&o.index); self.outfits.remove(&o.index);
} else { } 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.used_space -= o.space;
self.stats.subtract(&o.stats); self.stats.subtract(&o.stats);
{
// This index will exist, since we checked the hashmap
let index = self let index = self
.shield_generators .shield_generators
.iter() .iter()
.position(|g| g.outfit.index == o.index) .position(|g| g.outfit.index == o.index);
.unwrap(); if let Some(index) = index {
self.shield_generators.remove(index); self.shield_generators.remove(index);
} }
@ -198,6 +206,29 @@ impl OutfitSet {
&self.used_space &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<Outfit>, 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. /// Get the combined stats of all outfits in this set.
/// There are two things to note here: /// There are two things to note here:
/// First, shield_delay is always zero. That is handled /// First, shield_delay is always zero. That is handled

View File

@ -14,7 +14,7 @@ use rapier2d::{
use super::{autopilot, collapse::ShipCollapseSequence, controller::ShipController, ShipControls}; use super::{autopilot, collapse::ShipCollapseSequence, controller::ShipController, ShipControls};
use crate::{ use crate::{
data::{ShipAutoPilot, ShipData, ShipPersonality, ShipState}, data::{OutfitRemoveResult, ShipAutoPilot, ShipData, ShipPersonality, ShipState},
phys::{ phys::{
get_phys_id, get_phys_id,
objects::{PhysEffect, PhysProjectile}, objects::{PhysEffect, PhysProjectile},
@ -505,8 +505,8 @@ impl PhysShip {
} }
/// Add one outfit to this ship /// Add one outfit to this ship
pub fn add_outfit(&mut self, o: Arc<Outfit>) { pub fn add_outfit(&mut self, o: &Arc<Outfit>) {
self.data.add_outfit(&o); self.data.add_outfit(o);
self.update_flares(); self.update_flares();
} }
@ -517,6 +517,13 @@ impl PhysShip {
} }
self.update_flares(); self.update_flares();
} }
/// Remove one outfit from this ship
pub fn remove_outfit(&mut self, o: &Arc<Outfit>) -> OutfitRemoveResult {
let r = self.data.remove_outfit(o);
self.update_flares();
return r;
}
} }
/// Public immutable /// Public immutable

View File

@ -192,7 +192,7 @@ impl PhysSim {
} }
/// Update a player ship's controls /// 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() { if player.ship.is_none() {
return; return;
} }