Fix the games

This commit is contained in:
Gwilym Kuiper 2023-02-23 21:18:29 +00:00
parent 92066f7adb
commit b90a5829b8
8 changed files with 57 additions and 98 deletions

View file

@ -90,7 +90,7 @@ pub struct PlayerDice {
}
struct Agb<'a> {
obj: ObjectController,
obj: ObjectController<'a>,
vblank: VBlank,
star_background: StarBackground<'a>,
vram: VRamManager,
@ -101,7 +101,7 @@ pub fn main(mut gba: agb::Gba) -> ! {
save::init_save(&mut gba).expect("Could not initialize save game");
if save::load_high_score() > 1000 {
save::save_high_score(&mut gba, 0).expect("Could not reset high score");
save::save_high_score(&mut gba.save, 0).expect("Could not reset high score");
}
let gfx = gba.display.object.get();
@ -208,7 +208,7 @@ pub fn main(mut gba: agb::Gba) -> ! {
agb.obj.commit();
agb.sfx.customise();
if save::load_high_score() < current_level {
save::save_high_score(&mut gba, current_level)
save::save_high_score(&mut gba.save, current_level)
.expect("Could not save high score");
}
break;

View file

@ -1,4 +1,4 @@
use agb::save::Error;
use agb::save::{Error, SaveManager};
use agb::sync::Static;
use agb::Gba;
@ -15,7 +15,7 @@ pub fn init_save(gba: &mut Gba) -> Result<(), Error> {
if buffer[0] != 0 {
access.prepare_write(0..1)?.write(0, &[0])?;
core::mem::drop(access);
save_high_score(gba, 0)?;
save_high_score(&mut gba.save, 0)?;
} else {
let mut buffer = [0; 4];
access.read(1, &mut buffer)?;
@ -35,9 +35,8 @@ pub fn load_high_score() -> u32 {
HIGH_SCORE.read()
}
pub fn save_high_score(gba: &mut Gba, score: u32) -> Result<(), Error> {
gba.save
.access()?
pub fn save_high_score(save: &mut SaveManager, score: u32) -> Result<(), Error> {
save.access()?
.prepare_write(1..5)?
.write(1, &score.to_le_bytes())?;
HIGH_SCORE.write(score);

View file

@ -45,14 +45,14 @@ enum BattleOrMenu {
}
pub struct Sfx<'a> {
mixer: &'a mut Mixer,
mixer: &'a mut Mixer<'a>,
state: BattleOrMenu,
current_bgm: ChannelId,
}
impl<'a> Sfx<'a> {
pub fn new(mixer: &'a mut Mixer) -> Self {
pub fn new(mixer: &'a mut Mixer<'a>) -> Self {
let mut title_music = SoundChannel::new_high_priority(TITLE_BGM);
title_music.should_loop();
let title_channel = mixer.play_sound(title_music).unwrap();

View file

@ -20,6 +20,7 @@ use agb::{
sound::mixer::Frequency,
};
use alloc::boxed::Box;
use sfx::SfxPlayer;
mod enemies;
mod level_display;
@ -693,7 +694,7 @@ impl<'a, 'b> PlayingLevel<'a, 'b> {
fn update_frame(
&mut self,
sfx_player: &mut sfx::SfxPlayer,
sfx_player: &mut SfxPlayer,
vram: &mut VRamManager,
controller: &'a ObjectController,
) -> UpdateState {
@ -798,13 +799,17 @@ pub fn main(mut agb: agb::Gba) -> ! {
}
}
let mut mixer = agb.mixer.mixer(Frequency::Hz10512);
mixer.enable();
let mut sfx = sfx::SfxPlayer::new(&mut mixer);
world_display.commit(&mut vram);
world_display.show();
splash_screen::show_splash_screen(
splash_screen::SplashScreen::Start,
None,
None,
&mut sfx,
&mut splash_screen,
&mut vram,
);
@ -816,10 +821,6 @@ pub fn main(mut agb: agb::Gba) -> ! {
vram.set_background_palettes(tile_sheet::PALETTES);
let object = agb.display.object.get();
let mut mixer = agb.mixer.mixer(Frequency::Hz10512);
mixer.enable();
let mut music_box = sfx::MusicBox::new();
let vblank = agb::interrupt::VBlank::get();
let mut current_level = 0;
@ -829,8 +830,7 @@ pub fn main(mut agb: agb::Gba) -> ! {
break;
}
music_box.before_frame(&mut mixer);
mixer.frame();
sfx.frame();
vblank.wait_for_vblank();
level_display::write_level(
@ -844,8 +844,7 @@ pub fn main(mut agb: agb::Gba) -> ! {
world_display.commit(&mut vram);
world_display.show();
music_box.before_frame(&mut mixer);
mixer.frame();
sfx.frame();
vblank.wait_for_vblank();
let map_current_level = current_level;
@ -889,20 +888,17 @@ pub fn main(mut agb: agb::Gba) -> ! {
);
while level.background.init_background(&mut vram) != PartialUpdateStatus::Done {
music_box.before_frame(&mut mixer);
mixer.frame();
sfx.frame();
vblank.wait_for_vblank();
}
while level.background.init_foreground(&mut vram) != PartialUpdateStatus::Done {
music_box.before_frame(&mut mixer);
mixer.frame();
sfx.frame();
vblank.wait_for_vblank();
}
for _ in 0..20 {
music_box.before_frame(&mut mixer);
mixer.frame();
sfx.frame();
vblank.wait_for_vblank();
}
@ -913,17 +909,12 @@ pub fn main(mut agb: agb::Gba) -> ! {
world_display.hide();
loop {
match level.update_frame(
&mut sfx::SfxPlayer::new(&mut mixer, &music_box),
&mut vram,
&object,
) {
match level.update_frame(&mut sfx, &mut vram, &object) {
UpdateState::Normal => {}
UpdateState::Dead => {
level.dead_start();
while level.dead_update(&object) {
music_box.before_frame(&mut mixer);
mixer.frame();
sfx.frame();
vblank.wait_for_vblank();
object.commit();
}
@ -935,8 +926,7 @@ pub fn main(mut agb: agb::Gba) -> ! {
}
}
music_box.before_frame(&mut mixer);
mixer.frame();
sfx.frame();
vblank.wait_for_vblank();
object.commit();
}
@ -949,8 +939,7 @@ pub fn main(mut agb: agb::Gba) -> ! {
splash_screen::show_splash_screen(
splash_screen::SplashScreen::End,
Some(&mut mixer),
Some(&mut music_box),
&mut sfx,
&mut splash_screen,
&mut vram,
);

View file

@ -37,40 +37,14 @@ mod effects {
pub const SNAIL_DEATH: &[u8] = agb::include_wav!("sfx/snail-death.wav");
}
pub struct MusicBox {
frame: i32,
}
impl MusicBox {
pub fn new() -> Self {
MusicBox { frame: 0 }
}
pub fn before_frame(&mut self, mixer: &mut Mixer) {
if self.frame == 0 {
// play the introduction
mixer.play_sound(SoundChannel::new_high_priority(music_data::INTRO_MUSIC));
} else if self.frame == music_data::TRIGGER_MUSIC_POINT
|| (self.frame - music_data::TRIGGER_MUSIC_POINT) % music_data::LOOP_MUSIC == 0
{
mixer.play_sound(SoundChannel::new_high_priority(music_data::LOOP));
}
self.frame += 1;
}
}
pub struct SfxPlayer<'a> {
mixer: &'a mut Mixer,
mixer: &'a mut Mixer<'a>,
frame: i32,
}
impl<'a> SfxPlayer<'a> {
pub fn new(mixer: &'a mut Mixer, music_box: &MusicBox) -> Self {
SfxPlayer {
mixer,
frame: music_box.frame,
}
pub fn new(mixer: &'a mut Mixer<'a>) -> Self {
SfxPlayer { mixer, frame: 0 }
}
pub fn catch(&mut self) {
@ -120,7 +94,24 @@ impl<'a> SfxPlayer<'a> {
fn play_random(&mut self, effect: &[&'static [u8]]) {
self.mixer.play_sound(SoundChannel::new(
effect[(self.frame as usize) % effect.len()],
effect[agb::rng::gen() as usize % effect.len()],
));
}
pub fn frame(&mut self) {
if self.frame == 0 {
// play the introduction
self.mixer
.play_sound(SoundChannel::new_high_priority(music_data::INTRO_MUSIC));
} else if self.frame == music_data::TRIGGER_MUSIC_POINT
|| (self.frame - music_data::TRIGGER_MUSIC_POINT) % music_data::LOOP_MUSIC == 0
{
self.mixer
.play_sound(SoundChannel::new_high_priority(music_data::LOOP));
}
self.frame += 1;
self.mixer.frame();
}
}

View file

@ -1,8 +1,5 @@
use super::sfx::MusicBox;
use agb::{
display::tiled::{RegularMap, TileFormat, TileSet, TileSetting, TiledMap, VRamManager},
sound::mixer::Mixer,
};
use super::sfx::SfxPlayer;
use agb::display::tiled::{RegularMap, TileFormat, TileSet, TileSetting, TiledMap, VRamManager};
agb::include_gfx!("gfx/splash_screens.toml");
@ -13,8 +10,7 @@ pub enum SplashScreen {
pub fn show_splash_screen(
which: SplashScreen,
mut mixer: Option<&mut Mixer>,
mut music_box: Option<&mut MusicBox>,
sfx: &mut SfxPlayer,
map: &mut RegularMap,
vram: &mut VRamManager,
) {
@ -32,13 +28,7 @@ pub fn show_splash_screen(
let mut input = agb::input::ButtonController::new();
if let Some(ref mut mixer) = mixer {
if let Some(ref mut music_box) = music_box {
music_box.before_frame(mixer);
}
mixer.frame();
}
sfx.frame();
vblank.wait_for_vblank();
for y in 0..20u16 {
@ -51,13 +41,7 @@ pub fn show_splash_screen(
);
}
if let Some(ref mut mixer) = mixer {
if let Some(ref mut music_box) = music_box {
music_box.before_frame(mixer);
}
mixer.frame();
}
sfx.frame();
vblank.wait_for_vblank();
}
@ -75,12 +59,8 @@ pub fn show_splash_screen(
) {
break;
}
if let Some(mixer) = &mut mixer {
if let Some(music_box) = &mut music_box {
music_box.before_frame(mixer);
}
mixer.frame();
}
sfx.frame();
vblank.wait_for_vblank();
}

View file

@ -533,7 +533,7 @@ struct Player<'a> {
}
impl<'a> Player<'a> {
fn new(object_controller: &'a ObjectController) -> Player {
fn new(object_controller: &'a ObjectController<'a>) -> Player {
let mut entity = Entity::new(
object_controller,
Rect::new((0_u16, 0_u16).into(), (4_u16, 12_u16).into()),

View file

@ -27,11 +27,11 @@ const BLUE_SPIRIT: &[u8] = agb::include_wav!("sfx/03 - Blue Spirit (Main Loop).w
pub struct Sfx<'a> {
bgm: Option<ChannelId>,
mixer: &'a mut Mixer,
mixer: &'a mut Mixer<'a>,
}
impl<'a> Sfx<'a> {
pub fn new(mixer: &'a mut Mixer) -> Self {
pub fn new(mixer: &'a mut Mixer<'a>) -> Self {
Self { mixer, bgm: None }
}