use sdl2::rect::{Point, Rect}; use sdl2::render::Canvas; use sdl2::video::Window; use crate::physics::PhysVec; use crate::physics::Position; use crate::Camera; use crate::Drawable; use crate::SpriteAtlas; pub struct Doodad { pub sprite: String, pub pos: Position, pub scale: u32, pub angle: f64, } impl Drawable for Doodad { fn position(&self) -> PhysVec { self.pos.to_cartesian().into() } fn draw( &self, canvas: &mut Canvas, sa: &SpriteAtlas, c: &Camera, ) -> Result<(), String> { let win_size = PhysVec::from(canvas.window().size()); let draw_pos = self.screen_position(canvas, &c); // Don't draw if off screen if draw_pos.x < 0.0 || draw_pos.x > win_size.x || draw_pos.y < 0.0 || draw_pos.y > win_size.y { return Ok(()); } let sprite = sa.get(&self.sprite); let mut dest_rect_0 = Rect::new( 0, 0, sprite.rect.width() / self.scale, sprite.rect.height() / self.scale, ); dest_rect_0.center_on(Point::new( sprite.rect.width() as i32 / (2 * self.scale) as i32, sprite.rect.height() as i32 / (2 * self.scale) as i32, )); // set the current frame for time dest_rect_0.set_x(draw_pos.x.round() as i32); dest_rect_0.set_y(draw_pos.y.round() as i32); // copy the frame to the canvas canvas.copy_ex( &sprite.texture, Some(sprite.rect), Some(dest_rect_0), self.angle.to_degrees(), // angle Point::new( sprite.rect.width() as i32 / (2 * self.scale) as i32, sprite.rect.width() as i32 / (2 * self.scale) as i32, ), // center false, false, )?; return Ok(()); } }