it might work!?

This commit is contained in:
Evan Pratten 2021-10-03 21:42:46 -04:00
parent 9108f27b3d
commit a94e71c7ca
6 changed files with 64 additions and 52 deletions

View File

@ -13,10 +13,11 @@ use crate::{
#[derive(Debug)]
pub enum ControlFlag {
Quit,
SwitchLevel(usize),
UpdateLevelStart(DateTime<Utc>),
SaveProgress,
MaybeUpdateHighScore(usize, Duration),
BeginLevel(usize),
EndLevel,
// UpdateLevelStart(DateTime<Utc>),
// SaveProgress,
// MaybeUpdateHighScore(usize, Duration),
SoundTrigger(String),
}

View File

@ -218,7 +218,8 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box<dyn std:
// Get the main state machine
info!("Setting up the scene management state machine");
let mut game_state_machine =
build_screen_state_machine(&mut context.renderer.borrow_mut(), &raylib_thread, levels).unwrap();
build_screen_state_machine(&mut context.renderer.borrow_mut(), &raylib_thread, levels)
.unwrap();
game_state_machine
.force_change_state(Scenes::MainMenuScreen)
.unwrap();
@ -361,22 +362,21 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box<dyn std:
if let Some(flag) = flag {
match flag {
context::ControlFlag::Quit => break,
context::ControlFlag::SwitchLevel(level) => {
context::ControlFlag::BeginLevel(level) => {
context.as_mut().current_level = level;
context.as_mut().player_progress.save();
context.as_mut().level_start_time = Utc::now();
}
context::ControlFlag::UpdateLevelStart(time) => {
context.as_mut().level_start_time = time;
context.as_mut().player_progress.save();
}
context::ControlFlag::SaveProgress => {
context.as_mut().player_progress.save();
}
context::ControlFlag::MaybeUpdateHighScore(level, time) => {
context
.as_mut()
.player_progress
.maybe_write_new_time(level, &time);
context::ControlFlag::EndLevel => {
let now = Utc::now();
let elapsed = now - context.as_mut().level_start_time;
if elapsed.num_seconds().abs() > 1 {
let current_level = context.as_mut().current_level;
context
.as_mut()
.player_progress
.maybe_write_new_time(current_level, &elapsed);
context.as_mut().player_progress.save();
}
}
context::ControlFlag::SoundTrigger(name) => {
context.audio.play_sound(context.sounds.get(&name).unwrap());

View File

@ -104,13 +104,13 @@ impl Action<Scenes, ScreenError, GameContext> for InGameScreen {
if self.current_level_idx != context.current_level {
self.current_level_idx = context.current_level;
self.level_switch_timestamp = Utc::now();
context
.flag_send
.send(Some(ControlFlag::UpdateLevelStart(
self.level_switch_timestamp,
)))
.unwrap();
// self.level_switch_timestamp = Utc::now();
// context
// .flag_send
// .send(Some(ControlFlag::UpdateLevelStart(
// self.level_switch_timestamp,
// )))
// .unwrap();
}
// Grab exclusive access to the renderer
@ -138,30 +138,29 @@ impl Action<Scenes, ScreenError, GameContext> for InGameScreen {
let cur_level = self.levels.get(context.current_level).unwrap();
if self.player.position.x > cur_level.zones.win.x {
// Save the current time
let elapsed = Utc::now() - self.level_switch_timestamp;
context
.flag_send
.send(Some(ControlFlag::MaybeUpdateHighScore(
self.current_level_idx,
elapsed,
)))
.unwrap();
// let elapsed = Utc::now() - self.level_switch_timestamp;
// context
// .flag_send
// .send(Some(ControlFlag::MaybeUpdateHighScore(
// self.current_level_idx,
// elapsed,
// )))
// .unwrap();
// Save the progress
context
.flag_send
.send(Some(ControlFlag::SaveProgress))
.unwrap();
// End the level
context.flag_send.send(Some(ControlFlag::EndLevel)).unwrap();
// If this is the last level, win the game
if self.current_level_idx >= self.levels.len() - 1 {
return Ok(ActionFlag::SwitchState(Scenes::WinScreen));
} else {
// Otherwise, increment the level counter and switch to the next level
context
.flag_send
.send(Some(ControlFlag::SwitchLevel(self.current_level_idx + 1)))
.unwrap();
// context
// .flag_send
// .send(Some(ControlFlag::SwitchLevel(self.current_level_idx + 1)))
// .unwrap();
return Ok(ActionFlag::SwitchState(Scenes::NextLevelScreen));
}

View File

@ -83,15 +83,15 @@ impl Action<Scenes, ScreenError, GameContext> for LevelSelectScreen {
// Switch the level
context
.flag_send
.send(Some(ControlFlag::SwitchLevel(level)))
.send(Some(ControlFlag::BeginLevel(level)))
.unwrap();
context
.flag_send
.send(Some(ControlFlag::UpdateLevelStart(
Utc::now(),
)))
.unwrap();
// context
// .flag_send
// .send(Some(ControlFlag::UpdateLevelStart(
// Utc::now(),
// )))
// .unwrap();
// Enter the game
Ok(ActionFlag::SwitchState(Scenes::InGameScene))

View File

@ -6,13 +6,17 @@ use discord_sdk::activity::{ActivityBuilder, Assets};
use pkg_version::pkg_version_major;
use raylib::prelude::*;
use crate::{GameConfig, context::{ControlFlag, GameContext}, utilities::{
use crate::{
context::{ControlFlag, GameContext},
utilities::{
datastore::{load_texture_from_internal_data, ResourceLoadError},
game_version::get_version_string,
math::interpolate_exp,
non_ref_raylib::HackedRaylibHandle,
render_layer::ScreenSpaceRender,
}};
},
GameConfig,
};
use super::{Scenes, ScreenError};
use tracing::{debug, error, info, trace};
@ -87,6 +91,14 @@ impl Action<Scenes, ScreenError, GameContext> for NextLevelScreen {
.flag_send
.send(Some(ControlFlag::SoundTrigger("button-press".to_string())))
.unwrap();
// Start the next level
let current_level = context.current_level;
context
.flag_send
.send(Some(ControlFlag::BeginLevel(current_level + 1)))
.unwrap();
Ok(ActionFlag::SwitchState(Scenes::InGameScene))
} else {
Ok(ActionFlag::Continue)

View File

@ -69,7 +69,7 @@ impl Action<Scenes, ScreenError, GameContext> for WinScreen {
.flag_send
.send(Some(ControlFlag::SoundTrigger("button-press".to_string())))
.unwrap();
context.flag_send.send(Some(ControlFlag::SwitchLevel(0))).unwrap();
context.flag_send.send(Some(ControlFlag::EndLevel)).unwrap();
Ok(ActionFlag::SwitchState(Scenes::MainMenuScreen))
} else {
Ok(ActionFlag::Continue)