-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.go
executable file
·116 lines (97 loc) · 2.25 KB
/
grid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
tcell "github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/views"
)
type GridBlock struct {
X int
Y int
Style tcell.Style
Occupied bool
}
type Grid struct {
Width int
Height int
Data []GridBlock
}
func NewGrid(w, h int) *Grid {
data := make([]GridBlock, w*h)
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
data[y*w+x].X = x
data[y*w+x].Y = y
}
}
return &Grid{
Width: w,
Height: h,
Data: data,
}
}
func (g *Grid) SetOccupied(x, y int, style tcell.Style) {
block := &g.Data[y*g.Width+x]
block.Occupied = true
block.Style = style
}
func (g *Grid) Clear() {
for i := range g.Data {
g.Data[i].Occupied = false
}
}
func (g *Grid) Draw(v *views.ViewPort) {
for y := 0; y < g.Height; y++ {
for x := 0; x < g.Width; x++ {
i := y*g.Width + x
block := &g.Data[i]
// Debug the position
// value := []rune(strconv.Itoa(i))[0]
value := ' '
if !block.Occupied {
v.SetContent(x, y, value, nil, tcell.StyleDefault.
Foreground(tcell.ColorWhite).
Background(tcell.ColorBlack))
continue
}
v.SetContent(x, y, value, nil, block.Style)
}
}
}
func (g *Grid) CanPieceMoveLeft(piece *Sprite) bool {
return g.CanPieceMoveVertically(piece) &&
piece.PosX > 0
}
func (g *Grid) CanPieceMoveRight(piece *Sprite) bool {
return g.CanPieceMoveVertically(piece) &&
piece.PosX < g.Width-piece.Width()
}
func (g *Grid) CanPieceMoveLaterally(piece *Sprite) bool {
return g.CanPieceMoveLeft(piece) && g.CanPieceMoveRight(piece)
}
func (g *Grid) GetBottomRow(piece *Sprite) []GridBlock {
from := (piece.PosY+piece.Height()-1)*g.Width + piece.PosX
to := from + piece.Width()
return g.Data[from:to]
}
func (g *Grid) GetBlock(x, y int) *GridBlock {
index := y*g.Width + x
if index >= len(g.Data) {
return nil
}
return &g.Data[index]
}
func (g *Grid) CanPieceMoveVertically(piece *Sprite) bool {
blocks := g.GetBottomRow(piece)
for _, block := range blocks {
blockBelow := g.GetBlock(block.X, block.Y+1)
if blockBelow == nil {
continue
}
if blockBelow.Occupied {
return false
}
}
return piece.PosY < g.Height-piece.Height()
}
func (g *Grid) CanPieceMove(piece *Sprite) bool {
return g.CanPieceMoveLaterally(piece) || g.CanPieceMoveVertically(piece)
}