Browse Source

game::piece is no longer an Option

Inderjit Gill 1 year ago
parent
commit
7083c8188c
1 changed files with 93 additions and 110 deletions
  1. 93
    110
      src/game.rs

+ 93
- 110
src/game.rs View File

@@ -117,10 +117,9 @@ pub struct Game {
117 117
     board_offset: Block2D,
118 118
 
119 119
     shapes: HashMap<ShapeKind, Shape>,
120
-    piece: Option<Piece>,               // the current piece under player control
120
+    piece: Piece,               // the current piece under player control
121 121
     next_shape: ShapeKind,
122 122
 
123
-
124 123
     is_game_over: bool,
125 124
 
126 125
     score: i32,
@@ -151,14 +150,14 @@ impl Game {
151 150
             board_offset: Block2D { x: 5, y: 5 },
152 151
 
153 152
             shapes: define_shapes(),
154
-            piece: Some(Piece {
153
+            piece: Piece {
155 154
                 shape_kind: ShapeKind::J,
156 155
                 pos: BoardPos {
157 156
                     x: 5,
158 157
                     y: 19,
159 158
                 },
160 159
                 angle: PieceAngle::R0,
161
-            }),
160
+            },
162 161
             next_shape: ShapeKind::L,
163 162
 
164 163
             is_game_over: false,
@@ -223,43 +222,35 @@ impl Game {
223 222
 
224 223
         self.terrible_rng = elapsed as i32;
225 224
 
226
-        if let Some(ref piece) = self.piece {
227
-            if let Some(shape) = self.shapes.get(&piece.shape_kind) {
228
-                remove_shape_from_board(&mut self.board, shape, &piece.pos, &piece.angle);
229
-            }
225
+        if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
226
+            remove_shape_from_board(&mut self.board, shape, &self.piece.pos, &self.piece.angle);
230 227
         }
231 228
 
232 229
         apply_user_input(self, controller, delta);
233 230
 
234 231
         if self.piece_is_dropping {
235
-            if let Some(ref mut piece) = self.piece {
236
-                if let Some(shape) = self.shapes.get(&piece.shape_kind) {
237
-                    if !lower_piece(&self.board, shape, piece) {
238
-                        // NOTE: don't lock in the shape when it stops dropping.
239
-                        //
240
-                        // the game 'feels' better if the user has time to modify the
241
-                        // piece's position at the bottom of the drop
242
-                        self.piece_is_dropping = false;
243
-                    }
232
+            if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
233
+                if !lower_piece(&self.board, shape, &mut self.piece) {
234
+                    // NOTE: don't lock in the shape when it stops dropping.
235
+                    //
236
+                    // the game 'feels' better if the user has time to modify the
237
+                    // piece's position at the bottom of the drop
238
+                    self.piece_is_dropping = false;
244 239
                 }
245 240
             }
246 241
         } else if is_time_to_lower_piece(self, delta) {
247 242
 
248 243
             let mut piece_cant_go_lower = false;
249 244
 
250
-            if let Some(ref mut piece) = self.piece {
251
-                if let Some(shape) = self.shapes.get(&piece.shape_kind) {
252
-                    piece_cant_go_lower = !lower_piece(&self.board, shape, piece);
253
-                }
245
+            if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
246
+                piece_cant_go_lower = !lower_piece(&self.board, shape, &mut self.piece);
254 247
             }
255 248
 
256 249
             if piece_cant_go_lower {
257 250
                 let mut num_lines = 0;
258
-                if let Some(ref piece) = self.piece {
259
-                    if let Some(shape) = self.shapes.get(&piece.shape_kind) {
260
-                        add_shape_to_board(&mut self.board, shape, &piece.pos, &piece.angle);
261
-                        num_lines = remove_complete_lines(&mut self.board);
262
-                    }
251
+                if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
252
+                    add_shape_to_board(&mut self.board, shape, &self.piece.pos, &self.piece.angle);
253
+                    num_lines = remove_complete_lines(&mut self.board);
263 254
                 }
264 255
 
265 256
                 if num_lines > 0 {
@@ -273,10 +264,8 @@ impl Game {
273 264
             }
274 265
         }
275 266
 
276
-        if let Some(ref piece) = self.piece {
277
-            if let Some(shape) = self.shapes.get(&piece.shape_kind) {
278
-                add_shape_to_board(&mut self.board, shape, &piece.pos, &piece.angle);
279
-            }
267
+        if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
268
+            add_shape_to_board(&mut self.board, shape, &self.piece.pos, &self.piece.angle);
280 269
         }
281 270
 
282 271
         true
@@ -306,22 +295,18 @@ fn check_for_game_over(game: &mut Game) -> bool {
306 295
     // new pieces are spawned at the highest valid point,
307 296
     // if any of those blocks are already active then it's game over
308 297
     //
309
-    if let Some(ref piece) = game.piece {
310
-        if let Some(shape) = game.shapes.get(&piece.shape_kind) {
311
-            return is_colliding(&game.board, shape, &piece.pos, &piece.angle);
312
-        }
298
+    if let Some(shape) = game.shapes.get(&game.piece.shape_kind) {
299
+        return is_colliding(&game.board, shape, &game.piece.pos, &game.piece.angle);
313 300
     }
314 301
     false
315 302
 }
316 303
 
317 304
 fn use_next_shape(game: &mut Game) {
318
-    if let Some(ref mut piece) = game.piece {
319
-        piece.shape_kind = game.next_shape.clone();
320
-        if let Some(shape) = game.shapes.get(&piece.shape_kind) {
321
-            piece.pos.x = ((game.board.width as i32) - shape.aabb_width) / 2;
322
-            piece.pos.y = (game.board.height as i32) - shape.real_height;
323
-            piece.angle = PieceAngle::R0;
324
-        }
305
+    game.piece.shape_kind = game.next_shape.clone();
306
+    if let Some(shape) = game.shapes.get(&game.piece.shape_kind) {
307
+        game.piece.pos.x = ((game.board.width as i32) - shape.aabb_width) / 2;
308
+        game.piece.pos.y = (game.board.height as i32) - shape.real_height;
309
+        game.piece.angle = PieceAngle::R0;
325 310
     }
326 311
     game.next_shape = get_random_shape(game);
327 312
 }
@@ -363,88 +348,86 @@ fn lower_piece(board: &Board, shape: &Shape, piece: &mut Piece) -> bool {
363 348
 }
364 349
 
365 350
 fn apply_user_input(game: &mut Game, controller: &Controller, delta: f32) {
366
-    if let Some(ref mut piece) = game.piece {
367
-        if let Some(shape) = game.shapes.get(&piece.shape_kind) {
351
+    if let Some(shape) = game.shapes.get(&game.piece.shape_kind) {
368 352
 
369
-            if can_apply_input(&mut game.left_cooldown, controller.left, game.default_cooldown, delta) {
370
-                let new_position = BoardPos { x: piece.pos.x - 1, y: piece.pos.y };
371
-                if is_allowed(&game.board, shape, &new_position, &piece.angle) {
372
-                    piece.pos = new_position;
373
-                }
353
+        if can_apply_input(&mut game.left_cooldown, controller.left, game.default_cooldown, delta) {
354
+            let new_position = BoardPos { x: game.piece.pos.x - 1, y: game.piece.pos.y };
355
+            if is_allowed(&game.board, shape, &new_position, &game.piece.angle) {
356
+                game.piece.pos = new_position;
374 357
             }
358
+        }
375 359
 
376
-            if can_apply_input(&mut game.right_cooldown, controller.right, game.default_cooldown, delta) {
377
-                let new_position = BoardPos { x: piece.pos.x + 1, y: piece.pos.y };
378
-                if is_allowed(&game.board, shape, &new_position, &piece.angle) {
379
-                    piece.pos = new_position;
380
-                }
360
+        if can_apply_input(&mut game.right_cooldown, controller.right, game.default_cooldown, delta) {
361
+            let new_position = BoardPos { x: game.piece.pos.x + 1, y: game.piece.pos.y };
362
+            if is_allowed(&game.board, shape, &new_position, &game.piece.angle) {
363
+                game.piece.pos = new_position;
381 364
             }
365
+        }
382 366
 
383
-            if can_apply_input(&mut game.up_cooldown, controller.up, game.default_cooldown, delta) {
384
-                let new_angle;
385
-                if shape.fully_rotate {
386
-                    new_angle = match piece.angle {
387
-                        PieceAngle::R0 => PieceAngle::R270,
388
-                        PieceAngle::R270 => PieceAngle::R180,
389
-                        PieceAngle::R180 => PieceAngle::R90,
390
-                        PieceAngle::R90 => PieceAngle::R0,
391
-                    };
392
-                } else {
393
-                    new_angle = match piece.angle {
394
-                        PieceAngle::R0 => PieceAngle::R90,
395
-                        _ => PieceAngle::R0,
396
-                    };
397
-                }
398
-
399
-                if is_allowed(&game.board, shape, &piece.pos, &new_angle) {
400
-                    piece.angle = new_angle;
401
-                }
367
+        if can_apply_input(&mut game.up_cooldown, controller.up, game.default_cooldown, delta) {
368
+            let new_angle;
369
+            if shape.fully_rotate {
370
+                new_angle = match game.piece.angle {
371
+                    PieceAngle::R0 => PieceAngle::R270,
372
+                    PieceAngle::R270 => PieceAngle::R180,
373
+                    PieceAngle::R180 => PieceAngle::R90,
374
+                    PieceAngle::R90 => PieceAngle::R0,
375
+                };
376
+            } else {
377
+                new_angle = match game.piece.angle {
378
+                    PieceAngle::R0 => PieceAngle::R90,
379
+                    _ => PieceAngle::R0,
380
+                };
402 381
             }
403 382
 
404
-            if can_apply_input(&mut game.down_cooldown, controller.down, game.default_cooldown, delta) {
405
-                game.piece_is_dropping = true;
383
+            if is_allowed(&game.board, shape, &game.piece.pos, &new_angle) {
384
+                game.piece.angle = new_angle;
406 385
             }
386
+        }
407 387
 
408
-            if can_apply_input(&mut game.a_cooldown, controller.a, game.default_cooldown, delta) {
409
-                let new_angle;
410
-                if shape.fully_rotate {
411
-                    new_angle = match piece.angle {
412
-                        PieceAngle::R0 => PieceAngle::R90,
413
-                        PieceAngle::R90 => PieceAngle::R180,
414
-                        PieceAngle::R180 => PieceAngle::R270,
415
-                        PieceAngle::R270 => PieceAngle::R0,
416
-                    };
417
-                } else {
418
-                    new_angle = match piece.angle {
419
-                        PieceAngle::R0 => PieceAngle::R90,
420
-                        _ => PieceAngle::R0,
421
-                    };
422
-                }
388
+        if can_apply_input(&mut game.down_cooldown, controller.down, game.default_cooldown, delta) {
389
+            game.piece_is_dropping = true;
390
+        }
423 391
 
424
-                if is_allowed(&game.board, shape, &piece.pos, &new_angle) {
425
-                    piece.angle = new_angle;
426
-                }
392
+        if can_apply_input(&mut game.a_cooldown, controller.a, game.default_cooldown, delta) {
393
+            let new_angle;
394
+            if shape.fully_rotate {
395
+                new_angle = match game.piece.angle {
396
+                    PieceAngle::R0 => PieceAngle::R90,
397
+                    PieceAngle::R90 => PieceAngle::R180,
398
+                    PieceAngle::R180 => PieceAngle::R270,
399
+                    PieceAngle::R270 => PieceAngle::R0,
400
+                };
401
+            } else {
402
+                new_angle = match game.piece.angle {
403
+                    PieceAngle::R0 => PieceAngle::R90,
404
+                    _ => PieceAngle::R0,
405
+                };
427 406
             }
428 407
 
429
-            if can_apply_input(&mut game.b_cooldown, controller.b, game.default_cooldown, delta) {
430
-                let new_angle;
431
-                if shape.fully_rotate {
432
-                    new_angle = match piece.angle {
433
-                        PieceAngle::R0 => PieceAngle::R270,
434
-                        PieceAngle::R270 => PieceAngle::R180,
435
-                        PieceAngle::R180 => PieceAngle::R90,
436
-                        PieceAngle::R90 => PieceAngle::R0,
437
-                    };
438
-                } else {
439
-                    new_angle = match piece.angle {
440
-                        PieceAngle::R0 => PieceAngle::R90,
441
-                        _ => PieceAngle::R0,
442
-                    };
443
-                }
408
+            if is_allowed(&game.board, shape, &game.piece.pos, &new_angle) {
409
+                game.piece.angle = new_angle;
410
+            }
411
+        }
444 412
 
445
-                if is_allowed(&game.board, shape, &piece.pos, &new_angle) {
446
-                    piece.angle = new_angle;
447
-                }
413
+        if can_apply_input(&mut game.b_cooldown, controller.b, game.default_cooldown, delta) {
414
+            let new_angle;
415
+            if shape.fully_rotate {
416
+                new_angle = match game.piece.angle {
417
+                    PieceAngle::R0 => PieceAngle::R270,
418
+                    PieceAngle::R270 => PieceAngle::R180,
419
+                    PieceAngle::R180 => PieceAngle::R90,
420
+                    PieceAngle::R90 => PieceAngle::R0,
421
+                };
422
+            } else {
423
+                new_angle = match game.piece.angle {
424
+                    PieceAngle::R0 => PieceAngle::R90,
425
+                    _ => PieceAngle::R0,
426
+                };
427
+            }
428
+
429
+            if is_allowed(&game.board, shape, &game.piece.pos, &new_angle) {
430
+                game.piece.angle = new_angle;
448 431
             }
449 432
         }
450 433
     }

Loading…
Cancel
Save