Galactica/src/doodad.rs

75 lines
1.5 KiB
Rust
Raw Normal View History

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;
use crate::Camera;
use crate::Drawable;
use crate::SpriteAtlas;
pub struct Doodad {
sprite: String,
pos: PhysVec,
scale: u32,
}
impl Doodad {
pub fn new(pos: PhysVec) -> Self {
Doodad {
sprite: "a0.png".to_owned(),
pos,
scale: 1,
}
}
}
impl Drawable for Doodad {
fn position(&self) -> PhysVec {
self.pos
}
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);
let mut dest_rect_0 = Rect::new(0, 0, 112 / self.scale, 112 / self.scale);
dest_rect_0.center_on(Point::new(
112 / (2 * self.scale) as i32,
112 / (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),
0.0, // angle
Point::new(120 / (2 * self.scale) as i32, 270 / (2 * self.scale) as i32), // center
false,
false,
)?;
return Ok(());
}
}