From e3e27c05dff5f79fe0df4b387fdbfa1a4d67e5aa Mon Sep 17 00:00:00 2001 From: ZennDev1337 Date: Thu, 12 Oct 2023 15:00:50 +0200 Subject: [PATCH] test_game --- Project/test_game/assets/player_8x8.png | Bin 302 -> 271 bytes Project/test_game/fxdata/fxdata-data.bin | Bin 124 -> 244 bytes Project/test_game/fxdata/fxdata.bin | Bin 256 -> 256 bytes Project/test_game/fxdata/fxdata.h | 10 +-- Project/test_game/fxdata/fxdata.txt | 2 +- Project/test_game/src/character.rs | 97 +++++++++++++++++++++++ Project/test_game/src/fxdata.rs | 6 ++ Project/test_game/src/lib.rs | 72 +++++------------ Project/test_game/src/player.rs | 84 ++++++++++++++++++++ 9 files changed, 212 insertions(+), 59 deletions(-) create mode 100644 Project/test_game/src/character.rs create mode 100644 Project/test_game/src/fxdata.rs create mode 100644 Project/test_game/src/player.rs diff --git a/Project/test_game/assets/player_8x8.png b/Project/test_game/assets/player_8x8.png index 94426fa524f0b69f747bad333c4844c9524a1243..f78b44993360ccb6a7cbbfb86911c0c7bc983b9b 100644 GIT binary patch delta 230 zcmVHSpYypRC)|MgyF!{lQ^*G!n_Ahs*69&!OGxw+cZyf_?Xism(3ycV9Cwl2`Xvw z+}*6%?v}&N=V88}!D=}WZCM~%G(EWbb&IJYK?!$Q9?~165MRv90N~DpIlYmGi*gv& ze%=RLF2o0NXbLWx2kjx0IE590f-Z7BytnruxF(4XTiBV-3cHA{o{M@NR>8(&dAz1B37ck>UR#fk-yF00000 LNkvXXu0mjfM<{hN diff --git a/Project/test_game/fxdata/fxdata-data.bin b/Project/test_game/fxdata/fxdata-data.bin index ebdee8c81d16aa85105e76a8d8dca896d3725f9a..d6ed191fc58b7d5a97bbe945165b04578fcaa705 100644 GIT binary patch literal 244 zcmZXMAr6E<3`IXEPJo;Xa0CPh90DY#Kvq_8h>;D)Kp;2<0>J@Bpa_cWOeW1HyHEAy zwMB*u9%G2!H_yWyFy_9Qzut9aSye?5D-gYH)kTPytyQ;{nMg$aw~Mexd((tIj@G6r qFin6ukR;*9|9#xo|Lf}9aX6>GZO^ms%Q6f=UC;A*URU2=9^eDLy+(5Y literal 124 zcmZSJVBlb2kSqHq#sCCz-|E1$zqP$Kh!3Q_LG-+`x@jPKT3y*ZWd6LmIuPGwURmBW pCWa=Uy1Z!&j0p8$K2X7)H+SBE^d0zdV@Cvo!-5AN0zQ0T006v8BSHWG diff --git a/Project/test_game/fxdata/fxdata.bin b/Project/test_game/fxdata/fxdata.bin index 5bfb776efad8dab7e0af403c8dbe3c5f03939242..24fe8c3897bb533464fc08a36d9c8fc1697be148 100644 GIT binary patch literal 256 zcmZXMp%H{I5Jf*IDnO Self { + Sprite { + mode: dbmMasked, + frame: 0, + Sprite, + Width, + Height, + } + } +} + +#[derive(PartialEq, Debug, Copy, Clone)] +pub struct AnimationStateMachine { + pub left: bool, + pub hurt: u8, + pub animation_state: State, + pub idle_state: bool, +} +impl AnimationStateMachine { + pub const fn new() -> Self { + AnimationStateMachine { + left: false, + hurt: 0, + animation_state: State::Idle, + idle_state: false, + } + } + pub fn set_animation_state(&mut self, arduboy: Arduboy2) { + if arduboy.pressed(UP) + || arduboy.pressed(DOWN) + || arduboy.pressed(LEFT) + || arduboy.pressed(RIGHT) + { + self.animation_state = State::Run; + } else if self.hurt > 0 { + self.hurt -= 1; + self.animation_state = State::Hurt + } else { + self.animation_state = State::Idle; + } + if arduboy.every_x_frames(240) { + self.idle_state = !self.idle_state; + } + } +} + +#[derive(PartialEq, Debug, Copy, Clone)] +pub enum State { + Run, + Idle, + Hurt, + Death, +} +impl State { + pub fn get_frames(&self) -> (u8, u8) { + match self { + State::Run => (0, 3), + State::Idle => (4, 7), + State::Hurt => (8, 10), + State::Death => (11, 14), + } + } + pub fn next_state(&mut self) { + match self { + State::Run => *self = State::Idle, + State::Idle => *self = State::Hurt, + State::Hurt => *self = State::Death, + State::Death => *self = State::Run, + } + } + pub fn last_state(&mut self) { + match self { + State::Run => *self = State::Death, + State::Idle => *self = State::Run, + + State::Hurt => *self = State::Idle, + State::Death => *self = State::Hurt, + } + } +} diff --git a/Project/test_game/src/fxdata.rs b/Project/test_game/src/fxdata.rs new file mode 100644 index 0000000..026692b --- /dev/null +++ b/Project/test_game/src/fxdata.rs @@ -0,0 +1,6 @@ +pub const FX_DATA_PAGE: u16 = 0xffff; +pub const FX_DATA_BYTES: u32 = 244; +pub const FX_PLAYER: u32 = 0x000000; +pub const FX_PLAYER_WIDTH: u16 = 8; +pub const FX_PLAYERHEIGHT: u16 = 8; +pub const FX_PLAYER_FRAMES: u8 = 15; diff --git a/Project/test_game/src/lib.rs b/Project/test_game/src/lib.rs index fee5eb2..8afc440 100644 --- a/Project/test_game/src/lib.rs +++ b/Project/test_game/src/lib.rs @@ -1,19 +1,17 @@ #![no_std] #![allow(non_upper_case_globals)] +mod character; + +mod fxdata; +mod player; + //Include the Arduboy Library use arduboy_rust::prelude::*; - -extern crate arduboy_rust; -use fx_consts::*; - -const FX_DATA_PAGE: u16 = 0xffff; -const FX_DATA_BYTES: u32 = 244; -const FX_DATA_TILES: u32 = 0x000000; -const FX_DATA_TILES_WIDTH: u16 = 8; -const FX_DATA_TILESHEIGHT: u16 = 8; -const FX_DATA_TILES_FRAMES: u8 = 15; +use character::Character; +use fxdata::*; +use player::Player; #[allow(dead_code)] const arduboy: Arduboy2 = Arduboy2::new(); @@ -21,40 +19,15 @@ const arduboy: Arduboy2 = Arduboy2::new(); // Progmem data // dynamic ram variables -static mut lol: u8 = 0; -enum State { - Run, - Idle1, - Idle2, - Hurt, - Death, -} -impl State { - fn get_frames(&self) -> (u8, u8) { - match self { - State::Run => (0, 3), - State::Idle1 => (4, 5), - State::Idle2 => (6, 7), - State::Hurt => (8, 10), - State::Death => (11, 14), - } - } - fn next_state(&mut self) { - match self { - State::Run => *self = State::Idle1, - State::Idle1 => *self = State::Idle2, - State::Idle2 => *self = State::Hurt, - State::Hurt => *self = State::Death, - State::Death => *self = State::Run, - } - } -} -static mut state: State = State::Run; + +static mut player: Player = Player::new(FX_PLAYER, 8, 8); + // The setup() function runs once when you turn your Arduboy on #[no_mangle] pub unsafe extern "C" fn setup() { // put your setup code here, to run once: arduboy.begin(); + arduboy.set_frame_rate(60); fx::begin_data(FX_DATA_PAGE); arduboy.clear() } @@ -68,19 +41,12 @@ pub unsafe extern "C" fn loop_() { return; } arduboy.poll_buttons(); - if A.just_pressed() { - state.next_state(); - } - let (min, max) = state.get_frames(); - if arduboy.every_x_frames(10) { - lol += 1; - if lol < min { - lol = min; - } - if lol > max { - lol = min; - } - } - fx::draw_bitmap(0, 0, FX_DATA_TILES, lol, dbmNormal); + game_loop_(&mut player); + fx::display_clear() } + +unsafe fn game_loop_(character: &mut impl Character) { + character.game_loop(arduboy); + character.draw(); +} diff --git a/Project/test_game/src/player.rs b/Project/test_game/src/player.rs new file mode 100644 index 0000000..39473f0 --- /dev/null +++ b/Project/test_game/src/player.rs @@ -0,0 +1,84 @@ +use crate::character::{AnimationStateMachine, Character, Sprite, State}; +use arduboy_rust::prelude::fx_consts::{ + dbfFlip, dbfMasked, dbmFlip, dbmInvert, dbmMasked, dbmNormal, dbmReverse, +}; +use arduboy_rust::prelude::*; + +#[derive(PartialEq, Debug, Copy, Clone)] +pub struct Player { + posx: i16, + posy: i16, + sprite: Sprite, + animation_state_machine: AnimationStateMachine, +} +impl Character for Player { + fn game_loop(&mut self, arduboy: Arduboy2) { + let (mut min, mut max) = self.animation_state_machine.animation_state.get_frames(); + if self.animation_state_machine.animation_state == State::Idle { + if self.animation_state_machine.idle_state { + max -= 2 + } else { + min += 2 + } + } + let speed = match self.animation_state_machine.animation_state { + State::Run => 10, + State::Idle => 30, + State::Hurt => 20, + State::Death => 20, + }; + if arduboy.every_x_frames(speed) { + self.sprite.frame += 1; + if self.sprite.frame < min { + self.sprite.frame = min; + } + if self.sprite.frame > max { + self.sprite.frame = min; + } + } + self.controller(); + self.animation_state_machine.set_animation_state(arduboy); + } + fn draw(&self) { + fx::draw_bitmap( + self.posx, + self.posy, + self.sprite.Sprite, + self.sprite.frame, + self.sprite.mode, + ) + } +} +impl Player { + pub const fn new(Sprite: u32, Width: u16, Height: u16) -> Self { + Player { + posx: 0, + posy: 0, + sprite: Sprite::new(Sprite, Width, Height), + animation_state_machine: AnimationStateMachine::new(), + } + } + + pub fn controller(&mut self) { + if A.just_pressed() { + self.animation_state_machine.animation_state.next_state() + } + if B.just_pressed() { + self.animation_state_machine.animation_state.last_state() + } + if UP.pressed() { + self.posy -= 1; + } + if DOWN.pressed() { + self.posy += 1; + } + if LEFT.pressed() { + self.posx -= 1; + self.sprite.mode = dbmMasked + } + if RIGHT.pressed() { + self.posx += 1; + self.sprite.mode = dbmMasked + } + } +}