Galactica/crates/gameobject/src/ship.rs

76 lines
1.7 KiB
Rust
Raw Normal View History

2024-01-01 15:41:47 -08:00
use rand::Rng;
use crate::{OutfitSet, Projectile};
use galactica_content as content;
#[derive(Debug)]
pub struct Ship {
pub handle: content::ShipHandle,
pub faction: content::FactionHandle,
pub outfits: OutfitSet,
// TODO: unified ship stats struct, like space
// TODO: unified outfit stats struct: check
pub hull: f32,
}
impl Ship {
pub fn new(
ct: &content::Content,
handle: content::ShipHandle,
faction: content::FactionHandle,
outfits: OutfitSet,
) -> Self {
let s = ct.get_ship(handle);
Ship {
handle: handle,
faction,
outfits,
hull: s.hull,
}
}
2024-01-01 16:38:12 -08:00
/// Has this ship been destroyed?
pub fn is_destroyed(&self) -> bool {
self.hull <= 0.0
}
2024-01-01 16:55:19 -08:00
/// Called whenever a projectile hits this ship.
/// Returns true if that projectile should be destroyed and false otherwise.
2024-01-01 15:41:47 -08:00
pub fn handle_projectile_collision(&mut self, ct: &content::Content, p: &Projectile) -> bool {
let f = ct.get_faction(self.faction);
let r = f.relationships.get(&p.faction).unwrap();
match r {
content::Relationship::Hostile => {
// TODO: implement death and spawning, and enable damage
//s.hull -= p.damage;
return true;
}
_ => return false,
}
}
pub fn fire_guns(&mut self) -> Vec<(Projectile, content::GunPoint)> {
self.outfits
.iter_guns_points()
2024-01-01 15:51:57 -08:00
.filter(|(g, _)| g.cooldown <= 0.0)
2024-01-01 15:41:47 -08:00
.map(|(g, p)| {
let mut rng = rand::thread_rng();
g.cooldown = g.kind.rate + &rng.gen_range(-g.kind.rate_rng..=g.kind.rate_rng);
(
Projectile {
content: g.kind.projectile.clone(),
lifetime: g.kind.projectile.lifetime
+ rng.gen_range(
-g.kind.projectile.lifetime_rng..=g.kind.projectile.lifetime_rng,
),
faction: self.faction,
},
p.clone(),
)
})
.collect()
}
}