mara2/src/sh0rk/mod.rs

92 lines
1.8 KiB
Rust

pub mod palette;
pub mod sys;
#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub struct Point {
pub x: i32,
pub y: i32,
}
impl From<(i32, i32)> for Point {
fn from((x, y): (i32, i32)) -> Self {
Point { x, y }
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Left,
Right,
Up,
Down,
}
use Direction::*;
pub struct SpriteAtlas<const N: usize> {
pub atlas: Sprite<N>,
pub width: u32,
pub height: u32,
pub animated: bool,
}
impl<const N: usize> SpriteAtlas<N> {
pub fn draw(&self, dir: Direction, step: u32, p: Point, flags: u32) {
let flags = flags
| if dir == Left {
sys::BLIT_FLIP_X
} else {
0
};
let frame: u32 = if self.animated {
match dir {
Left => 4,
Right => 4,
Up => 2,
Down => 0,
}
} else {
match dir {
Up => 1,
Down => 0,
_ => 2,
}
} + step;
palette::set_draw_color(self.atlas.palette);
sys::blit_sub(
&self.atlas.sprite,
p.x,
p.y,
self.width,
self.height,
self.width * frame,
0,
self.atlas.width,
self.atlas.flags | flags,
);
}
}
pub struct Sprite<const N: usize> {
pub palette: u16,
pub width: u32,
pub height: u32,
pub flags: u32,
pub sprite: [u8; N],
}
impl<const N: usize> Sprite<N> {
pub fn draw(&self, p: Point, flags: u32) {
palette::set_draw_color(self.palette);
sys::blit(
&self.sprite,
p.x,
p.y,
self.width,
self.height,
self.flags | flags,
);
}
}