diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2017-07-15 19:17:49 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2017-07-15 19:17:49 +0200 |
commit | 6379c1252e8ec51e607865638ff7d5ae79bd9c96 (patch) | |
tree | ae9dab9d7e9fa557c60835fc933e5b34e66a718a /src/ships.rs | |
parent | c14a74190d72ced3e6fdbf718fce25b4a0e8cc8f (diff) |
Adding shooting of new weapons
Diffstat (limited to 'src/ships.rs')
-rw-r--r-- | src/ships.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/ships.rs b/src/ships.rs index 104e986..422f24e 100644 --- a/src/ships.rs +++ b/src/ships.rs @@ -1,6 +1,8 @@ use std::fmt; use std::str; +use math::*; + #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)] pub enum Weapon { SingleShot, @@ -61,6 +63,75 @@ impl Weapon { //weird plus is to make the integer rounding up instead of down (required_energy + energy_per_round - 1) / energy_per_round } + + pub fn affected_cells(&self, target: Point, map_size: u16) -> Vec<Point> { + use Weapon::*; + + let p = target; + match self { + &SingleShot => { + vec!(Some(p)) + }, + &DoubleShotVertical => { + vec!( + p.move_point(Direction::North, 1, map_size), + p.move_point(Direction::South, 1, map_size) + ) + }, + &DoubleShotHorizontal => { + vec!( + p.move_point(Direction::East, 1, map_size), + p.move_point(Direction::West, 1, map_size) + ) + }, + &CornerShot => { + vec!( + p.move_point(Direction::NorthEast, 1, map_size), + p.move_point(Direction::SouthEast, 1, map_size), + p.move_point(Direction::NorthWest, 1, map_size), + p.move_point(Direction::SouthWest, 1, map_size), + ) + }, + &CrossShotDiagonal => { + vec!( + p.move_point(Direction::NorthEast, 1, map_size), + p.move_point(Direction::SouthEast, 1, map_size), + p.move_point(Direction::NorthWest, 1, map_size), + p.move_point(Direction::SouthWest, 1, map_size), + Some(p) + ) + }, + &CrossShotHorizontal => { + vec!( + p.move_point(Direction::North, 1, map_size), + p.move_point(Direction::East, 1, map_size), + p.move_point(Direction::South, 1, map_size), + p.move_point(Direction::West, 1, map_size), + Some(p) + ) + }, + &SeekerMissle => { + vec!( + Some(p), + + p.move_point(Direction::North, 1, map_size), + p.move_point(Direction::East, 1, map_size), + p.move_point(Direction::South, 1, map_size), + p.move_point(Direction::West, 1, map_size), + + p.move_point(Direction::NorthEast, 1, map_size), + p.move_point(Direction::SouthEast, 1, map_size), + p.move_point(Direction::NorthWest, 1, map_size), + p.move_point(Direction::SouthWest, 1, map_size), + + p.move_point(Direction::North, 2, map_size), + p.move_point(Direction::East, 2, map_size), + p.move_point(Direction::South, 2, map_size), + p.move_point(Direction::West, 2, map_size) + ) + } + }.iter().filter_map(|&p| p).collect::<Vec<_>>() + } } |