use crate::physics::Cartesian; use crate::physics::PhysBody; use crate::DrawContext; use crate::Drawable; use crate::SpriteAtlas; pub enum ShipKind { Gypsum, } impl ShipKind { fn sprite(&self) -> &'static str { match self { Self::Gypsum => "gypsum.png", } } } pub struct Ship { pub body: PhysBody, kind: ShipKind, } impl Ship { pub fn new(kind: ShipKind, pos: Cartesian) -> Self { Ship { body: PhysBody::new(pos), kind, } } /// 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); } } impl Drawable for Ship { fn draw(&self, dc: &mut DrawContext, sa: &SpriteAtlas) -> Result<(), String> { let pos = self.screen_position(dc); let sprite = sa.get(self.kind.sprite()); sprite.draw(dc.canvas, pos, self.body.angle.to_degrees(), 1.0)?; return Ok(()); } }