/
objects.go
376 lines (311 loc) · 6.38 KB
/
objects.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
package main
import (
"sdl"
"fmt"
"rand"
)
func AOSDNOASDNOIASND2() {
fmt.Println("ASDOASDIOANSDSA")
}
type Object interface {
Recter
HitPlayer(*Player)
Blit(*sdl.Surface)
Move()
IsDead() bool
}
type Objects []Object
func (o Objects) Blit(surface *sdl.Surface) {
for i := 0; i < len(o); i++ {
o[i].Blit(surface)
}
}
func (o Objects) Move() {
for i := 0; i < len(o); i++ {
o[i].Move()
}
}
func (o Objects) Remove(i int) Objects {
return append(o[0:i], o[i+1:len(o)]...)
}
func (o Objects) Hit(p *Player) {
for i := 0; i < len(o); i++ {
o[i].HitPlayer(p)
}
}
func (o Objects) Clean() Objects {
for i := 0; i < len(o); i++ {
if o[i].IsDead() {
o = o.Remove(i)
i--
}
}
return o
}
//Teleport
const (
TELE_VERTICAL = iota
TELE_HORIZONTAL
)
type Teleport struct {
X, Y float64
W, H float64
S, O int //Size, retning
Way Vector64
Boost float64
Conn *Teleport
Image *sdl.Surface
}
func NewTeleport(xy, way Vector64, s, o int, boost float64) *Teleport {
t := new(Teleport)
t.X = xy.X
t.Y = xy.Y
t.Way = way
t.S = s
t.O = o
t.Boost = boost
//Draw Stuff
if o == TELE_HORIZONTAL {
t.Image = IM.Obj[1]
t.H = float64(t.Image.H)
t.W = float64(s*BOXSIZE)
} else {
t.Image = IM.Obj[0]
t.W = float64(t.Image.W)
t.H = float64(s*BOXSIZE)
}
return t
}
func NewTeleportJSON(m JSONteleport) *Teleport {
o := 0
if m.Type == SO_TELEPORT_V {
o = TELE_HORIZONTAL
} else {
o = TELE_VERTICAL
}
return NewTeleport(Vector64{m.X,m.Y}, m.Way, m.Size, o, m.Boost)
}
func (t *Teleport) Blit(surface *sdl.Surface) {
if t.O == TELE_HORIZONTAL {
for x:=0; x<t.S*2; x++ {
surface.Blit(&sdl.Rect{int16(t.X - t.W/2)+int16(20*x), int16(t.Y - t.H/2), 0, 0}, t.Image, nil)
}
} else {
for x:=0; x<t.S*2; x++ {
surface.Blit(&sdl.Rect{int16(t.X - t.W/2) , int16(t.Y - t.H/2)+int16(20*x), 0, 0}, t.Image, nil)
}
}
}
func (t *Teleport) Connect(tele *Teleport) {
t.Conn = tele
}
func (t *Teleport) ConnectBoth(tele *Teleport) {
t.Conn = tele
tele.Conn = t
}
func (t *Teleport) GetRect() Rect {
return Rect{t.X, t.Y, t.W, t.H}
}
func (t *Teleport) HitPlayer(p *Player) {
if HitTest(t, p) {
if t.Conn == nil {
t.MoveTo(t, p)
} else {
t.MoveTo(t.Conn, p)
}
}
}
func (t *Teleport) Move() { //implement Object interface
}
func (t *Teleport) MoveTo(to *Teleport, player *Player) {
w := to.Way
p := &player.Velocity
//X-axis
switch {
case p.X <= 0 && w.X > 0:
p.X = -(p.X*t.Boost)
case p.X >= 0 && w.X > 0:
p.X = (p.X*t.Boost)
case p.X <= 0 && w.X < 0:
p.X = p.X*t.Boost
case p.X >= 0 && w.X < 0:
p.X = -(p.X*t.Boost)
}
//Y-Axis
switch {
case p.Y <= 0 && w.Y > 0:
p.Y = -(p.Y*t.Boost)
case p.Y >= 0 && w.Y > 0:
p.Y = (p.Y*t.Boost)
case p.Y <= 0 && w.Y < 0:
p.Y = p.Y*t.Boost
case p.Y >= 0 && w.Y < 0:
p.Y = -(p.Y*t.Boost)
}
player.X = to.X + to.Way.X*to.W + to.Way.X*player.W/2
player.Y = to.Y + to.Way.Y*to.H + to.Way.Y*player.H/2
}
func (s *Teleport) IsDead() bool { //Implements interface Objects
return false
}
//Spikes
type Spike struct {
X, Y float64
W, H float64
Speed float64
Waypoint int
Waypoints []Vector64
Restart bool //Teleport to start
}
func NewSpike(xy Vector64, speed float64, restart bool, wp []Vector64) *Spike {
s := new(Spike)
s.X = xy.X
s.Y = xy.Y
s.Speed = speed
s.Restart = restart
s.Waypoints = append([]Vector64{xy}, wp...)
s.W = float64(IM.Shots[2].W)
s.H = float64(IM.Shots[2].H)
return s
}
func NewSpikeJSON(m JSONobject) *Spike {
return NewSpike(Vector64{m.X,m.Y}, m.Speed, m.Restart, m.Waypoints)
}
func (s *Spike) Blit(surface *sdl.Surface) {
surface.Blit(&sdl.Rect{int16(s.X - s.W/2), int16(s.Y - s.H/2), 0, 0}, IM.Shots[2], nil)
}
func (s *Spike) GetRect() Rect {
return Rect{s.X, s.Y, s.W, s.H}
}
func (s *Spike) HitPlayer(p *Player) {
if !player.IsImmortal() && HitTest(s, p) {
p.WasHit()
}
}
func (m *Spike) 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 {
if m.Restart {
m.X = m.Waypoints[0].X
m.Y = m.Waypoints[0].Y
m.Waypoint = 0
} else {
m.Waypoint = 0
}
} else {
m.Waypoint++
}
}
}
func (s *Spike) IsDead() bool { //Implements interface Objects
return false
}
//Spoils of War
const (
SP_HEART = iota
SP_UPGRADE
)
type Spoils struct {
X, Y float64
W, H float64
Velocity Vector64
Type int
Frame int
Dead bool
}
type UpgradeAnimation struct {
frame int
}
func NewSpoils(x,y float64) *Spoils {
s := new(Spoils)
if rand.Float64() > 0.2 {
s.X, s.Y = -100,-100
s.Dead = true
return s
}
s.X = x
s.Y = y
s.Type = rand.Intn(2)
s.Velocity = Vector64{(rand.Float64()*10-5)*20,(rand.Float64()*10-5)*20}
switch s.Type {
case SP_HEART:
s.W = float64(IM.Misc[0].W)
s.H = float64(IM.Misc[0].H)
case SP_UPGRADE:
s.W = 10
s.H = 10
}
return s
}
func (s *Spoils) Blit(surface *sdl.Surface) {
switch s.Type {
case SP_HEART:
surface.Blit(&sdl.Rect{int16(s.X - s.W/2), int16(s.Y - s.H/2), 0, 0}, IM.Misc[0], nil)
case SP_UPGRADE:
surface.FillRect(&sdl.Rect{int16(s.X - s.W/2), int16(s.Y - s.H/2), 10, 10},uint32(s.Frame))
s.Frame = s.Frame*s.Frame+1
if s.Frame > 0xFFFFFF {
s.Frame = 0
}
}
}
func (s *Spoils) GetRect() Rect {
return Rect{s.X, s.Y, s.W, s.H}
}
func (s *Spoils) HitPlayer(p *Player) {
if HitTest(s, p) {
switch s.Type {
case SP_HEART:
p.Lives++
case SP_UPGRADE:
if p.Lvl < 3 {
p.Lvl++
}
}
s.Dead = true
}
}
func (s *Spoils) Move() {
s.X += s.Velocity.X/fps()
s.Y += s.Velocity.Y/fps()
if s.X > SCREENWIDTH+50 || s.X < -50 {
s.Dead = true
}
if s.Y > SCREENHEIGHT+50 || s.Y < -50 {
s.Dead = true
}
}
func (s *Spoils) IsDead() bool {
return s.Dead
}