diff --git a/game_core/src/snake/snake.rs b/game_core/src/snake/snake.rs index ff5ef60..e6ede75 100644 --- a/game_core/src/snake/snake.rs +++ b/game_core/src/snake/snake.rs @@ -95,6 +95,20 @@ impl Snake { field.get_tile(pos.x as usize, pos.y as usize) == Tile::Food } + pub fn keep_within_boundaries(&mut self, width: isize, height: isize) { + if self.body.position.x >= width { + self.body.position.x = 0; + } else if self.body.position.x < 0 { + self.body.position.x = width - 1; + } + + if self.body.position.y >= height { + self.body.position.y = 0; + } else if self.body.position.y < 0 { + self.body.position.y = height - 1; + } + } + pub fn do_move(&mut self) { println!("moving?"); // probably a more optimal way of approaching this is just diff --git a/game_core/src/snake/states.rs b/game_core/src/snake/states.rs index ab81bb0..2b6f9aa 100644 --- a/game_core/src/snake/states.rs +++ b/game_core/src/snake/states.rs @@ -55,6 +55,8 @@ impl SnakeState for MovingState { self.dt_accumulator -= interval; } + snake.keep_within_boundaries(field.width as isize, field.height as isize); + None } }