-
Notifications
You must be signed in to change notification settings - Fork 11
/
riser.go
485 lines (436 loc) · 14.2 KB
/
riser.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package riser
import (
"fmt"
"time"
"github.com/divVerent/aaaaxy/internal/animation"
"github.com/divVerent/aaaaxy/internal/engine"
"github.com/divVerent/aaaaxy/internal/game/constants"
"github.com/divVerent/aaaaxy/internal/game/interfaces"
"github.com/divVerent/aaaaxy/internal/game/mixins"
"github.com/divVerent/aaaaxy/internal/level"
m "github.com/divVerent/aaaaxy/internal/math"
"github.com/divVerent/aaaaxy/internal/propmap"
"github.com/divVerent/aaaaxy/internal/sound"
)
type riserState int
const (
Inactive riserState = iota
IdlingUp
MovingUp
MovingLeft
MovingRight
GettingCarried
)
type Riser struct {
mixins.Physics
World *engine.World
Entity *engine.Entity
NormalSize m.Delta
CarriedSize m.Delta
State riserState
Anim animation.State
FadeFrame int
PlayerOnGroundVec m.Delta
carrySound, pushSound, riseSound riserSound
}
const (
// SmallRiserWidth is the hitbox width of the riser.
// Actual width is 16 (one extra pixel to left and right).
SmallRiserWidth = 14
// SmallRiserHeight is the hitbox height of the riser.
// Actual height is 16 (one extra pixel to left and right).
SmallRiserHeight = 14
// SmallRiserOffsetDX is the riser's render offset.
SmallRiserOffsetDX = -1
// SmallRiserOffsetDY is the riser's render offset.
SmallRiserOffsetDY = -1
// LargeRiserWidth is the hitbox width of the riser.
// Actual width is 16 (one extra pixel to left and right).
LargeRiserWidth = 30
// LargeRiserHeight is the hitbox height of the riser.
// Actual height is 32 (one extra pixel to left and right).
LargeRiserHeight = 14
// LargeRiserOffsetDX is the riser's render offset.
LargeRiserOffsetDX = -1
// LargeRiserOffsetDY is the riser's render offset.
LargeRiserOffsetDY = -1
// RiserBorderPixels is the riser's border size.
RiserBorderPixels = 1
// CarriedSmallRiserWidth is the hitbox width of a riser being carried.
CarriedSmallRiserWidth = 8
// CarriedSmallRiserHeight is the hitbox height of a riser being carried.
CarriedSmallRiserHeight = 8
// CarriedLargeRiserWidth is the hitbox width of a riser being carried.
CarriedLargeRiserWidth = 24
// CarriedLargeRiserHeight is the hitbox height of a riser being carried.
CarriedLargeRiserHeight = 8
// IdleSpeed is the speed the riser moves upwards when not used.
IdleSpeed = 15 * constants.SubPixelScale / engine.GameTPS
// UpSpeed is the speed the riser moves upwards when the player is standing on it.
UpSpeed = 60 * constants.SubPixelScale / engine.GameTPS
// SideSpeed is the speed of the riser when pushed away.
SideSpeed = 60 * constants.SubPixelScale / engine.GameTPS
// FadeFrames is how many frames risers take to fade in or out.
FadeFrames = 16
// FollowFactor is how fast a riser should follow the player per second.
FollowFactor = 24.0
// FollowMaxDistance is the max distance allowed while following the player.
// Hardest part: Multi-Party Authorization.
FollowMaxDistance = 24
// RepelSpeed is the speed at which risers repel each other until they no longer overlap.
RepelSpeed = 15 * constants.SubPixelScale / engine.GameTPS
)
func (r *Riser) Spawn(w *engine.World, sp *level.SpawnableProps, e *engine.Entity) error {
r.Physics.Init(w, e, level.ObjectSolidContents, r.handleTouch)
r.World = w
r.Entity = e
if r.Entity.Rect.Size.DY > 16 {
// We actually allow this, but create a _small_ riser centered at the same place.
// We can't use a large one as it might be in solid.
// Do NOT adjust Y; we keep the upper edge as is,
// as the upper edge is what matters for standing on it.
r.Entity.Rect.Size.DY = 16
}
if r.Entity.Rect.Size.DY != 16 {
return fmt.Errorf("unexpected riser height: got %v, want 16", r.Entity.Rect.Size.DY)
}
var sprite string
switch r.Entity.Rect.Size.DX {
case 16:
r.NormalSize = m.Delta{DX: SmallRiserWidth, DY: SmallRiserHeight}
r.CarriedSize = m.Delta{DX: CarriedSmallRiserWidth, DY: CarriedSmallRiserHeight}
r.Entity.RenderOffset = m.Delta{DX: SmallRiserOffsetDX, DY: SmallRiserOffsetDY}
sprite = "riser_small"
case 32:
r.NormalSize = m.Delta{DX: LargeRiserWidth, DY: LargeRiserHeight}
r.CarriedSize = m.Delta{DX: CarriedLargeRiserWidth, DY: CarriedLargeRiserHeight}
r.Entity.RenderOffset = m.Delta{DX: LargeRiserOffsetDX, DY: LargeRiserOffsetDY}
sprite = "riser_large"
default:
return fmt.Errorf("unexpected riser width: got %v, want 16 or 32", r.Entity.Rect.Size.DX)
}
r.Entity.BorderPixels = RiserBorderPixels
r.Entity.Rect.Origin = r.Entity.Rect.Origin.Sub(r.Entity.RenderOffset)
r.Entity.Rect.Size = r.NormalSize
w.SetZIndex(r.Entity, constants.RiserMovingZ)
r.Entity.Alpha = 0 // We fade in.
r.State = Inactive
r.Entity.Orientation = m.Identity()
var parseErr error
if propmap.ValueOrP(sp.Properties, "flipped", false, &parseErr) {
r.OnGroundVec = r.OnGroundVec.Mul(-1)
}
err := r.Anim.Init(sprite, map[string]*animation.Group{
"inactive": {
Frames: 1,
FrameInterval: 16,
NextInterval: 16,
NextAnim: "inactive",
},
"idle": {
Frames: 1,
FrameInterval: 16,
NextInterval: 16,
NextAnim: "idle",
},
"left": {
Frames: 2,
FrameInterval: 16,
NextInterval: 32,
NextAnim: "left",
},
"right": {
Frames: 2,
FrameInterval: 16,
NextInterval: 32,
NextAnim: "right",
},
"up": {
Frames: 2,
FrameInterval: 16,
NextInterval: 32,
NextAnim: "up",
},
"idlestand": {
Frames: 1,
FrameInterval: 16,
NextInterval: 16,
NextAnim: "idlestand",
},
"leftstand": {
Frames: 2,
FrameInterval: 16,
NextInterval: 32,
NextAnim: "leftstand",
},
"rightstand": {
Frames: 2,
FrameInterval: 16,
NextInterval: 32,
NextAnim: "rightstand",
},
"upstand": {
Frames: 2,
FrameInterval: 16,
NextInterval: 32,
NextAnim: "upstand",
},
}, "inactive")
if err != nil {
return fmt.Errorf("could not initialize riser animation: %w", err)
}
r.carrySound, err = newRiserSound(1, "riser_carry")
if err != nil {
return err
}
r.pushSound, err = newRiserSound(2, "riser_push")
if err != nil {
return err
}
r.riseSound, err = newRiserSound(2, "riser_rise")
if err != nil {
return err
}
return parseErr
}
func (r *Riser) Despawn() {
r.pushSound.despawn()
r.carrySound.despawn()
r.riseSound.despawn()
}
type riserSound struct {
// Constants.
minFrames int
on, off *sound.GroupedSound
// State.
counter int
}
func newRiserSound(minFrames int, name string) (riserSound, error) {
onSnd, err := sound.Load(name + ".ogg")
if err != nil {
return riserSound{}, fmt.Errorf("could not load %v sound: %w", name, err)
}
offSnd, err := sound.Load(name + "_stop.ogg")
if err != nil {
return riserSound{}, fmt.Errorf("could not load %v_stop sound: %w", name, err)
}
return riserSound{
minFrames: minFrames,
on: onSnd.Grouped(),
off: offSnd.Grouped(),
}, nil
}
func (s *riserSound) update(cur bool) {
if cur {
s.counter++
if s.counter == s.minFrames {
s.on.Play()
if !s.on.IsPlayingNotForGameplay() {
s.on.Reset()
}
}
} else {
if s.counter >= s.minFrames {
s.on.Close()
s.off.Play()
if s.off.CurrentNotForGameplay() >= 100*time.Millisecond {
s.off.Reset()
}
}
s.counter = 0
}
}
func (s *riserSound) despawn() {
s.on.Close()
s.off.Close()
s.counter = 0
}
func (r *Riser) PreDespawn() {
r.pushSound.update(false)
r.carrySound.update(false)
r.riseSound.update(false)
}
func (r *Riser) Update() {
playerAbilities := r.World.Player.Impl.(interfaces.Abilityer)
playerButtons := r.World.Player.Impl.(interfaces.ActionPresseder)
playerPhysics := r.World.Player.Impl.(interfaces.Physics)
canCarry := playerAbilities.HasAbility("carry")
canPush := playerAbilities.HasAbility("push")
canStand := playerAbilities.HasAbility("stand")
actionPressed := playerButtons.ActionPressed()
playerOnMe := playerPhysics.ReadGroundEntity() == r.Entity
playerDelta := r.World.Player.Rect.Delta(r.Entity.Rect)
playerAboveMe := playerDelta.DX == 0 && playerDelta.Dot(r.OnGroundVec) < 0
if canCarry && !playerOnMe && actionPressed && (playerDelta.IsZero() || (r.State == GettingCarried && playerDelta.Norm1() <= FollowMaxDistance)) {
r.State = GettingCarried
} else if canPush && actionPressed {
if r.World.Player.Rect.Center().X < r.Entity.Rect.Center().X {
r.State = MovingRight
} else {
r.State = MovingLeft
}
} else if canStand && playerAboveMe {
r.State = MovingUp
} else if canCarry || canPush || canStand {
r.State = IdlingUp
} else {
r.State = Inactive
}
suffix := ""
if canStand {
suffix = "stand"
}
switch r.State {
case Inactive:
r.Anim.SetGroup("inactive")
r.Velocity = m.Delta{}
case IdlingUp:
r.Anim.SetGroup("idle" + suffix)
r.Velocity = r.OnGroundVec.Mul(-IdleSpeed)
case MovingUp:
r.Anim.SetGroup("up" + suffix)
r.Velocity = r.OnGroundVec.Mul(-UpSpeed)
case MovingLeft:
r.Anim.SetGroup("left" + suffix)
r.Velocity = r.OnGroundVec.Mul(-IdleSpeed).Add(m.Delta{DX: -SideSpeed, DY: 0})
case MovingRight:
r.Anim.SetGroup("right" + suffix)
r.Velocity = r.OnGroundVec.Mul(-IdleSpeed).Add(m.Delta{DX: SideSpeed, DY: 0})
case GettingCarried:
r.Anim.SetGroup("idle" + suffix)
// r.Velocity = playerPhysics.ReadVelocity() // Hacky carry physics; good enough?
pxDelta := r.World.Player.Rect.Center().Delta(r.Entity.Rect.Center())
subDelta := playerPhysics.ReadSubPixel().Sub(r.SubPixel)
fullDelta := pxDelta.Mul(constants.SubPixelScale).Add(subDelta)
r.Velocity = fullDelta.MulFixed(m.NewFixedFloat64(FollowFactor / engine.GameTPS))
if r.PlayerOnGroundVec.IsZero() {
// All OK, just need to initialize grabbing.
} else if r.PlayerOnGroundVec != playerPhysics.ReadOnGroundVec() {
// Player's onground vec changed. Apply the change to ours.
// TODO(divVerent): Actually make this smarter? Like, actually apply the transform by which the player changed?
r.OnGroundVec = r.OnGroundVec.Mul(-1)
}
r.PlayerOnGroundVec = playerPhysics.ReadOnGroundVec()
}
if r.State == GettingCarried {
// Never solid during carrying.
r.World.MutateContents(r.Entity, level.SolidContents, 0)
} else if canStand && playerAboveMe {
// Solid to player when player is above.
r.World.MutateContents(r.Entity, level.SolidContents, level.SolidContents)
} else {
// Otherwise, only solid to objects.
r.World.MutateContents(r.Entity, level.SolidContents, level.ObjectSolidContents)
}
if playerOnMe || !canStand || r.State == GettingCarried {
r.Physics.IgnoreEnt = r.World.Player // Move upwards despite player standing on it.
} else {
r.Physics.IgnoreEnt = nil
}
if r.State == GettingCarried {
r.World.SetZIndex(r.Entity, constants.RiserCarriedZ)
} else {
r.World.SetZIndex(r.Entity, constants.RiserMovingZ)
}
// Adjust hitbox size.
targetSize := r.NormalSize
if r.State == GettingCarried {
targetSize = r.CarriedSize
}
targetSizeChange := targetSize.Sub(r.Entity.Rect.Size)
if r.ModifyHitBoxCentered(targetSizeChange) != targetSizeChange {
r.World.Detach(r.Entity)
}
// Also, risers that touch each other repel each other.
r.World.ForEachEntity(func(other *engine.Entity) {
if other == r.Entity {
return
}
otherR, ok := other.Impl.(*Riser)
if !ok {
return
}
dr := r.Entity.Rect.Delta(other.Rect)
if dr.Norm0() <= RiserBorderPixels {
pxDelta := r.Entity.Rect.Center().Delta(other.Rect.Center())
subDelta := r.SubPixel.Sub(otherR.SubPixel)
fullDelta := pxDelta.Mul(constants.SubPixelScale).Add(subDelta)
left := m.Right().Apply(r.OnGroundVec)
var scaledDelta m.Delta
if fullDelta.IsZero() {
// On full overlap, move one of them _left/right_ which is the most gameplay friendly direction.
// This will cause the other one to get repelled to the right.
// Why not down? Yes, these are easier to grab then,
// but it helps more that one can stand between them and push them away from each other.
// How to pick left/right deterministically? We want no randomness...
// So we pick them alternatingly, so eventually repelling will work.
if r.World.PlayerState.Frames()%2 == 0 {
scaledDelta = left.Mul(RepelSpeed)
} else {
scaledDelta = left.Mul(-RepelSpeed)
}
} else {
// Similarly, ensure that the left/right component is at least half the work.
// We do this by assuming an increased L/R delta of exactly the value of the U/D delta.
upDown := fullDelta.Sub(left.Mul(fullDelta.Dot(left))).Norm1()
if fullDelta.Dot(left) < 0 {
upDown = -upDown
}
fullDelta = fullDelta.Add(left.Mul(upDown))
scaledDelta = fullDelta.WithLengthFixed(m.NewFixed(RepelSpeed))
}
r.Velocity = r.Velocity.Add(scaledDelta)
}
})
// Run physics.
if !r.Velocity.IsZero() {
r.Physics.Update() // May call handleTouch.
}
// Carry is a clear state.
r.carrySound.update(r.State == GettingCarried)
// For push and moving up, decide sound by whether we're actually moving.
r.pushSound.update((r.State == MovingLeft || r.State == MovingRight) && r.Velocity.DX != 0)
r.riseSound.update(r.State == MovingUp && (r.Velocity.DY == UpSpeed || r.Velocity.DY == -UpSpeed))
r.Anim.Update(r.Entity)
if r.OnGroundVec.DY < 0 {
r.Entity.Orientation = m.FlipY()
} else {
r.Entity.Orientation = m.Identity()
}
if r.Entity.Detached() {
if r.FadeFrame > 0 {
r.FadeFrame--
}
if r.FadeFrame == 0 {
r.World.Despawn(r.Entity)
}
} else {
if r.FadeFrame < FadeFrames {
r.FadeFrame++
}
}
r.Entity.Alpha = float64(r.FadeFrame) / float64(FadeFrames)
}
func (r *Riser) handleTouch(trace engine.TraceResult) {
// Risers can touch stuff. Gonna use this for switches.
r.World.TouchEvent(r.Entity, trace.HitEntities)
}
func (r *Riser) Touch(other *engine.Entity) {
// Nothing happens; we rather handle this on other's Touch event.
}
func init() {
engine.RegisterEntityType(&Riser{})
}