Galactica/crates/gameobject/src/ship.rs

76 lines
1.7 KiB
Rust

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,
}
}
/// Has this ship been destroyed?
pub fn is_destroyed(&self) -> bool {
self.hull <= 0.0
}
/// Called whenever a projectile hits this ship.
/// Returns true if that projectile should be destroyed and false otherwise.
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()
.filter(|(g, _)| g.cooldown <= 0.0)
.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()
}
}