Add world resetting and state saving
This commit is contained in:
parent
7dac0bbfd1
commit
7979295f7a
@ -77,6 +77,22 @@ impl GameProgress {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self, new_progress: &GameProgress) {
|
||||||
|
|
||||||
|
// Bring in new data
|
||||||
|
self.coins = new_progress.coins;
|
||||||
|
self.inventory = new_progress.inventory.clone();
|
||||||
|
self.max_depth = self.max_depth.max(new_progress.max_depth);
|
||||||
|
// self.fastest_time = self.fastest_time.min(new_progress.fastest_time);
|
||||||
|
|
||||||
|
// Write to file
|
||||||
|
let result = self.to_file("./assets/savestate.json".to_string());
|
||||||
|
if result.is_err() {
|
||||||
|
println!("Could not save game state. Holding in RAM");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This structure contains the entire game state, and should be passed around to various logic functions.
|
/// This structure contains the entire game state, and should be passed around to various logic functions.
|
||||||
|
@ -87,6 +87,10 @@ impl Screen for MainMenuScreen {
|
|||||||
// Check clicks
|
// Check clicks
|
||||||
if mouse_clicked {
|
if mouse_clicked {
|
||||||
if hovering_play_button {
|
if hovering_play_button {
|
||||||
|
// Reset the world
|
||||||
|
game_core.world.reset(&mut game_core.player);
|
||||||
|
|
||||||
|
// Start playing
|
||||||
return Some(GameState::InGame);
|
return Some(GameState::InGame);
|
||||||
} else if hovering_shop_button {
|
} else if hovering_shop_button {
|
||||||
return Some(GameState::InShop);
|
return Some(GameState::InShop);
|
||||||
|
@ -146,9 +146,20 @@ pub fn render_shop(
|
|||||||
|
|
||||||
// Handle click actions on the buttons
|
// Handle click actions on the buttons
|
||||||
if draw_handle.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) {
|
if draw_handle.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) {
|
||||||
|
|
||||||
|
// Handle saving core state
|
||||||
|
if menu_button.is_hovered(draw_handle) || play_button.is_hovered(draw_handle) {
|
||||||
|
let new_progress = game_core.player.create_statistics(game_core, draw_handle.get_time());
|
||||||
|
game_core.progress.update(&new_progress);
|
||||||
|
}
|
||||||
|
|
||||||
if menu_button.is_hovered(draw_handle) {
|
if menu_button.is_hovered(draw_handle) {
|
||||||
return Some(GameState::MainMenu);
|
return Some(GameState::MainMenu);
|
||||||
} else if play_button.is_hovered(draw_handle) {
|
} else if play_button.is_hovered(draw_handle) {
|
||||||
|
// Reset the world
|
||||||
|
game_core.world.reset(&mut game_core.player);
|
||||||
|
|
||||||
|
// Start playing
|
||||||
return Some(GameState::InGame);
|
return Some(GameState::InGame);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
14
src/main.rs
14
src/main.rs
@ -58,6 +58,8 @@ fn main() {
|
|||||||
|
|
||||||
// Set up the game's core state
|
// Set up the game's core state
|
||||||
let mut game_core = GameCore::new(&mut raylib, &raylib_thread, world, game_progress);
|
let mut game_core = GameCore::new(&mut raylib, &raylib_thread, world, game_progress);
|
||||||
|
game_core.player.inventory = game_core.progress.inventory.clone();
|
||||||
|
game_core.player.coins = game_core.progress.coins;
|
||||||
|
|
||||||
// Set up the game's profiler
|
// Set up the game's profiler
|
||||||
let mut profiler = GameProfiler::new();
|
let mut profiler = GameProfiler::new();
|
||||||
@ -125,6 +127,12 @@ fn main() {
|
|||||||
|
|
||||||
// Handle game quit
|
// Handle game quit
|
||||||
if new_state == GameState::GameQuit {
|
if new_state == GameState::GameQuit {
|
||||||
|
// Save the game state
|
||||||
|
let new_progress = game_core
|
||||||
|
.player
|
||||||
|
.create_statistics(&game_core, draw_handle.get_time());
|
||||||
|
game_core.progress.update(&new_progress);
|
||||||
|
|
||||||
// For now, just quit
|
// For now, just quit
|
||||||
// This also throws a SEGFAULT.. yay for unsafe code..
|
// This also throws a SEGFAULT.. yay for unsafe code..
|
||||||
info!("User quit game");
|
info!("User quit game");
|
||||||
@ -181,6 +189,12 @@ fn main() {
|
|||||||
game_core.last_frame_time = draw_handle.get_time();
|
game_core.last_frame_time = draw_handle.get_time();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save the game state
|
||||||
|
let new_progress = game_core
|
||||||
|
.player
|
||||||
|
.create_statistics(&game_core, raylib.get_time());
|
||||||
|
game_core.progress.update(&new_progress);
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
profiler.stop();
|
profiler.stop();
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,12 @@ impl Player {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reset(&mut self, position: Vector2) {
|
||||||
|
self.position = position;
|
||||||
|
self.breath_percent = 1.0;
|
||||||
|
self.boost_percent = 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn collides_with_rec(&self, rectangle: &Rectangle) -> bool {
|
pub fn collides_with_rec(&self, rectangle: &Rectangle) -> bool {
|
||||||
// // Build a bounding box of the player by their corners
|
// // Build a bounding box of the player by their corners
|
||||||
// let top_left_corner = self.position - (self.size / 2.0);
|
// let top_left_corner = self.position - (self.size / 2.0);
|
||||||
|
12
src/world.rs
12
src/world.rs
@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use failure::Error;
|
use failure::Error;
|
||||||
|
|
||||||
use crate::entities::{enemy::{jellyfish::JellyFish, octopus::Octopus}, fish::FishEntity};
|
use crate::{entities::{enemy::{jellyfish::JellyFish, octopus::Octopus}, fish::FishEntity}, player::Player};
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct World {
|
pub struct World {
|
||||||
@ -58,10 +58,12 @@ impl World {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
pub fn reset(&mut self) {
|
pub fn reset(&mut self, player: &mut Player) {
|
||||||
for fish in self.fish.iter_mut() {
|
// Init all fish
|
||||||
fish.following_player = false;
|
self.fish = FishEntity::new_from_positions(&self.fish_positions);
|
||||||
}
|
|
||||||
|
// Reset the player
|
||||||
|
player.reset(self.player_spawn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user