test_game

This commit is contained in:
ZennDev1337 2023-10-12 15:00:50 +02:00
parent 36cb36ffee
commit e3e27c05df
9 changed files with 212 additions and 59 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 302 B

After

Width:  |  Height:  |  Size: 271 B

Before After
Before After

Binary file not shown.

View file

@ -7,10 +7,10 @@ using uint24_t = __uint24;
// Initialize FX hardware using FX::begin(FX_DATA_PAGE); in the setup() function.
constexpr uint16_t FX_DATA_PAGE = 0xffff;
constexpr uint24_t FX_DATA_BYTES = 124;
constexpr uint24_t FX_DATA_BYTES = 244;
constexpr uint24_t FX_DATA_TILES = 0x000000;
constexpr uint16_t FX_DATA_TILES_WIDTH = 8;
constexpr uint16_t FX_DATA_TILESHEIGHT = 8;
constexpr uint8_t FX_DATA_TILES_FRAMES = 15;
constexpr uint24_t FX_PLAYER = 0x000000;
constexpr uint16_t FX_PLAYER_WIDTH = 8;
constexpr uint16_t FX_PLAYERHEIGHT = 8;
constexpr uint8_t FX_PLAYER_FRAMES = 15;

View file

@ -1 +1 @@
image_t FX_DATA_TILES = "../assets/player_8x8.png"
image_t FX_PLAYER = "../assets/player_8x8.png"

View file

@ -0,0 +1,97 @@
use arduboy_rust::prelude::fx_consts::{dbmMasked, dbmNormal};
use arduboy_rust::prelude::*;
pub trait Character {
fn game_loop(&mut self, arduboy: Arduboy2);
fn draw(&self);
}
#[derive(PartialEq, Debug, Copy, Clone)]
pub struct Sprite {
pub mode: u8,
pub frame: u8,
pub Sprite: u32,
pub Width: u16,
pub Height: u16,
}
impl Sprite {
pub const fn new(Sprite: u32, Width: u16, Height: u16) -> 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,
}
}
}

View file

@ -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;

View file

@ -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();
}

View file

@ -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
}
}
}