Browse Source

cargo fmt

Inderjit Gill 1 year ago
parent
commit
164008cee2
7 changed files with 309 additions and 144 deletions
  1. 0
    1
      src/controller.rs
  2. 175
    76
      src/game.rs
  3. 106
    34
      src/geometry.rs
  4. 7
    9
      src/harbour.rs
  5. 19
    16
      src/lib.rs
  6. 0
    1
      src/text.rs
  7. 2
    7
      src/units.rs

+ 0
- 1
src/controller.rs View File

@@ -66,5 +66,4 @@ impl Controller {
66 66
             ControllerAction::Down => true,
67 67
         }
68 68
     }
69
-
70 69
 }

+ 175
- 76
src/game.rs View File

@@ -1,12 +1,12 @@
1
-use Config;
1
+use controller::Controller;
2 2
 use error;
3
+use geometry::Geometry;
3 4
 use log;
5
+use sen_colour::{Colour, Format};
6
+use sprite::Sprite;
4 7
 use std::collections::HashMap;
5 8
 use units::*;
6
-use sprite::Sprite;
7
-use controller::Controller;
8
-use geometry::Geometry;
9
-use sen_colour::{Colour, Format};
9
+use Config;
10 10
 
11 11
 #[derive(Clone, Copy)]
12 12
 pub struct Block {
@@ -19,7 +19,7 @@ impl Block {
19 19
     pub fn new(col: Col) -> Block {
20 20
         Block {
21 21
             occupied: false,
22
-            offset: Vec2D {x: 0.0, y: 0.0 },
22
+            offset: Vec2D { x: 0.0, y: 0.0 },
23 23
             colour: col,
24 24
         }
25 25
     }
@@ -42,10 +42,12 @@ pub struct Board {
42 42
 impl Board {
43 43
     pub fn new(config: &Config) -> Board {
44 44
         // width: usize, height: usize, inactive_colour: Col, crt_update_speed: f32
45
-        let inactive_colour = Col::new(config.block_inactive_col_r,
46
-                                       config.block_inactive_col_g,
47
-                                       config.block_inactive_col_b,
48
-                                       config.block_inactive_col_a);
45
+        let inactive_colour = Col::new(
46
+            config.block_inactive_col_r,
47
+            config.block_inactive_col_g,
48
+            config.block_inactive_col_b,
49
+            config.block_inactive_col_a,
50
+        );
49 51
         let width = config.board_width;
50 52
         let height = config.board_height;
51 53
         let crt_update_speed = config.crt_update_speed;
@@ -61,10 +63,12 @@ impl Board {
61 63
 
62 64
     pub fn reconfigure(&mut self, config: &Config) {
63 65
         // not updating board width/height in order to retain the current state of the board
64
-        self.inactive_colour = Col::new(config.block_inactive_col_r,
65
-                                        config.block_inactive_col_g,
66
-                                        config.block_inactive_col_b,
67
-                                        config.block_inactive_col_a);
66
+        self.inactive_colour = Col::new(
67
+            config.block_inactive_col_r,
68
+            config.block_inactive_col_g,
69
+            config.block_inactive_col_b,
70
+            config.block_inactive_col_a,
71
+        );
68 72
         self.crt_update_speed = config.crt_update_speed;
69 73
     }
70 74
 
@@ -80,11 +84,10 @@ impl Board {
80 84
 
81 85
     pub fn remove_line(&mut self, line_number: usize, inactive_colour: &Col) -> error::Result<()> {
82 86
         if line_number >= self.height {
83
-            return Err(error::TetrisError::LineRemoval)
87
+            return Err(error::TetrisError::LineRemoval);
84 88
         }
85 89
 
86
-        for line in line_number..(self.height-1) {
87
-
90
+        for line in line_number..(self.height - 1) {
88 91
             // copy top_line onto line
89 92
             //
90 93
             let top_line = line + 1;
@@ -146,7 +149,7 @@ pub struct Game {
146 149
     board_offset: Block2D,
147 150
 
148 151
     shapes: HashMap<ShapeKind, Shape>,
149
-    piece: Piece,               // the current piece under player control
152
+    piece: Piece, // the current piece under player control
150 153
     next_piece_shape: ShapeKind,
151 154
     next_piece_colour: Col,
152 155
 
@@ -163,7 +166,7 @@ pub struct Game {
163 166
     time_to_next_lowering: f32, // ms
164 167
     piece_is_dropping: bool,
165 168
 
166
-    default_cooldown: f32,      // ms
169
+    default_cooldown: f32, // ms
167 170
     left_cooldown: f32,
168 171
     right_cooldown: f32,
169 172
     up_cooldown: f32,
@@ -184,15 +187,15 @@ impl Game {
184 187
     pub fn new(config: &Config) -> Game {
185 188
         Game {
186 189
             board: Board::new(config),
187
-            board_offset: Block2D { x: config.board_offset_x, y: config.board_offset_y },
190
+            board_offset: Block2D {
191
+                x: config.board_offset_x,
192
+                y: config.board_offset_y,
193
+            },
188 194
 
189 195
             shapes: define_shapes(),
190 196
             piece: Piece {
191 197
                 shape_kind: ShapeKind::J,
192
-                pos: BoardPos {
193
-                    x: 5,
194
-                    y: 19,
195
-                },
198
+                pos: BoardPos { x: 5, y: 19 },
196 199
                 angle: PieceAngle::R0,
197 200
                 colour: Col::new(0.4, 0.8, 0.2, 0.9),
198 201
             },
@@ -208,7 +211,7 @@ impl Game {
208 211
             level: 0,
209 212
             lines: 0,
210 213
 
211
-            tetris: false,      // was the last scoring move a tetris?
214
+            tetris: false, // was the last scoring move a tetris?
212 215
 
213 216
             time_to_next_lowering: get_time_to_next_lowering(0),
214 217
             piece_is_dropping: false,
@@ -244,9 +247,11 @@ impl Game {
244 247
 
245 248
     pub fn init(&mut self, random: f32) -> error::Result<()> {
246 249
         self.next_piece_shape = get_random_shape(1.0 - random)?;
247
-        self.next_piece_colour = get_colour_from_hsl(((1.0 - random) * 360.0) as f64,
248
-                                                     self.colour_saturation,
249
-                                                     self.colour_lightness)?;
250
+        self.next_piece_colour = get_colour_from_hsl(
251
+            ((1.0 - random) * 360.0) as f64,
252
+            self.colour_saturation,
253
+            self.colour_lightness,
254
+        )?;
250 255
 
251 256
         use_next_shape(self, random)?;
252 257
         Ok(())
@@ -262,26 +267,25 @@ impl Game {
262 267
         let x = self.board_offset.x + 11;
263 268
         let y = self.board_offset.y + 3;
264 269
 
265
-        if let Ok(c) = get_colour_from_hsl(0.0,
266
-                                           self.colour_saturation,
267
-                                           self.colour_lightness) {
270
+        if let Ok(c) = get_colour_from_hsl(0.0, self.colour_saturation, self.colour_lightness) {
268 271
             render_score(geometry, self.score, Block2D { x, y: y + 14 }, c);
269 272
             render_level(geometry, self.level, Block2D { x, y: y + 10 }, c);
270 273
             render_lines(geometry, self.lines, Block2D { x, y: y + 6 }, c);
271 274
         }
272 275
         if let Some(shape) = self.shapes.get(&self.next_piece_shape) {
273
-            render_next_shape(geometry,
274
-                              &shape,
275
-                              Block2D { x, y },
276
-                              self.next_piece_colour);
276
+            render_next_shape(geometry, &shape, Block2D { x, y }, self.next_piece_colour);
277 277
         }
278 278
     }
279 279
 
280 280
     // counting on the fact that delta will always be a 'sensible' value.
281 281
     // so if the player has paused the game for a while, on the first resumed call to tick_playing
282 282
     // delta should be 0 rather than a large multi-second value
283
-    pub fn tick_playing(&mut self, controller: &Controller, delta: f32, random: f32) -> error::Result<bool> {
284
-
283
+    pub fn tick_playing(
284
+        &mut self,
285
+        controller: &Controller,
286
+        delta: f32,
287
+        random: f32,
288
+    ) -> error::Result<bool> {
285 289
         if self.is_game_over {
286 290
             log("game over");
287 291
             return Ok(false);
@@ -304,7 +308,6 @@ impl Game {
304 308
                 }
305 309
             }
306 310
         } else if is_time_to_lower_piece(self, delta) {
307
-
308 311
             let mut piece_cant_go_lower = false;
309 312
 
310 313
             if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
@@ -314,7 +317,13 @@ impl Game {
314 317
             if piece_cant_go_lower {
315 318
                 let mut num_lines = 0;
316 319
                 if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
317
-                    add_shape_to_board(&mut self.board, shape, &self.piece.pos, &self.piece.angle, &self.piece.colour);
320
+                    add_shape_to_board(
321
+                        &mut self.board,
322
+                        shape,
323
+                        &self.piece.pos,
324
+                        &self.piece.angle,
325
+                        &self.piece.colour,
326
+                    );
318 327
                     num_lines = remove_complete_lines(&mut self.board)?;
319 328
                 }
320 329
 
@@ -337,7 +346,13 @@ impl Game {
337 346
         }
338 347
 
339 348
         if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
340
-            add_shape_to_board(&mut self.board, shape, &self.piece.pos, &self.piece.angle, &self.piece.colour);
349
+            add_shape_to_board(
350
+                &mut self.board,
351
+                shape,
352
+                &self.piece.pos,
353
+                &self.piece.angle,
354
+                &self.piece.colour,
355
+            );
341 356
         }
342 357
 
343 358
         // update the colour of all the blocks that aren't occupied
@@ -361,7 +376,11 @@ fn score_for_current_move(game: &Game, num_lines: i32) -> i32 {
361 376
         1 => 100,
362 377
         2 => 300,
363 378
         3 => 500,
364
-        4 => if game.tetris { 1200 } else { 800 },
379
+        4 => if game.tetris {
380
+            1200
381
+        } else {
382
+            800
383
+        },
365 384
         _ => 0,
366 385
     }
367 386
 }
@@ -396,9 +415,11 @@ fn use_next_shape(game: &mut Game, random: f32) -> error::Result<()> {
396 415
     }
397 416
 
398 417
     game.next_piece_shape = get_random_shape(random)?;
399
-    game.next_piece_colour = get_colour_from_hsl((random * 360.0) as f64,
400
-                                                 game.colour_saturation,
401
-                                                 game.colour_lightness)?;
418
+    game.next_piece_colour = get_colour_from_hsl(
419
+        (random * 360.0) as f64,
420
+        game.colour_saturation,
421
+        game.colour_lightness,
422
+    )?;
402 423
 
403 424
     Ok(())
404 425
 }
@@ -411,10 +432,8 @@ fn get_colour_from_hsl(hue: f64, saturation: f64, lightness: f64) -> error::Resu
411 432
 fn col_from_colour(colour: &Colour) -> error::Result<Col> {
412 433
     if let Ok(rgb) = colour.clone_as(Format::RGB) {
413 434
         match rgb {
414
-            Colour::RGB(r, g, b, a) => {
415
-                Ok(Col::new(r as f32, g as f32, b as f32, a as f32))
416
-            },
417
-            _ => Err(error::TetrisError::InvalidColourFormat)
435
+            Colour::RGB(r, g, b, a) => Ok(Col::new(r as f32, g as f32, b as f32, a as f32)),
436
+            _ => Err(error::TetrisError::InvalidColourFormat),
418 437
         }
419 438
     } else {
420 439
         Err(error::TetrisError::InvalidColourFormat)
@@ -448,7 +467,10 @@ fn is_time_to_lower_piece(game: &mut Game, delta: f32) -> bool {
448 467
 // if possible vertically drop the piece by one row, otherwise return false
449 468
 //
450 469
 fn lower_piece(board: &Board, shape: &Shape, piece: &mut Piece) -> bool {
451
-    let new_position = BoardPos { x: piece.pos.x, y: piece.pos.y - 1 };
470
+    let new_position = BoardPos {
471
+        x: piece.pos.x,
472
+        y: piece.pos.y - 1,
473
+    };
452 474
 
453 475
     if is_allowed(board, shape, &new_position, &piece.angle) {
454 476
         piece.pos = new_position;
@@ -460,22 +482,42 @@ fn lower_piece(board: &Board, shape: &Shape, piece: &mut Piece) -> bool {
460 482
 
461 483
 fn apply_user_input(game: &mut Game, controller: &Controller, delta: f32) {
462 484
     if let Some(shape) = game.shapes.get(&game.piece.shape_kind) {
463
-
464
-        if can_apply_input(&mut game.left_cooldown, controller.left, game.default_cooldown, delta) {
465
-            let new_position = BoardPos { x: game.piece.pos.x - 1, y: game.piece.pos.y };
485
+        if can_apply_input(
486
+            &mut game.left_cooldown,
487
+            controller.left,
488
+            game.default_cooldown,
489
+            delta,
490
+        ) {
491
+            let new_position = BoardPos {
492
+                x: game.piece.pos.x - 1,
493
+                y: game.piece.pos.y,
494
+            };
466 495
             if is_allowed(&game.board, shape, &new_position, &game.piece.angle) {
467 496
                 game.piece.pos = new_position;
468 497
             }
469 498
         }
470 499
 
471
-        if can_apply_input(&mut game.right_cooldown, controller.right, game.default_cooldown, delta) {
472
-            let new_position = BoardPos { x: game.piece.pos.x + 1, y: game.piece.pos.y };
500
+        if can_apply_input(
501
+            &mut game.right_cooldown,
502
+            controller.right,
503
+            game.default_cooldown,
504
+            delta,
505
+        ) {
506
+            let new_position = BoardPos {
507
+                x: game.piece.pos.x + 1,
508
+                y: game.piece.pos.y,
509
+            };
473 510
             if is_allowed(&game.board, shape, &new_position, &game.piece.angle) {
474 511
                 game.piece.pos = new_position;
475 512
             }
476 513
         }
477 514
 
478
-        if can_apply_input(&mut game.up_cooldown, controller.up, game.default_cooldown, delta) {
515
+        if can_apply_input(
516
+            &mut game.up_cooldown,
517
+            controller.up,
518
+            game.default_cooldown,
519
+            delta,
520
+        ) {
479 521
             let new_angle;
480 522
             if shape.fully_rotate {
481 523
                 new_angle = match game.piece.angle {
@@ -496,11 +538,21 @@ fn apply_user_input(game: &mut Game, controller: &Controller, delta: f32) {
496 538
             }
497 539
         }
498 540
 
499
-        if can_apply_input(&mut game.down_cooldown, controller.down, game.default_cooldown, delta) {
541
+        if can_apply_input(
542
+            &mut game.down_cooldown,
543
+            controller.down,
544
+            game.default_cooldown,
545
+            delta,
546
+        ) {
500 547
             game.piece_is_dropping = true;
501 548
         }
502 549
 
503
-        if can_apply_input(&mut game.a_cooldown, controller.a, game.default_cooldown, delta) {
550
+        if can_apply_input(
551
+            &mut game.a_cooldown,
552
+            controller.a,
553
+            game.default_cooldown,
554
+            delta,
555
+        ) {
504 556
             let new_angle;
505 557
             if shape.fully_rotate {
506 558
                 new_angle = match game.piece.angle {
@@ -521,7 +573,12 @@ fn apply_user_input(game: &mut Game, controller: &Controller, delta: f32) {
521 573
             }
522 574
         }
523 575
 
524
-        if can_apply_input(&mut game.b_cooldown, controller.b, game.default_cooldown, delta) {
576
+        if can_apply_input(
577
+            &mut game.b_cooldown,
578
+            controller.b,
579
+            game.default_cooldown,
580
+            delta,
581
+        ) {
525 582
             let new_angle;
526 583
             if shape.fully_rotate {
527 584
                 new_angle = match game.piece.angle {
@@ -605,7 +662,13 @@ fn remove_complete_lines(board: &mut Board) -> error::Result<i32> {
605 662
     Ok(offset as i32)
606 663
 }
607 664
 
608
-fn add_shape_to_board(board: &mut Board, shape: &Shape, pos: &BoardPos, angle: &PieceAngle, colour: &Col) {
665
+fn add_shape_to_board(
666
+    board: &mut Board,
667
+    shape: &Shape,
668
+    pos: &BoardPos,
669
+    angle: &PieceAngle,
670
+    colour: &Col,
671
+) {
609 672
     for pos in get_board_positions(shape, pos, angle) {
610 673
         if is_valid_board_position(board, &pos) {
611 674
             set_block(board, pos.x, pos.y, true, colour);
@@ -637,7 +700,6 @@ fn get_board_positions(shape: &Shape, pos: &BoardPos, angle: &PieceAngle) -> Vec
637 700
     for y_offset in 0..shape.aabb_height {
638 701
         for x_offset in 0..shape.aabb_width {
639 702
             if shape.form[y_offset as usize][x_offset as usize] {
640
-
641 703
                 match angle {
642 704
                     PieceAngle::R0 => {
643 705
                         x = x_offset;
@@ -646,7 +708,7 @@ fn get_board_positions(shape: &Shape, pos: &BoardPos, angle: &PieceAngle) -> Vec
646 708
                     PieceAngle::R90 => {
647 709
                         x = y_offset;
648 710
                         y = shape.aabb_width - x_offset - 1;
649
-                    },
711
+                    }
650 712
                     PieceAngle::R180 => {
651 713
                         x = shape.aabb_width - x_offset - 1;
652 714
                         y = shape.aabb_height - y_offset - 1;
@@ -657,7 +719,10 @@ fn get_board_positions(shape: &Shape, pos: &BoardPos, angle: &PieceAngle) -> Vec
657 719
                     }
658 720
                 }
659 721
 
660
-                res.push(BoardPos {x: pos.x + x, y: pos.y + y});
722
+                res.push(BoardPos {
723
+                    x: pos.x + x,
724
+                    y: pos.y + y,
725
+                });
661 726
             }
662 727
         }
663 728
     }
@@ -678,7 +743,11 @@ fn update_block_colours(board: &mut Board) {
678 743
     for mut row in &mut board.board {
679 744
         for mut block in row {
680 745
             if !block.occupied {
681
-                block.colour = move_col_closer(&block.colour, &board.inactive_colour, board.crt_update_speed);
746
+                block.colour = move_col_closer(
747
+                    &block.colour,
748
+                    &board.inactive_colour,
749
+                    board.crt_update_speed,
750
+                );
682 751
             }
683 752
         }
684 753
     }
@@ -715,10 +784,15 @@ fn render_board(geometry: &mut Geometry, board: &Board, board_offset: &Block2D)
715 784
 
716 785
     for row in &board.board {
717 786
         for block in row {
718
-            geometry.push_sprite(Sprite::Block,
719
-                                 block.colour,
720
-                                 Block2D{ x: x + board_offset.x, y: y + board_offset.y },
721
-                                 block.offset);
787
+            geometry.push_sprite(
788
+                Sprite::Block,
789
+                block.colour,
790
+                Block2D {
791
+                    x: x + board_offset.x,
792
+                    y: y + board_offset.y,
793
+                },
794
+                block.offset,
795
+            );
722 796
             x += 1;
723 797
         }
724 798
         x = 0;
@@ -728,33 +802,58 @@ fn render_board(geometry: &mut Geometry, board: &Board, board_offset: &Block2D)
728 802
 
729 803
 fn render_score(geometry: &mut Geometry, score: i32, offset: Block2D, colour: Col) {
730 804
     geometry.push_text("SCORE", offset, colour);
731
-    geometry.push_text(&score.to_string(), Block2D { x: offset.x, y: offset.y - 1 }, colour);
805
+    geometry.push_text(
806
+        &score.to_string(),
807
+        Block2D {
808
+            x: offset.x,
809
+            y: offset.y - 1,
810
+        },
811
+        colour,
812
+    );
732 813
 }
733 814
 
734 815
 fn render_level(geometry: &mut Geometry, level: i32, offset: Block2D, colour: Col) {
735 816
     geometry.push_text("LEVEL", offset, colour);
736
-    geometry.push_text(&level.to_string(), Block2D { x: offset.x, y: offset.y - 1 }, colour);
817
+    geometry.push_text(
818
+        &level.to_string(),
819
+        Block2D {
820
+            x: offset.x,
821
+            y: offset.y - 1,
822
+        },
823
+        colour,
824
+    );
737 825
 }
738 826
 
739 827
 fn render_lines(geometry: &mut Geometry, lines: i32, offset: Block2D, colour: Col) {
740 828
     geometry.push_text("LINES", offset, colour);
741
-    geometry.push_text(&lines.to_string(), Block2D { x: offset.x, y: offset.y - 1 }, colour);
829
+    geometry.push_text(
830
+        &lines.to_string(),
831
+        Block2D {
832
+            x: offset.x,
833
+            y: offset.y - 1,
834
+        },
835
+        colour,
836
+    );
742 837
 }
743 838
 
744 839
 fn render_next_shape(geometry: &mut Geometry, shape: &Shape, offset: Block2D, colour: Col) {
745
-    let board_positions = get_board_positions(shape, &BoardPos { x: 0, y: 0}, &PieceAngle::R0);
840
+    let board_positions = get_board_positions(shape, &BoardPos { x: 0, y: 0 }, &PieceAngle::R0);
746 841
     for pos in board_positions {
747
-        geometry.push_sprite(Sprite::Block,
748
-                             colour,
749
-                             Block2D {x: pos.x + offset.x, y: pos.y + offset.y},
750
-                             Vec2D {x: 0.0, y: 0.0 });
842
+        geometry.push_sprite(
843
+            Sprite::Block,
844
+            colour,
845
+            Block2D {
846
+                x: pos.x + offset.x,
847
+                y: pos.y + offset.y,
848
+            },
849
+            Vec2D { x: 0.0, y: 0.0 },
850
+        );
751 851
     }
752 852
 }
753 853
 
754 854
 fn define_shapes() -> HashMap<ShapeKind, Shape> {
755 855
     let mut res = HashMap::new();
756 856
 
757
-
758 857
     // origin is in the bottom left
759 858
     // indices are in [y][x] order
760 859
     //

+ 106
- 34
src/geometry.rs View File

@@ -1,7 +1,7 @@
1
-use Config;
1
+use sprite::{get_sprite_location, Sprite};
2
+use text::sprite_location_from_char;
2 3
 use units::*;
3
-use sprite::{Sprite, get_sprite_location};
4
-use text::{sprite_location_from_char};
4
+use Config;
5 5
 
6 6
 struct UV {
7 7
     u: f32,
@@ -29,7 +29,6 @@ pub struct Geometry {
29 29
 }
30 30
 
31 31
 impl Geometry {
32
-
33 32
     pub fn new(config: &Config) -> Geometry {
34 33
         Geometry {
35 34
             render_texture_width: config.render_texture_width,
@@ -61,15 +60,17 @@ impl Geometry {
61 60
 
62 61
         self.crt.clear();
63 62
 
64
-        let max_u = (self.block_width  * (self.block_limit_x as f32)) / self.render_texture_width as f32; // 0.5000;
65
-        let max_v = (self.block_height * (self.block_limit_y as f32)) / self.render_texture_height as f32; // 0.6875;
63
+        let max_u =
64
+            (self.block_width * (self.block_limit_x as f32)) / self.render_texture_width as f32; // 0.5000;
65
+        let max_v =
66
+            (self.block_height * (self.block_limit_y as f32)) / self.render_texture_height as f32; // 0.6875;
66 67
 
67 68
         // note: the aspect ratio of max_u/max_v should be the same as canvas_width/canvas_height
68 69
 
69 70
         self.push_crt(0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0);
70
-        self.push_crt(  w, 0.0, 1.0, 1.0, 1.0, 1.0, max_u, 0.0);
71
-        self.push_crt(0.0,   h, 1.0, 1.0, 1.0, 1.0, 0.0, max_v);
72
-        self.push_crt(  w,   h, 1.0, 1.0, 1.0, 1.0, max_u, max_v);
71
+        self.push_crt(w, 0.0, 1.0, 1.0, 1.0, 1.0, max_u, 0.0);
72
+        self.push_crt(0.0, h, 1.0, 1.0, 1.0, 1.0, 0.0, max_v);
73
+        self.push_crt(w, h, 1.0, 1.0, 1.0, 1.0, max_u, max_v);
73 74
     }
74 75
 
75 76
     pub fn reconfigure(&mut self, config: &Config) {
@@ -106,7 +107,7 @@ impl Geometry {
106 107
     pub fn push_line(&mut self, p1: Vec2D, p2: Vec2D, thickness: f32, c: Col) {
107 108
         // keep thickness relative to block_width just like the other thickness parameters
108 109
         //
109
-        let hw = (thickness * self.block_width) / 2.0;       // half-width
110
+        let hw = (thickness * self.block_width) / 2.0; // half-width
110 111
 
111 112
         let (t1, t2, t3, t4) = self.get_sprite_uv(Sprite::DebugFilled);
112 113
 
@@ -114,17 +115,61 @@ impl Geometry {
114 115
         let op = opposite_normal(n);
115 116
 
116 117
         self.dup();
117
-        self.push(p1.x + (hw * n.x), p1.y + (hw * n.y), 0.0, 0.0, 0.0, 1.0, 0.0, 0.0);
118
-
119
-        self.push(p1.x + (hw * n.x), p1.y + (hw * n.y), c.r, c.g, c.b, c.a, t1.u, t1.v);
120
-        self.push(p1.x + (hw * op.x), p1.y + (hw * op.y), c.r, c.g, c.b, c.a, t2.u, t2.v);
121
-        self.push(p2.x + (hw * n.x), p2.y + (hw * n.y), c.r, c.g, c.b, c.a, t3.u, t3.v);
122
-        self.push(p2.x + (hw * op.x), p2.y + (hw * op.y), c.r, c.g, c.b, c.a, t4.u, t4.v);
118
+        self.push(
119
+            p1.x + (hw * n.x),
120
+            p1.y + (hw * n.y),
121
+            0.0,
122
+            0.0,
123
+            0.0,
124
+            1.0,
125
+            0.0,
126
+            0.0,
127
+        );
128
+
129
+        self.push(
130
+            p1.x + (hw * n.x),
131
+            p1.y + (hw * n.y),
132
+            c.r,
133
+            c.g,
134
+            c.b,
135
+            c.a,
136
+            t1.u,
137
+            t1.v,
138
+        );
139
+        self.push(
140
+            p1.x + (hw * op.x),
141
+            p1.y + (hw * op.y),
142
+            c.r,
143
+            c.g,
144
+            c.b,
145
+            c.a,
146
+            t2.u,
147
+            t2.v,
148
+        );
149
+        self.push(
150
+            p2.x + (hw * n.x),
151
+            p2.y + (hw * n.y),
152
+            c.r,
153
+            c.g,
154
+            c.b,
155
+            c.a,
156
+            t3.u,
157
+            t3.v,
158
+        );
159
+        self.push(
160
+            p2.x + (hw * op.x),
161
+            p2.y + (hw * op.y),
162
+            c.r,
163
+            c.g,
164
+            c.b,
165
+            c.a,
166
+            t4.u,
167
+            t4.v,
168
+        );
123 169
     }
124 170
 
125 171
     // todo: push_path/push_lines
126 172
 
127
-
128 173
     pub fn centre_point_from_block(&self, block: Block2D) -> Vec2D {
129 174
         let w = self.block_width as f32;
130 175
         let h = self.block_height as f32;
@@ -172,13 +217,7 @@ impl Geometry {
172 217
         self.push_outline(x1, y1, x2, y2, thickness, c.r, c.g, c.b, c.a);
173 218
     }
174 219
 
175
-    pub fn push_sprite(
176
-        &mut self,
177
-        sprite: Sprite,
178
-        col: Col,
179
-        pos: Block2D,
180
-        offset: Vec2D,
181
-    ) {
220
+    pub fn push_sprite(&mut self, sprite: Sprite, col: Col, pos: Block2D, offset: Vec2D) {
182 221
         // need local variable because they're used in calls to add_vert
183 222
         let w = self.block_width;
184 223
         let h = self.block_height;
@@ -203,7 +242,7 @@ impl Geometry {
203 242
     pub fn push_text(
204 243
         &mut self,
205 244
         text: &str,
206
-        pos: Block2D,            // in blockspace screen-coordinates
245
+        pos: Block2D, // in blockspace screen-coordinates
207 246
         col: Col,
208 247
     ) {
209 248
         let mut v2 = self.point_from_block(pos);
@@ -248,7 +287,18 @@ impl Geometry {
248 287
         self.push(x2, y2, r, g, b, a, t4.u, t4.v);
249 288
     }
250 289
 
251
-    fn push_outline(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, thickness: f32, r: f32, g: f32, b: f32, a: f32) {
290
+    fn push_outline(
291
+        &mut self,
292
+        x1: f32,
293
+        y1: f32,
294
+        x2: f32,
295
+        y2: f32,
296
+        thickness: f32,
297
+        r: f32,
298
+        g: f32,
299
+        b: f32,
300
+        a: f32,
301
+    ) {
252 302
         let w = self.block_width;
253 303
         let h = self.block_height;
254 304
 
@@ -314,10 +364,21 @@ impl Geometry {
314 364
         let u = (sprite_location.col as f32) * u_unit;
315 365
         let v = (sprite_location.row as f32) * v_unit;
316 366
 
317
-        (UV{u, v: 1.0 - (v + v_unit)},
318
-         UV{u: u + u_unit, v: 1.0 - (v + v_unit)},
319
-         UV{u, v: 1.0 - v},
320
-         UV{u: u + u_unit, v: 1.0 - v})
367
+        (
368
+            UV {
369
+                u,
370
+                v: 1.0 - (v + v_unit),
371
+            },
372
+            UV {
373
+                u: u + u_unit,
374
+                v: 1.0 - (v + v_unit),
375
+            },
376
+            UV { u, v: 1.0 - v },
377
+            UV {
378
+                u: u + u_unit,
379
+                v: 1.0 - v,
380
+            },
381
+        )
321 382
     }
322 383
 
323 384
     fn get_char_uv(&self, c: char) -> (UV, UV, UV, UV) {
@@ -329,9 +390,20 @@ impl Geometry {
329 390
         let u = (sprite_location.col as f32) * u_unit;
330 391
         let v = (sprite_location.row as f32) * v_unit;
331 392
 
332
-        (UV{u, v: 1.0 - (v + v_unit)},
333
-         UV{u: u + u_unit, v: 1.0 - (v + v_unit)},
334
-         UV{u, v: 1.0 - v},
335
-         UV{u: u + u_unit, v: 1.0 - v})
393
+        (
394
+            UV {
395
+                u,
396
+                v: 1.0 - (v + v_unit),
397
+            },
398
+            UV {
399
+                u: u + u_unit,
400
+                v: 1.0 - (v + v_unit),
401
+            },
402
+            UV { u, v: 1.0 - v },
403
+            UV {
404
+                u: u + u_unit,
405
+                v: 1.0 - v,
406
+            },
407
+        )
336 408
     }
337 409
 }

+ 7
- 9
src/harbour.rs View File

@@ -1,8 +1,8 @@
1
-use log;
2
-use Config;
3
-use controller::{Controller, ControllerButton, ControllerAction};
1
+use controller::{Controller, ControllerAction, ControllerButton};
4 2
 use game::Game;
5 3
 use geometry::Geometry;
4
+use log;
5
+use Config;
6 6
 
7 7
 #[derive(PartialEq)]
8 8
 pub enum GameMode {
@@ -47,7 +47,7 @@ impl Harbour {
47 47
         self.geometry.init();
48 48
         match self.game.init(random) {
49 49
             Ok(_) => (),
50
-            Err(_) => log("harbour::init errored")
50
+            Err(_) => log("harbour::init errored"),
51 51
         }
52 52
     }
53 53
 
@@ -89,11 +89,9 @@ impl Harbour {
89 89
         }
90 90
 
91 91
         match self.mode {
92
-            GameMode::Playing => {
93
-                match self.game.tick_playing(&self.controller, delta, random) {
94
-                    Ok(res) => res,
95
-                    Err(_) => false,
96
-                }
92
+            GameMode::Playing => match self.game.tick_playing(&self.controller, delta, random) {
93
+                Ok(res) => res,
94
+                Err(_) => false,
97 95
             },
98 96
             GameMode::Paused => false,
99 97
         }

+ 19
- 16
src/lib.rs View File

@@ -1,23 +1,26 @@
1 1
 #![feature(use_extern_macros, wasm_custom_section, wasm_import_module)]
2
-#![cfg_attr(feature = "cargo-clippy", allow(many_single_char_names, too_many_arguments))]
2
+#![cfg_attr(
3
+    feature = "cargo-clippy",
4
+    allow(many_single_char_names, too_many_arguments)
5
+)]
3 6
 
4 7
 #[macro_use]
5 8
 extern crate failure;
6
-extern crate wasm_bindgen;
7 9
 extern crate sen_colour;
10
+extern crate wasm_bindgen;
8 11
 
9 12
 use wasm_bindgen::prelude::*;
10 13
 
14
+pub mod controller;
11 15
 pub mod error;
12
-pub mod units;
13
-pub mod sprite;
14
-pub mod text;
15
-pub mod harbour;
16 16
 pub mod game;
17 17
 pub mod geometry;
18
-pub mod controller;
18
+pub mod harbour;
19
+pub mod sprite;
20
+pub mod text;
21
+pub mod units;
19 22
 
20
-use controller::{ControllerButton, ControllerAction};
23
+use controller::{ControllerAction, ControllerButton};
21 24
 use harbour::Harbour;
22 25
 
23 26
 #[wasm_bindgen(js_namespace = console)]
@@ -46,15 +49,15 @@ impl KeyEventReturn {
46 49
 //
47 50
 #[wasm_bindgen]
48 51
 pub struct Config {
49
-    pub render_texture_width: i32,   // the texture that the game is rendered onto
52
+    pub render_texture_width: i32, // the texture that the game is rendered onto
50 53
     pub render_texture_height: i32,
51 54
 
52
-    pub canvas_width: i32,           // the canvas that the rendertexture is rendered onto
55
+    pub canvas_width: i32, // the canvas that the rendertexture is rendered onto
53 56
     pub canvas_height: i32,
54 57
 
55
-    pub block_size: i32,             // the number of blocks that can fit along the render_texture
56
-    pub block_limit_x: i32,          // the largest block index along the x axis
57
-    pub block_limit_y: i32,          // the largest block index along the y axis
58
+    pub block_size: i32, // the number of blocks that can fit along the render_texture
59
+    pub block_limit_x: i32, // the largest block index along the x axis
60
+    pub block_limit_y: i32, // the largest block index along the y axis
58 61
 
59 62
     pub tileset_texture_width: i32,  // tileset texture width
60 63
     pub tileset_texture_height: i32, // tileset texture height
@@ -68,7 +71,7 @@ pub struct Config {
68 71
     pub board_offset_x: i32,
69 72
     pub board_offset_y: i32,
70 73
 
71
-    pub default_cooldown: f32,  // ms
74
+    pub default_cooldown: f32, // ms
72 75
 
73 76
     // board
74 77
     pub board_width: usize,
@@ -79,7 +82,7 @@ pub struct Config {
79 82
     pub block_inactive_col_b: f32,
80 83
     pub block_inactive_col_a: f32,
81 84
 
82
-    pub crt_update_speed: f32,       // higher the value, the less 'ghosting'
85
+    pub crt_update_speed: f32, // higher the value, the less 'ghosting'
83 86
 }
84 87
 
85 88
 #[wasm_bindgen]
@@ -126,7 +129,7 @@ impl Bridge {
126 129
     #[wasm_bindgen(constructor)]
127 130
     pub fn new(config: &Config) -> Bridge {
128 131
         Bridge {
129
-            harbour: Harbour::new(config)
132
+            harbour: Harbour::new(config),
130 133
         }
131 134
     }
132 135
 

+ 0
- 1
src/text.rs View File

@@ -98,6 +98,5 @@ pub fn sprite_location_from_char(c: char) -> SpriteLocation {
98 98
         '>' => SpriteLocation { row: 28, col: 24 },
99 99
 
100 100
         _ => SpriteLocation { row: 23, col: 10 },
101
-
102 101
     }
103 102
 }

+ 2
- 7
src/units.rs View File

@@ -10,9 +10,7 @@ pub struct Col {
10 10
 
11 11
 impl Col {
12 12
     pub fn new(r: f32, g: f32, b: f32, a: f32) -> Col {
13
-        Col {
14
-            r, g, b, a
15
-        }
13
+        Col { r, g, b, a }
16 14
     }
17 15
 }
18 16
 
@@ -51,10 +49,7 @@ pub fn normal(p1: Vec2D, p2: Vec2D) -> Vec2D {
51 49
 }
52 50
 
53 51
 pub fn opposite_normal(v: Vec2D) -> Vec2D {
54
-    Vec2D {
55
-        x: -v.x,
56
-        y: -v.y,
57
-    }
52
+    Vec2D { x: -v.x, y: -v.y }
58 53
 }
59 54
 
60 55
 // 2D Vector in blockspace

Loading…
Cancel
Save