-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtile.go
209 lines (187 loc) · 4.24 KB
/
tile.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"math/rand"
"strconv"
)
type TileSet struct {
id int
Size int // Number of tiles
Number int
NumberStr string // Textual representation of the bin number.
Sprites []*Sprite
numberSlots []*Sprite
Speed float64
ClickFunc func(x, y float32)
sizex float32
sizey float32
scale float32
gh *Game
hidden bool
tileWidth float32
tileHeight float32
tile *Sprite
}
func (t *TileSet) Init(size int, number int, g *Game) {
t.gh = g
t.id = g.NewTileID()
t.Speed = float64(g.size.HeightPx / 5)
t.Number = number
t.Size = size
t.sizex = t.tileWidth
t.sizey = t.tileHeight
t.NumberStr = strconv.Itoa(number)
c := &Sprite{hidden: false}
t.Sprites = append(t.Sprites, c)
c.Init(0.04, 0.1, 0.5, 0.745, 0.1, "tile", g, SpritePlay)
c.ChangeEffect(EffectTileTop)
t.tile = c
// 0.037 is just an offset for the actuall texture that is badly aligned :P
w := float32(0.732)
for x := float32(0); x < w; x += w / 4.0 {
s := g.tex.AddText("0", x+w/4-0.07, 0.125, 0.7, 0.05, 0.05, EffectNumber, SpritePlay)
t.numberSlots = append(t.numberSlots, s...)
s = g.tex.AddText("1", x+w/4-0.07, 0.125, 0.7, 0.05, 0.05, EffectNumber, SpritePlay)
s[0].Hide()
t.numberSlots = append(t.numberSlots, s...)
}
n := g.tex.AddText(t.NumberStr, 0.84, 0.121, 0.7, 0.06, 0.06, EffectNumber, SpritePlay)
t.Sprites = append(t.Sprites, n...)
}
func (t *TileSet) SetSpeed(s float64) {
t.Speed = s
}
func (t *TileSet) Click(x, y float32) {
t.gh.sound.Play("click")
slot0 := 0
slot1 := 0
w := float32(0.732)
if x > t.tile.fx && x < t.tile.fx+w/4 {
slot0 = 0
slot1 = 1
} else if x > t.tile.fx+w/4 && x < t.tile.fx+(w/4)*2 {
slot0 = 2
slot1 = 3
} else if x > t.tile.fx+(w/4)*2 && x < t.tile.fx+(w/4)*3 {
slot0 = 4
slot1 = 5
} else if x > t.tile.fx+(w/4)*3 && x < t.tile.fx+(w/4)*4 {
slot0 = 6
slot1 = 7
}
c0 := t.numberSlots[slot0]
c1 := t.numberSlots[slot1]
if c0.hidden {
c0.Show()
c1.Hide()
} else {
c0.Hide()
c1.Show()
}
// Verify number
t.VerifyNumber()
}
func (t *TileSet) Hide() {
for i := range t.Sprites {
t.Sprites[i].Hide()
}
for i := range t.numberSlots {
t.numberSlots[i].Hide()
}
t.hidden = true
}
func (t *TileSet) Reset(offset float32, bitRot bool) {
for i := range t.Sprites {
t.Sprites[i].Show()
t.Sprites[i].ChangeY(offset)
t.Sprites[i].ChangeEffect(EffectNumber)
}
for i := range t.numberSlots {
if t.numberSlots[i].Texture.Name != "1" {
t.numberSlots[i].Show()
}
t.numberSlots[i].ChangeY(offset)
t.numberSlots[i].ChangeEffect(EffectNumber)
}
// If bitot, enable some of them by default.
if bitRot {
n := rand.Intn(5)
for i := 0; i < n; i++ {
pos := rand.Intn(8)
if pos%2 == 0 {
t.numberSlots[pos].Show()
t.numberSlots[pos+1].Hide()
} else {
t.numberSlots[pos].Show()
t.numberSlots[pos-1].Hide()
}
// make sure we don't set the bit for the actual number
num := 0
for i := 0; i < t.Size*2; i += 2 {
if t.numberSlots[i].hidden {
num |= (1 << (3 - (i / 2)))
}
}
if num == t.Number {
n++
}
}
}
t.hidden = false
}
func (t *TileSet) VerifyNumber() {
num := 0
for i := 0; i < t.Size*2; i += 2 {
if t.numberSlots[i].hidden {
num |= (1 << (3 - (i / 2)))
}
}
// If correct number, add points based on how many bits are set.
// More bits => more flips => bigger reward
if num == t.Number {
t.Hide()
n := t.Number
score := 0
for n > 0 {
score += n & 1
n >>= 1
}
t.gh.mode.AddScore(score)
t.gh.sound.Play("tile")
}
}
func (t *TileSet) Update(dt float64) {
if t.hidden || t.gh.mode.gameOver {
return
}
// Check for collissions
if t.tile.fy <= 0.036 {
return
}
for _, o := range t.gh.tiles {
if o.hidden {
continue
}
if o.id == t.id {
continue
}
if t.tile.fy-0.115 < o.tile.fy && o.tile.fy < t.tile.fy {
t.Speed = o.Speed
return
}
}
for i := range t.Sprites {
t.Sprites[i].ChangeY(-float32(t.Speed * dt))
}
for i := range t.numberSlots {
t.numberSlots[i].ChangeY(-float32(t.Speed * dt))
}
}
func (t *TileSet) GameOver() {
t.gh.sound.Play("gameover")
for i := range t.Sprites {
t.Sprites[i].ChangeEffect(EffectGameOver)
}
for i := range t.numberSlots {
t.numberSlots[i].ChangeEffect(EffectGameOver)
}
}