summaryrefslogtreecommitdiff
path: root/src/engine
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2018-05-10 23:01:22 +0200
committerJustin Worthe <justin@worthe-it.co.za>2018-05-10 23:01:22 +0200
commit4755702ef08d70961b5248cb706a592a406d0556 (patch)
treebfe8ff9e016338a58b6d0d9314d1b67b17cafea9 /src/engine
parent11c791a59ac60241f253cdfb4e8765039d15edff (diff)
Split to library. Reimplemented sample strategy in new state.
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/command.rs6
-rw-r--r--src/engine/mod.rs147
2 files changed, 98 insertions, 55 deletions
diff --git a/src/engine/command.rs b/src/engine/command.rs
index 603ee42..eab98c1 100644
--- a/src/engine/command.rs
+++ b/src/engine/command.rs
@@ -9,11 +9,9 @@ pub enum Command {
impl fmt::Display for Command {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- use Command::*;
-
match self {
- &Nothing => write!(f, ""),
- &Build(p, b) => write!(f, "{},{},{}", p.x, p.y, b as u8),
+ &Command::Nothing => write!(f, ""),
+ &Command::Build(p, b) => write!(f, "{},{},{}", p.x, p.y, b as u8),
}
}
}
diff --git a/src/engine/mod.rs b/src/engine/mod.rs
index 4ea63a3..be95c03 100644
--- a/src/engine/mod.rs
+++ b/src/engine/mod.rs
@@ -29,12 +29,6 @@ pub enum GameStatus {
InvalidMove
}
-impl GameStatus {
- fn is_complete(&self) -> bool {
- *self != GameStatus::Continue
- }
-}
-
#[derive(Debug, Clone)]
pub struct Player {
pub energy: u16,
@@ -53,51 +47,6 @@ pub struct Building {
pub energy_generated_per_turn: u16
}
-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 is_constructed(&self) -> bool {
- self.construction_time_left == 0
- }
-
- fn is_shooty(&self) -> bool {
- self.is_constructed() && self.weapon_damage > 0
- }
-}
-
#[derive(Debug, Clone)]
pub struct Missile {
pub pos: Point,
@@ -215,4 +164,100 @@ impl GameState {
(false, false) => GameStatus::Continue,
};
}
+
+ pub fn unoccupied_player_cells_in_row(&self, settings: &GameSettings, y: u8) -> Vec<Point> {
+ (0..settings.size.x/2)
+ .map(|x| Point::new(x, y))
+ .filter(|&p| !self.player_buildings.iter().any(|b| b.pos == p))
+ .collect()
+ }
+
+ pub fn unoccupied_player_cells(&self, settings: &GameSettings) -> Vec<Point> {
+ (0..settings.size.y)
+ .flat_map(|y| (0..settings.size.x/2).map(|x| Point::new(x, y)).collect::<Vec<_>>())
+ .filter(|&p| !self.player_buildings.iter().any(|b| b.pos == p))
+ .collect()
+ }
+}
+
+impl GameStatus {
+ fn is_complete(&self) -> bool {
+ *self != GameStatus::Continue
+ }
+}
+
+impl Player {
+ pub fn can_afford_all_buildings(&self, settings: &GameSettings) -> bool {
+ self.can_afford_attack_buildings(settings) &&
+ self.can_afford_defence_buildings(settings) &&
+ self.can_afford_energy_buildings(settings)
+ }
+
+ pub fn can_afford_attack_buildings(&self, settings: &GameSettings) -> bool {
+ self.energy >= settings.attack_price
+ }
+ pub fn can_afford_defence_buildings(&self, settings: &GameSettings) -> bool {
+ self.energy >= settings.defence_price
+ }
+ pub fn can_afford_energy_buildings(&self, settings: &GameSettings) -> bool {
+ 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 is_constructed(&self) -> bool {
+ self.construction_time_left == 0
+ }
+
+ fn is_shooty(&self) -> bool {
+ self.is_constructed() && self.weapon_damage > 0
+ }
+}
+
+#[test]
+fn how_big() {
+ use std::mem;
+ assert_eq!(4, mem::size_of::<Player>());
+ assert_eq!(12, mem::size_of::<Building>());
+ assert_eq!(6, mem::size_of::<Missile>());
+ assert_eq!(112, mem::size_of::<GameState>());
+ assert_eq!(24, mem::size_of::<Vec<Building>>());
+ assert_eq!(24, mem::size_of::<Vec<Missile>>());
+
}