Galactica/src/ship.rs

49 lines
1021 B
Rust
Raw Normal View History

2023-12-21 11:26:44 -08:00
use crate::physics::Cartesian;
2023-12-20 19:05:12 -08:00
use crate::physics::PhysBody;
2023-12-21 11:26:44 -08:00
use crate::DrawContext;
2023-12-20 19:05:12 -08:00
use crate::Drawable;
use crate::SpriteAtlas;
2023-12-21 11:26:44 -08:00
pub enum ShipKind {
Gypsum,
}
impl ShipKind {
fn sprite(&self) -> &'static str {
match self {
Self::Gypsum => "gypsum.png",
}
}
}
2023-12-20 19:05:12 -08:00
pub struct Ship {
pub body: PhysBody,
kind: ShipKind,
}
impl Ship {
2023-12-21 11:26:44 -08:00
pub fn new(kind: ShipKind, pos: Cartesian) -> Self {
2023-12-20 19:05:12 -08:00
Ship {
2023-12-21 11:26:44 -08:00
body: PhysBody::new(pos),
2023-12-20 19:05:12 -08:00
kind,
}
}
2023-12-21 11:26:44 -08:00
/// Get the position of this drawable on the screen
/// (0, 0) is at top-left corner.
///
/// Returned position is this object's center.
fn screen_position(&self, dc: &DrawContext) -> Cartesian {
return ((self.body.pos - dc.camera.pos) - dc.top_left) * Cartesian::new(1.0, -1.0);
2023-12-20 19:05:12 -08:00
}
2023-12-21 11:26:44 -08:00
}
2023-12-20 19:05:12 -08:00
2023-12-21 11:26:44 -08:00
impl Drawable for Ship {
fn draw(&self, dc: &mut DrawContext, sa: &SpriteAtlas) -> Result<(), String> {
let pos = self.screen_position(dc);
2023-12-20 19:05:12 -08:00
let sprite = sa.get(self.kind.sprite());
2023-12-21 11:26:44 -08:00
sprite.draw(dc.canvas, pos, self.body.angle.to_degrees(), 1.0)?;
2023-12-20 19:05:12 -08:00
return Ok(());
}
}