Browse Source

Improved score management

Arnaud Vergnet 4 years ago
parent
commit
879ae46abe
1 changed files with 43 additions and 29 deletions
  1. 43
    29
      screens/Tetris/GameLogic.js

+ 43
- 29
screens/Tetris/GameLogic.js View File

@@ -4,31 +4,18 @@ import Tetromino from "./Tetromino";
4 4
 
5 5
 export default class GameLogic {
6 6
 
7
-    static levelTicks = {
8
-        '1': 1000,
9
-        '2': 900,
10
-        '3': 800,
11
-        '4': 700,
12
-        '5': 600,
13
-        '6': 500,
14
-        '7': 400,
15
-        '8': 300,
16
-        '9': 200,
17
-        '10': 150,
18
-    };
19
-
20
-    static levelThresholds = {
21
-        '1': 100,
22
-        '2': 300,
23
-        '3': 500,
24
-        '4': 700,
25
-        '5': 1000,
26
-        '6': 1500,
27
-        '7': 2000,
28
-        '8': 3000,
29
-        '9': 4000,
30
-        '10': 5000,
31
-    };
7
+    static levelTicks = [
8
+        1000,
9
+        800,
10
+        600,
11
+        400,
12
+        300,
13
+        200,
14
+        150,
15
+        100,
16
+    ];
17
+
18
+    static scoreLinesModifier = [40, 100, 300, 1200];
32 19
 
33 20
     currentGrid: Array<Array<Object>>;
34 21
 
@@ -59,6 +46,8 @@ export default class GameLogic {
59 46
 
60 47
     colors: Object;
61 48
 
49
+    levelProgression: number;
50
+
62 51
     constructor(height: number, width: number, colors: Object) {
63 52
         this.height = height;
64 53
         this.width = width;
@@ -120,6 +109,16 @@ export default class GameLogic {
120 109
         return finalGrid;
121 110
     }
122 111
 
112
+    getLinesRemovedPoints(numberRemoved: number) {
113
+        if (numberRemoved < 1 || numberRemoved > 4)
114
+            return 0;
115
+        return GameLogic.scoreLinesModifier[numberRemoved-1] * (this.level + 1);
116
+    }
117
+
118
+    canLevelUp() {
119
+        return this.levelProgression > this.level * 5;
120
+    }
121
+
123 122
     freezeTetromino() {
124 123
         let coord = this.currentObject.getCellsCoordinates();
125 124
         for (let i = 0; i < coord.length; i++) {
@@ -136,8 +135,22 @@ export default class GameLogic {
136 135
         for (let i = 0; i < lines.length; i++) {
137 136
             this.currentGrid.splice(lines[i], 1);
138 137
             this.currentGrid.unshift(this.getEmptyLine());
139
-            this.score += 100;
140 138
         }
139
+        switch (lines.length) {
140
+            case 1:
141
+                this.levelProgression += 1;
142
+                break;
143
+            case 2:
144
+                this.levelProgression += 3;
145
+                break;
146
+            case 3:
147
+                this.levelProgression += 5;
148
+                break;
149
+            case 4: // Did a tetris !
150
+                this.levelProgression += 8;
151
+                break;
152
+        }
153
+        this.score += this.getLinesRemovedPoints(lines.length);
141 154
     }
142 155
 
143 156
     getLinesToClear(coord: Object) {
@@ -203,7 +216,7 @@ export default class GameLogic {
203 216
     }
204 217
 
205 218
     setNewGameTick(level: number) {
206
-        if (level > 10)
219
+        if (level >= GameLogic.levelTicks.length)
207 220
             return;
208 221
         this.gameTick = GameLogic.levelTicks[level];
209 222
         clearInterval(this.gameTickInterval);
@@ -213,7 +226,7 @@ export default class GameLogic {
213 226
     onTick(callback: Function) {
214 227
         this.tryMoveTetromino(0, 1);
215 228
         callback(this.score, this.level, this.getFinalGrid());
216
-        if (this.level <= 10 && this.score > GameLogic.levelThresholds[this.level]) {
229
+        if (this.canLevelUp()) {
217 230
             this.level++;
218 231
             this.setNewGameTick(this.level);
219 232
         }
@@ -306,7 +319,8 @@ export default class GameLogic {
306 319
         this.gamePaused = false;
307 320
         this.gameTime = 0;
308 321
         this.score = 0;
309
-        this.level = 1;
322
+        this.level = 0;
323
+        this.levelProgression = 0;
310 324
         this.gameTick = GameLogic.levelTicks[this.level];
311 325
         this.currentGrid = this.getEmptyGrid();
312 326
         this.createTetromino();

Loading…
Cancel
Save