Galactica/crates/content/src/part/outfitspace.rs

68 lines
1.5 KiB
Rust
Raw Normal View History

2023-12-30 21:05:06 -08:00
pub(crate) mod syntax {
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct OutfitSpace {
pub outfit: Option<u32>,
pub weapon: Option<u32>,
pub engine: Option<u32>,
}
}
/// Represents outfit space, either that available in a ship
/// or that used by an outfit.
#[derive(Debug, Clone, Copy)]
pub struct OutfitSpace {
/// Total available outfit space.
/// This should be greater than weapon and engine.
pub outfit: u32,
/// Space for weapons
pub weapon: u32,
/// Space for engine
pub engine: u32,
}
impl OutfitSpace {
/// Make a new, zero OutfitSpace
pub fn new() -> Self {
Self {
outfit: 0,
weapon: 0,
engine: 0,
}
}
/// Does this outfit contain `smaller`?
2024-01-01 15:41:47 -08:00
pub fn can_contain(&self, smaller: &Self) -> bool {
2023-12-30 21:05:06 -08:00
self.outfit >= (smaller.outfit + smaller.weapon + smaller.engine)
&& self.weapon >= smaller.weapon
&& self.engine >= smaller.engine
}
/// Free outfit space
pub fn free(&mut self, rhs: &Self) {
self.outfit += rhs.outfit + rhs.weapon + rhs.engine;
self.weapon += rhs.weapon;
self.engine += rhs.engine;
}
/// Occupy outfit space
pub fn occupy(&mut self, rhs: &Self) {
self.outfit -= rhs.outfit + rhs.weapon + rhs.engine;
self.weapon -= rhs.weapon;
self.engine -= rhs.engine;
}
}
impl From<syntax::OutfitSpace> for OutfitSpace {
fn from(value: syntax::OutfitSpace) -> Self {
Self {
outfit: value.outfit.unwrap_or(0),
engine: value.engine.unwrap_or(0),
weapon: value.weapon.unwrap_or(0),
}
}
}