summaryrefslogtreecommitdiff
path: root/src/engine
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2018-05-12 15:10:35 +0200
committerJustin Worthe <justin@worthe-it.co.za>2018-05-12 15:10:35 +0200
commit2d6ee1dddda6a51692adff10160bb93a76430b0b (patch)
treea511c252097e15cb3b6f9ebbd675e14f1e3665a8 /src/engine
parent4755702ef08d70961b5248cb706a592a406d0556 (diff)
Added new building specifications
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/command.rs2
-rw-r--r--src/engine/mod.rs60
-rw-r--r--src/engine/settings.rs29
3 files changed, 46 insertions, 45 deletions
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<Building>, command: Command, size: &Point) -> bool {
+ fn perform_command(buildings: &mut Vec<Building>, 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
+ }
+ }
+
}