Browse Source

saturation and lightness configurable from pause menu

Inderjit Gill 1 year ago
parent
commit
7fd75eead9
2 changed files with 213 additions and 69 deletions
  1. 203
    63
      src/game.rs
  2. 10
    6
      src/lib.rs

+ 203
- 63
src/game.rs View File

@@ -60,7 +60,7 @@ impl CachedHSLuv {
60 60
             s,
61 61
             l,
62 62
             a,
63
-            cached_rgb: rgb
63
+            cached_rgb: rgb,
64 64
         }
65 65
     }
66 66
 }
@@ -102,35 +102,26 @@ impl Block {
102 102
         self.hsl = self.flashing_dull_colour;
103 103
     }
104 104
 
105
+    pub fn updated_saturation_and_lightness(&mut self, saturation: f32, lightness: f32) {
106
+        self.hsl = CachedHSLuv::new(self.hsl.h, saturation, lightness, self.hsl.a);
107
+
108
+        // also calculate the flashing colours now
109
+        let bright_l = clampf(self.hsl.l + 20.0, 0.0, 100.0);
110
+        self.flashing_bright_colour =
111
+            CachedHSLuv::new(self.hsl.h, self.hsl.s, bright_l, self.hsl.a);
112
+
113
+        let dull_l = clampf(self.hsl.l - 20.0, 0.0, 100.0);
114
+        self.flashing_dull_colour = CachedHSLuv::new(self.hsl.h, self.hsl.s, dull_l, self.hsl.a);
115
+    }
116
+
105 117
     pub fn set(&mut self, occupied: bool, hsl: &CachedHSLuv) {
106 118
         self.occupied = occupied;
107 119
         if occupied {
108 120
             // set the colour straight away if it's occupied
109 121
             // (unoccupied blocks will fade out, see update_block_colours)
110
-
111
-            self.hsl = *hsl;
112
-
113
-            // also calculate the flashing colours now
114
-            let bright_l = clampf(hsl.l + 20.0, 0.0, 100.0);
115
-            if let Ok(bright) = get_colour_from_hsl(hsl.h, hsl.s, bright_l, 1.0) { // 70
116
-                self.flashing_bright_colour = CachedHSLuv {
117
-                    h: hsl.h,
118
-                    s: hsl.s,
119
-                    l: bright_l,
120
-                    a: hsl.a,
121
-                    cached_rgb: bright,
122
-                };
123
-            }
124
-            let dull_l = clampf(hsl.l - 20.0, 0.0, 100.0);
125
-            if let Ok(dull) = get_colour_from_hsl(hsl.h, hsl.s, dull_l, 1.0) { // 30
126
-                self.flashing_dull_colour = CachedHSLuv {
127
-                    h: hsl.h,
128
-                    s: hsl.s,
129
-                    l: dull_l,
130
-                    a: hsl.a,
131
-                    cached_rgb: dull,
132
-                };
133
-            }
122
+            self.hsl.h = hsl.h;
123
+            self.hsl.a = hsl.a;
124
+            self.updated_saturation_and_lightness(hsl.s, hsl.l);
134 125
         }
135 126
     }
136 127
 }
@@ -147,10 +138,12 @@ impl Board {
147 138
     pub fn new(config: &Config) -> Board {
148 139
         // width: usize, height: usize, inactive_colour: Col, crt_update_speed: f32
149 140
 
150
-        let cached_inactive_colour = CachedHSLuv::new(config.block_inactive_col_h,
151
-                                                      config.block_inactive_col_s,
152
-                                                      config.block_inactive_col_l,
153
-                                                      config.block_inactive_col_a);
141
+        let cached_inactive_colour = CachedHSLuv::new(
142
+            config.block_inactive_col_h,
143
+            config.colour_saturation,
144
+            config.colour_lightness,
145
+            config.block_inactive_col_a,
146
+        );
154 147
 
155 148
         let width = config.board_width;
156 149
         let height = config.board_height;
@@ -174,12 +167,40 @@ impl Board {
174 167
     }
175 168
 
176 169
     pub fn reconfigure(&mut self, config: &Config) {
177
-        self.inactive_colour = CachedHSLuv::new(config.block_inactive_col_h,
178
-                                                config.block_inactive_col_s,
179
-                                                config.block_inactive_col_l,
180
-                                                config.block_inactive_col_a);
170
+        self.inactive_colour = CachedHSLuv::new(
171
+            config.block_inactive_col_h,
172
+            config.colour_saturation,
173
+            config.colour_lightness,
174
+            config.block_inactive_col_a,
175
+        );
176
+
177
+        self.update_blocks_saturation_and_lightness(
178
+            config.colour_saturation,
179
+            config.colour_lightness,
180
+        );
181
+    }
182
+
183
+    pub fn updated_saturation_and_lightness(&mut self, saturation: f32, lightness: f32) {
184
+        self.inactive_colour = CachedHSLuv::new(
185
+            self.inactive_colour.h,
186
+            saturation,
187
+            lightness,
188
+            self.inactive_colour.a,
189
+        );
190
+
191
+        self.update_blocks_saturation_and_lightness(saturation, lightness);
192
+    }
181 193
 
182
-        // todo: update the existing board state with the config.colour_saturation and config.colour_lightness
194
+    fn update_blocks_saturation_and_lightness(&mut self, saturation: f32, lightness: f32) {
195
+        for line in 0..self.height {
196
+            for x in 0..self.width {
197
+                if self.board[line][x].occupied {
198
+                    self.board[line][x].updated_saturation_and_lightness(saturation, lightness);
199
+                } else {
200
+                    self.board[line][x].deactivate(&self.inactive_colour);
201
+                }
202
+            }
203
+        }
183 204
     }
184 205
 
185 206
     pub fn set_crt_update_speed(&mut self, new_crt_update_speed: f32) {
@@ -298,6 +319,10 @@ pub struct Game {
298 319
     menu_max_crt_update_speed: i32,
299 320
     menu_curvature: i32,
300 321
     menu_max_curvature: i32,
322
+    menu_saturation: i32,
323
+    menu_max_saturation: i32,
324
+    menu_lightness: i32,
325
+    menu_max_lightness: i32,
301 326
 
302 327
     is_game_over: bool,
303 328
 
@@ -358,7 +383,7 @@ impl Game {
358 383
             colour_saturation: config.colour_saturation,
359 384
             colour_lightness: config.colour_lightness,
360 385
 
361
-            menu_num_items: 5,
386
+            menu_num_items: 7,
362 387
             menu_active_item: 0,
363 388
             menu_volume: config.menu_volume,
364 389
             menu_max_volume: config.menu_max_volume,
@@ -366,6 +391,10 @@ impl Game {
366 391
             menu_max_crt_update_speed: config.menu_max_crt_update_speed,
367 392
             menu_curvature: config.menu_curvature,
368 393
             menu_max_curvature: config.menu_max_curvature,
394
+            menu_saturation: config.menu_saturation,
395
+            menu_max_saturation: config.menu_max_saturation,
396
+            menu_lightness: config.menu_lightness,
397
+            menu_max_lightness: config.menu_max_lightness,
369 398
 
370 399
             is_game_over: false,
371 400
 
@@ -477,10 +506,12 @@ impl Game {
477 506
             render_lines(geometry, self.lines, Block2D { x, y: y + 6 }, c);
478 507
         }
479 508
         if let Some(shape) = self.shapes.get(&self.next_piece_shape) {
480
-            if let Ok(colour) = get_colour_from_hsl(self.next_piece_hue,
481
-                                                    self.colour_saturation,
482
-                                                    self.colour_lightness,
483
-                                                    1.0) {
509
+            if let Ok(colour) = get_colour_from_hsl(
510
+                self.next_piece_hue,
511
+                self.colour_saturation,
512
+                self.colour_lightness,
513
+                1.0,
514
+            ) {
484 515
                 render_next_shape(geometry, &shape, Block2D { x, y }, colour);
485 516
             }
486 517
         }
@@ -602,11 +633,7 @@ impl Game {
602 633
 
603 634
                     if piece_cant_go_lower {
604 635
                         if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
605
-                            add_shape_to_board(
606
-                                &mut self.board,
607
-                                shape,
608
-                                &self.piece
609
-                            );
636
+                            add_shape_to_board(&mut self.board, shape, &self.piece);
610 637
 
611 638
                             if any_complete_lines(&self.board) {
612 639
                                 let num_lines = num_complete_lines(&self.board);
@@ -640,7 +667,7 @@ impl Game {
640 667
                         }
641 668
                     }
642 669
                 }
643
-            },
670
+            }
644 671
             GameState::PieceIsDropping => {
645 672
                 apply_user_input(self, controller, delta);
646 673
 
@@ -657,12 +684,11 @@ impl Game {
657 684
                         }
658 685
                     }
659 686
                 }
660
-            },
687
+            }
661 688
             GameState::LinesFlashing => {
662 689
                 self.lines_flashing_time += delta;
663 690
                 self.board.flash_complete_lines(self.lines_flashing_time);
664
-                if self.lines_flashing_time > 500.0 {
665
-
691
+                if self.lines_flashing_time > 600.0 {
666 692
                     let num_lines = remove_complete_lines(&mut self.board)?;
667 693
                     update_score(self, num_lines);
668 694
 
@@ -672,11 +698,7 @@ impl Game {
672 698
         }
673 699
 
674 700
         if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
675
-            add_shape_to_board(
676
-                &mut self.board,
677
-                shape,
678
-                &self.piece
679
-            );
701
+            add_shape_to_board(&mut self.board, shape, &self.piece);
680 702
         }
681 703
 
682 704
         // update the colour of all the blocks that aren't occupied
@@ -729,7 +751,12 @@ fn use_next_shape(game: &mut Game, random: f32) -> error::Result<()> {
729 751
         game.piece.pos.x = ((game.board.width as i32) - shape.aabb_width) / 2;
730 752
         game.piece.pos.y = (game.board.height as i32) - shape.real_height;
731 753
         game.piece.angle = PieceAngle::R0;
732
-        game.piece.colour = CachedHSLuv::new(game.next_piece_hue, game.colour_saturation, game.colour_lightness, 1.0);
754
+        game.piece.colour = CachedHSLuv::new(
755
+            game.next_piece_hue,
756
+            game.colour_saturation,
757
+            game.colour_lightness,
758
+            1.0,
759
+        );
733 760
     }
734 761
 
735 762
     match game.piece.shape_kind {
@@ -755,7 +782,12 @@ fn get_colour_from_hsl(
755 782
     lightness: f32,
756 783
     alpha: f32,
757 784
 ) -> error::Result<Col> {
758
-    let hsluv = Colour::HSLuv(hue as f64, saturation as f64, lightness as f64, alpha as f64);
785
+    let hsluv = Colour::HSLuv(
786
+        hue as f64,
787
+        saturation as f64,
788
+        lightness as f64,
789
+        alpha as f64,
790
+    );
759 791
     col_from_colour(&hsluv)
760 792
 }
761 793
 
@@ -971,7 +1003,7 @@ fn is_colliding(board: &Board, shape: &Shape, pos: &BoardPos, angle: &PieceAngle
971 1003
 fn any_complete_lines(board: &Board) -> bool {
972 1004
     for y in 0..board.height {
973 1005
         if board.is_line_filled(y) {
974
-            return true
1006
+            return true;
975 1007
         }
976 1008
     }
977 1009
     false
@@ -1002,11 +1034,7 @@ fn remove_complete_lines(board: &mut Board) -> error::Result<i32> {
1002 1034
     Ok(offset as i32)
1003 1035
 }
1004 1036
 
1005
-fn add_shape_to_board(
1006
-    board: &mut Board,
1007
-    shape: &Shape,
1008
-    piece: &Piece
1009
-) {
1037
+fn add_shape_to_board(board: &mut Board, shape: &Shape, piece: &Piece) {
1010 1038
     let pos = &piece.pos;
1011 1039
     let angle = &piece.angle;
1012 1040
     let colour = &piece.colour;
@@ -1097,7 +1125,7 @@ fn move_cached_col_closer(src: &CachedHSLuv, dest: &CachedHSLuv, delta: f32) ->
1097 1125
             move_f32_closer(src.cached_rgb.g, dest.cached_rgb.g, delta),
1098 1126
             move_f32_closer(src.cached_rgb.b, dest.cached_rgb.b, delta),
1099 1127
             move_f32_closer(src.cached_rgb.a, dest.cached_rgb.a, delta),
1100
-        )
1128
+        ),
1101 1129
     }
1102 1130
 }
1103 1131
 
@@ -1263,7 +1291,41 @@ fn update_pause_menu(game: &mut Game, controller: &Controller, random: f32) -> b
1263 1291
         game.menu_curvature = clamp(game.menu_curvature, 0, game.menu_max_curvature);
1264 1292
     }
1265 1293
 
1294
+    // saturation
1266 1295
     if game.menu_active_item == 4 {
1296
+        if controller.just_pressed(ControllerButton::Left) {
1297
+            game.menu_saturation -= 1;
1298
+            update = true;
1299
+        }
1300
+        if controller.just_pressed(ControllerButton::Right) {
1301
+            game.menu_saturation += 1;
1302
+            update = true;
1303
+        }
1304
+        game.menu_saturation = clamp(game.menu_saturation, 0, game.menu_max_saturation);
1305
+
1306
+        if update {
1307
+            update_saturation(game);
1308
+        }
1309
+    }
1310
+
1311
+    // lightness
1312
+    if game.menu_active_item == 5 {
1313
+        if controller.just_pressed(ControllerButton::Left) {
1314
+            game.menu_lightness -= 1;
1315
+            update = true;
1316
+        }
1317
+        if controller.just_pressed(ControllerButton::Right) {
1318
+            game.menu_lightness += 1;
1319
+            update = true;
1320
+        }
1321
+        game.menu_lightness = clamp(game.menu_lightness, 0, game.menu_max_lightness);
1322
+
1323
+        if update {
1324
+            update_lightness(game);
1325
+        }
1326
+    }
1327
+
1328
+    if game.menu_active_item == 6 {
1267 1329
         if controller.just_pressed(ControllerButton::Start) {
1268 1330
             let _ = game.restart(random);
1269 1331
             update = true;
@@ -1276,6 +1338,68 @@ fn update_pause_menu(game: &mut Game, controller: &Controller, random: f32) -> b
1276 1338
     update
1277 1339
 }
1278 1340
 
1341
+fn mc_m(xa: f32, ya: f32, xb: f32, yb: f32) -> f32 {
1342
+    (ya - yb) / (xa - xb)
1343
+}
1344
+
1345
+fn mc_c(xa: f32, ya: f32, m: f32) -> f32 {
1346
+    ya - (m * xa)
1347
+}
1348
+
1349
+fn remap(val: f32, from_a: f32, from_b: f32, to_a: f32, to_b: f32, clamping: bool) -> f32 {
1350
+    let from_m = mc_m(from_a, 0.0, from_b, 1.0);
1351
+    let from_c = mc_c(from_a, 0.0, from_m);
1352
+
1353
+    let to_m = mc_m(0.0, to_a, 1.0, to_b);
1354
+    let to_c = mc_c(0.0, to_a, to_m);
1355
+
1356
+    let from_interp = (from_m * val) + from_c;
1357
+    let to_interp = from_interp;
1358
+
1359
+    let mut res = (to_m * to_interp) + to_c;
1360
+
1361
+    if clamping {
1362
+        res = if from_interp < 0.0 {
1363
+            to_a
1364
+        } else if from_interp > 1.0 {
1365
+            to_b
1366
+        } else {
1367
+            res
1368
+        }
1369
+    }
1370
+
1371
+    res
1372
+}
1373
+
1374
+fn update_saturation(game: &mut Game) {
1375
+    game.colour_saturation = remap(
1376
+        game.menu_saturation as f32,
1377
+        0.0,
1378
+        game.menu_max_saturation as f32,
1379
+        40.0,
1380
+        100.0,
1381
+        true,
1382
+    );
1383
+    // log(&format!("saturation: {}", game.colour_saturation));
1384
+
1385
+    game.board
1386
+        .updated_saturation_and_lightness(game.colour_saturation, game.colour_lightness);
1387
+}
1388
+
1389
+fn update_lightness(game: &mut Game) {
1390
+    game.colour_lightness = remap(
1391
+        game.menu_lightness as f32,
1392
+        0.0,
1393
+        game.menu_max_lightness as f32,
1394
+        30.0,
1395
+        90.0,
1396
+        true,
1397
+    );
1398
+    // log(&format!("lightness: {}", game.colour_lightness));
1399
+    game.board
1400
+        .updated_saturation_and_lightness(game.colour_saturation, game.colour_lightness);
1401
+}
1402
+
1279 1403
 fn update_audio_volume(game: &Game) {
1280 1404
     let volume = game.menu_volume as f32 / game.menu_max_volume as f32;
1281 1405
     audio_volume(volume);
@@ -1350,7 +1474,21 @@ fn render_pause_menu(
1350 1474
         game.menu_max_curvature,
1351 1475
         game.menu_curvature,
1352 1476
     );
1353
-    let restart = build_menu_item(game, 4, "Restart");
1477
+    let saturation = build_menu_item_slider(
1478
+        game,
1479
+        4,
1480
+        "Saturation  ",
1481
+        game.menu_max_saturation,
1482
+        game.menu_saturation,
1483
+    );
1484
+    let lightness = build_menu_item_slider(
1485
+        game,
1486
+        5,
1487
+        "Lightness   ",
1488
+        game.menu_max_lightness,
1489
+        game.menu_lightness,
1490
+    );
1491
+    let restart = build_menu_item(game, 6, "Restart");
1354 1492
 
1355 1493
     // remember to update Game::menu_num_items when adding another menu option
1356 1494
 
@@ -1359,6 +1497,8 @@ fn render_pause_menu(
1359 1497
         &vol[..],
1360 1498
         &crt_update_speed[..],
1361 1499
         &curvature[..],
1500
+        &saturation[..],
1501
+        &lightness[..],
1362 1502
         &restart[..],
1363 1503
     ];
1364 1504
 

+ 10
- 6
src/lib.rs View File

@@ -97,8 +97,6 @@ pub struct Config {
97 97
     pub board_height: usize,
98 98
 
99 99
     pub block_inactive_col_h: f32,
100
-    pub block_inactive_col_s: f32,
101
-    pub block_inactive_col_l: f32,
102 100
     pub block_inactive_col_a: f32,
103 101
 
104 102
     pub menu_volume: i32,
@@ -107,6 +105,10 @@ pub struct Config {
107 105
     pub menu_max_crt_update_speed: i32,
108 106
     pub menu_curvature: i32,
109 107
     pub menu_max_curvature: i32,
108
+    pub menu_saturation: i32,
109
+    pub menu_max_saturation: i32,
110
+    pub menu_lightness: i32,
111
+    pub menu_max_lightness: i32,
110 112
 }
111 113
 
112 114
 #[wasm_bindgen]
@@ -129,8 +131,8 @@ impl Config {
129 131
             sprite_width: 16,
130 132
             sprite_height: 16,
131 133
 
132
-            colour_saturation: 85.0,
133
-            colour_lightness: 50.0,
134
+            colour_saturation: 88.0,
135
+            colour_lightness: 54.0,
134 136
 
135 137
             board_offset_x: 1,
136 138
             board_offset_y: 1,
@@ -141,8 +143,6 @@ impl Config {
141 143
             board_height: 20,
142 144
 
143 145
             block_inactive_col_h: 111.1,
144
-            block_inactive_col_s: 100.0,
145
-            block_inactive_col_l: 57.7,
146 146
             block_inactive_col_a: 0.1,
147 147
 
148 148
             menu_volume: 5,
@@ -151,6 +151,10 @@ impl Config {
151 151
             menu_max_crt_update_speed: 5,
152 152
             menu_curvature: 3,
153 153
             menu_max_curvature: 5,
154
+            menu_saturation: 4,
155
+            menu_max_saturation: 5,
156
+            menu_lightness: 2,
157
+            menu_max_lightness: 5,
154 158
         }
155 159
     }
156 160
 }

Loading…
Cancel
Save