From af1f9f5d8910f3020bb7ea17eecbfa27d9bcf8e8 Mon Sep 17 00:00:00 2001 From: dawnDus <96957561+dawndus@users.noreply.github.com> Date: Fri, 21 Jan 2022 11:31:25 -0500 Subject: [PATCH] Loading counter --- src/components/nikumaru.rs | 42 +++++++++++++++++++++++++++++++++++++- src/scene/title_scene.rs | 8 ++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/components/nikumaru.rs b/src/components/nikumaru.rs index fcd7e83..f6bf39c 100644 --- a/src/components/nikumaru.rs +++ b/src/components/nikumaru.rs @@ -1,9 +1,12 @@ +use byteorder::{ReadBytesExt, LE}; + use crate::common::Rect; -use crate::components::draw_common::{Alignment, draw_number, draw_number_zeros}; +use crate::components::draw_common::{draw_number, draw_number_zeros, Alignment}; use crate::entity::GameEntity; use crate::frame::Frame; use crate::framework::context::Context; use crate::framework::error::GameResult; +use crate::framework::filesystem; use crate::player::Player; use crate::shared_game_state::{SharedGameState, TimingMode}; @@ -17,6 +20,43 @@ impl NikumaruCounter { pub fn new() -> NikumaruCounter { NikumaruCounter { tick: 0, shown: false } } + + fn load_saved_time(&mut self, ctx: &mut Context) -> GameResult { + if let Ok(mut data) = filesystem::user_open(ctx, "/290.rec") { + let mut ticks: [u32; 4] = [0, 0, 0, 0]; + + for iter in 0..=3 { + ticks[iter] = data.read_u32::()?; + } + + let random = data.read_u32::()?; + let random_list: [u8; 4] = random.to_le_bytes(); + + for iter in 0..=3 { + ticks[iter] = u32::from_le_bytes([ + ticks[iter].to_le_bytes()[0] - random_list[iter], + ticks[iter].to_le_bytes()[1] - random_list[iter], + ticks[iter].to_le_bytes()[2] - random_list[iter], + ticks[iter].to_le_bytes()[3] - random_list[iter] / 2, + ]); + } + + if ticks[0] == ticks[1] && ticks[0] == ticks[2] { + return Ok(ticks[0]); + } + } else { + log::warn!("Cannot open 290.rec."); + } + Ok(0) + } + + pub fn load_counter(&mut self, ctx: &mut Context) -> GameResult { + self.tick = self.load_saved_time(ctx)? as usize; + if self.tick > 0 { + self.shown = true; + } + Ok(()) + } } impl GameEntity<&Player> for NikumaruCounter { diff --git a/src/scene/title_scene.rs b/src/scene/title_scene.rs index 7f89e7f..3bda25c 100644 --- a/src/scene/title_scene.rs +++ b/src/scene/title_scene.rs @@ -1,5 +1,7 @@ use crate::common::{Color, VERSION_BANNER}; use crate::components::background::Background; +use crate::components::nikumaru::NikumaruCounter; +use crate::entity::GameEntity; use crate::frame::Frame; use crate::framework::context::Context; use crate::framework::error::GameResult; @@ -34,6 +36,7 @@ pub struct TitleScene { save_select_menu: Menu, background: Background, frame: Frame, + nikumaru_rec: NikumaruCounter, stage: Stage, textures: StageTexturePaths, } @@ -67,6 +70,7 @@ impl TitleScene { save_select_menu: Menu::new(0, 0, 200, 0), background: Background::new(), frame: Frame::new(), + nikumaru_rec: NikumaruCounter::new(), stage: fake_stage, textures, } @@ -117,6 +121,8 @@ impl Scene for TitleScene { self.controller.update(state, ctx)?; self.controller.update_trigger(); + self.nikumaru_rec.load_counter(ctx)?; + Ok(()) } @@ -211,6 +217,8 @@ impl Scene for TitleScene { self.draw_text_centered(&VERSION_BANNER, state.canvas_size.1 - 15.0, state, ctx)?; self.draw_text_centered(COPYRIGHT_PIXEL, state.canvas_size.1 - 30.0, state, ctx)?; + self.nikumaru_rec.draw(state, ctx, &self.frame)?; + match self.current_menu { CurrentMenu::MainMenu => self.main_menu.draw(state, ctx)?, CurrentMenu::OptionMenu => self.option_menu.draw(state, ctx)?,