6f5081ab57d0068707f9dfcc150c64e3d3332f91

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

Message

Fix game over panic

Diff

This diff is truncated to protect this page.

  1diff --git a/game/board.go b/game/board.go
  2index 57e31564251480abcea7e4f50c43f7a654168efe..c15f533c5c0c2cbff5c87024cd28301e8e2f2558 100644
  3--- a/game/board.go
  4+++ b/game/board.go
  5@@ -21,12 +21,12 @@ func InitBoard() *Board {
  6 	board := &Board{
  7 		NextPiece: elements.GetRandomPiece(),
  8 	}
  9-	board.getNextPiece()
 10+	_ = board.getNextPiece()
 11 
 12 	return board
 13 }
 14 
 15-func (b *Board) getNextPiece() bool {
 16+func (b *Board) getNextPiece() error {
 17 	b.AlreadyHeld = false
 18 	b.ActivePiecePos = rl.NewVector2(5, 0)
 19 	b.ActivePiece = b.NextPiece
 20@@ -34,20 +34,25 @@ func (b *Board) getNextPiece() bool {
 21 	for _, block := range b.ActivePiece.Blocks {
 22 		newX, newY := b.ActivePiecePos.X+block.Position.X, b.ActivePiecePos.Y+block.Position.Y
 23 		if newY >= 0 && b.Board[int(newX)][int(newY)] != nil {
 24-			return true
 25+			return ErrGameOver
 26 		}
 27 	}
 28-	return false
 29+	return nil
 30 }
 31 
 32-func (b *Board) setBlocksFromActivePiece() {
 33+func (b *Board) setBlocksFromActivePiece() error {
 34 	for _, block := range b.ActivePiece.Blocks {
 35 		newX, newY := b.ActivePiecePos.X+block.Position.X, b.ActivePiecePos.Y+block.Position.Y
 36+		if newY < 0 {
 37+			return ErrGameOver
 38+		}
 39 		b.Board[int(newX)][int(newY)] = &elements.Block{
 40 			Position: rl.NewVector2(newX, newY),
 41 			Color:    b.ActivePiece.Blocks[0].Color,
 42 		}
 43 	}
 44+
 45+	return nil
 46 }
 47 
 48 func (b *Board) checkHorizontalCollision(left bool) bool {
 49@@ -73,37 +78,43 @@ func (b *Board) checkHorizontalCollision(left bool) bool {
 50 	return false
 51 }
 52 
 53-func (b *Board) descendActivePiece() bool {
 54+func (b *Board) descendActivePiece() (bool, error) {
 55 	b.ActivePiecePos.Y += 1
 56 	for _, activeBlock := range b.ActivePiece.Blocks {
 57-		if b.ActivePiecePos.Y+activeBlock.Position.Y == 20 {
 58-			b.ActivePiecePos.Y -= 1
 59-			b.setBlocksFromActivePiece()
 60-			return true
 61-		}
 62-
 63-		if b.Board[int(b.ActivePiecePos.X+activeBlock.Position.X)][int(b.ActivePiecePos.Y+activeBlock.Position.Y)] != nil {
 64+		if b.ActivePiecePos.Y+activeBlock.Position.Y == 20 ||
 65+			b.Board[int(b.ActivePiecePos.X+activeBlock.Position.X)][int(b.ActivePiecePos.Y+activeBlock.Position.Y)] != nil {
 66 			b.ActivePiecePos.Y -= 1
 67-			b.setBlocksFromActivePiece()
 68-			return true
 69+			return true, b.setBlocksFromActivePiece()
 70 		}
 71 	}
 72-	return false
 73+	return false, nil
 74 }
 75 
 76 // DescendActivePiece moves the piece down by one block. If the piece cannot move down, it will be placed on the board.
 77-// After that, a new piece will be generated. If the new piece cannot be placed, true is returned.
 78-func (b *Board) DescendActivePiece() bool {
 79-	if b.descendActivePiece() {
 80+// After that, a new piece will be generated. If the new piece cannot be placed, a game over error is returned.
 81+func (b *Board) DescendActivePiece() error {
 82+	placed, err := b.descendActivePiece()
 83+	if err != nil {
 84+		return err
 85+	}
 86+
 87+	if placed {
 88 		return b.getNextPiece()
 89 	}
 90-	return false
 91+	return nil
 92 }
 93 
 94 // DropActivePiece moves the piece down until it cannot move down anymore. After that, a new piece will be generated.
 95-// If the new piece cannot be placed, true is returned.
 96-func (b *Board) DropActivePiece() bool {
 97-	for !b.descendActivePiece() {
 98+// If the new piece cannot be placed, a game over error is returned.
 99+func (b *Board) DropActivePiece() error {
100+	for {
101+		placed, err := b.descendActivePiece()
102+		if err != nil {
103+			return err
104+		}
105diff --git a/game/game.go b/game/game.go
106index 7186a0f07f83c4ad388b010236b090adb38eeb94..beac0b4ce9fe02007e63dbf6456e62558ba1ad4d 100644
107--- a/game/game.go
108+++ b/game/game.go
109@@ -1,11 +1,13 @@
110 package game
111 
112 import (
113-	"gitea.theedgeofrage.com/theedgeofrage/yeetris/elements"
114 	"github.com/gen2brain/raylib-go/raylib"
115+	"github.com/go-errors/errors"
116+
117+	"gitea.theedgeofrage.com/theedgeofrage/yeetris/elements"
118 )
119 
120-const DEBUG = false
121+var ErrGameOver = errors.New("game over")
122 
123 // Game type
124 type Game struct {
125@@ -33,6 +35,7 @@ func (g *Game) Init() {
126 }
127 
128 func (g *Game) Update() {
129+	var err error
130 	defer g.Board.ClearLines()
131 	if g.GameOver {
132 		if rl.IsKeyPressed(rl.KeyEnter) {
133@@ -62,13 +65,16 @@ func (g *Game) Update() {
134 		g.Board.RotateActivePiece(true)
135 	}
136 	if rl.IsKeyPressed(rl.KeyN) {
137-		g.Board.DescendActivePiece()
138+		err = g.Board.DescendActivePiece()
139 	}
140 	if rl.IsKeyPressed(rl.KeySpace) {
141-		g.Board.DropActivePiece()
142+		err = g.Board.DropActivePiece()
143+	}
144+	if g.FramesCounter%g.TickRate == 0 {
145+		err = g.Board.DescendActivePiece()
146 	}
147-	if g.FramesCounter%g.TickRate == 0 && !DEBUG {
148-		g.Board.DescendActivePiece()
149+	if errors.Is(err, ErrGameOver) {
150+		g.GameOver = true
151 	}
152 	g.FramesCounter++
153 }
154diff --git a/go.mod b/go.mod
155index 129be15cbc78e51740365b972d09172ccc291ad6..7148e095d0bc81c27e7b8ab6f5249f1fe74d5718 100644
156--- a/go.mod
157+++ b/go.mod
158@@ -2,7 +2,10 @@ module gitea.theedgeofrage.com/theedgeofrage/yeetris
159 
160 go 1.21.6
161 
162-require github.com/gen2brain/raylib-go/raylib v0.0.0-20240125111008-83d871a38f28
163+require (
164+	github.com/gen2brain/raylib-go/raylib v0.0.0-20240125111008-83d871a38f28
165+	github.com/go-errors/errors v1.5.1
166+)
167 
168 require (
169 	github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa // indirect
170diff --git a/go.sum b/go.sum
171index b957b9cbe34e2b9dd34445e2976fa3727b9a981b..f7abda4c714657fd91a5340b1dd7a2066ed72be6 100644
172--- a/go.sum
173+++ b/go.sum
174@@ -2,5 +2,7 @@ github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa h1:Ik7
175 github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
176 github.com/gen2brain/raylib-go/raylib v0.0.0-20240125111008-83d871a38f28 h1:e0HOaKlvsdC0ZtfWbEaaWAxYTSxUZoCAFS36jLfsVRA=
177 github.com/gen2brain/raylib-go/raylib v0.0.0-20240125111008-83d871a38f28/go.mod h1:P/hDjVwz/9fhR0ww3+umzDpDA7Bf7Tce4xNChHIEFqE=
178+github.com/go-errors/errors v1.5.1 h1:ZwEMSLRCapFLflTpT7NKaAc7ukJ8ZPEjzlxt8rPN8bk=
179+github.com/go-errors/errors v1.5.1/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
180 golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
181 golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=