gate collision

Signed-off-by: Jessie Williams <quorawings@gmail.com>
This commit is contained in:
Jessie Williams 2022-03-06 22:56:26 -05:00
parent 037d6a5d54
commit f36431f491
6 changed files with 96 additions and 33 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 391 B

After

Width:  |  Height:  |  Size: 395 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 151 B

After

Width:  |  Height:  |  Size: 150 B

View File

@ -4,7 +4,13 @@ mod sh0rk;
mod sprites; mod sprites;
use fastrand::Rng; use fastrand::Rng;
use sh0rk::{palette, sys::*, Point, Direction::{self, *}}; use sh0rk::{
palette,
sys::*,
Direction::{self, *},
Rect,
Point,
};
static mut GAME: Game = Game::new(); static mut GAME: Game = Game::new();
@ -18,6 +24,8 @@ struct Game {
mara_dir: Direction, mara_dir: Direction,
mara_speed: i32, mara_speed: i32,
bonk_timer: Option<u8>, bonk_timer: Option<u8>,
gate_count: u8,
gate_pos: Point,
} }
impl Game { impl Game {
@ -26,12 +34,14 @@ impl Game {
frame_count: 0, frame_count: 0,
prev_gamepad: 0, prev_gamepad: 0,
rng: None, rng: None,
mai_position: Point { x: 76, y: 48 }, mai_position: Point { x: 76, y: 32 },
mara_position: Point { x: 32, y: 90 }, mara_position: Point { x: 32, y: 90 },
mara_frame: 0, mara_frame: 0,
mara_dir: Right, mara_dir: Right,
mara_speed: 0, mara_speed: 0,
bonk_timer: None, bonk_timer: None,
gate_count: 4,
gate_pos: Point { x: 40, y: 64 },
} }
} }
@ -99,11 +109,7 @@ impl Game {
let gamepad = unsafe { *GAMEPAD1 }; let gamepad = unsafe { *GAMEPAD1 };
let just_pressed = gamepad & (gamepad ^ self.prev_gamepad); let just_pressed = gamepad & (gamepad ^ self.prev_gamepad);
let speed = if just_pressed & BUTTON_2 != 0 { let speed = if just_pressed & BUTTON_2 != 0 { 4 } else { 2 };
4
} else {
2
};
if gamepad & BUTTON_UP != 0 { if gamepad & BUTTON_UP != 0 {
self.mara_dir = Up; self.mara_dir = Up;
@ -124,22 +130,44 @@ impl Game {
self.mara_dir = Right; self.mara_dir = Right;
self.mara_speed = speed; self.mara_speed = speed;
} }
self.prev_gamepad = gamepad;
self.mara_walk(); self.mara_walk();
let player = Rect { base: self.mara_position.clone(), width: 16, height: 16 };
let gate = Rect { base: self.gate_pos.clone(), width: 12, height: 4 * self.gate_count as i32 };
if player.collides(&gate) {
self.mara_speed = 0;
match self.mara_dir {
Right => self.mara_position = self.mara_position.add((-2, 0)),
Left => self.mara_position = self.mara_position.add((2, 0)),
Up => self.mara_position = self.mara_position.add((0, 2)),
Down => self.mara_position = self.mara_position.add((0, -2)),
};
self.bonk();
}
// draw
for i in 0..self.gate_count {
let mut gate = self.gate_pos.clone();
gate.y = gate.y + (sprites::GATE.height * i as u32) as i32;
sprites::GATE.draw(gate, 0);
}
palette::set_draw_color(4);
line(
self.gate_pos.x + 1,
self.gate_pos.y - 1,
self.gate_pos.x + 10,
self.gate_pos.y - 1,
);
palette::set_draw_color(2); palette::set_draw_color(2);
text("Mara: Sh0rk of\nJustice 2\npowered by:\nsh0rk engine v0", 10, 10); text("Mara: Sh0rk of\nJustice 2", 10, 10);
palette::set_draw_color(3); palette::set_draw_color(4);
text("From Within, 2022", 10, 144); text("From Within, 2022", 10, 144);
sprites::MAI.draw(self.mai_position, 0); sprites::MAI.draw(self.mai_position, 0);
sprites::MARA.draw(self.mara_dir, self.mara_frame, self.mara_position, 0);
sprites::MARA.draw(
self.mara_dir,
self.mara_frame,
self.mara_position,
0,
);
} }
} }

View File

@ -7,12 +7,48 @@ pub struct Point {
pub y: 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 { impl From<(i32, i32)> for Point {
fn from((x, y): (i32, i32)) -> Self { fn from((x, y): (i32, i32)) -> Self {
Point { x, y } 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)] #[derive(Clone, Copy, PartialEq, Eq)]
pub enum Direction { pub enum Direction {
Left, Left,
@ -31,12 +67,7 @@ pub struct SpriteAtlas<const N: usize> {
impl<const N: usize> SpriteAtlas<N> { impl<const N: usize> SpriteAtlas<N> {
pub fn draw(&self, dir: Direction, step: u32, p: Point, flags: u32) { pub fn draw(&self, dir: Direction, step: u32, p: Point, flags: u32) {
let flags = flags let flags = flags | if dir == Left { sys::BLIT_FLIP_X } else { 0 };
| if dir == Left {
sys::BLIT_FLIP_X
} else {
0
};
let frame: u32 = if self.animated { let frame: u32 = if self.animated {
match dir { match dir {

View File

@ -10,6 +10,10 @@ pub fn set_palette(palette: [u32; 4]) {
} }
} }
pub fn warmlight() {
set_palette([0xffd191, 0x66605c, 0x211e20, 0xff924f]);
}
pub fn moonlight() { pub fn moonlight() {
set_palette([0xf3eaab, 0x86a0b7, 0x3d476a, 0x19152a]); set_palette([0xf3eaab, 0x86a0b7, 0x3d476a, 0x19152a]);
} }

View File

@ -1,17 +1,17 @@
use crate::sh0rk::{Sprite, SpriteAtlas}; use crate::sh0rk::{Sprite, SpriteAtlas};
pub const GATE: Sprite<12> = Sprite { pub const GATE: Sprite<12> = Sprite {
palette: 0x1320, palette: 0x4210,
width: 12, width: 12,
height: 14, height: 4,
flags: 1, flags: 1,
sprite: [ sprite: [
0x3d, 0x5a, 0xa7, 0xf5, 0xaa, 0x57, 0xd6, 0xa5, 0x6b, 0x1f, 0xff, 0xfc, 0xfd, 0x5a, 0xa7, 0xf5, 0xaa, 0x57, 0xd6, 0xa5, 0x6b, 0x3f, 0xff, 0xfc,
], ],
}; };
pub const MAI: Sprite<64> = Sprite { pub const MAI: Sprite<64> = Sprite {
palette: 0x1320, palette: 0x1420,
width: 16, width: 16,
height: 16, height: 16,
flags: 1, flags: 1,
@ -34,14 +34,14 @@ pub const MARA: SpriteAtlas<384> = SpriteAtlas {
0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfc, 0x3f, 0x00,
0x00, 0xfc, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x36, 0xff, 0xc3, 0x9c, 0x00, 0xfc, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x36, 0xff, 0xc3, 0x9c,
0x36, 0xff, 0xc3, 0x9c, 0x3a, 0xc3, 0xff, 0xac, 0x3a, 0xc3, 0xff, 0xac, 0x00, 0x00, 0x36, 0xff, 0xc3, 0x9c, 0x3a, 0xc3, 0xff, 0xac, 0x3a, 0xc3, 0xff, 0xac, 0x00, 0x00,
0xec, 0x00, 0x00, 0x00, 0xec, 0x00, 0x35, 0xd6, 0xfe, 0x5c, 0x35, 0xd6, 0xfe, 0x5c, 0xec, 0x00, 0x00, 0x00, 0xec, 0x00, 0x35, 0xd5, 0xbe, 0x5c, 0x35, 0xd6, 0xbe, 0x5c,
0x3a, 0xbf, 0xab, 0xac, 0x3a, 0xbf, 0xab, 0xac, 0x30, 0x00, 0xeb, 0x00, 0x30, 0x00, 0x3a, 0xbf, 0xab, 0xac, 0x3a, 0xbf, 0xab, 0xac, 0x30, 0x00, 0xeb, 0x00, 0x30, 0x00,
0xeb, 0x00, 0x3a, 0xdb, 0x56, 0xac, 0x3a, 0xdb, 0x56, 0xac, 0x3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