summaryrefslogtreecommitdiff
path: root/src/game.rs
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2019-06-24 21:04:27 +0200
committerJustin Worthe <justin@worthe-it.co.za>2019-06-24 21:04:27 +0200
commit1aeab6da05a0c7b7dad4d06a38b282a82d5e1a51 (patch)
treede76a41b53f4ba383bda118dfb97bd2ff53f7a23 /src/game.rs
parent6d6cb9e39c4ec3f113833042f1b7bc8082c5d650 (diff)
New command types for select and bombs
Diffstat (limited to 'src/game.rs')
-rw-r--r--src/game.rs44
1 files changed, 25 insertions, 19 deletions
diff --git a/src/game.rs b/src/game.rs
index 18731a2..a4a4a23 100644
--- a/src/game.rs
+++ b/src/game.rs
@@ -1,5 +1,5 @@
use crate::geometry::*;
-use crate::command::Command;
+use crate::command::{Command, Action};
use crate::json;
mod player;
@@ -136,9 +136,13 @@ impl GameBoard {
}
pub fn simulate(&mut self, moves: [Command; 2]) {
- self.simulate_moves(moves);
- self.simulate_digs(moves);
- self.simulate_shoots(moves);
+ let actions = [moves[0].action, moves[1].action];
+
+ // TODO: simulate_select
+ self.simulate_moves(actions);
+ self.simulate_digs(actions);
+ // TODO: simulate_bombs
+ self.simulate_shoots(actions);
for player in &mut self.players {
player.clear_dead_worms();
@@ -156,9 +160,9 @@ impl GameBoard {
};
}
- fn simulate_moves(&mut self, moves: [Command; 2]) {
- match moves {
- [Command::Move(p1), Command::Move(p2)] if p1.x == p2.x && p1.y == p2.y => {
+ fn simulate_moves(&mut self, actions: [Action; 2]) {
+ match actions {
+ [Action::Move(p1), Action::Move(p2)] if p1.x == p2.x && p1.y == p2.y => {
// TODO: Get this from some sort of config rather
let damage = 20;
@@ -174,8 +178,8 @@ impl GameBoard {
}
},
_ => {
- for player_index in 0..moves.len() {
- if let Command::Move(p) = moves[player_index] {
+ for player_index in 0..actions.len() {
+ if let Action::Move(p) = actions[player_index] {
debug_assert_eq!(Some(false), self.map.at(p), "Movement target wasn't empty, ({}, {})", p.x, p.y);
self.players[player_index].moves_score += 5;
@@ -207,12 +211,12 @@ impl GameBoard {
}
}
- fn simulate_digs(&mut self, moves: [Command; 2]) {
- for player_index in 0..moves.len() {
- if let Command::Dig(p) = moves[player_index] {
+ fn simulate_digs(&mut self, actions: [Action; 2]) {
+ for player_index in 0..actions.len() {
+ if let Action::Dig(p) = actions[player_index] {
debug_assert!(
Some(true) == self.map.at(p) ||
- (player_index == 1 && moves[0] == Command::Dig(p)),
+ (player_index == 1 && actions[0] == Action::Dig(p)),
"Tried to dig through air, ({}, {})", p.x, p.y
);
debug_assert!{
@@ -228,9 +232,9 @@ impl GameBoard {
}
}
- fn simulate_shoots(&mut self, moves: [Command; 2]) {
- 'players_loop: for player_index in 0..moves.len() {
- if let Command::Shoot(dir) = moves[player_index] {
+ fn simulate_shoots(&mut self, actions: [Action; 2]) {
+ 'players_loop: for player_index in 0..actions.len() {
+ if let Action::Shoot(dir) = actions[player_index] {
if let Some(worm) = self.players[player_index].active_worm() {
let (center, weapon_range, weapon_damage) = {
(worm.position, worm.weapon_range, worm.weapon_damage)
@@ -294,6 +298,7 @@ impl GameBoard {
}
pub fn valid_move_commands(&self, player_index: usize) -> ArrayVec<[Command;8]> {
+ // TODO: This might also involve selects
if let Some(worm) = self.players[player_index].active_worm() {
Direction::all()
.iter()
@@ -301,8 +306,8 @@ impl GameBoard {
.map(|d| worm.position + d)
.filter(|p| !self.occupied_cells.contains(p))
.filter_map(|p| match self.map.at(p) {
- Some(false) => Some(Command::Move(p)),
- Some(true) => Some(Command::Dig(p)),
+ Some(false) => Some(Command::new(Action::Move(p))),
+ Some(true) => Some(Command::new(Action::Dig(p))),
_ => None,
})
.collect()
@@ -312,6 +317,7 @@ impl GameBoard {
}
pub fn valid_shoot_commands(&self, player_index: usize, center: Point2d<i8>, weapon_range: u8) -> ArrayVec<[Command;8]> {
+ // TODO: We might also throw bombs
let range = weapon_range as i8;
let dir_range = ((weapon_range as f32 + 1.) / 2f32.sqrt()).floor() as i8;
@@ -349,7 +355,7 @@ impl GameBoard {
self.map.at(center + diff * distance) != Some(false) &&
!self.players[player_index].worms.iter().any(|w| w.position == center + diff * distance))
})
- .map(|(dir, _range)| Command::Shoot(dir))
+ .map(|(dir, _range)| Command::new(Action::Shoot(dir)))
.collect()
}
}