| 1 |
use rand::Rng; |
| 2 |
use std::ops::Add; |
| 3 |
|
| 4 |
use crate::renderer::Renderer; |
| 5 |
|
| 6 |
#[derive(Copy, Clone, PartialEq)] |
| 7 |
pub enum GameState { |
| 8 |
Playing, |
| 9 |
Paused, |
| 10 |
GameOver, |
| 11 |
} |
| 12 |
|
| 13 |
#[derive(PartialEq, Clone, Copy)] |
| 14 |
pub enum GameEvent { |
| 15 |
Resume, |
| 16 |
Restart, |
| 17 |
Quit, |
| 18 |
FoodEaten, |
| 19 |
Died, |
| 20 |
} |
| 21 |
|
| 22 |
#[derive(Copy, Clone, PartialEq)] |
| 23 |
pub enum PlayerDirection { |
| 24 |
Up, |
| 25 |
Down, |
| 26 |
Right, |
| 27 |
Left, |
| 28 |
} |
| 29 |
|
| 30 |
#[derive(Copy, Clone, PartialEq)] |
| 31 |
pub struct Point(pub i32, pub i32); |
| 32 |
|
| 33 |
impl Add<Point> for Point { |
| 34 |
type Output = Point; |
| 35 |
|
| 36 |
fn add(self, rhs: Point) -> Self::Output { |
| 37 |
Point(self.0 + rhs.0, self.1 + rhs.1) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
pub struct GameContext { |
| 42 |
pub player_position: Vec<Point>, |
| 43 |
pub player_direction: PlayerDirection, |
| 44 |
pub food: Vec<Point>, |
| 45 |
pub state: GameState, |
| 46 |
} |
| 47 |
|
| 48 |
impl GameContext { |
| 49 |
pub fn new() -> Result<Self, String> { |
| 50 |
Ok(Self { |
| 51 |
player_position: vec![Point(3, 1), Point(2, 1), Point(1, 1)], |
| 52 |
player_direction: PlayerDirection::Right, |
| 53 |
food: vec![Point(3, 3)], |
| 54 |
state: GameState::Playing, |
| 55 |
}) |
| 56 |
} |
| 57 |
|
| 58 |
pub fn next_tick(&mut self) -> Option<GameEvent> { |
| 59 |
if self.state != GameState::Playing { |
| 60 |
return None; |
| 61 |
} |
| 62 |
|
| 63 |
let back_position = *self.player_position.last().unwrap(); |
| 64 |
let next_head_position = self.next_head_position(self.player_direction); |
| 65 |
|
| 66 |
if self.player_position.contains(&next_head_position) { |
| 67 |
return Some(GameEvent::Died); |
| 68 |
} |
| 69 |
self.player_position.insert(0, next_head_position); |
| 70 |
|
| 71 |
if self.food.contains(&next_head_position) { |
| 72 |
self.player_position.push(back_position); |
| 73 |
|
| 74 |
self.food.retain(|p| *p != next_head_position); |
| 75 |
|
| 76 |
self.generate_food(); |
| 77 |
return Some(GameEvent::FoodEaten); |
| 78 |
} else { |
| 79 |
self.player_position.pop(); |
| 80 |
} |
| 81 |
None |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
fn generate_food(&mut self) { |
| 86 |
let empty_points: Vec<Point> = (0..Renderer::GRID_Y_SIZE) |
| 87 |
.flat_map(|y| (0..Renderer::GRID_X_SIZE).map(move |x| Point(x as i32, y as i32))) |
| 88 |
.filter(|p| !self.player_position.contains(p) && !self.food.contains(p)) |
| 89 |
.collect(); |
| 90 |
let mut rng = rand::rng(); |
| 91 |
if empty_points.is_empty() { |
| 92 |
return; |
| 93 |
}; |
| 94 |
self.food |
| 95 |
.push(empty_points[rng.random_range(0..empty_points.len() - 1)]) |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
pub fn move_up(&mut self) { |
| 100 |
if self.player_direction != PlayerDirection::Down { |
| 101 |
self.player_direction = PlayerDirection::Up; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
pub fn move_down(&mut self) { |
| 106 |
if self.player_direction != PlayerDirection::Up { |
| 107 |
self.player_direction = PlayerDirection::Down; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
pub fn move_right(&mut self) { |
| 112 |
if self.player_direction != PlayerDirection::Left { |
| 113 |
self.player_direction = PlayerDirection::Right; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
pub fn move_left(&mut self) { |
| 118 |
if self.player_direction != PlayerDirection::Right { |
| 119 |
self.player_direction = PlayerDirection::Left; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
pub fn next_head_position(&mut self, direction: PlayerDirection) -> Point { |
| 124 |
let head_position = self.player_position.first().unwrap(); |
| 125 |
let next_head_position = match direction { |
| 126 |
PlayerDirection::Up => *head_position + Point(0, -1), |
| 127 |
PlayerDirection::Down => *head_position + Point(0, 1), |
| 128 |
PlayerDirection::Right => *head_position + Point(1, 0), |
| 129 |
PlayerDirection::Left => *head_position + Point(-1, 0), |
| 130 |
}; |
| 131 |
match next_head_position { |
| 132 |
mut head if head.0.is_negative() => { |
| 133 |
head.0 = Renderer::GRID_X_SIZE as i32 - 1; |
| 134 |
head |
| 135 |
} |
| 136 |
|
| 137 |
mut head if head.0 > Renderer::GRID_X_SIZE as i32 => { |
| 138 |
head.0 = 0; |
| 139 |
head |
| 140 |
} |
| 141 |
|
| 142 |
mut head if head.1.is_negative() => { |
| 143 |
head.1 = Renderer::GRID_Y_SIZE as i32 - 1; |
| 144 |
head |
| 145 |
} |
| 146 |
|
| 147 |
mut head if head.1 > Renderer::GRID_Y_SIZE as i32 => { |
| 148 |
head.1 = 0; |
| 149 |
head |
| 150 |
} |
| 151 |
_ => next_head_position, |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
pub fn restart(&mut self) { |
| 156 |
*self = Self::new().unwrap(); |
| 157 |
} |
| 158 |
} |