diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2019-05-18 21:28:18 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2019-05-18 21:28:18 +0200 |
commit | b3d6b7cb77a660fc8a8e96645627da16c6b7c059 (patch) | |
tree | a394b7d28083f182aa8fd3c27ce9fa90be122cab /src/game | |
parent | dad50b87af3ecd23387bcf78dd16399a33074540 (diff) |
Started breaking up state for easier unit testing
Diffstat (limited to 'src/game')
-rw-r--r-- | src/game/map.rs | 64 | ||||
-rw-r--r-- | src/game/player.rs | 49 | ||||
-rw-r--r-- | src/game/powerup.rs | 7 |
3 files changed, 120 insertions, 0 deletions
diff --git a/src/game/map.rs b/src/game/map.rs new file mode 100644 index 0000000..c062c8f --- /dev/null +++ b/src/game/map.rs @@ -0,0 +1,64 @@ +use crate::geometry::*; +use crate::constants::*; + +#[derive(Default, Debug, PartialEq, Eq, Clone)] +pub struct Map { + cells: [u64; MAP_U64S] +} + +impl Map { + pub fn at(&self, p: Point2d<i8>) -> Option<bool> { + if p.y < 0 || p.y as usize >= MAP_SIZE { + None + } else { + let row_data = &MAP_ROW_SIZE[p.y as usize]; + if p.x < row_data.x_offset as i8 || p.x as usize >= row_data.x_offset + row_data.len() { + None + } else { + let global_bit = row_data.start_bit + p.x as usize - row_data.x_offset; + let integer = global_bit / 64; + let bit = global_bit % 64; + let mask = 1 << bit; + Some(self.cells[integer] & mask != 0) + } + } + } + + pub fn set(&mut self, p: Point2d<i8>) { + if p.y < 0 || p.y as usize >= MAP_SIZE { + debug_assert!(false, "Tried to set an out of bounds bit, {:?}", p); + } else { + let row_data = &MAP_ROW_SIZE[p.y as usize]; + if p.x < row_data.x_offset as i8 || p.x as usize >= row_data.x_offset + row_data.len() { + debug_assert!(false, "Tried to set an out of bounds bit, {:?}", p); + } else { + let global_bit = row_data.start_bit + p.x as usize - row_data.x_offset; + let integer = global_bit / 64; + let bit = global_bit % 64; + let mask = 1 << bit; + self.cells[integer] |= mask; + } + } + } + pub fn clear(&mut self, p: Point2d<i8>) { + if p.y < 0 || p.y as usize >= MAP_SIZE { + debug_assert!(false, "Tried to set an out of bounds bit, {:?}", p); + } else { + let row_data = &MAP_ROW_SIZE[p.y as usize]; + if p.x < row_data.x_offset as i8 || p.x as usize >= row_data.x_offset + row_data.len() { + debug_assert!(false, "Tried to set an out of bounds bit, {:?}", p); + } else { + let global_bit = row_data.start_bit + p.x as usize - row_data.x_offset; + let integer = global_bit / 64; + let bit = global_bit % 64; + let mask = !(1 << bit); + self.cells[integer] &= mask; + } + } + } +} + +#[cfg(test)] +mod test { + // TODO: Property test for at, set and clear +} diff --git a/src/game/player.rs b/src/game/player.rs new file mode 100644 index 0000000..acfc494 --- /dev/null +++ b/src/game/player.rs @@ -0,0 +1,49 @@ +use arrayvec::ArrayVec; +use crate::geometry::*; + +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct Player { + pub active_worm: usize, + pub worms: ArrayVec<[Worm; 3]> +} + +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct Worm { + pub id: i32, + pub health: i32, + pub position: Point2d<i8>, + pub weapon_damage: i32, + pub weapon_range: u8 +} + +impl Player { + pub fn find_worm(&self, id: i32) -> Option<&Worm> { + self.worms + .iter() + .find(|w| w.id == id) + } + + pub fn find_worm_mut(&mut self, id: i32) -> Option<&mut Worm> { + self.worms + .iter_mut() + .find(|w| w.id == id) + } + + pub fn active_worm(&self) -> &Worm { + &self.worms[self.active_worm] + } + + pub fn active_worm_mut(&mut self) -> &mut Worm { + &mut self.worms[self.active_worm] + } + + pub fn health(&self) -> i32 { + self.worms + .iter() + .map(|w| w.health) + .sum() + } + + // TODO: Cycle to next worm +} + diff --git a/src/game/powerup.rs b/src/game/powerup.rs new file mode 100644 index 0000000..2f07816 --- /dev/null +++ b/src/game/powerup.rs @@ -0,0 +1,7 @@ +use crate::geometry::*; + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub struct Powerup { + pub position: Point2d<i8>, + pub value: i32 +} |