From f36431f49186596828e584f53ac30725355b8a41 Mon Sep 17 00:00:00 2001 From: Jessie Williams Date: Sun, 6 Mar 2022 22:56:26 -0500 Subject: [PATCH] gate collision Signed-off-by: Jessie Williams --- sprites/Mara.png | Bin 391 -> 395 bytes sprites/gate.png | Bin 151 -> 150 bytes src/lib.rs | 62 +++++++++++++++++++++++++++++++------------ src/sh0rk/mod.rs | 43 +++++++++++++++++++++++++----- src/sh0rk/palette.rs | 4 +++ src/sprites.rs | 20 +++++++------- 6 files changed, 96 insertions(+), 33 deletions(-) diff --git a/sprites/Mara.png b/sprites/Mara.png index 5ac24ff557b16fa8d6305749c4e37a3eeb99b466..7a00cf0e95753ebda34641313bcabc987e1c6e44 100644 GIT binary patch delta 311 zcmV-70m%M`1B(NYU4I!#L_t(IjjfbXjszhHM1^qwGp|5_LgU9I>r6U4TMGp2wM1lL znE5NGy~?;&5sUa%CK;I7Wd&B_km2g#un_IPsxr9)Wj@Sm+cO+g4nZh(Hfsy2kBrdp zVkY~^z(V2~;eB3;$zn9~k>mPPSp@}1p*z;jj0fL#n%70tx=r_*7>CADE97nKY z9D&AqT4I^lQc)aKM#z@((^wy7EBnZvY!xKrtnxz3p*0`O-Z~RU+eT44d%k?20S30# znHib>(r+TcSJHyVx z_F!Nzydt9ewfrhu-}Q7lW_CsJ#92ojSZh8UKt-j+EDSjt#yn>qKVuVlP3CnpLK-Tv+qL;?|isZJUSM+3V#k3^1@+XXZGn zols{pmg}X%1~$Qw!|z5i@P7pGo6qGQ+HKlfAaf1q&1?RHGaE?ep6&X2gdjcns%Ymt z@H9>56r^i?Y%HP#eE%P@_CV)Y@M(C$Gz8Is`-6(8&>vVn3N0EYp#cB@002ovPDHLk FV1lcbm2v<8 diff --git a/sprites/gate.png b/sprites/gate.png index 8279ffa80c177b8db329720f6980f131ca0c6bc7..60018a576413a8e5cd33fc6efac5d528e875dba6 100644 GIT binary patch delta 64 zcmbQvIE`^atfh*li(?4K%;bc`#Ke@86ecD&F*X6|Gbt>E2XZ#3a5D#Ru}Ls+TQZdH T`}g%S0}yz+`njxgN@xNAWv&xC delta 65 zcmbQnIGu4qtd**#i(?4K%;bcG#Ke@86ecD&F$3u{DJ+EtayF=NGczl5^B6ENaQQIg Utv>T|F9Q&Gy85}Sb4q9e0CQOq?f?J) diff --git a/src/lib.rs b/src/lib.rs index b53bdf2..02810ff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,13 @@ mod sh0rk; mod sprites; use fastrand::Rng; -use sh0rk::{palette, sys::*, Point, Direction::{self, *}}; +use sh0rk::{ + palette, + sys::*, + Direction::{self, *}, + Rect, + Point, +}; static mut GAME: Game = Game::new(); @@ -18,6 +24,8 @@ struct Game { mara_dir: Direction, mara_speed: i32, bonk_timer: Option, + gate_count: u8, + gate_pos: Point, } impl Game { @@ -26,12 +34,14 @@ impl Game { frame_count: 0, prev_gamepad: 0, rng: None, - mai_position: Point { x: 76, y: 48 }, + mai_position: Point { x: 76, y: 32 }, mara_position: Point { x: 32, y: 90 }, mara_frame: 0, mara_dir: Right, mara_speed: 0, bonk_timer: None, + gate_count: 4, + gate_pos: Point { x: 40, y: 64 }, } } @@ -90,7 +100,7 @@ impl Game { if rem == 0 { self.bonk_timer = None; } else { - self.bonk_timer = Some(rem-1); + self.bonk_timer = Some(rem - 1); } } @@ -99,11 +109,7 @@ impl Game { let gamepad = unsafe { *GAMEPAD1 }; let just_pressed = gamepad & (gamepad ^ self.prev_gamepad); - let speed = if just_pressed & BUTTON_2 != 0 { - 4 - } else { - 2 - }; + let speed = if just_pressed & BUTTON_2 != 0 { 4 } else { 2 }; if gamepad & BUTTON_UP != 0 { self.mara_dir = Up; @@ -124,22 +130,44 @@ impl Game { self.mara_dir = Right; self.mara_speed = speed; } + self.prev_gamepad = gamepad; 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); - text("Mara: Sh0rk of\nJustice 2\npowered by:\nsh0rk engine v0", 10, 10); - palette::set_draw_color(3); + text("Mara: Sh0rk of\nJustice 2", 10, 10); + palette::set_draw_color(4); text("From Within, 2022", 10, 144); 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); } } diff --git a/src/sh0rk/mod.rs b/src/sh0rk/mod.rs index d42fe24..d9e887a 100644 --- a/src/sh0rk/mod.rs +++ b/src/sh0rk/mod.rs @@ -7,12 +7,48 @@ pub struct Point { pub y: i32, } +impl Point { + pub fn add(&self, p: T) -> Point + where + T: Into, + { + 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, @@ -31,12 +67,7 @@ pub struct SpriteAtlas { impl SpriteAtlas { 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 flags = flags | if dir == Left { sys::BLIT_FLIP_X } else { 0 }; let frame: u32 = if self.animated { match dir { diff --git a/src/sh0rk/palette.rs b/src/sh0rk/palette.rs index d98f94f..ec220e2 100644 --- a/src/sh0rk/palette.rs +++ b/src/sh0rk/palette.rs @@ -10,6 +10,10 @@ pub fn set_palette(palette: [u32; 4]) { } } +pub fn warmlight() { + set_palette([0xffd191, 0x66605c, 0x211e20, 0xff924f]); +} + pub fn moonlight() { set_palette([0xf3eaab, 0x86a0b7, 0x3d476a, 0x19152a]); } diff --git a/src/sprites.rs b/src/sprites.rs index 6765ab9..bd2d796 100644 --- a/src/sprites.rs +++ b/src/sprites.rs @@ -1,17 +1,17 @@ use crate::sh0rk::{Sprite, SpriteAtlas}; pub const GATE: Sprite<12> = Sprite { - palette: 0x1320, + palette: 0x4210, width: 12, - height: 14, + height: 4, flags: 1, 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 { - palette: 0x1320, + palette: 0x1420, width: 16, height: 16, 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, 0x00, 0xfc, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x36, 0xff, 0xc3, 0x9c, 0x36, 0x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