mara2/src/sh0rk/mod.rs

126 lines
2.6 KiB
Rust

pub mod palette;
pub mod sys;
pub mod music;
pub use music::{Note, Song};
#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub struct Point {
pub x: i32,
pub y: i32,
}
impl Point {
pub fn add<T>(&self, p: T) -> Point
where
T: Into<Point>,
{
let p = p.into();
Point {
x: self.x + p.x,
y: self.y + p.y,
}
}
}
impl From<(i32, i32)> for Point {
fn from((x, y): (i32, i32)) -> Self {
Point { x, y }
}
}
#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub struct Rect {
pub base: Point,
pub width: i32,
pub height: i32,
}
impl Rect {
pub fn inside(&self, pt: &Point) -> bool {
let ul = self.base.clone();
let lr = self.base.add((self.width, self.height));
ul.x >= pt.x && lr.x <= pt.x && ul.y >= pt.y && lr.y <= pt.y
}
pub fn collides(&self, other: &Rect) -> bool {
self.base.x < other.base.x + other.width
&& self.base.x + self.width > other.base.x
&& self.base.y < other.base.y + other.height
&& self.base.y + self.height > other.base.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,
);
}
}