2023-12-20 19:05:12 -08:00
|
|
|
use sdl2::rect::{Point, Rect};
|
|
|
|
use sdl2::render::Canvas;
|
|
|
|
use sdl2::video::Window;
|
|
|
|
|
|
|
|
use crate::physics::PhysVec;
|
2023-12-20 20:06:54 -08:00
|
|
|
use crate::physics::Position;
|
2023-12-20 19:05:12 -08:00
|
|
|
use crate::Camera;
|
|
|
|
use crate::Drawable;
|
|
|
|
use crate::SpriteAtlas;
|
|
|
|
|
|
|
|
pub struct Doodad {
|
2023-12-20 20:06:54 -08:00
|
|
|
pub sprite: String,
|
|
|
|
pub pos: Position,
|
|
|
|
pub scale: u32,
|
|
|
|
pub angle: f64,
|
2023-12-20 19:05:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drawable for Doodad {
|
|
|
|
fn position(&self) -> PhysVec {
|
2023-12-20 20:06:54 -08:00
|
|
|
self.pos.to_cartesian().into()
|
2023-12-20 19:05:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
&self,
|
|
|
|
canvas: &mut Canvas<Window>,
|
|
|
|
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);
|
|
|
|
|
2023-12-20 20:06:54 -08:00
|
|
|
let mut dest_rect_0 = Rect::new(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
sprite.rect.width() / self.scale,
|
|
|
|
sprite.rect.height() / self.scale,
|
|
|
|
);
|
2023-12-20 19:05:12 -08:00
|
|
|
dest_rect_0.center_on(Point::new(
|
2023-12-20 20:06:54 -08:00
|
|
|
sprite.rect.width() as i32 / (2 * self.scale) as i32,
|
|
|
|
sprite.rect.height() as i32 / (2 * self.scale) as i32,
|
2023-12-20 19:05:12 -08:00
|
|
|
));
|
|
|
|
|
|
|
|
// 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),
|
2023-12-20 20:06:54 -08:00
|
|
|
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
|
2023-12-20 19:05:12 -08:00
|
|
|
false,
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|