545a76109ef3bc64188d7ee19aac4e14bae2ff9d
- Author
- TheEdgeOfRage <git@theedgeofrage.com>
- Committer
- TheEdgeOfRage <git@theedgeofrage.com>
- Date
Message
Diff
This diff is truncated to protect this page.
1diff --git a/elements/pieces.go b/elements/pieces.go
2index 9589e46343b449c571de3c6f1e9bd6da85d99f0e..08607390435a90472413d8dff226403729e0f562 100644
3--- a/elements/pieces.go
4+++ b/elements/pieces.go
5@@ -8,7 +8,7 @@ type Piece struct {
6 Blocks []*Block
7 }
8
9-var Pieces = []*Piece{
10+var Pieces = []Piece{
11 {
12 Blocks: []*Block{
13 {rl.NewVector2(-1, 0), Cyan, false},
14@@ -67,8 +67,24 @@ var Pieces = []*Piece{
15 },
16 }
17
18-func (p *Piece) Draw(pos rl.Vector2, scale rl.Vector2, offset rl.Vector2) {
19+func (p Piece) Draw(pos rl.Vector2, scale rl.Vector2, offset rl.Vector2) {
20 for _, block := range p.Blocks {
21 block.Draw(pos, scale, offset)
22 }
23 }
24+
25+func GetRandomPiece() *Piece {
26+ base := Pieces[rl.GetRandomValue(0, int32(len(Pieces)-1))]
27+ newPiece := &Piece{
28+ Blocks: make([]*Block, 4),
29+ }
30+ for i, block := range base.Blocks {
31+ newPiece.Blocks[i] = &Block{
32+ Position: block.Position,
33+ Color: block.Color,
34+ LongBoi: block.LongBoi,
35+ }
36+ }
37+
38+ return newPiece
39+}
40diff --git a/game/board.go b/game/board.go
41index 6d4768b9870823babc8c18337684942854c5bd06..57e31564251480abcea7e4f50c43f7a654168efe 100644
42--- a/game/board.go
43+++ b/game/board.go
44@@ -7,35 +7,30 @@ import (
45 "github.com/gen2brain/raylib-go/raylib"
46 )
47
48-type Board interface {
49- DescendActivePiece() bool
50- DropActivePiece() bool
51- MoveActivePiece(direction int)
52- RotateActivePiece(clockwise bool)
53- ClearLines()
54- Draw(offset, scale rl.Vector2)
55-}
56-
57-type board struct {
58+type Board struct {
59 Board [10][20]*elements.Block
60
61 ActivePiece *elements.Piece
62 ActivePiecePos rl.Vector2
63 NextPiece *elements.Piece
64+ HeldPiece *elements.Piece
65+ AlreadyHeld bool
66 }
67
68-var _ Board = (*board)(nil)
69-
70-func InitBoard() *board {
71- board := &board{}
72- board.newActivePiece()
73+func InitBoard() *Board {
74+ board := &Board{
75+ NextPiece: elements.GetRandomPiece(),
76+ }
77+ board.getNextPiece()
78
79 return board
80 }
81
82-func (b *board) newActivePiece() bool {
83+func (b *Board) getNextPiece() bool {
84+ b.AlreadyHeld = false
85 b.ActivePiecePos = rl.NewVector2(5, 0)
86- b.ActivePiece = elements.Pieces[rl.GetRandomValue(0, int32(len(elements.Pieces)-1))]
87+ b.ActivePiece = b.NextPiece
88+ b.NextPiece = elements.GetRandomPiece()
89 for _, block := range b.ActivePiece.Blocks {
90 newX, newY := b.ActivePiecePos.X+block.Position.X, b.ActivePiecePos.Y+block.Position.Y
91 if newY >= 0 && b.Board[int(newX)][int(newY)] != nil {
92@@ -45,7 +40,7 @@ func (b *board) newActivePiece() bool {
93 return false
94 }
95
96-func (b *board) setBlocksFromActivePiece() {
97+func (b *Board) setBlocksFromActivePiece() {
98 for _, block := range b.ActivePiece.Blocks {
99 newX, newY := b.ActivePiecePos.X+block.Position.X, b.ActivePiecePos.Y+block.Position.Y
100 b.Board[int(newX)][int(newY)] = &elements.Block{
101@@ -55,7 +50,7 @@ func (b *board) setBlocksFromActivePiece() {
102 }
103 }
104
105-func (b *board) checkHorizontalCollision(left bool) bool {
106+func (b *Board) checkHorizontalCollision(left bool) bool {
107 for _, activeBlock := range b.ActivePiece.Blocks {
108 xPos := int(b.ActivePiecePos.X + activeBlock.Position.X)
109 yPos := int(b.ActivePiecePos.Y + activeBlock.Position.Y)
110@@ -78,7 +73,7 @@ func (b *board) checkHorizontalCollision(left bool) bool {
111 return false
112 }
113
114-func (b *board) descendActivePiece() bool {
115+func (b *Board) descendActivePiece() bool {
116 b.ActivePiecePos.Y += 1
117 for _, activeBlock := range b.ActivePiece.Blocks {
118 if b.ActivePiecePos.Y+activeBlock.Position.Y == 20 {
119@@ -98,23 +93,23 @@ func (b *board) descendActivePiece() bool {
120
121 // DescendActivePiece moves the piece down by one block. If the piece cannot move down, it will be placed on the board.
122 // After that, a new piece will be generated. If the new piece cannot be placed, true is returned.
123-func (b *board) DescendActivePiece() bool {
124+func (b *Board) DescendActivePiece() bool {
125 if b.descendActivePiece() {
126- return b.newActivePiece()
127+ return b.getNextPiece()
128 }
129 return false
130 }
131
132 // DropActivePiece moves the piece down until it cannot move down anymore. After that, a new piece will be generated.
133 // If the new piece cannot be placed, true is returned.
134-func (b *board) DropActivePiece() bool {
135+func (b *Board) DropActivePiece() bool {
136 for !b.descendActivePiece() {
137 }
138- return b.newActivePiece()
139+ return b.getNextPiece()
140 }
141
142 // MoveActivePiece moves the piece left or right. If the piece cannot move in the specified direction, nothing happens.
143-func (b *board) MoveActivePiece(direction int) {
144diff --git a/game/game.go b/game/game.go
145index ece776ed607f0b9589b38958ccf9a85b64497784..7186a0f07f83c4ad388b010236b090adb38eeb94 100644
146--- a/game/game.go
147+++ b/game/game.go
148@@ -16,7 +16,7 @@ type Game struct {
149
150 Scale rl.Vector2
151
152- Board Board
153+ Board *Board
154 UI *UI
155 }
156
157@@ -49,6 +49,9 @@ func (g *Game) Update() {
158 return
159 }
160
161+ if rl.IsKeyPressed(rl.KeyC) {
162+ g.Board.HoldPiece()
163+ }
164 if rl.IsKeyPressed(rl.KeyI) {
165 g.Board.MoveActivePiece(1)
166 }
167@@ -77,7 +80,9 @@ func (g *Game) Draw() {
168 g.UI.Draw(g)
169
170 if !g.GameOver {
171- g.Board.Draw(g.UI.BoardOffset, g.Scale)
172+ g.Board.DrawBoard(g.UI.BoardOffset, g.Scale)
173+ g.Board.DrawNextPiece(g.UI.NextPiecePosition, g.Scale)
174+ g.Board.DrawHeldPiece(g.UI.HeldPiecePosition, g.Scale)
175 } else {
176 rl.DrawText(
177 "PRESS [ENTER] TO PLAY AGAIN",
178diff --git a/game/ui.go b/game/ui.go
179index f8add3ad072872089e7c5ffc49037fe6842c9f23..d383ed2b28c18b1ad2b457b4f2cddee0eb5261a3 100644
180--- a/game/ui.go
181+++ b/game/ui.go
182@@ -1,19 +1,21 @@
183 package game
184
185 import (
186- // "fmt"
187+ "fmt"
188
189 "gitea.theedgeofrage.com/theedgeofrage/yeetris/elements"
190 "github.com/gen2brain/raylib-go/raylib"
191 )
192
193 type UI struct {
194- ScreenWidth int32
195- ScreenHeight int32
196- BoardWidth int32
197- BoardHeight int32
198- BoardOffset rl.Vector2
199- UIOffsetWidth int32
200+ ScreenWidth int32
201+ ScreenHeight int32
202+ BoardWidth int32
203+ BoardHeight int32
204+ BoardOffset rl.Vector2
205+ UIOffsetWidth int32
206+ NextPiecePosition rl.Vector2
207+ HeldPiecePosition rl.Vector2
208
209 Score int
210 }
211@@ -26,12 +28,14 @@ func InitUI(scale rl.Vector2) *UI {
212 rl.InitWindow(screenWidth, screenHeight, "Yeetris")
213
214 return &UI{
215- ScreenWidth: screenWidth,
216- ScreenHeight: screenHeight,
217- BoardWidth: boardWidth,
218- BoardHeight: boardHeight,
219- BoardOffset: rl.NewVector2(20, 20),
220- UIOffsetWidth: boardWidth + 40 + 20,
221+ ScreenWidth: screenWidth,
222+ ScreenHeight: screenHeight,
223+ BoardWidth: boardWidth,
224+ BoardHeight: boardHeight,
225+ BoardOffset: rl.NewVector2(20, 20),
226+ UIOffsetWidth: boardWidth + 40 + 20,
227+ NextPiecePosition: rl.NewVector2(float32(boardWidth+40+20+110), 250),
228+ HeldPiecePosition: rl.NewVector2(float32(boardWidth+40+20+110), 510),
229 }
230 }
231
232@@ -43,6 +47,11 @@ func drawRectangleBorder(x, y, width, height, border int32, color rl.Color) {
233 func (u *UI) Draw(g *Game) {
234 drawRectangleBorder(int32(u.BoardOffset.X), int32(u.BoardOffset.Y), u.BoardWidth, u.BoardHeight, 20, elements.Gray)
235 rl.DrawText("Yeetris", u.UIOffsetWidth, 20, 40, rl.White)
236+ rl.DrawText(fmt.Sprintf("Score: %d", u.Score), u.UIOffsetWidth, 70, 20, rl.White)
237+ rl.DrawText("Next Piece:", u.UIOffsetWidth, 110, 20, rl.White)
238+ drawRectangleBorder(u.UIOffsetWidth+10, 150, 200, 200, 10, elements.Gray)
239+ rl.DrawText("Hold:", u.UIOffsetWidth, 370, 20, rl.White)
240+ drawRectangleBorder(u.UIOffsetWidth+10, 410, 200, 200, 10, elements.Gray)
241
242 if g.Pause {
243 rl.DrawText(