-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rs
411 lines (367 loc) · 13.1 KB
/
game.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! This module defines utilities central to the game state.
use crate::{food::Food, snake::Snake};
use andiskaz::{
color::{BasicColor, Color2},
coord::{Coord, Vec2},
error::Error,
event::{Event, Key, KeyEvent, ResizeEvent},
screen::Screen,
string::{TermGrapheme, TermString},
style::Style,
terminal::Terminal,
tile::Tile,
tstring,
};
use gardiz::{direc::Direction, rect::Rect};
use std::time::Duration;
use tokio::time;
/// In what manner the game ended.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum EndKind {
/// User won!
Win,
/// User lost...
Loss,
/// User cancelled via ESC.
Cancel,
}
/// Tag of the internal state of the game.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum State {
/// Game running normally.
Running,
/// Screen has been put to a bad size.
BadScreen,
/// The game ended.
Ended(EndKind),
}
/// The central game state.
#[derive(Debug)]
pub struct Game {
/// Bounds of the game's plane.
bounds: Rect<Coord>,
/// Food/fruit's data.
food: Food,
/// Snake's data.
snake: Snake,
/// Tile for the vertical component of the border.
vertical_tile: Tile,
/// Tile for the horizontal component of the border.
horizontal_tile: Tile,
/// Tile for the corner component of the border.
corner_tile: Tile,
/// Message displayed above the border.
message: TermString,
/// Game state, initially "running".
state: State,
}
impl Game {
/// Initializes the game state, given terminal info.
pub async fn new<'terminal>(
screen: &mut Screen<'terminal>,
) -> Result<Self, Error> {
// Tile for the snake's body.
let grapheme = TermGrapheme::new_lossy("O");
let colors = Color2 {
foreground: BasicColor::Black.into(),
background: BasicColor::White.into(),
};
let body_tile = Tile { grapheme, colors };
// Tile for the snake's head.
let grapheme = TermGrapheme::new_lossy("@");
let colors = Color2 {
foreground: BasicColor::Black.into(),
background: BasicColor::White.into(),
};
let head_tile = Tile { grapheme, colors };
// Tile for the food/fruit.
let grapheme = TermGrapheme::new_lossy("ɔ́");
let colors = Color2 {
foreground: BasicColor::Black.into(),
background: BasicColor::LightRed.into(),
};
let food_tile = Tile { grapheme, colors };
// Tile for the vertical component of the border.
let grapheme = TermGrapheme::new_lossy("|");
let colors = Color2 {
foreground: BasicColor::Black.into(),
background: BasicColor::LightGreen.into(),
};
let vertical_tile = Tile { grapheme, colors };
// Tile for the horizontal component of the border.
let grapheme = TermGrapheme::new_lossy("—");
let colors = Color2 {
foreground: BasicColor::Black.into(),
background: BasicColor::LightGreen.into(),
};
let horizontal_tile = Tile { grapheme, colors };
// Tile for the corner component of the border.
let grapheme = TermGrapheme::new_lossy("+");
let colors = Color2 {
foreground: BasicColor::Black.into(),
background: BasicColor::LightGreen.into(),
};
let corner_tile = Tile { grapheme, colors };
// Message shown above the border.
let message = tstring!["ESC to exit, arrows to move"];
// Bounds of the plane (where the snake is allowed to go to).
let bounds = Self::make_bounds(screen.size());
// Initialization of snake and food.
let snake = Snake::new(body_tile, head_tile, bounds);
let food = Food::new(food_tile, &snake, bounds);
Ok(Self {
bounds,
snake,
food,
corner_tile,
vertical_tile,
horizontal_tile,
message,
state: State::Running,
})
}
/// Runs the game, given the initial state, terminal handle, and interval
/// between ticks.
pub async fn run(
mut self,
terminal: &mut Terminal,
tick: Duration,
) -> Result<EndKind, Error> {
// Interval between ticks.
let mut interval = time::interval(tick);
loop {
match self.state {
State::Running => {
// Immediately acquires a terminal locked session.
let mut session = terminal.lock_now().await?;
// Let's interact with the event, change state, and see if
// it ended.
self.tick(session.event());
// But before, we will render what we have.
self.render(&mut session.screen());
},
State::BadScreen => {
let mut session = terminal.lock_now().await?;
// If there is an event, it potentially resized the screen
// to a valid size.
if let Some(event) = session.event() {
self.restricted_event(event);
}
},
// Game ended so we can stop the loop.
State::Ended(end) => break Ok(end),
}
interval.tick().await;
}
}
/// Computes the plane bounds from the screen size.
fn make_bounds(screen_size: Vec2) -> Rect<Coord> {
Rect::from_range_incl(
Vec2 { x: 1, y: 2 },
Vec2 { x: screen_size.x - 2, y: screen_size.y - 2 },
)
}
/// Performs the game's logical operations inside a game tick.
fn tick(&mut self, event: Option<Event>) {
// Handles the event.
if let Some(curr_event) = event {
self.full_event(curr_event);
}
// Moves the snake.
self.move_snake();
}
/// Handles an event, reacting to the full list of used events, be it
/// resizing or key pressing.
fn full_event(&mut self, event: Event) {
match event {
// ESC was pressed. Cancels the game.
Event::Key(KeyEvent { main_key: Key::Esc, .. }) => {
self.state = State::Ended(EndKind::Cancel);
},
// Arrow up was pressed, with no modifiers. Changes direction to up.
Event::Key(KeyEvent {
main_key: Key::Up,
shift: false,
ctrl: false,
alt: false,
}) => {
self.snake.change_direction(Direction::Up);
},
// Arrow down was pressed, with no modifiers. Changes direction to
// down.
Event::Key(KeyEvent {
main_key: Key::Down,
shift: false,
ctrl: false,
alt: false,
}) => {
self.snake.change_direction(Direction::Down);
},
// Arrow left was pressed, with no modifiers. Changes direction to
// left.
Event::Key(KeyEvent {
main_key: Key::Left,
shift: false,
ctrl: false,
alt: false,
}) => {
self.snake.change_direction(Direction::Left);
},
// Arrow right was pressed, with no modifiers. Changes direction to
// right.
Event::Key(KeyEvent {
main_key: Key::Right,
shift: false,
ctrl: false,
alt: false,
}) => {
self.snake.change_direction(Direction::Right);
},
// Screen was resized. Propagates the new size.
Event::Resize(resize) => {
self.resize(resize);
},
// Other keys, ignores.
Event::Key(_) => (),
}
}
/// Handles an event, but only to a restricted set of used events: resizing
/// or ESC.
fn restricted_event(&mut self, event: Event) {
match event {
// ESC was pressed. Cancels the game.
Event::Key(KeyEvent { main_key: Key::Esc, .. }) => {
self.state = State::Ended(EndKind::Cancel);
},
// Screen was resized. Propagates the new size.
Event::Resize(event) => {
if event.size.is_some() {
// If valid screen size, then we can keep the game running.
self.state = State::Running;
}
},
// Other keys, ignores.
Event::Key(_) => (),
}
}
/// Moves the snake in the current direction.
fn move_snake(&mut self) {
match self.snake.mov(self.bounds, &self.food) {
// Handles the case where the food was eaten.
Some(eaten) => {
if self.snake.length() >= self.win_size() {
self.state = State::Ended(EndKind::Win);
} else if eaten {
// Handles the case where the food was eaten: it needs to be
// regenerated.
self.food.regenerate(&self.snake, self.bounds);
}
if self.snake.head_intersects() {
// If the head intersects the body, game over.
self.state = State::Ended(EndKind::Loss)
}
},
// Out of bounds. End of the game.
None => self.state = State::Ended(EndKind::Loss),
}
}
/// Renders all game data into the screen.
fn render(&self, screen: &mut Screen) {
// Clears screen with black as background color.
screen.clear(BasicColor::Black.into());
// Renders the snake.
self.snake.render(screen);
// Renders the food.
self.food.render(screen);
// Renders the borders.
self.render_borders(screen);
// Renders the message above the borders.
self.render_message(screen);
}
/// Renders the borders via the given locked screen.
fn render_borders(&self, screen: &mut Screen) {
// Top border.
for x in self.bounds.start.x .. self.bounds.end().x {
screen.set(
Vec2 { x, y: self.bounds.start.y - 1 },
self.horizontal_tile.clone(),
);
}
// Down border.
for x in self.bounds.start.x .. self.bounds.end().x {
screen.set(
Vec2 { x, y: self.bounds.end().y },
self.horizontal_tile.clone(),
);
}
// Left border.
for y in self.bounds.start.y .. self.bounds.end().y + 1 {
screen.set(
Vec2 { y, x: self.bounds.start.x - 1 },
self.vertical_tile.clone(),
);
}
// Right border.
for y in self.bounds.start.y .. self.bounds.end().y + 1 {
screen.set(
Vec2 { y, x: self.bounds.end().x },
self.vertical_tile.clone(),
);
}
// Corners.
screen.set(
Vec2 { x: self.bounds.start.x - 1, y: self.bounds.start.y - 1 },
self.corner_tile.clone(),
);
screen.set(
Vec2 { x: self.bounds.start.x - 1, y: self.bounds.end().y },
self.corner_tile.clone(),
);
screen.set(
Vec2 { x: self.bounds.end().x, y: self.bounds.start.y - 1 },
self.corner_tile.clone(),
);
screen.set(
Vec2 { x: self.bounds.end().x, y: self.bounds.end().y },
self.corner_tile.clone(),
);
}
/// Renders the message above the borders.
fn render_message(&self, screen: &mut Screen) {
// White foreground, black background.
let colors = Color2 {
foreground: BasicColor::White.into(),
background: BasicColor::Black.into(),
};
// Centralized, at the top.
let style = Style::with_colors(colors).align(1, 2);
// Writes the text into the buffer.
screen.styled_text(&self.message, style);
}
/// Resizes the game state about screen size.
fn resize(&mut self, event: ResizeEvent) {
if let Some(size) = event.size {
// New bounds.
self.bounds = Self::make_bounds(size);
if self.snake.saturate_at_bounds(self.bounds) {
// We will consider that the game is lost if snake gets outside
// of the plane when resizing.
self.state = State::Ended(EndKind::Loss);
}
if !self.food.in_bounds(self.bounds) {
// If the food is outside of the plane, regenerates.
self.food.regenerate(&self.snake, self.bounds);
}
} else {
// Screen size is not valid? Save this as a bad screen state.
self.state = State::BadScreen;
}
}
/// Computes the size of the snake when the player wins.
fn win_size(&self) -> usize {
let x = usize::from(self.bounds.size.x);
let y = usize::from(self.bounds.size.y);
// Mean(x,y)/2
(x + y) / 4
}
}