Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and Cleanups #258

Merged
merged 9 commits into from
Dec 10, 2022
42 changes: 21 additions & 21 deletions game/src/backend/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,10 +51,10 @@ pub(crate) const SANDSTURM_CR: Resources<i16> = Resources {
/// `Rect` - Returns the collision area of the machine.
/// `Vec<Trade>` - Returns the trades of the machine.
/// `Vec<Resources>` - Returns the resources of the machine.
pub(crate) fn gen_all_machines() -> [(String, Rect, Vec<Trade>, Resources<i16>); 7] {
[
pub(crate) fn gen_all_machines() -> Vec<Machine> {
vec![
// Oxygen machine
(
Machine::new_by_const((
MACHINE_NAMES[0].to_string(),
Rect {
x: 280.0,
Expand Down Expand Up @@ -93,9 +93,9 @@ pub(crate) fn gen_all_machines() -> [(String, Rect, Vec<Trade>, Resources<i16>);
energy: -30,
life: 0,
},
),
)),
// Electricity machine
(
Machine::new_by_const((
MACHINE_NAMES[1].to_string(),
Rect {
x: 282.0,
Expand Down Expand Up @@ -134,9 +134,9 @@ pub(crate) fn gen_all_machines() -> [(String, Rect, Vec<Trade>, Resources<i16>);
energy: 200,
life: 0,
},
),
// worker machine
(
)),
// Worker machine
Machine::new_by_const((
MACHINE_NAMES[2].to_string(),
Rect {
x: 1000.0,
Expand Down Expand Up @@ -167,9 +167,9 @@ pub(crate) fn gen_all_machines() -> [(String, Rect, Vec<Trade>, Resources<i16>);
energy: -15,
life: 0,
},
),
// 3d_printer machine
(
)),
// 3d Printer machine
Machine::new_by_const((
MACHINE_NAMES[3].to_string(),
Rect {
x: 930.0,
Expand Down Expand Up @@ -200,9 +200,9 @@ pub(crate) fn gen_all_machines() -> [(String, Rect, Vec<Trade>, Resources<i16>);
energy: -25,
life: 0,
},
),
)),
// Communication module
(
Machine::new_by_const((
MACHINE_NAMES[4].to_string(),
Rect {
x: 1640.0,
Expand Down Expand Up @@ -233,9 +233,9 @@ pub(crate) fn gen_all_machines() -> [(String, Rect, Vec<Trade>, Resources<i16>);
energy: -30,
life: 0,
},
),
// first hole
(
)),
// First hole
Machine::new_by_const((
MACHINE_NAMES[5].to_string(),
Rect {
x: 780.0,
Expand All @@ -256,9 +256,9 @@ pub(crate) fn gen_all_machines() -> [(String, Rect, Vec<Trade>, Resources<i16>);
energy: -5,
life: 0,
},
),
// second hole
(
)),
// Second hole
Machine::new_by_const((
MACHINE_NAMES[6].to_string(),
Rect {
x: 680.0,
Expand All @@ -279,6 +279,6 @@ pub(crate) fn gen_all_machines() -> [(String, Rect, Vec<Trade>, Resources<i16>);
energy: -5,
life: 0,
},
),
)),
]
}
6 changes: 3 additions & 3 deletions game/src/backend/gamestate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -263,7 +263,7 @@ impl GameState {
&text,
Vec2::new(800.0, 400.0 + (i * 30) as f32),
scale
)
);
});
}

Expand Down Expand Up @@ -537,7 +537,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)?;
}
Expand Down
8 changes: 1 addition & 7 deletions game/src/backend/generate_machines.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions game/src/backend/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
};
Expand All @@ -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<dyn Screen>),
Popup(Popup),
Pop,
Expand Down
2 changes: 1 addition & 1 deletion game/src/game_core/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl Event {
if event.name == "Sandsturm" {}
});
// restore resources of inactive events
for event in gamestate.events.iter() {
for event in &gamestate.events {
if !event.is_active() {
if let Some(resources) = event.resources {
gamestate.player.resources_change =
Expand Down
1 change: 1 addition & 0 deletions game/src/languages/german.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub const ENERGY_STRING: &str = "Kälte";
pub const AIR_AND_ENERGY_STRING: &str = "Kälte und zu wenig Luft";
pub const DEATH_REASON_STRING: &str = "Du bist gestorben an";
pub const ADDITIONAL_INFO_STRING: &str = "Bitte drücke ESC!";
pub const RESUME_ERROR_STRING: &str = "Du brauchst zuerst einen Spielstand";

/// Constant for all strings used in `IntroScreen`
pub const INTRO_TEXT: &str = "Du bist auf dem Mars gestrandet und musst überleben.\nDazu musst du die \
Expand Down
30 changes: 18 additions & 12 deletions game/src/main_menu/mainmenu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ 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::backend::screen::Popup;
use crate::game_core::infoscreen::InfoScreen;
use crate::languages::german::BUTTON_TEXT;
use crate::languages::german::{BUTTON_TEXT, RESUME_ERROR_STRING};
use ggez::{graphics, Context};
use std::sync::mpsc::{channel, Receiver, Sender};

Expand All @@ -18,15 +19,14 @@ 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.
#[derive(Debug)]
pub struct MainMenu {
buttons: Vec<Button>,
receiver: Receiver<Message>,
_sender: Sender<Message>,
screen_sender: Sender<StackCommand>,
background_image: Option<graphics::Image>,
}
Expand All @@ -42,7 +42,7 @@ impl MainMenu {

let start_button = Button::new(
BUTTON_TEXT[0].to_string(),
Start,
Resume,
sender.clone(),
graphics::Rect::new(1322., 350., 450., 120.),
RLColor::GREY,
Expand All @@ -61,7 +61,7 @@ impl MainMenu {
let exit_button = Button::new(
BUTTON_TEXT[2].to_string(),
Exit,
sender.clone(),
sender,
graphics::Rect::new(1322., 630., 450., 120.),
RLColor::GREY,
RLColor::DARK_GREY,
Expand All @@ -70,7 +70,6 @@ impl MainMenu {
Self {
buttons: vec![start_button, new_game_button, exit_button],
receiver,
_sender: sender,
screen_sender,
background_image: None,
}
Expand Down Expand Up @@ -105,11 +104,18 @@ impl Screen for MainMenu {
InfoScreen::new_introscreen(cloned_sender),
)))?;
}
Start => self.screen_sender.send(StackCommand::Push(Box::new({
let mut gamestate = GameState::load(false).unwrap_or_default();
gamestate.init(ctx)?;
gamestate
})))?,
Resume => {
if let Ok(mut gamestate) = GameState::load(false) {
self.screen_sender.send(StackCommand::Push(Box::new({
gamestate.init(ctx)?;
gamestate
})))?;
} else {
self.screen_sender.send(StackCommand::Popup(Popup::warning(
RESUME_ERROR_STRING.to_string(),
)))?;
}
}
}
}
Ok(())
Expand Down