-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgravity.go
More file actions
57 lines (51 loc) · 1.22 KB
/
gravity.go
File metadata and controls
57 lines (51 loc) · 1.22 KB
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
package main
import (
"math"
"github.com/creativeprojects/cavern/lib"
)
type Gravity struct {
*Collide
speedY float64
landed bool
}
func NewGravity(level *Level, sprite *lib.Sprite) *Gravity {
return &Gravity{
Collide: NewCollide(level, sprite),
speedY: 0,
landed: false,
}
}
// UpdateFall updates the coordinates when falling down.
// it returns true when the sprite just landed on a block
func (g *Gravity) UpdateFall() bool {
// Apply gravity, without going over the maximum fall speed
g.speedY = math.Min(g.speedY+1, MaxFallSpeed)
dy := 1.0
if g.speedY <= 0 {
dy = -1.0
}
moved := g.CollideMove(0, dy, math.Abs(g.speedY))
if moved {
g.landed = false
} else {
g.speedY = 0
if !g.landed {
// just landed on a block
g.landed = true
return true
}
return false
}
if g.Y(lib.YTop) >= WindowHeight {
// fallen off the bottom, reappear at the top
g.MoveTo(g.RawX(), g.RawY()-WindowHeight)
}
return false
}
// UpdateFreeFall updates the coordinates when falling down without stopping on any block
func (g *Gravity) UpdateFreeFall() {
// Apply gravity, without going over the maximum fall speed
g.speedY = math.Min(g.speedY+1, MaxFallSpeed)
// collision detection disabled
g.Move(0, g.speedY)
}