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
|
package modernworld
import (
"math/rand"
"time"
tl "github.com/JoelOtter/termloop"
)
type AlienCluster struct {
Aliens [][]*Alien
Lasers []*Laser
TimeToMove float64
WaitingTime float64
WaitingTimeToMoveNextRow float64
CurrentRowMoving int
Direction int
MoveSize int
IsMoving bool
IsMovingDown bool
IsAllDead bool
ReachedEndArena bool
}
func NewAlienCluster() *AlienCluster {
lineSize := 13
alienCluster := AlienCluster{
TimeToMove: 0,
WaitingTime: 1,
WaitingTimeToMoveNextRow: 0.07,
CurrentRowMoving: -1,
Direction: 3,
MoveSize: 3,
IsMoving: false,
ReachedEndArena: false,
IsMovingDown: false,
}
alienCluster.Aliens = append(alienCluster.Aliens, CreateAliensLine(Strong, lineSize))
alienCluster.Aliens = append(alienCluster.Aliens, CreateAliensLine(Medium, lineSize))
alienCluster.Aliens = append(alienCluster.Aliens, CreateAliensLine(Medium, lineSize))
alienCluster.Aliens = append(alienCluster.Aliens, CreateAliensLine(Basic, lineSize))
alienCluster.Aliens = append(alienCluster.Aliens, CreateAliensLine(Basic, lineSize))
return &alienCluster
}
func (alienCluster *AlienCluster) UpdateAliensPositions(timeDelta float64, arena *Arena) {
alienCluster.prepareMovement()
if alienCluster.canMove(timeDelta) {
alienCluster.move(arena)
}
}
func (alienCluster *AlienCluster) prepareMovement() {
if alienCluster.CurrentRowMoving == -1 {
alienCluster.CurrentRowMoving = len(alienCluster.Aliens) - 1
alienCluster.IsMoving = false
}
if alienCluster.ReachedEndArena {
alienCluster.changeDirection()
}
}
func (alienCluster *AlienCluster) changeDirection() {
alienCluster.Direction *= -1
alienCluster.ReachedEndArena = false
}
func (alienCluster *AlienCluster) canMove(timeDelta float64) bool {
alienCluster.TimeToMove += timeDelta
if alienCluster.isWaitingToMoveFirstRow() || alienCluster.isWaitingToMoveNextRow() {
return false
}
return true
}
func (alienCluster *AlienCluster) isWaitingToMoveFirstRow() bool {
return alienCluster.IsMoving == false && alienCluster.TimeToMove < alienCluster.WaitingTime
}
func (alienCluster *AlienCluster) isWaitingToMoveNextRow() bool {
return alienCluster.IsMoving && alienCluster.TimeToMove < alienCluster.WaitingTimeToMoveNextRow
}
func (alienCluster *AlienCluster) move(arena *Arena) {
alienCluster.TimeToMove = 0
alienCluster.IsMoving = true
if alienCluster.IsMovingDown {
alienCluster.moveDown()
} else {
alienCluster.moveSideways(arena)
}
alienCluster.CurrentRowMoving -= 1
}
func (alienCluster *AlienCluster) moveDown() {
row := alienCluster.getCurrentLine()
for _, alien := range row {
x, y := alien.Position()
alien.SetPosition(x, y+alienCluster.MoveSize)
}
finishedMovingRows := alienCluster.CurrentRowMoving == 0
if finishedMovingRows {
alienCluster.IsMovingDown = false
}
}
func (alienCluster *AlienCluster) moveSideways(arena *Arena) {
row := alienCluster.getCurrentLine()
for _, alien := range row {
x, y := alien.Position()
w, _ := alien.Size()
x = x + alienCluster.Direction
alien.SetPosition(x, y)
if alien.IsAlive {
alienCluster.checkIfReachedEndOfArena(x, w, arena)
}
}
}
func (alienCluster *AlienCluster) getCurrentLine() []*Alien {
row := alienCluster.Aliens[alienCluster.CurrentRowMoving]
return row
}
func (alienCluster *AlienCluster) checkIfReachedEndOfArena(x int, w int, arena *Arena) {
if alienCluster.CurrentRowMoving == 0 && (x+w >= arena.End-alienCluster.MoveSize || x <= arena.Init+alienCluster.MoveSize) {
alienCluster.ReachedEndArena = true
alienCluster.IsMovingDown = true
}
}
func (alienCluster *AlienCluster) RemoveDeadAliensAndGetPoints(level *tl.BaseLevel) int {
hasAtLeastOneAlive := false
points := 0
for _, alienRow := range alienCluster.Aliens {
for _, alien := range alienRow {
if alien.IsAlive == false && alien.IsRendered == true {
points += alien.Points
alien.IsRendered = false
level.RemoveEntity(alien)
}
if alien.IsAlive == true {
hasAtLeastOneAlive = true
}
}
}
alienCluster.IsAllDead = !hasAtLeastOneAlive
return points
}
func (alienCluster *AlienCluster) Shoot() {
if alienCluster.canShoot() {
alien := alienCluster.selectRandomAlien()
if alien == nil {
return
}
x, y := alien.Position()
width, _ := alien.Size()
alienGunPosition := x + (width-1)/2
distanceToAlien := y + 2
laser := NewAlienLaser(alienGunPosition, distanceToAlien)
alienCluster.Lasers = append(alienCluster.Lasers, laser)
}
}
func (alienCluster *AlienCluster) canShoot() bool {
return len(alienCluster.Lasers) == 0
}
func (alienCluster *AlienCluster) selectRandomAlien() *Alien {
var shooterAlien *Alien
rowSize := len(alienCluster.Aliens) - 1
col := alienCluster.selectRandomAlienColumn()
for row := rowSize; row >= 0; row-- {
alien := alienCluster.Aliens[row][col]
if alien.IsAlive {
shooterAlien = alien
break
}
}
return shooterAlien
}
func (alienCluster *AlienCluster) selectRandomAlienColumn() int {
rand.Seed(time.Now().UnixNano())
col := rand.Intn(len(alienCluster.Aliens[0]))
return col
}
func (alienCluster *AlienCluster) IsAllAliensDead() bool {
return alienCluster.IsAllDead
}
|