-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbomb.go
246 lines (223 loc) · 6.12 KB
/
bomb.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"sync"
"time"
)
type Direction string
type VerticalDirection string
const (
TOP Direction = "TOP"
RIGHT Direction = "RIGHT"
LEFT Direction = "LEFT"
BOTTOM Direction = "BOTTOM"
TOPRIGHT Direction = "TOPRIGHT"
TOPLEFT Direction = "TOPLEFT"
BOTTOMRIGHT Direction = "BOTTOMRIGHT"
BOTTOMLEFT Direction = "BOTTOMLEFT"
)
const (
X VerticalDirection = "X"
Y VerticalDirection = "Y"
)
const BombEmoji rune = '💣'
const ExplosionEmoji rune = '💥'
type Coordinates struct {
x int
y int
}
type Bomb struct {
bombId int
currentX int
currentY int
distanceX int
distanceY int
xDonePercent float32
yDonePercent float32
direction Direction
initialX int
initialY int
finalX int
finalY int
lastDrawnPosition Coordinates
explodeIn time.Duration
isDead bool
hero *Hero
speed uint
}
func calculateDirection(xDifference, yDifference int) Direction {
switch true {
case xDifference == 0 && yDifference > 0:
return BOTTOM
case xDifference == 0 && yDifference < 0:
return TOP
case xDifference > 0 && yDifference == 0:
return RIGHT
case xDifference < 0 && yDifference == 0:
return LEFT
case xDifference > 0 && yDifference > 0:
return BOTTOMRIGHT
case xDifference > 0 && yDifference < 0:
return TOPRIGHT
case xDifference < 0 && yDifference > 0:
return BOTTOMLEFT
case xDifference < 0 && yDifference < 0:
return TOPLEFT
default:
return ""
}
}
// will be used later when we add options to the game (non-stop bombs options for exp)
func calculateFinalPosition(maxX, maxY, currentX, currentY, distanceX, distanceY int, direction Direction) (int, int) {
switch true {
case direction == TOP:
return currentX, 0
case direction == BOTTOM:
return currentX, maxY
case direction == RIGHT:
return maxX, currentY
case direction == LEFT:
return 0, currentY
case direction == TOPRIGHT:
leftForY := currentY
leftForX := maxX - currentX
if leftForY < leftForX {
timesToMultiply := leftForX / distanceX
return currentX + (distanceX * timesToMultiply), currentY - (distanceY * timesToMultiply)
}
timesToMultiply := leftForY / distanceY
return currentX + (distanceX * timesToMultiply), currentY - (distanceY * timesToMultiply)
default:
return 0, 0
}
}
func (b *Bomb) draw(ticker *time.Ticker, bombExploded chan string, explosionComplete chan struct{}, explosionWaitGroup *sync.WaitGroup) {
s := *b.hero.settings.screen
style := *b.hero.settings.theme
isExplosionCompleteChannelClosed := false
go func() {
// wait until bomb explodes
time.Sleep(b.explodeIn)
// send two signal to the bombExploded channel to hero and bomb itself
bombExploded <- ""
bombExploded <- ""
// Make the explosion animation stay for a second
time.Sleep(1 * time.Second)
// Finish the explosion animation
if !isExplosionCompleteChannelClosed {
close(explosionComplete)
}
}()
go func() {
for {
select {
case <-ticker.C:
if b.hero.isPaused && !isExplosionCompleteChannelClosed {
close(explosionComplete)
}
if b.lastDrawnPosition.x != 0 && b.lastDrawnPosition.y != 0 {
s.SetContent(b.lastDrawnPosition.x, b.lastDrawnPosition.y, ' ', nil, style)
}
if !b.isDead {
s.SetContent(b.currentX, b.currentY, BombEmoji, nil, style)
b.lastDrawnPosition.x = b.currentX
b.lastDrawnPosition.y = b.currentY
b.move()
} else {
s.SetContent(b.currentX, b.currentY, ExplosionEmoji, nil, style)
}
s.Show()
case <-bombExploded:
s.SetContent(b.currentX, b.currentY, ExplosionEmoji, nil, style)
b.lastDrawnPosition.x = b.currentX
b.lastDrawnPosition.y = b.currentY
explosionWaitGroup.Done()
explosionWaitGroup.Wait()
case <-explosionComplete:
isExplosionCompleteChannelClosed = true
s.SetContent(b.currentX, b.currentY, ' ', nil, style)
s.Show()
ticker.Stop()
return
}
}
}()
}
func (b *Bomb) calculateDonePercent(verticalDirection VerticalDirection) {
if verticalDirection == X {
// do not exceed the finalX
if b.xDonePercent >= 1 {
return
}
// calculate xDonePercent for RIGHT
if b.direction == RIGHT || b.direction == BOTTOMRIGHT || b.direction == TOPRIGHT {
takenDistance := b.currentX - b.initialX
b.xDonePercent = float32(takenDistance) / float32(b.distanceX)
return
}
// calculate xDonePercent for LEFT
takenDistance := (b.currentX - b.initialX) * -1
b.xDonePercent = float32(takenDistance) / float32(b.distanceX)
return
} else if verticalDirection == Y {
// do not exceed the finalY
if b.yDonePercent >= 1 {
return
}
// calculate yDonePercent for TOP
if b.direction == TOP || b.direction == TOPRIGHT || b.direction == TOPLEFT {
takenDistance := (b.currentY - b.initialY) * -1
b.yDonePercent = float32(takenDistance) / float32(b.distanceY)
return
}
// calculate yDonePercent for BOTTOM
takenDistance := b.currentY - b.initialY
b.yDonePercent = float32(takenDistance) / float32(b.distanceY)
return
}
}
func (b *Bomb) move() {
if b.xDonePercent == 0 && b.yDonePercent == 0 {
// First move
if b.distanceX > b.distanceY {
if b.direction == RIGHT || b.direction == BOTTOMRIGHT || b.direction == TOPRIGHT {
b.currentX++
b.calculateDonePercent(X)
} else {
b.currentX--
b.calculateDonePercent(X)
}
} else {
if b.direction == TOP || b.direction == TOPRIGHT || b.direction == TOPLEFT {
b.currentY--
b.calculateDonePercent(Y)
} else {
b.currentY++
b.calculateDonePercent(Y)
}
}
return
}
if b.xDonePercent >= 1 && b.yDonePercent >= 1 {
return
}
// Move in X direction
if b.yDonePercent >= b.xDonePercent {
if b.direction == RIGHT || b.direction == BOTTOMRIGHT || b.direction == TOPRIGHT {
b.currentX++
b.calculateDonePercent(X)
} else {
b.currentX--
b.calculateDonePercent(X)
}
return
} else if b.xDonePercent > b.yDonePercent {
// Move in Y direction
if b.direction == TOP || b.direction == TOPRIGHT || b.direction == TOPLEFT {
b.currentY--
b.calculateDonePercent(Y)
} else {
b.currentY++
b.calculateDonePercent(Y)
}
}
}