2022-03-07 13:59:13 +00:00
|
|
|
#![allow(dead_code, unused_imports)]
|
|
|
|
|
2022-03-07 02:10:49 +00:00
|
|
|
#[cfg(feature = "buddy-alloc")]
|
|
|
|
mod alloc;
|
|
|
|
mod sh0rk;
|
|
|
|
mod sprites;
|
|
|
|
|
|
|
|
use fastrand::Rng;
|
2022-03-07 03:56:26 +00:00
|
|
|
use sh0rk::{
|
|
|
|
palette,
|
|
|
|
sys::*,
|
|
|
|
Direction::{self, *},
|
2022-03-07 12:37:13 +00:00
|
|
|
Song, Point, Rect,
|
2022-03-07 03:56:26 +00:00
|
|
|
};
|
2022-03-07 02:10:49 +00:00
|
|
|
|
|
|
|
static mut GAME: Game = Game::new();
|
|
|
|
|
|
|
|
struct Game {
|
|
|
|
frame_count: u32,
|
|
|
|
prev_gamepad: u8,
|
|
|
|
rng: Option<Rng>,
|
|
|
|
mara_position: Point,
|
|
|
|
mara_frame: u32,
|
|
|
|
mara_dir: Direction,
|
2022-03-07 02:40:57 +00:00
|
|
|
mara_speed: i32,
|
|
|
|
bonk_timer: Option<u8>,
|
2022-03-07 03:56:26 +00:00
|
|
|
gate_count: u8,
|
|
|
|
gate_pos: Point,
|
2022-03-07 12:37:13 +00:00
|
|
|
|
2022-03-07 13:59:13 +00:00
|
|
|
// song: Song<48>,
|
2022-03-07 02:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Game {
|
|
|
|
const fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
frame_count: 0,
|
|
|
|
prev_gamepad: 0,
|
|
|
|
rng: None,
|
2022-03-07 13:59:13 +00:00
|
|
|
mara_position: Point { x: 16, y: 96 },
|
2022-03-07 02:10:49 +00:00
|
|
|
mara_frame: 0,
|
|
|
|
mara_dir: Right,
|
2022-03-07 02:40:57 +00:00
|
|
|
mara_speed: 0,
|
|
|
|
bonk_timer: None,
|
2022-03-07 03:56:26 +00:00
|
|
|
gate_count: 4,
|
2022-03-07 13:59:13 +00:00
|
|
|
gate_pos: Point { x: 40, y: 80 },
|
|
|
|
|
|
|
|
// song: Song::new(sh0rk::CHOPSTICKS),
|
2022-03-07 02:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 02:40:57 +00:00
|
|
|
fn bonk(&mut self) {
|
|
|
|
if self.bonk_timer.is_some() {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-07 02:10:49 +00:00
|
|
|
|
2022-03-07 02:40:57 +00:00
|
|
|
tone(220 | (40 << 16), 6 | (6 << 8), 50, TONE_TRIANGLE);
|
|
|
|
self.bonk_timer = Some(12);
|
|
|
|
}
|
2022-03-07 02:10:49 +00:00
|
|
|
|
2022-03-07 02:40:57 +00:00
|
|
|
fn mara_walk(&mut self) {
|
|
|
|
if self.mara_speed == 0 {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-07 02:10:49 +00:00
|
|
|
|
|
|
|
if self.frame_count % 15 == 0 {
|
|
|
|
self.mara_frame = if self.mara_frame == 1 { 0 } else { 1 };
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.frame_count % 4 == 0 {
|
|
|
|
if self.mara_dir == Right && self.mara_position.x >= 144 {
|
2022-03-07 02:40:57 +00:00
|
|
|
self.mara_speed = 0;
|
|
|
|
self.mara_position.x = 144;
|
|
|
|
self.bonk();
|
2022-03-07 02:10:49 +00:00
|
|
|
}
|
|
|
|
if self.mara_dir == Down && self.mara_position.y >= 120 {
|
2022-03-07 02:40:57 +00:00
|
|
|
self.mara_speed = 0;
|
|
|
|
self.mara_position.y = 120;
|
|
|
|
self.bonk();
|
2022-03-07 02:10:49 +00:00
|
|
|
}
|
|
|
|
if self.mara_dir == Left && self.mara_position.x <= 0 {
|
2022-03-07 02:40:57 +00:00
|
|
|
self.mara_speed = 0;
|
|
|
|
self.mara_position.x = 0;
|
|
|
|
self.bonk();
|
2022-03-07 02:10:49 +00:00
|
|
|
}
|
2022-03-07 13:59:13 +00:00
|
|
|
if self.mara_dir == Up && self.mara_position.y <= 80 {
|
2022-03-07 02:40:57 +00:00
|
|
|
self.mara_speed = 0;
|
2022-03-07 13:59:13 +00:00
|
|
|
self.mara_position.y = 80;
|
2022-03-07 02:40:57 +00:00
|
|
|
self.bonk();
|
2022-03-07 02:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
match self.mara_dir {
|
2022-03-07 02:40:57 +00:00
|
|
|
Left => self.mara_position.x -= self.mara_speed,
|
|
|
|
Right => self.mara_position.x += self.mara_speed,
|
|
|
|
Up => self.mara_position.y -= self.mara_speed,
|
|
|
|
Down => self.mara_position.y += self.mara_speed,
|
2022-03-07 02:10:49 +00:00
|
|
|
};
|
|
|
|
}
|
2022-03-07 02:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&mut self) {
|
|
|
|
self.frame_count += 1;
|
2022-03-07 13:59:13 +00:00
|
|
|
// self.song.update();
|
2022-03-07 12:37:13 +00:00
|
|
|
|
2022-03-07 02:40:57 +00:00
|
|
|
if let Some(rem) = self.bonk_timer {
|
|
|
|
if rem == 0 {
|
|
|
|
self.bonk_timer = None;
|
|
|
|
} else {
|
2022-03-07 03:56:26 +00:00
|
|
|
self.bonk_timer = Some(rem - 1);
|
2022-03-07 02:40:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.mara_speed = 0;
|
|
|
|
|
|
|
|
let gamepad = unsafe { *GAMEPAD1 };
|
|
|
|
|
2022-03-07 13:59:13 +00:00
|
|
|
let speed = if gamepad & BUTTON_2 != 0 { 4 } else { 2 };
|
|
|
|
// if just_pressed & BUTTON_1 != 0 {
|
|
|
|
// self.song.pause();
|
|
|
|
// }
|
2022-03-07 02:40:57 +00:00
|
|
|
|
|
|
|
if gamepad & BUTTON_UP != 0 {
|
|
|
|
self.mara_dir = Up;
|
|
|
|
self.mara_speed = speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if gamepad & BUTTON_DOWN != 0 {
|
|
|
|
self.mara_dir = Down;
|
|
|
|
self.mara_speed = speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if gamepad & BUTTON_LEFT != 0 {
|
|
|
|
self.mara_dir = Left;
|
|
|
|
self.mara_speed = speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if gamepad & BUTTON_RIGHT != 0 {
|
|
|
|
self.mara_dir = Right;
|
|
|
|
self.mara_speed = speed;
|
|
|
|
}
|
2022-03-07 03:56:26 +00:00
|
|
|
self.prev_gamepad = gamepad;
|
2022-03-07 02:40:57 +00:00
|
|
|
|
|
|
|
self.mara_walk();
|
|
|
|
|
2022-03-07 12:37:13 +00:00
|
|
|
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,
|
|
|
|
};
|
2022-03-07 03:56:26 +00:00
|
|
|
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
|
2022-03-07 13:59:13 +00:00
|
|
|
sprites::TITLE.draw((0,-8).into(), 0);
|
|
|
|
sprites::SUBTITLE.draw((0,52).into(), 0);
|
|
|
|
|
2022-03-07 03:56:26 +00:00
|
|
|
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(4);
|
2022-03-07 02:40:57 +00:00
|
|
|
text("From Within, 2022", 10, 144);
|
|
|
|
|
2022-03-07 03:56:26 +00:00
|
|
|
sprites::MARA.draw(self.mara_dir, self.mara_frame, self.mara_position, 0);
|
2022-03-07 02:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe fn start() {
|
|
|
|
palette::en4();
|
|
|
|
GAME.rng = Some(Rng::with_seed(420));
|
2022-03-07 13:59:13 +00:00
|
|
|
// GAME.song.pause();
|
2022-03-07 02:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe fn update() {
|
|
|
|
GAME.update();
|
|
|
|
}
|