From a94e71c7ca60b2b456791906cdcd94691acf9743 Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Sun, 3 Oct 2021 21:42:46 -0400 Subject: [PATCH] it might work!? --- game/src/context.rs | 9 +++--- game/src/lib.rs | 30 ++++++++--------- game/src/scenes/ingame_scene/mod.rs | 45 +++++++++++++------------- game/src/scenes/level_select_screen.rs | 14 ++++---- game/src/scenes/next_level_screen.rs | 16 +++++++-- game/src/scenes/win_screen.rs | 2 +- 6 files changed, 64 insertions(+), 52 deletions(-) diff --git a/game/src/context.rs b/game/src/context.rs index 422e72c..b602228 100644 --- a/game/src/context.rs +++ b/game/src/context.rs @@ -13,10 +13,11 @@ use crate::{ #[derive(Debug)] pub enum ControlFlag { Quit, - SwitchLevel(usize), - UpdateLevelStart(DateTime), - SaveProgress, - MaybeUpdateHighScore(usize, Duration), + BeginLevel(usize), + EndLevel, + // UpdateLevelStart(DateTime), + // SaveProgress, + // MaybeUpdateHighScore(usize, Duration), SoundTrigger(String), } diff --git a/game/src/lib.rs b/game/src/lib.rs index 36c5d47..ef8a8a1 100644 --- a/game/src/lib.rs +++ b/game/src/lib.rs @@ -218,7 +218,8 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box Result<(), Box 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()); diff --git a/game/src/scenes/ingame_scene/mod.rs b/game/src/scenes/ingame_scene/mod.rs index 25b98e2..722e9c3 100644 --- a/game/src/scenes/ingame_scene/mod.rs +++ b/game/src/scenes/ingame_scene/mod.rs @@ -104,13 +104,13 @@ impl Action 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 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)); } diff --git a/game/src/scenes/level_select_screen.rs b/game/src/scenes/level_select_screen.rs index 192f005..f783f2f 100644 --- a/game/src/scenes/level_select_screen.rs +++ b/game/src/scenes/level_select_screen.rs @@ -83,15 +83,15 @@ impl Action 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)) diff --git a/game/src/scenes/next_level_screen.rs b/game/src/scenes/next_level_screen.rs index 464ff8c..7418515 100644 --- a/game/src/scenes/next_level_screen.rs +++ b/game/src/scenes/next_level_screen.rs @@ -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 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) diff --git a/game/src/scenes/win_screen.rs b/game/src/scenes/win_screen.rs index b8c2377..afd7515 100644 --- a/game/src/scenes/win_screen.rs +++ b/game/src/scenes/win_screen.rs @@ -69,7 +69,7 @@ impl Action 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)