at main 3.5 kB view raw
1use crate::{Fixed, goose_sprites}; 2use agb::display::object::Object; 3use agb::display::{GraphicsFrame, HEIGHT, Priority, WIDTH}; 4use agb::fixnum::{Num, Vector2D, num, vec2}; 5use agb::input::Button; 6 7pub enum GameState { 8 Start, 9 Playing, 10 GameOver, 11} 12 13pub struct GameLoop { 14 pub score: u16, 15 pub game_state: GameState, 16} 17 18impl GameLoop { 19 pub fn new() -> Self { 20 Self { 21 score: 0, 22 game_state: GameState::Start, 23 } 24 } 25 26 pub fn run(&mut self, gba: &mut agb::Gba) { 27 let mut gfx = gba.graphics.get(); 28 29 let mut button_controller = agb::input::ButtonController::new(); 30 let mut goose = Goose::new(); 31 32 loop { 33 button_controller.update(); 34 let mut frame = gfx.frame(); 35 36 let just_pressed = button_controller.is_just_pressed(Button::A); 37 goose.move_goose(just_pressed); 38 goose.show(&mut frame); 39 frame.commit(); 40 } 41 } 42} 43 44/// I really don't have a better name for these 45#[derive(Copy, Clone)] 46pub enum GooseState { 47 Zero = 0, 48 One = 1, 49 Two = 2, 50 Three = 3, 51} 52 53pub struct Goose { 54 pos: Vector2D<Fixed>, 55 velocity: Vector2D<Fixed>, 56 current_state: GooseState, 57 //Should increase every game play loop and reset at 64 58 tick_counter: u8, 59} 60 61impl Goose { 62 pub fn new() -> Self { 63 Self { 64 pos: vec2(num!(5), num!(5)), 65 velocity: vec2(num!(0.20), num!(0)), 66 current_state: GooseState::Zero, 67 tick_counter: 0, 68 } 69 } 70 71 pub fn show(&mut self, frame: &mut GraphicsFrame) { 72 let _ = Object::new(goose_sprites::GOOSE.sprite(self.current_state as usize)) 73 .set_pos(self.pos.round()) 74 .set_priority(Priority::P1) 75 .show(frame); 76 } 77 78 fn animate(&mut self) { 79 // Advance animation every 8 ticks 80 self.tick_counter = self.tick_counter.wrapping_add(1); 81 if self.tick_counter % 8 == 0 { 82 self.current_state = match self.current_state { 83 GooseState::Zero => GooseState::One, 84 GooseState::One => GooseState::Two, 85 GooseState::Two => GooseState::Three, 86 GooseState::Three => GooseState::Zero, 87 } 88 } 89 //Just a catch to not let that number get too big 90 if self.tick_counter >= 64 { 91 self.tick_counter = 0; 92 } 93 } 94 95 fn reset(&mut self) { 96 self.pos = vec2(num!(5), num!(5)); 97 self.velocity = vec2(num!(0.20), num!(0)); 98 } 99 100 pub fn move_goose(&mut self, button_pressed: bool) { 101 //If the button is pressed, go up a bit 102 if button_pressed { 103 self.velocity.y = num!(-1); 104 } else { 105 self.velocity.y += num!(0.02); 106 } 107 108 self.pos += self.velocity; 109 110 //Right now just cycling through may change animations around 111 self.animate(); 112 113 //Keep in frame right now. Will later do the scrolling map thing 114 if self.pos.y < num!(0) { 115 self.pos.y = num!(0); 116 self.velocity.y = num!(0); 117 } 118 if self.pos.y >= num!(HEIGHT) { 119 //TODO call game end here later 120 self.reset(); 121 } 122 123 // Keep X within screen horizontally (optional: clamp) 124 if self.pos.x < num!(0) { 125 self.pos.x = num!(0); 126 } 127 let max_x = num!(WIDTH - 1); 128 if self.pos.x > max_x { 129 self.pos.x = max_x; 130 } 131 } 132}