/
player.go
458 lines (400 loc) · 9.36 KB
/
player.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package game
import (
"image/color"
"log"
"time"
"github.com/Bredgren/game1/game/camera"
"github.com/Bredgren/geo"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
)
const (
playerMoveSpeed = 500
playerGravity = 50
playerJumpSpeed = 700
playerJumpTime = 500 * time.Millisecond
playerPunchTime = 200 * time.Millisecond
playerPunchGap = 100 * time.Millisecond
)
/*
awaken
* cannot do anything
-> normal
normal
* can move
* can jump, if on ground
* can punch
-> charge
-> uppercut
-> slam
charge
* cannot move
* cannot jump
* cannot punch
-> launch
launch
* cannot move
* cannot jump
* cannot punch
-> normal (after time limit or ground contact)
uppercut
* cannot move
* cannot jump
* cannot punch
-> normal (after time limit)
slam
* cannot move
* if on the ground then the character does a small jump first
* cannot jump
* cannot punch
-> normal (after time limit or ground contact)
death
<- any state
*/
type playerState int
const (
awaken playerState = iota
normal
// charge
// launchAttack
// uppercutAttack
// slamAttack
// death
)
type player struct {
cam *camera.Camera
pos geo.Vec
vel geo.Vec
left bool // Move left button is down
right bool // Move right button is down
move float64 // Gampad axis for movement
jump bool // Jump button is down
punch bool // Punch button is down
punchAxis geo.Vec
punchWithGamepad bool
launch bool // Launch button is down
canJump bool
isJumping bool
jumpTime time.Duration
flipDir bool
// punchTime time.Duration
// punchGap time.Duration
state playerState
// currentSprite *sprite.Sprite
// awakenSprite sprite.Sprite
// idleSprite sprite.Sprite
// moveSprite sprite.Sprite
// punchSprite sprite.Sprite
// chargeSprite sprite.Sprite
// launchSprite sprite.Sprite
coreHitbox hitbox
attackHitbox hitbox
}
func newPlayer(cam *camera.Camera) *player {
p := &player{
cam: cam,
canJump: true,
isJumping: false,
jumpTime: 0,
state: awaken,
// awakenSprite: sprite.Get("awaken"),
// idleSprite: sprite.Get("idle"),
// moveSprite: sprite.Get("move"),
// punchSprite: sprite.Get("punch"),
// chargeSprite: sprite.Get("charge"),
// launchSprite: sprite.Get("launch"),
}
// p.currentSprite = &p.idleSprite
p.coreHitbox = hitbox{
Label: "PlayerCore",
Bounds: geo.RectWH(2, 2),
Callback: p.coreHit,
Active: true,
Owner: &p,
}
p.attackHitbox = hitbox{
Label: "PlayerAttack",
Callback: p.attackHit,
Owner: &p,
}
return p
}
func (p *player) awaken() {
// p.currentSprite = &p.awakenSprite
// p.currentSprite.Begin(false)
p.state = awaken
}
func (p *player) awoke() bool {
// return p.awakenSprite.Ended()
return true
}
func (p *player) doNormal() {
p.state = normal
// p.currentSprite = &p.idleSprite
}
// func (p *player) shouldStartPunch() bool {
// p.punchWithGamepad = p.punchAxis.Len2() > 0.25
// return p.punchGap <= 0 && (p.punch || p.punchWithGamepad)
// }
//
// func (p *player) doPunch() {
// p.punch = false
// p.state = playerPunch
// p.currentSprite = &p.punchSprite
// p.punchTime = playerPunchTime
// }
//
// func (p *player) doStartLaunch() {
// p.state = charge
// p.currentSprite = &p.chargeSprite
// }
//
// func (p *player) doLaunch() {
// p.state = playerLaunch
// p.currentSprite = &p.launchSprite
// }
func (p *player) update(dt time.Duration) {
// p.punchGap -= dt
switch p.state {
case awaken:
if p.awoke() {
p.doNormal()
}
case normal:
// case idle:
// p.updateMove()
// if p.move != 0 {
// p.doMove()
// }
// if p.shouldStartPunch() {
// p.doPunch()
// }
// if p.launch {
// p.doStartLaunch()
// }
// case playerMove:
// p.updateMove()
// if p.move == 0 {
// p.doIdle()
// }
// if p.shouldStartPunch() {
// p.doPunch()
// }
// if p.launch {
// p.doStartLaunch()
// }
// case playerPunch:
// p.updateMove()
// p.punchTime -= dt
// if p.punchTime <= 0 {
// p.punchGap = playerPunchGap
// if p.move == 0 {
// p.doMove()
// } else {
// p.doIdle()
// }
// }
// case charge:
// if !p.launch {
// p.doLaunch()
// }
// case playerLaunch:
// p.doIdle()
}
switch p.state {
case awaken:
case normal:
p.updateMove()
p.updateMovement(dt)
// case idle:
// p.updateMovement(dt)
// case playerMove:
// p.updateMovement(dt)
// case playerPunch:
// p.updateMovement(dt)
// case charge:
// case playerLaunch:
}
p.updateHitboxes()
// p.currentSprite.Update(dt)
// Reset Move for next frame, it will be set each frame by user input.
p.move = 0
}
func (p *player) updateHitboxes() {
// Since the player is being updated before the camera this will be off by a frame.
// I don't think it will be noticeable though.
// mousePos := p.cam.WorldCoords(geo.VecXYi(ebiten.CursorPosition()))
// centerY := p.currentSprite.Size().Y / 2
// p.coreHitbox.Bounds.SetTopLeft(p.pos.Plus(geo.VecXY(0, -centerY)).XY())
switch p.state {
// case awaken, idle, playerMove:
case awaken, normal:
p.attackHitbox.Active = false
// case playerPunch:
// p.attackHitbox.Active = true
// p.attackHitbox.Bounds.SetSize(8, 8)
// center := p.pos.Plus(geo.VecXY(0, -centerY))
// toMouse := mousePos.Minus(center)
// if p.punchWithGamepad {
// toMouse = p.punchAxis
// }
// toMouse.SetLen(9)
// p.attackHitbox.Bounds.SetMid(center.Plus(toMouse).XY())
// case charge:
// // p.coreHitbox.Bounds.SetTopLeft(p.pos.Plus(geo.VecXY(0, -centerY)).XY())
// case playerLaunch:
}
}
func (p *player) draw(dst *ebiten.Image, cam *camera.Camera) {
// pos := cam.ScreenCoords(p.pos)
geom := ebiten.GeoM{}
// size := p.currentSprite.Size()
// bounds := geo.RectWH(size.XY())
// bounds.SetBottomMid(pos.XY())
switch p.state {
case awaken:
case normal:
// case idle:
// case playerMove:
p.flipDir = p.vel.X < 0
// case playerPunch:
// mPos := geo.VecXYi(ebiten.CursorPosition())
// pos = geo.VecXY(bounds.Mid())
//
// if p.punchWithGamepad {
// mPos = pos.Plus(p.punchAxis)
// }
//
// p.flipDir = mPos.X < pos.X
// angle := mPos.Minus(pos).Angle()
// if p.flipDir {
// angle += math.Pi
// angle *= -1
// }
// geom.Translate(-size.X/2, -size.Y/2)
// geom.Rotate(-angle)
// geom.Translate(size.X/2, size.Y/2)
// case charge:
// mPos := geo.VecXYi(ebiten.CursorPosition())
// if p.punchAxis.Len() != 0 {
// mPos = pos.Plus(p.punchAxis)
// }
//
// p.flipDir = false
// angle := mPos.Minus(pos).AngleFrom(geo.VecXY(0, -1))
//
// geom.Translate(-size.X/2, -size.Y)
// geom.Rotate(-angle)
// geom.Translate(size.X/2, size.Y)
// case playerLaunch:
}
if p.flipDir {
geom.Scale(-1, 1)
// geom.Translate(size.X, 0)
}
// p.currentSprite.Draw(dst, pos, geom)
// debug draw hitboxes
if p.attackHitbox.Active {
x, y := cam.ScreenCoords(geo.VecXY(p.attackHitbox.Bounds.TopLeft())).XY()
w, h := p.attackHitbox.Bounds.Size()
ebitenutil.DrawRect(dst, x, y, w, h, color.RGBA{0xFF, 0x00, 0x00, 0x88})
}
if p.coreHitbox.Active {
x, y := cam.ScreenCoords(geo.VecXY(p.coreHitbox.Bounds.TopLeft())).XY()
w, h := p.coreHitbox.Bounds.Size()
ebitenutil.DrawRect(dst, x, y, w, h, color.RGBA{0x00, 0xFF, 0x00, 0x88})
}
}
func (p *player) updateMove() {
if p.move == 0 { // If gamepad axis isn't being used, check left/right buttons.
if p.left {
p.move = -1
}
if p.right {
p.move = 1
}
}
}
func (p *player) updateMovement(dt time.Duration) {
p.vel.X = p.move * playerMoveSpeed
// Check if it's time to jump before handling jump the jump state so that we start
// jumping as soon as possible
if !p.isJumping && p.jump && p.canJump {
p.isJumping = true
p.jumpTime = playerJumpTime
}
if p.isJumping {
if p.jumpTime <= 0 || !p.jump {
p.isJumping = false
} else {
p.jumpTime -= dt
p.vel.Y = geo.Lerp(0, -playerJumpSpeed, p.jumpTime.Seconds()/playerJumpTime.Seconds())
}
} else {
p.vel.Y += playerGravity
}
p.pos.Add(p.vel.Times(dt.Seconds()))
// Ground collision
if p.pos.Y > 0 {
p.pos.Y = 0
p.canJump = true
} else {
p.canJump = false
}
}
func (p *player) Pos() geo.Vec {
return p.pos
}
func (p *player) SetPos(pos geo.Vec) {
p.pos = pos
}
func (p *player) handleLeft(down bool) bool {
p.left = down
return false
}
func (p *player) handleRight(down bool) bool {
p.right = down
return false
}
func (p *player) handleMove(val float64) bool {
p.move = val
return false
}
func (p *player) handleJump(down bool) bool {
p.jump = down
return false
}
func (p *player) handlePunch(down bool) bool {
p.punch = down
return false
}
func (p *player) handlePunchH(val float64) bool {
p.punchAxis.X = val
return false
}
func (p *player) handlePunchV(val float64) bool {
p.punchAxis.Y = -val
return false
}
func (p *player) handleLaunch(down bool) bool {
p.launch = down
return false
}
func (p *player) hitboxes() []*hitbox {
return []*hitbox{&p.coreHitbox, &p.attackHitbox}
}
func (p *player) coreHit(other *hitbox) {
log.Println("coreHit:", other.Label)
}
func (p *player) attackHit(other *hitbox) {
// log.Println("attackHit:", other.Label)
switch other.Label {
case "ground":
p.vel.Y = -125
p.cam.Shaker.Amplitude = 10
p.cam.Shaker.Duration = 500 * time.Millisecond
p.cam.Shaker.Frequency = 5
p.cam.Shaker.Falloff = geo.EaseOutQuad
p.cam.StartShake()
}
}