From 0f93e06570df5b25f02821906ee30c1bcdedfefb Mon Sep 17 00:00:00 2001 From: Bene <37740907+Nereuxofficial@users.noreply.github.com> Date: Sat, 10 Dec 2022 13:09:54 +0100 Subject: [PATCH 1/5] Cleanup --- game/src/backend/constants.rs | 42 +++++++++++++-------------- game/src/backend/generate_machines.rs | 8 +---- game/src/backend/screen.rs | 4 +-- 3 files changed, 23 insertions(+), 31 deletions(-) diff --git a/game/src/backend/constants.rs b/game/src/backend/constants.rs index 73e2575..5eb548f 100644 --- a/game/src/backend/constants.rs +++ b/game/src/backend/constants.rs @@ -3,7 +3,7 @@ use crate::backend::rlcolor::RLColor; use crate::game_core::player::gen_inventory; use crate::game_core::resources::Resources; use crate::languages::german::MACHINE_NAMES; -use crate::machines::machine::State; +use crate::machines::machine::{Machine, State}; use crate::machines::trade::Trade; use ggez::graphics::{Color, Rect}; use std::string::ToString; @@ -51,10 +51,10 @@ pub(crate) const SANDSTURM_CR: Resources = Resources { /// `Rect` - Returns the collision area of the machine. /// `Vec` - Returns the trades of the machine. /// `Vec` - Returns the resources of the machine. -pub(crate) fn gen_all_machines() -> [(String, Rect, Vec, Resources); 7] { - [ +pub(crate) fn gen_all_machines() -> Vec { + vec![ // Oxygen machine - ( + Machine::new_by_const(( MACHINE_NAMES[1].to_string(), Rect { x: 280.0, @@ -93,9 +93,9 @@ pub(crate) fn gen_all_machines() -> [(String, Rect, Vec, Resources); energy: -30, life: 0, }, - ), + )), // Electricity machine - ( + Machine::new_by_const(( MACHINE_NAMES[2].to_string(), Rect { x: 282.0, @@ -134,9 +134,9 @@ pub(crate) fn gen_all_machines() -> [(String, Rect, Vec, Resources); energy: 200, life: 0, }, - ), - // worker machine - ( + )), + // Worker machine + Machine::new_by_const(( MACHINE_NAMES[3].to_string(), Rect { x: 1000.0, @@ -167,9 +167,9 @@ pub(crate) fn gen_all_machines() -> [(String, Rect, Vec, Resources); energy: -15, life: 0, }, - ), - // 3d_printer machine - ( + )), + // Worker machine + Machine::new_by_const(( MACHINE_NAMES[4].to_string(), Rect { x: 930.0, @@ -200,9 +200,9 @@ pub(crate) fn gen_all_machines() -> [(String, Rect, Vec, Resources); energy: -25, life: 0, }, - ), + )), // Communication module - ( + Machine::new_by_const(( MACHINE_NAMES[5].to_string(), Rect { x: 1640.0, @@ -233,9 +233,9 @@ pub(crate) fn gen_all_machines() -> [(String, Rect, Vec, Resources); energy: -20, life: 0, }, - ), - // first hole - ( + )), + // Second hole + Machine::new_by_const(( MACHINE_NAMES[6].to_string(), Rect { x: 780.0, @@ -256,9 +256,9 @@ pub(crate) fn gen_all_machines() -> [(String, Rect, Vec, Resources); energy: -5, life: 0, }, - ), - // second hole - ( + )), + // Second hole + Machine::new_by_const(( MACHINE_NAMES[7].to_string(), Rect { x: 680.0, @@ -279,6 +279,6 @@ pub(crate) fn gen_all_machines() -> [(String, Rect, Vec, Resources); energy: -5, life: 0, }, - ), + )), ] } diff --git a/game/src/backend/generate_machines.rs b/game/src/backend/generate_machines.rs index 0c91532..dfdb2ac 100644 --- a/game/src/backend/generate_machines.rs +++ b/game/src/backend/generate_machines.rs @@ -1,8 +1,6 @@ //!DIESE DATEI IST ZUM TESTEN VON SANDER use crate::backend::gamestate::GameState; -use crate::machines::machine::Machine; - use crate::backend::constants::gen_all_machines; use crate::backend::rlcolor::RLColor; use crate::backend::utils::get_draw_params; @@ -15,11 +13,7 @@ use tracing::info; impl GameState { pub fn create_machine(&mut self) { info!("Generating all Machines"); - let all = gen_all_machines(); - for m in &all { - let new_ms = Machine::new_by_const(m.clone()); - self.machines.push(new_ms); - } + self.machines = gen_all_machines(); } pub fn draw_machines(&self, canvas: &mut Canvas, scale: Vec2, ctx: &mut Context) -> RLResult { diff --git a/game/src/backend/screen.rs b/game/src/backend/screen.rs index 7a21e19..b4aa829 100644 --- a/game/src/backend/screen.rs +++ b/game/src/backend/screen.rs @@ -151,14 +151,12 @@ impl Screenstack { /// Possible commands are: /// `Push`: Pushes a new screen on the stack, /// `Pop`: Pops the current screen, - /// `None`: Does nothing /// `Popup`: Adds a new popup to the stack /// # Arguments /// * `command` - The command to handle fn process_command(&mut self, command: StackCommand) { // Match the command given back by the screen match command { - StackCommand::None => {} StackCommand::Push(mut screen) => { screen.set_sender(self.sender.clone()); self.screens.push(screen); @@ -167,6 +165,7 @@ impl Screenstack { if self.screens.len() == 1 { std::process::exit(0) } else { + // Clear our popups in order to not display them outside of the Gamestate self.popup.clear(); self.screens.pop(); }; @@ -186,7 +185,6 @@ impl Screenstack { /// We can tell the `Screenstack` to push the `Gamestate` screen onto the /// `Screenstack` pub enum StackCommand { - None, Push(Box), Popup(Popup), Pop, From 7a8993606ef76706df7918685265048440c75c68 Mon Sep 17 00:00:00 2001 From: Bene <37740907+Nereuxofficial@users.noreply.github.com> Date: Sat, 10 Dec 2022 13:28:22 +0100 Subject: [PATCH 2/5] Resume now does not initialize a new game --- game/src/backend/gamestate.rs | 4 ++-- game/src/main_menu/mainmenu.rs | 23 ++++++++++++----------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/game/src/backend/gamestate.rs b/game/src/backend/gamestate.rs index ef8ebf6..f9b8a5a 100644 --- a/game/src/backend/gamestate.rs +++ b/game/src/backend/gamestate.rs @@ -108,7 +108,7 @@ impl GameState { /// and checks if the player has died. /// # Returns /// * `RLResult`: A `RLResult` to validate the success of the tick function - pub fn tick(&mut self, _ctx: &mut Context) -> RLResult { + pub fn tick(&mut self) -> RLResult { // Update Resources self.player.resources = self .player @@ -528,7 +528,7 @@ impl Screen for GameState { /// Updates the game and handles input. Returns `StackCommand::Pop` when Escape is pressed. fn update(&mut self, ctx: &mut Context) -> RLResult { if ctx.time.check_update_time(DESIRED_FPS) { - self.tick(ctx)?; + self.tick()?; self.move_player(ctx)?; Event::update_events(ctx, self)?; } diff --git a/game/src/main_menu/mainmenu.rs b/game/src/main_menu/mainmenu.rs index 50f5b8f..b60fbce 100644 --- a/game/src/main_menu/mainmenu.rs +++ b/game/src/main_menu/mainmenu.rs @@ -5,7 +5,7 @@ use crate::backend::{ utils::get_scale, }; use crate::main_menu::button::Button; -use crate::main_menu::mainmenu::Message::{Exit, NewGame, Start}; +use crate::main_menu::mainmenu::Message::{Exit, NewGame, Resume}; use crate::RLResult; use crate::game_core::infoscreen::InfoScreen; @@ -18,7 +18,7 @@ use std::sync::mpsc::{channel, Receiver, Sender}; pub enum Message { Exit, NewGame, - Start, + Resume, } /// Main menu screen of the game with buttons to start a new game, load a game or exit the game. @@ -26,7 +26,6 @@ pub enum Message { pub struct MainMenu { buttons: Vec