Skip to content

Commit

Permalink
improve replay saving logic for UI
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhail-vlasenko committed Dec 29, 2024
1 parent 0377be5 commit f190cb9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ffi/src/game_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl GameState {
// save the replay
if SETTINGS.read().unwrap().record_replays && !self.recorded_replay.is_empty() {
let path = PathBuf::from(SETTINGS.read().unwrap().replay_folder.clone().into_owned());
let name = self.recorded_replay.make_save_name();
let name = self.recorded_replay.make_save_name().unwrap(); // unwrap is safe here because record_replays
let path = path.join(name.clone());
self.recorded_replay.save(path.as_path());
self.recorded_replay = Replay::new();
Expand Down
17 changes: 12 additions & 5 deletions game_logic/src/auxiliary/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,20 @@ impl Replay {
Self::from_binary_string(&data)
}

pub fn make_save_name(&self) -> String {
/// Returns the name of the save file based on the current time and player score.
/// Returns None if there are no states and the save is not possible.
pub fn make_save_name(&self) -> Option<String> {
let mut name = String::from("replay_");
name.push_str(&chrono::Local::now().format("%Y-%m-%d_%H-%M-%S").to_string());
let score = self.states.last().unwrap().player.get_score();
name.push_str(&format!("_score_{}", score));
name.push_str(".postcard");
name
let state_option = self.states.last();
if let Some(state) = state_option {
name.push_str(&format!("_score_{}", state.player.get_score()));
name.push_str(".postcard");
Some(name)
} else {
None
}

}

pub fn apply_state(&mut self, field: &mut Field, player: &mut Player) {
Expand Down
17 changes: 13 additions & 4 deletions minecraft/src/graphics/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use game_logic::crafting::consumable::Consumable;
use game_logic::character::player::Player;
use game_logic::crafting::interactable::InteractableKind;
use crate::graphics::buffers::Buffers;
use crate::graphics::ui::egui_manager::EguiManager;
use crate::graphics::ui::egui_manager::{EguiManager, ReplaySaveStatus};
use crate::graphics::instance::*;
use crate::graphics::texture_bind_groups::TextureBindGroups;
use crate::graphics::vertex::{CENTERED_SQUARE_VERTICES, HP_BAR_SCALING_COEF, INDICES, make_animation_vertices, make_hp_vertices, PROJECTILE_ARROW_VERTICES, Vertex, VERTICES};
Expand Down Expand Up @@ -179,6 +179,9 @@ impl<'a> State<'a> {

let buffers = Buffers::new(&device, field.get_map_render_distance() as i32);

// Set the record_replays to true when running with graphics
SETTINGS.write().unwrap().record_replays = true;

Self {
surface,
window,
Expand Down Expand Up @@ -206,6 +209,7 @@ impl<'a> State<'a> {
self.player = player;
self.recorded_replay = replay;
self.milestone_tracker = milestone_tracker;
self.egui_manager.replay_save_status = ReplaySaveStatus::Empty;
}

pub fn get_size(&self) -> PhysicalSize<u32> {
Expand Down Expand Up @@ -337,10 +341,15 @@ impl<'a> State<'a> {
if self.egui_manager.save_replay_clicked {
let path = PathBuf::from(SETTINGS.read().unwrap().replay_folder.clone().into_owned());
let name = self.recorded_replay.make_save_name();
let path = path.join(name.clone());
self.recorded_replay.save(path.as_path());
self.egui_manager.save_replay_clicked = false;
self.egui_manager.replay_save_name = Some(name);
if let Some(name) = name {
let path = path.join(name.clone());
self.recorded_replay.save(path.as_path());
self.egui_manager.replay_save_status = ReplaySaveStatus::Saved(name);
} else {
self.egui_manager.replay_save_status = ReplaySaveStatus::Error;
}

}

let mob_positions_and_hp = self.field.close_mob_info(|mob| {
Expand Down
19 changes: 15 additions & 4 deletions minecraft/src/graphics/ui/egui_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct EguiManager {
pub main_menu: MainMenu,
pub main_menu_open: RefCell<bool>,
pub save_replay_clicked: bool,
pub replay_save_name: Option<String>,
pub replay_save_status: ReplaySaveStatus,
}

impl EguiManager {
Expand All @@ -36,7 +36,7 @@ impl EguiManager {
main_menu: MainMenu::new(),
main_menu_open: RefCell::new(true),
save_replay_clicked: false,
replay_save_name: None,
replay_save_status: ReplaySaveStatus::Empty,
}
}

Expand Down Expand Up @@ -246,11 +246,16 @@ impl EguiManager {
.font(FontId::proportional(60.0))).clicked() {
self.save_replay_clicked = true;
}
if self.replay_save_name.is_some() {
if let ReplaySaveStatus::Saved(name) = &self.replay_save_status {
ui.add(Label::new(RichText::new(
format!("Replay saved as: {}", self.replay_save_name.as_ref().unwrap()))
format!("Replay saved as: {}", name))
.font(FontId::proportional(20.0))
));
} else if let ReplaySaveStatus::Error = &self.replay_save_status {
ui.add(Label::new(RichText::new("Could not save the replay. Check if replay recording is enabled in settings.")
.font(FontId::proportional(20.0))
.color(Color32::RED)
));
}
});
}
Expand Down Expand Up @@ -283,3 +288,9 @@ fn time_to_weekday(time: f32) -> String {
format!("Week {}, {}", week, weekday)
}

pub enum ReplaySaveStatus {
Empty,
Saved(String),
Error,
}

2 changes: 1 addition & 1 deletion minecraft/src/graphics/ui/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl MainMenu {
columns[1].add_space(10.0);

columns[1].label("Replays:");
columns[1].checkbox(&mut settings.record_replays, "Save Replays");
columns[1].checkbox(&mut settings.record_replays, "Record Replays");
let mut replay_folder = settings.replay_folder.clone().into_owned();
columns[1].horizontal(|ui| {
ui.label("Replay Folder:");
Expand Down

0 comments on commit f190cb9

Please sign in to comment.