/
monsters.go
439 lines (364 loc) · 7.13 KB
/
monsters.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package main
import (
"sdl"
"fmt"
"math"
)
func AOSDNOASDNOIASND() {
fmt.Println("ASDOASDIOANSDSA")
}
type Monster interface {
Recter
Move()
Damage(int) bool
Blit(surface *sdl.Surface)
Attack() (*EnemyLaser, bool)
}
//Group of monsters
type Monsters []Monster
func (m Monsters) Blit(surface *sdl.Surface) {
for i := 0; i < len(m); i++ {
m[i].Blit(surface)
}
}
func (m Monsters) Move() {
for i := 0; i < len(m); i++ {
m[i].Move()
}
}
func (m Monsters) Attack() []*EnemyLaser {
l := []*EnemyLaser{}
for i := 0; i < len(m); i++ {
out, ok := m[i].Attack()
if ok {
l = append(l, out)
}
}
return l
}
func (m Monsters) Remove(i int) Monsters {
return append(m[0:i], m[i+1:len(m)]...)
}
//GHOST
type MonsterGhost struct {
X float64
Y float64
W float64
H float64
HP int
Speed float64
GotHit int64
Waypoints []Vector64
Waypoint int
}
func NewMonsterGhost(x, y, speed float64, Waypoints []Vector64) *MonsterGhost {
m := new(MonsterGhost)
m.X = x
m.Y = y
m.HP = 10
m.Speed = speed
m.Waypoints = append([]Vector64{Vector64{x,y}}, Waypoints...)
return m
}
func NewGhostJSON(m JSONmonster) *MonsterGhost {
return NewMonsterGhost(m.X,m.Y,m.Speed,m.Waypoints)
}
func (m *MonsterGhost) Attack() (*EnemyLaser, bool) {
return nil, false
}
func (m *MonsterGhost) Damage(dmg int) bool {
m.HP -= dmg
if m.HP <= 0 {
return true
}
m.GotHit = now()+50e6
return false
}
func (m *MonsterGhost) Blit(surface *sdl.Surface) {
m.H = float64(IM.Monsters[0][0].H)
m.W = float64(IM.Monsters[0][0].W)
if difftime(m.GotHit) > 0 {
surface.Blit(&sdl.Rect{int16(m.X - m.W/2), int16(m.Y - m.H/2), 0, 0}, IM.Monsters[0][0], nil)
}
}
func (m *MonsterGhost) GetRect() Rect {
return Rect{m.X, m.Y, m.W, m.H}
}
func (m *MonsterGhost) Move() {
if len(m.Waypoints) < 2 {
return
}
if m.X < m.Waypoints[m.Waypoint].X {
if m.Waypoints[m.Waypoint].X - m.X < m.Speed/fps() {
m.X += m.Waypoints[m.Waypoint].X - m.X
} else {
m.X += m.Speed/fps()
}
}
if m.X > m.Waypoints[m.Waypoint].X {
if -m.Waypoints[m.Waypoint].X + m.X < m.Speed/fps() {
m.X -= -m.Waypoints[m.Waypoint].X + m.X
} else {
m.X -= m.Speed/fps()
}
}
if m.Y < m.Waypoints[m.Waypoint].Y {
if m.Waypoints[m.Waypoint].Y - m.Y < m.Speed/fps() {
m.Y += m.Waypoints[m.Waypoint].Y - m.Y
} else {
m.Y += m.Speed/fps()
}
}
if m.Y > m.Waypoints[m.Waypoint].Y {
if m.Y - m.Waypoints[m.Waypoint].Y < m.Speed/fps() {
m.Y -= m.Y - m.Waypoints[m.Waypoint].Y
} else {
m.Y -= m.Speed/fps()
}
}
if m.Y == m.Waypoints[m.Waypoint].Y && m.X == m.Waypoints[m.Waypoint].X {
if m.Waypoint == len(m.Waypoints)-1 {
m.Waypoint = 0
} else {
m.Waypoint++
}
}
}
///DEATH SHELL
const (
DS_SPEED = 400
)
const (
DS_NORMAL = iota
DS_ISBOSS
)
const (
DS_LEFT = iota
DS_RIGHT
)
type Deathshell struct {
X float64
Y float64
W float64
H float64
HP int
GotHit int64
CoolDown int64
Reloading int
//Boss Specifics
Speed float64
IsBoss int
Direction int
}
func NewDeathshell(x, y, speed float64, boss int) *Deathshell {
d := new(Deathshell)
d.X = x
d.Y = y
d.Speed = speed
d.IsBoss = boss
if boss == DS_ISBOSS {
d.HP = 50
} else {
d.HP = 20
}
return d
}
func NewDeathShellJSON(m JSONmonster) *Deathshell {
return NewDeathshell(m.X, m.Y, m.Speed, m.IsBoss)
}
func (d *Deathshell) Damage(dmg int) bool {
d.HP -= dmg
if d.HP <= 0 {
return true
}
d.GotHit = now()+50e6
return false
}
func (l *Deathshell) Attack() (*EnemyLaser, bool) {
if difftime(l.CoolDown) < 0 {
return nil, false
}
l.CoolDown = now()+300e6
l.Reloading++
if l.Reloading == 4 {
l.Reloading = 0
l.CoolDown = now()+3e9
}
diff := Vector64{l.X-player.X, l.Y-player.Y}
c := diff.Length()
if diff.Y < 0 {
n := NewEnemyLaser(l.X, l.Y, Vector64{-diff.X/c*DS_SPEED, math.Sin(math.Acos(diff.X/c))*DS_SPEED})
return n , true
} else {
n := NewEnemyLaser(l.X, l.Y, Vector64{-diff.X/c*DS_SPEED, -math.Sin(math.Acos(diff.X/c))*DS_SPEED})
return n, true
}
return nil, false
}
func (d *Deathshell) Blit(surface *sdl.Surface) {
var img *sdl.Surface
if d.IsBoss == DS_ISBOSS {
img = IM.Monsters[1][1+d.Direction]
} else {
img = IM.Monsters[1][0]
}
d.H = float64(img.H)
d.W = float64(img.W)
if difftime(d.GotHit) > 0 {
surface.Blit(&sdl.Rect{int16(d.X - d.W/2), int16(d.Y - d.H/2), 0, 0}, img, nil)
}
}
func (d *Deathshell) GetRect() Rect {
return Rect{d.X, d.Y, d.W, d.H}
}
func (d *Deathshell) Move() {
if d.IsBoss == DS_NORMAL {
return
}
if player.X > d.X {
d.X += d.Speed/fps()
d.Direction = DS_RIGHT
}
if player.X < d.X {
d.X -= d.Speed/fps()
d.Direction = DS_LEFT
}
if player.Y > d.Y{
d.Y += d.Speed/fps()
}
if player.Y < d.Y{
d.Y -= d.Speed/fps()
}
}
//ROBOT
const (
ROBOT_ISBOSS = 2
ROBOT_NORMAL = 0
)
const (
ROBOT_LEFT = iota
ROBOT_RIGHT
)
type Robot struct {
X float64
Y float64
W float64
H float64
HP int
Speed float64
GotHit int64
IsBoss int
Direction int
}
func NewRobot(x, y, speed float64, boss int) *Robot {
d := new(Robot)
d.X = x
d.Y = y
d.IsBoss = boss
d.Speed = speed
if boss == ROBOT_ISBOSS {
d.HP = 50
} else {
d.HP = 15
}
return d
}
func NewRobotJSON(m JSONmonster) *Robot {
return NewRobot(m.X,m.Y,m.Speed,m.IsBoss)
}
func (r *Robot) Damage(dmg int) bool {
r.HP -= dmg
if r.HP <= 0 {
return true
}
r.GotHit = now()+50e6
return false
}
func (l *Robot) Attack() (*EnemyLaser, bool) {
return nil, false
}
func (r *Robot) Blit(surface *sdl.Surface) {
img := IM.Monsters[2][r.Direction+r.IsBoss]
r.H = float64(img.H)
r.W = float64(img.W)
if difftime(r.GotHit) > 0 {
surface.Blit(&sdl.Rect{int16(r.X - r.W/2), int16(r.Y - r.H/2), 0, 0}, img, nil)
}
}
func (r *Robot) GetRect() Rect {
return Rect{r.X, r.Y, r.W, r.H}
}
func (r *Robot) Move() {
if player.X > r.X {
r.X += r.Speed/fps()
r.Direction = ROBOT_RIGHT
}
if player.X < r.X {
r.X -= r.Speed/fps()
r.Direction = ROBOT_LEFT
}
if player.Y > r.Y{
r.Y += r.Speed/fps()
}
if player.Y < r.Y{
r.Y -= r.Speed/fps()
}
}
//MALBORO
const MAL_SPEED = 400
type Malboro struct {
X float64
Y float64
W float64
H float64
HP int
GotHit int64
CoolDown int64
Reloading int
Direction float64 //radianer
}
func NewMalboro(x, y float64) *Malboro {
d := new(Malboro)
d.X = x
d.Y = y
d.HP = 30
return d
}
func NewMalboroJSON(m JSONmonster) *Malboro {
return NewMalboro(m.X,m.Y)
}
func (d *Malboro) Damage(dmg int) bool {
d.HP -= dmg
if d.HP <= 0 {
return true
}
d.GotHit = now()+50e6
return false
}
func (l *Malboro) Attack() (*EnemyLaser, bool) {
if difftime(l.CoolDown) < 0 {
return nil, false
}
l.CoolDown = now()+300e6
l.Reloading++
if l.Reloading == 11 {
l.Reloading = 0
l.CoolDown = now()+3e9
}
n := NewEnemyLaser(l.X, l.Y, Vector64{MAL_SPEED*math.Cos(l.Direction), MAL_SPEED*math.Sin(l.Direction)})
return n, true
}
func (d *Malboro) Blit(surface *sdl.Surface) {
img := IM.Monsters[3][0]
d.H = float64(img.H)
d.W = float64(img.W)
if difftime(d.GotHit) > 0 {
surface.Blit(&sdl.Rect{int16(d.X - d.W/2), int16(d.Y - d.H/2), 0, 0}, img, nil)
}
}
func (d *Malboro) GetRect() Rect {
return Rect{d.X, d.Y, d.W, d.H}
}
func (d *Malboro) Move() {
d.Direction += math.Pi/2/fps()
}