-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.rs
161 lines (139 loc) · 4.41 KB
/
context.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::ops::Add;
use rand::{rngs::ThreadRng, Rng};
pub enum GameState {
Playing,
Paused,
Over,
}
#[derive(Copy, Clone, PartialEq)]
pub enum PlayerDirection {
Up,
Down,
Right,
Left,
}
#[derive(Copy, Clone, PartialEq)]
pub struct Point(pub i32, pub i32);
pub struct Context {
pub state: GameState,
pub player_position: Vec<Point>,
pub player_direction: PlayerDirection,
pub food: Option<Point>,
pub board_size: Point,
last_tick_direction: PlayerDirection,
rng: ThreadRng,
}
impl Add<Point> for Point {
type Output = Point;
fn add(self, rhs: Point) -> Self::Output {
Point(self.0 + rhs.0, self.1 + rhs.1)
}
}
impl Context {
pub fn new() -> Context {
Context {
state: GameState::Paused,
player_position: vec![Point(20, 15), Point(19, 15), Point(18, 15)],
player_direction: PlayerDirection::Right,
last_tick_direction: PlayerDirection::Right,
food: None,
board_size: Point(40, 30),
rng: rand::thread_rng(),
}
}
pub fn next_tick(&mut self) {
if self.food.is_none() {
self.spawn_food();
}
if let GameState::Over | GameState::Paused = self.state {
return;
}
let head_position = self.player_position.first().unwrap();
let next_head_position = match self.player_direction {
PlayerDirection::Up => *head_position + Point(0, -1),
PlayerDirection::Down => *head_position + Point(0, 1),
PlayerDirection::Right => *head_position + Point(1, 0),
PlayerDirection::Left => *head_position + Point(-1, 0),
};
if self.is_game_over(next_head_position) {
self.state = GameState::Over;
return;
}
if matches!(self.food, Some(food) if food == next_head_position) {
self.move_player(next_head_position, true);
self.food = None;
return;
}
self.move_player(next_head_position, false);
}
fn spawn_food(&mut self) {
let Point(size_x, size_y) = self.board_size;
loop {
let food = Point(
self.rng.gen_range(1..(size_x - 1)),
self.rng.gen_range(1..(size_y - 1)),
);
if !self.player_position.iter().any(|dot| *dot == food) {
self.food = Some(food);
return;
}
}
}
fn move_player(&mut self, next_head_position: Point, grow: bool) {
if !grow {
self.player_position.pop();
}
self.player_position.reverse();
self.player_position.push(next_head_position);
self.player_position.reverse();
self.last_tick_direction = self.player_direction;
}
pub fn move_up(&mut self) {
if self.last_tick_direction == PlayerDirection::Down {
return;
}
self.player_direction = PlayerDirection::Up;
}
pub fn move_down(&mut self) {
if self.last_tick_direction == PlayerDirection::Up {
return;
}
self.player_direction = PlayerDirection::Down;
}
pub fn move_right(&mut self) {
if self.last_tick_direction == PlayerDirection::Left {
return;
}
self.player_direction = PlayerDirection::Right;
}
pub fn move_left(&mut self) {
if self.last_tick_direction == PlayerDirection::Right {
return;
}
self.player_direction = PlayerDirection::Left;
}
fn is_game_over(&mut self, next_head_position: Point) -> bool {
let Point(x, y) = next_head_position;
let Point(x_size, y_size) = self.board_size;
x == 0
|| y == 0
|| x == x_size - 1
|| y == y_size - 1
|| self
.player_position
.iter()
.any(|point| *point == next_head_position)
}
pub fn toggle_pause(&mut self) {
self.state = match self.state {
GameState::Playing => GameState::Paused,
GameState::Paused => GameState::Playing,
GameState::Over => {
self.player_position = vec![Point(20, 15), Point(19, 15), Point(18, 15)];
self.player_direction = PlayerDirection::Right;
self.last_tick_direction = PlayerDirection::Right;
GameState::Playing
}
}
}
}