ef58dba80abd4676b5bc4c38265202f4c8a2df80

Author
TheEdgeOfRage <git@theedgeofrage.com>
Committer
TheEdgeOfRage <git@theedgeofrage.com>
Date

Message

Add scoring

Diff

 1diff --git a/game/board.go b/game/board.go
 2index c15f533c5c0c2cbff5c87024cd28301e8e2f2558..6618eebb2327eaf62be3912f9170a93dd903b990 100644
 3--- a/game/board.go
 4+++ b/game/board.go
 5@@ -216,7 +216,8 @@ func (b *Board) HoldPiece() {
 6 }
 7 
 8 // ClearLines clears all lines that are completely filled.
 9-func (g *Board) ClearLines() {
10+func (g *Board) ClearLines() int {
11+	clearedLines := 0
12 	for y := 19; y >= 0; {
13 		skip := false
14 		for x := 0; x < 10; x++ {
15@@ -229,8 +230,15 @@ func (g *Board) ClearLines() {
16 			y--
17 			continue
18 		}
19-		for ty := y; ty > 0; ty-- {
20+		clearedLines++
21+		for ty := y; ty >= 0; ty-- {
22 			for x := 0; x < 10; x++ {
23+				// always clear top line
24+				if ty == 0 {
25+					g.Board[x][ty] = nil
26+					continue
27+				}
28+				// move all blocks above cleared line down
29 				if g.Board[x][ty-1] != nil {
30 					g.Board[x][ty] = g.Board[x][ty-1]
31 					g.Board[x][ty].Position.Y++
32@@ -239,10 +247,8 @@ func (g *Board) ClearLines() {
33 				}
34 			}
35 		}
36-		for x := 0; x < 10; x++ {
37-			g.Board[x][0] = nil
38-		}
39 	}
40+	return clearedLines
41 }
42 
43 func (b *Board) DrawGrid(offset, scale rl.Vector2) {
44diff --git a/game/game.go b/game/game.go
45index beac0b4ce9fe02007e63dbf6456e62558ba1ad4d..1055edcd26b0ab33f902b0b473baccad894f4cdc 100644
46--- a/game/game.go
47+++ b/game/game.go
48@@ -36,7 +36,6 @@ func (g *Game) Init() {
49 
50 func (g *Game) Update() {
51 	var err error
52-	defer g.Board.ClearLines()
53 	if g.GameOver {
54 		if rl.IsKeyPressed(rl.KeyEnter) {
55 			g.Init()
56@@ -76,6 +75,18 @@ func (g *Game) Update() {
57 	if errors.Is(err, ErrGameOver) {
58 		g.GameOver = true
59 	}
60+
61+	clearedLines := g.Board.ClearLines()
62+	switch clearedLines {
63+	case 1:
64+		g.UI.Score += 40
65+	case 2:
66+		g.UI.Score += 100
67+	case 3:
68+		g.UI.Score += 300
69+	case 4:
70+		g.UI.Score += 1200
71+	}
72 	g.FramesCounter++
73 }
74