Browse Source

in-game option: restart working

Inderjit Gill 6 months ago
parent
commit
0940e96bc8
3 changed files with 58 additions and 41 deletions
  1. 57
    10
      src/game.rs
  2. 1
    1
      src/lib.rs
  3. 0
    30
      web/js/index.js

+ 57
- 10
src/game.rs View File

@@ -83,6 +83,14 @@ impl Board {
83 83
         }
84 84
     }
85 85
 
86
+    pub fn reset(&mut self) {
87
+        for line in 0..self.height {
88
+            for x in 0..self.width {
89
+                self.board[line][x].reset(&self.inactive_colour);
90
+            }
91
+        }
92
+    }
93
+
86 94
     pub fn reconfigure(&mut self, config: &Config) {
87 95
         // not updating board width/height in order to retain the current state of the board
88 96
         self.inactive_colour = Col::new(
@@ -107,7 +115,7 @@ impl Board {
107 115
         line.iter().all(|block| block.occupied)
108 116
     }
109 117
 
110
-    pub fn remove_line(&mut self, line_number: usize, inactive_colour: &Col) -> error::Result<()> {
118
+    pub fn remove_line(&mut self, line_number: usize) -> error::Result<()> {
111 119
         if line_number >= self.height {
112 120
             return Err(error::TetrisError::LineRemoval);
113 121
         }
@@ -123,7 +131,7 @@ impl Board {
123 131
         }
124 132
         let highest_line = self.height - 1;
125 133
         for x in 0..self.width {
126
-            self.board[highest_line][x].reset(inactive_colour);
134
+            self.board[highest_line][x].reset(&self.inactive_colour);
127 135
         }
128 136
 
129 137
         Ok(())
@@ -299,6 +307,41 @@ impl Game {
299 307
         game
300 308
     }
301 309
 
310
+    pub fn restart(&mut self, random: f32) -> error::Result<()> {
311
+        self.is_game_over = false;
312
+        self.score = 0;
313
+        self.level = 0;
314
+        self.lines = 0;
315
+        self.tetris = false;
316
+        self.time_to_next_lowering = get_time_to_next_lowering(0);
317
+        self.left_cooldown = 0.0;
318
+        self.right_cooldown = 0.0;
319
+        self.up_cooldown = 0.0;
320
+        self.down_cooldown = 0.0;
321
+        self.a_cooldown = 0.0;
322
+        self.b_cooldown = 0.0;
323
+        self.i_count = 0;
324
+        self.j_count = 0;
325
+        self.l_count = 0;
326
+        self.o_count = 0;
327
+        self.s_count = 0;
328
+        self.t_count = 0;
329
+        self.z_count = 0;
330
+
331
+        self.board.reset();
332
+        self.next_piece_shape = get_random_shape(1.0 - random)?;
333
+        self.next_piece_colour = get_colour_from_hsl(
334
+            ((1.0 - random) * 360.0) as f64,
335
+            self.colour_saturation,
336
+            self.colour_lightness,
337
+            1.0
338
+        )?;
339
+
340
+        use_next_shape(self, random)?;
341
+
342
+        Ok(())
343
+    }
344
+
302 345
     pub fn get_curvature(&self) -> f32 {
303 346
         let f = self.menu_curvature as f32 / self.menu_max_curvature as f32;
304 347
         f * (1.0 / 8.0)
@@ -384,7 +427,6 @@ impl Game {
384 427
     }
385 428
 
386 429
     pub fn tick(&mut self, controller: &Controller, delta: f32, random: f32) -> bool {
387
-
388 430
         if controller.just_released(ControllerButton::Start) {
389 431
             if self.mode == GameMode::Playing {
390 432
                 play_sound(SoundEffect::PauseIn);
@@ -426,15 +468,15 @@ impl Game {
426 468
                 Ok(res) => res,
427 469
                 Err(_) => false,
428 470
             },
429
-            GameMode::Paused => match self.tick_paused(controller) {
471
+            GameMode::Paused => match self.tick_paused(controller, random) {
430 472
                 Ok(res) => res,
431 473
                 Err(_) => false,
432 474
             },
433 475
         }
434 476
     }
435 477
 
436
-    fn tick_paused(&mut self, controller: &Controller) -> error::Result<bool> {
437
-        let updated = update_pause_menu(self, controller);
478
+    fn tick_paused(&mut self, controller: &Controller, random: f32) -> error::Result<bool> {
479
+        let updated = update_pause_menu(self, controller, random);
438 480
         Ok(updated)
439 481
     }
440 482
 
@@ -825,11 +867,9 @@ fn is_colliding(board: &Board, shape: &Shape, pos: &BoardPos, angle: &PieceAngle
825 867
 fn remove_complete_lines(board: &mut Board) -> error::Result<i32> {
826 868
     let mut offset = 0;
827 869
 
828
-    let c = board.inactive_colour;
829
-
830 870
     for y in 0..board.height {
831 871
         if board.is_line_filled(y - offset) {
832
-            board.remove_line(y - offset, &c)?;
872
+            board.remove_line(y - offset)?;
833 873
             offset += 1;
834 874
         }
835 875
     }
@@ -1027,7 +1067,7 @@ fn render_next_shape(geometry: &mut Geometry, shape: &Shape, offset: Block2D, co
1027 1067
 }
1028 1068
 
1029 1069
 
1030
-fn update_pause_menu(game: &mut Game, controller: &Controller) -> bool {
1070
+fn update_pause_menu(game: &mut Game, controller: &Controller, random: f32) -> bool {
1031 1071
 
1032 1072
     let mut update = false;
1033 1073
 
@@ -1097,6 +1137,13 @@ fn update_pause_menu(game: &mut Game, controller: &Controller) -> bool {
1097 1137
         game.menu_curvature = clamp(game.menu_curvature, 0, game.menu_max_curvature);
1098 1138
     }
1099 1139
 
1140
+    if game.menu_active_item == 4 {
1141
+        if controller.just_pressed(ControllerButton::Start) {
1142
+            let _ = game.restart(random);
1143
+            update = true;
1144
+        }
1145
+    }
1146
+
1100 1147
     if update {
1101 1148
         play_sound(SoundEffect::MenuMove);
1102 1149
     }

+ 1
- 1
src/lib.rs View File

@@ -126,7 +126,7 @@ impl Config {
126 126
             tileset_texture_height: 0, // tileset texture height
127 127
             sprite_width: 16,
128 128
             sprite_height: 16,
129
-            colour_saturation: 99.0,
129
+            colour_saturation: 85.0,
130 130
             colour_lightness: 50.0,
131 131
             board_offset_x: 1,
132 132
             board_offset_y: 1,

+ 0
- 30
web/js/index.js View File

@@ -119,36 +119,6 @@ function main() {
119 119
 
120 120
   const canvasElement = document.getElementById(gState.canvasId);
121 121
 
122
-  gState.config.render_texture_width = 512;
123
-  gState.config.render_texture_height = 512;
124
-
125
-  gState.config.block_size = 32; // how many blocks fit along a dimension
126
-  // 512 / 32 = 16 (the size of each block in pixels)
127
-
128
-  gState.config.block_limit_x = 16;
129
-  gState.config.block_limit_y = 22;
130
-
131
-  // the size of each sprite in the tileset (in pixels)
132
-  gState.config.sprite_width = 16;
133
-  gState.config.sprite_height = 16;
134
-
135
-  gState.config.colour_saturation = 85.0;
136
-  gState.config.colour_lightness = 50.0;
137
-
138
-  gState.config.default_cooldown = 100.0;
139
-
140
-  gState.config.block_inactive_col_r = 0.4;
141
-  gState.config.block_inactive_col_g = 0.6,
142
-  gState.config.block_inactive_col_b = 0.0,
143
-  gState.config.block_inactive_col_a = 0.1,
144
-
145
-  gState.config.menu_volume = 5;
146
-  gState.config.menu_max_volume = 5;
147
-  gState.config.menu_crt_update_speed = 4;
148
-  gState.config.menu_max_crt_update_speed = 5;
149
-  gState.config.menu_curvature = 3;
150
-  gState.config.menu_max_curvature = 5;
151
-
152 122
   resize_canvas_element(canvasElement, gState.config);
153 123
 
154 124
   gState.audio = new Audio(config);