-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
308 lines (255 loc) · 6.56 KB
/
common.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
package main
import (
"math"
"math/rand"
"rapidengine/child"
"rapidengine/material"
"rapidengine/ui"
)
// --------------------------------------------------
// ENEMY COMPONENTS
// --------------------------------------------------
type Common struct {
// Engine components
MonsterChild *child.Child2D
MonsterMaterial *material.BasicMaterial
// Monster Data
Damage float32
Health float32
MaxHealth float32
Dead bool
// Hitboxes
Hitbox1 Hitbox
aHitbox AABB
// Movement
VXMult float32
VYMult float32
GravMult float32
Direction int
TargetX float32
TargetY float32
// Temp state
State string
CurrentAnim string
AttackTimeout float64
// Health bar
HealthBar *ui.ProgressBar
HOffsetX float32
HOffsetY float32
NumJumps int
}
func (c *Common) Update() {
c.UpdateState()
c.UpdateMovement()
c.UpdateAnimations()
c.HealthBar.SetPercentage(c.Health / c.MaxHealth * 100)
c.HealthBar.Update(nil)
Engine.Renderer.RenderChild(c.HealthBar.BackChild)
Engine.Renderer.RenderChild(c.HealthBar.BarChild)
}
// UpdateState handles state changes
func (c *Common) UpdateState() {
// State updates
if c.State == "idleing" {
}
if c.State == "chasing" {
}
if c.State == "attacking" {
}
if c.State == "retreating" {
}
// State changes
dist := Distance(c.MonsterChild.X, c.MonsterChild.Y, Player1.PlayerChild.X, Player1.PlayerChild.Y)
if dist < 100 && c.State == "idleing" {
c.State = "chasing"
}
if dist > 500 {
c.State = "idleing"
}
}
// UpdateMovement handles world collision detection
// and moving the mob to a target point, as well as
// the health bar
func (c *Common) UpdateMovement() {
// Update position
c.MonsterChild.X += c.MonsterChild.VX * -float32(Engine.Renderer.DeltaFrameTime)
c.MonsterChild.Y += c.MonsterChild.VY * float32(Engine.Renderer.DeltaFrameTime)
// Update collision data
hx := c.MonsterChild.X + (c.MonsterChild.ScaleX / 2) - (c.Hitbox1.DAABB.Width / 2) + c.Hitbox1.OffX
hy := c.MonsterChild.Y + (c.MonsterChild.ScaleY / 2) - (c.Hitbox1.LAABB.Height / 2) + c.Hitbox1.OffY
c.Hitbox1.X = hx
c.Hitbox1.Y = hy
top, left, bottom, right, topleft, topright := CheckWorldCollision(c.Hitbox1, c.MonsterChild.VX, c.MonsterChild.VY, hx, hy)
// Update health bar
camX, camY, _ := Engine.Renderer.MainCamera.GetPosition()
c.HealthBar.SetPosition(hx+c.HOffsetX-camX+(float32(Engine.Config.ScreenWidth)/2), hy+c.HOffsetY-camY+(float32(Engine.Config.ScreenHeight)/2))
// Update attack hitboxes
flip := c.MonsterMaterial.Flipped
if flip == 0 {
flip = 1
} else {
flip = 0
}
c.aHitbox.X = (hx + (c.Hitbox1.DAABB.Width / 2)) + (c.aHitbox.OffX+c.aHitbox.Width)*float32(flip-1)
c.aHitbox.Y = hy + c.aHitbox.OffY
// Move toward target
dx := c.TargetX - c.MonsterChild.X
//dy := c.TargetY - c.MonsterChild.Y
if dx > 25 || dx < -25 {
if dx > 25 {
c.Direction = 1
} else {
c.Direction = -1
}
c.MonsterChild.VX = float32(c.Direction) * BaseSpeedX * c.VXMult
}
// World collision
if bottom {
c.NumJumps = 1
c.MonsterChild.VY = 0
} else {
c.MonsterChild.VY -= BaseGravity * float32(Engine.Renderer.DeltaFrameTime) * c.VYMult
}
if top && c.MonsterChild.VY > 10 {
c.MonsterChild.VY = 0
}
// Auto jumping
if (right || topright) && c.MonsterChild.VX < -50 {
c.MonsterChild.VX = 0
c.Jump()
}
if (left || topleft) && c.MonsterChild.VX > 50 {
c.MonsterChild.VX = 0
c.Jump()
}
// Testing movement
/*if Inputs.Keys["up"] && c.NumJumps > 0 {
c.Jump()
}
if Inputs.Keys["left"] {
c.MonsterChild.VX = 50
} else if Inputs.Keys["right"] {
c.MonsterChild.VX = -50
} else {
c.MonsterChild.VX = 0
}*/
if Inputs.Keys["i"] {
c.Hitbox1.OffY += 1
println(c.Hitbox1.OffX, c.Hitbox1.OffY)
}
if Inputs.Keys["k"] {
c.Hitbox1.OffY -= 1
println(c.Hitbox1.OffX, c.Hitbox1.OffY)
}
if Inputs.Keys["j"] {
c.Hitbox1.OffX += 1
println(c.Hitbox1.OffX, c.Hitbox1.OffY)
}
if Inputs.Keys["l"] {
c.Hitbox1.OffX -= 1
println(c.Hitbox1.OffX, c.Hitbox1.OffY)
}
}
func (c *Common) UpdateAttacking() {
dx := c.MonsterChild.X - Player1.PlayerChild.X
absdx := math.Abs(float64(dx))
if absdx < 100 && c.State == "normal" {
if c.AttackTimeout < 0 {
c.FacePlayer()
c.Attack()
c.AttackTimeout = 10
}
}
c.AttackTimeout -= Engine.Renderer.DeltaFrameTime
}
// UpdateAnimations plays the appropriate animation
// based on the state of the mob
func (c *Common) UpdateAnimations() {
if c.MonsterChild.VX > 0 {
c.MonsterMaterial.Flipped = 1
} else if c.MonsterChild.VX < 0 {
c.MonsterMaterial.Flipped = 0
}
if c.State == "attacking" {
return
}
if c.MonsterChild.VX > 0 && c.NumJumps > 0 && c.CurrentAnim != "walk" {
c.MonsterMaterial.PlayAnimation("walk")
c.CurrentAnim = "walk"
}
if c.MonsterChild.VX < 0 && c.NumJumps > 0 && c.CurrentAnim != "walk" {
c.MonsterMaterial.PlayAnimation("walk")
c.CurrentAnim = "walk"
}
if c.MonsterChild.VX == 0 && c.NumJumps > 0 && c.CurrentAnim != "idle" {
c.MonsterMaterial.PlayAnimation("idle")
c.CurrentAnim = "idle"
}
}
// --------------------------------------------------
// Helper functions
// --------------------------------------------------
func (c *Common) MoveTo(x, y float32) {
c.TargetX = x
c.TargetY = y
}
func (c *Common) Jump() {
if c.NumJumps > 0 {
c.NumJumps--
c.MonsterChild.VY = BaseSpeedY * c.VYMult
c.MonsterMaterial.PlayAnimationOnce("jump")
c.CurrentAnim = "jump"
}
}
func (c *Common) FacePlayer() {
dx := c.MonsterChild.X - Player1.PlayerChild.X
if dx < 0 {
}
}
func (c *Common) Attack() {
c.State = "attacking"
c.CurrentAnim = "attacking"
c.MonsterMaterial.PlayAnimationOnceCallback("attack", c.DoneHitting, c.AttackHitFrame)
}
func (c *Common) AttackHitFrame() {
if c.CheckPlayerCollision() {
Player1.Hit(25)
}
}
func (c *Common) Kill() {
Player1.Money += rand.Intn(4) + 2
// c.MonsterMaterial.PlayAnimationOnceCallback("die", c.SetDead, nil) when we have dying anim's in
c.SetDead() // temporary
}
func (c *Common) SetDead() {
c.Dead = true
}
func (c *Common) DoneHitting() {
c.State = "normal"
}
func (c *Common) CheckPlayerCollision() bool {
if c.aHitbox.CheckCollision(Player1.FullBox, 0, 0) {
return true
}
return false
}
// --------------------------------------------------
// Move this somewhere else eventually
// --------------------------------------------------
type Activator struct {
active bool
}
func (a *Activator) Activate() {
a.active = true
}
func (a *Activator) Deactivate() {
a.active = false
}
func (a *Activator) IsActive() bool {
return a.active
}
func Distance(x1, y1, x2, y2 float32) float32 {
return float32(math.Sqrt(
float64((x2-x1)*(x2-x1) + (y2-y2)*(y2-y1)),
))
}