From 2d6ee1dddda6a51692adff10160bb93a76430b0b Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sat, 12 May 2018 15:10:35 +0200 Subject: Added new building specifications --- src/engine/command.rs | 2 +- src/engine/mod.rs | 60 ++++++++++++++++---------------------------------- src/engine/settings.rs | 29 +++++++++++++++++++++--- 3 files changed, 46 insertions(+), 45 deletions(-) (limited to 'src/engine') diff --git a/src/engine/command.rs b/src/engine/command.rs index eab98c1..b5cf528 100644 --- a/src/engine/command.rs +++ b/src/engine/command.rs @@ -19,7 +19,7 @@ impl fmt::Display for Command { #[repr(u8)] #[derive(Debug, Clone, Copy)] pub enum BuildingType { - Defense = 0, + Defence = 0, Attack = 1, Energy = 2, } diff --git a/src/engine/mod.rs b/src/engine/mod.rs index be95c03..f05d985 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -2,9 +2,9 @@ pub mod command; pub mod geometry; pub mod settings; -use self::command::{BuildingType, Command}; +use self::command::Command; use self::geometry::Point; -use self::settings::GameSettings; +use self::settings::{GameSettings, BuildingSettings}; use std::ops::Fn; use std::cmp; @@ -61,8 +61,8 @@ impl GameState { } let mut state = self.clone(); - let player_valid = GameState::perform_command(&mut state.player_buildings, player_command, &settings.size); - let opponent_valid = GameState::perform_command(&mut state.opponent_buildings, opponent_command, &settings.size); + let player_valid = GameState::perform_command(&mut state.player_buildings, settings, player_command, &settings.size); + let opponent_valid = GameState::perform_command(&mut state.opponent_buildings, settings, opponent_command, &settings.size); if !player_valid || !opponent_valid { state.status = GameStatus::InvalidMove; @@ -87,13 +87,13 @@ impl GameState { state } - fn perform_command(buildings: &mut Vec, command: Command, size: &Point) -> bool { + fn perform_command(buildings: &mut Vec, settings: &GameSettings, command: Command, size: &Point) -> bool { match command { Command::Nothing => { true }, Command::Build(p, b) => { let occupied = buildings.iter().any(|b| b.pos == p); let in_range = p.x < size.x && p.y < size.y; - buildings.push(Building::new(p, b)); + buildings.push(Building::new(p, settings.building_settings(b))); !occupied && in_range }, } @@ -194,50 +194,28 @@ impl Player { } pub fn can_afford_attack_buildings(&self, settings: &GameSettings) -> bool { - self.energy >= settings.attack_price + self.energy >= settings.attack.price } pub fn can_afford_defence_buildings(&self, settings: &GameSettings) -> bool { - self.energy >= settings.defence_price + self.energy >= settings.defence.price } pub fn can_afford_energy_buildings(&self, settings: &GameSettings) -> bool { - self.energy >= settings.energy_price + self.energy >= settings.energy.price } } impl Building { - fn new(pos: Point, building: BuildingType) -> Building { - match building { - BuildingType::Defense => Building { - pos: pos, - health: 20, - construction_time_left: 3, - weapon_damage: 0, - weapon_speed: 0, - weapon_cooldown_time_left: 0, - weapon_cooldown_period: 0, - energy_generated_per_turn: 0 - }, - BuildingType::Attack => Building { - pos: pos, - health: 5, - construction_time_left: 1, - weapon_damage: 5, - weapon_speed: 1, - weapon_cooldown_time_left: 0, - weapon_cooldown_period: 3, - energy_generated_per_turn: 0 - }, - BuildingType::Energy => Building { - pos: pos, - health: 5, - construction_time_left: 1, - weapon_damage: 0, - weapon_speed: 0, - weapon_cooldown_time_left: 0, - weapon_cooldown_period: 0, - energy_generated_per_turn: 3 - } + fn new(pos: Point, blueprint: &BuildingSettings) -> Building { + Building { + pos: pos, + health: blueprint.health, + construction_time_left: blueprint.construction_time, + weapon_damage: blueprint.weapon_damage, + weapon_speed: blueprint.weapon_speed, + weapon_cooldown_time_left: 0, + weapon_cooldown_period: blueprint.weapon_cooldown_period, + energy_generated_per_turn: blueprint.energy_generated_per_turn } } diff --git a/src/engine/settings.rs b/src/engine/settings.rs index a6691d7..b23d6bd 100644 --- a/src/engine/settings.rs +++ b/src/engine/settings.rs @@ -1,10 +1,33 @@ use super::geometry::Point; +use super::command::BuildingType; #[derive(Debug)] pub struct GameSettings { pub size: Point, pub energy_income: u16, - pub energy_price: u16, - pub defence_price: u16, - pub attack_price: u16 + pub energy: BuildingSettings, + pub defence: BuildingSettings, + pub attack: BuildingSettings +} + +#[derive(Debug)] +pub struct BuildingSettings { + pub price: u16, + pub health: u16, + pub construction_time: u8, + pub weapon_damage: u16, + pub weapon_speed: u8, + pub weapon_cooldown_period: u8, + pub energy_generated_per_turn: u16 +} + +impl GameSettings { + pub fn building_settings(&self, building: BuildingType) -> &BuildingSettings { + match building { + BuildingType::Defence => &self.defence, + BuildingType::Attack => &self.attack, + BuildingType::Energy => &self.energy + } + } + } -- cgit v1.2.3