-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
mob.go
296 lines (254 loc) · 6.16 KB
/
mob.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
package main
import (
"fmt"
"image"
"math"
"math/rand"
"github.com/faiface/pixel"
)
type mob struct {
phys
graphics
life float64
carry interface{}
ai *ai
drawFunc func(dt, elapsed float64)
hpBar *pixel.Sprite
hpImg *pixel.PictureData
maxLife float64
}
// Create mob
func (m *mob) create(x, y float64) {
if m.ai != nil {
m.ai.create(m)
}
m.jumpPower = 200
if m.speed == 0 {
m.speed = 200
}
m.mass = 20
m.dir = 1
m.life = m.maxLife
// Create HP Bar
var img image.Image
img, _, _, _ = loadTexture(fmt.Sprintf("%v%v", wAssetMixedPath, "hpbar.png"))
m.hpImg = pixel.PictureDataFromImage(img)
m.hpBar = pixel.NewSprite(m.hpImg, pixel.R(0, 0, 40, 5))
// Initiate the graphics for the mob
m.createGfx(x, y, false)
m.createPhys(x, y, m.frameWidth, m.frameHeight)
m.graphics.scalexy = m.phys.scale
// Set entity type for bounds.
m.bounds.entity = m
}
func (m *mob) hit(posX, posY, vx, vy float64, power int) {
// Create some text above the mob.
m.graphics.hitTexts = append(m.graphics.hitTexts, &hitText{global.gFont.write(fmt.Sprintf("-%v", power*2)), 3.0})
pow := float64(power)
// If distance is close, explode, otherwise push away.
dist := distance(pixel.Vec{X: posX + pow/2, Y: posY + pow/2}, pixel.Vec{X: m.bounds.X + m.bounds.Width/2, Y: m.bounds.Y + m.bounds.Height/2})
if dist < float64(power*2) {
// If carry somerhing, hit that first!
if m.carry != nil {
switch item := m.carry.(type) {
case *weapon:
item.hit(posX, posY, vx, vy, power)
return
case *object:
item.hit(posX, posY, vx, vy, power)
return
}
}
x := int(math.Abs(float64(m.bounds.X - posX)))
y := int(math.Abs(float64(m.bounds.Y - posY)))
// Gfx update
m.hitGfx(x, y, posX, posY, vx, vy, power, true)
// Blood effect
global.gParticleEngine.effectBlood(posX, posY, vx, vy, 1)
m.setLife(-float64(power * 2))
} else {
if vx == 0 && vy == 0 {
if posX < m.bounds.X {
m.dir = 1
vx = pow
} else {
vx = -pow
m.dir = -1
}
}
// Temprorary throwable (in order for shockwave effect)
// Resets in move function
m.phys.throwable = true
m.phys.velo.Y += math.Abs(pow * 5 / dist)
m.phys.velo.X += m.dir * pow * 5 / dist
}
}
func (m *mob) setLife(change float64) {
m.life += change
if m.life > m.maxLife {
m.life = m.maxLife
}
m.hpBar.Set(m.hpImg, pixel.R(0, 0, 40*(m.life/100), 5))
if m.life <= 0 {
m.die()
}
}
// Shoot if weapon attached
func (m *mob) shoot() {
if m.carry != nil {
switch item := m.carry.(type) {
case *weapon:
item.shoot()
m.currentAnim = animShoot
}
}
}
func (m *mob) pickup() {
// Check if anything to pickup?
var obj objectInterface
for _, v := range global.gWorld.qt.RetrieveIntersections(m.bounds) {
switch i := v.entity.(type) {
case *object:
obj = i
case *item:
obj = i
case *explosive:
obj = i
case *weapon:
obj = i
}
}
if obj != nil {
if obj.isFree() && m.carry == nil {
if obj.getType() != itemPowerupHealth {
m.carry = obj
}
obj.setOwner(m)
}
}
}
func (m *mob) action() {
for _, v := range global.gWorld.qt.RetrieveIntersections(m.bounds) {
switch i := v.entity.(type) {
case *item:
i.action(m)
return
}
}
// Check if close to doors
for _, p := range global.gWorld.doors {
d := distance(p, pixel.Vec{X: m.bounds.X, Y: m.bounds.Y})
if d < 10 {
// Get a random new door position for player.
pos := global.gWorld.doors[rand.Intn(len(global.gWorld.doors)-1)]
m.bounds.X = pos.X
m.bounds.Y = pos.Y
break
}
}
}
// Throw/drop object
func (m *mob) throw() {
if m.carry != nil {
switch item := m.carry.(type) {
case *object:
item.removeOwner()
case *weapon:
item.removeOwner()
case *item:
item.removeOwner()
case *explosive:
item.removeOwner()
}
m.carry = nil
}
}
// Die
func (m *mob) die() {
// Drop weapon
m.throw()
m.life = 0
m.explodeGfx(m.bounds.X, m.bounds.Y, true)
global.gWorld.qt.Remove(m.bounds)
}
func (m *mob) move(x, y float64) {
m.phys.keyMove.X = x
m.phys.keyMove.Y = y
if x != 0 {
m.phys.throwable = false
if x > 0 {
m.dir = 1
} else {
m.dir = -1
}
}
}
func (m *mob) getPosition() pixel.Vec {
return pixel.Vec{X: m.bounds.X, Y: m.bounds.Y}
}
func (m *mob) draw(dt, elapsed float64) {
shooting := false
if m.currentAnim == animShoot {
shooting = true
}
for _, c := range m.graphics.hitTexts {
if c.ttl >= 0 {
c.canvas.Draw(global.gWin, pixel.IM.Scaled(pixel.ZV, 0.1*c.ttl).Moved(pixel.V(m.bounds.X+10, m.bounds.Y+m.bounds.Height+10)))
c.ttl -= dt * 2
}
}
// Update physics & AI
m.physics(dt)
if m.climbing {
m.currentAnim = animClimb
} else if m.jumping {
m.currentAnim = animJump
} else if m.velo.X != 0 {
m.currentAnim = animWalk
} else {
m.currentAnim = animIdle
}
if m.ai != nil {
go func() {
m.ai.update(dt, elapsed)
m.hitRightWall = false
m.hitLeftWall = false
}()
}
if shooting {
m.currentAnim = animShoot
}
// Update animation
m.animCounter += dt
idx := int(math.Floor(m.animCounter / m.animRate))
switch m.currentAnim {
case animWalk:
idx = m.walkFrames[idx%len(m.walkFrames)]
case animJump:
idx = m.jumpFrames[idx%len(m.jumpFrames)]
case animClimb:
idx = m.climbFrames[idx/2%len(m.climbFrames)]
case animShoot:
idx = m.shootFrames[idx%len(m.shootFrames)]
case animIdle:
idx = m.idleFrames[idx/30%len(m.idleFrames)]
default:
idx = m.idleFrames[idx/30%len(m.idleFrames)]
}
// reset animation
if m.currentAnim != animClimb && m.currentAnim != animJump {
m.currentAnim = animIdle
}
//m.batches[idx].SetMatrix(pixel.IM.ScaledXY(pixel.ZV, pixel.V(-m.dir, 1)).Moved(pixel.V(m.bounds.X+m.bounds.Width/2, m.bounds.Y+m.bounds.Height/2)))
m.canvas.Clear(pixel.RGBA{R: 0, G: 0, B: 0, A: 0})
//m.canvas.SetComposeMethod(pixel.ComposeOver)
//m.canvas.SetColorMask(pixel.Alpha(1))
m.batches[idx].Draw(m.canvas)
// Draw hpbar
m.hpBar.Draw(global.gWin, pixel.IM.Scaled(pixel.ZV, 0.3).Moved(pixel.V(m.bounds.X+m.bounds.Width/2, m.bounds.Y+m.bounds.Height+5)))
m.canvas.Draw(global.gWin, (pixel.IM.ScaledXY(pixel.ZV, pixel.V(-m.dir, 1)).Moved(pixel.V(m.bounds.X+m.bounds.Width/2, m.bounds.Y+m.bounds.Height/2))))
// Call custom draw
if m.drawFunc != nil {
m.drawFunc(dt, elapsed)
}
}