-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathmain.go
133 lines (111 loc) · 3.25 KB
/
main.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
package main
import (
"image/color"
"math"
"github.com/oakmound/oak/v4/alg/floatgeom"
"github.com/oakmound/oak/v4/collision"
"github.com/oakmound/oak/v4/event"
"github.com/oakmound/oak/v4/key"
oak "github.com/oakmound/oak/v4"
"github.com/oakmound/oak/v4/entities"
"github.com/oakmound/oak/v4/scene"
)
const (
// The only collision label we need for this demo is 'ground',
// indicating something we shouldn't be able to fall or walk through
Ground collision.Label = 1
)
func main() {
oak.AddScene("platformer", scene.Scene{Start: func(ctx *scene.Context) {
char := entities.New(ctx,
entities.WithRect(floatgeom.NewRect2WH(100, 100, 16, 32)),
entities.WithColor(color.RGBA{255, 0, 0, 255}),
entities.WithSpeed(floatgeom.Point2{3, 7}),
)
const fallSpeed = .2
event.Bind(ctx, event.Enter, char, func(c *entities.Entity, ev event.EnterPayload) event.Response {
// Move left and right with A and D
if oak.IsDown(key.A) {
char.Delta[0] = -char.Speed.X()
} else if oak.IsDown(key.D) {
char.Delta[0] = char.Speed.X()
} else {
char.Delta[0] = (0)
}
oldX, oldY := char.X(), char.Y()
char.ShiftDelta()
aboveGround := false
hit := collision.HitLabel(char.Space, Ground)
// If we've moved in y value this frame and in the last frame,
// we were below what we're trying to hit, we are still falling
if hit != nil && !(oldY != char.Y() && oldY+char.H() > hit.Y()) {
// Correct our y if we started falling into the ground
char.SetY(hit.Y() - char.H())
// Stop falling
char.Delta[1] = 0
// Jump with Space when on the ground
if oak.IsDown(key.Spacebar) {
char.Delta[1] -= char.Speed.Y()
}
aboveGround = true
} else {
//Restart when is below ground
if char.Y() > 500 {
char.Delta[1] = 0
char.SetY(100)
char.SetX(100)
}
// Fall if there's no ground
char.Delta[1] += fallSpeed
}
if hit != nil {
// If we walked into a piece of ground, move back
xover, yover := char.Space.Overlap(hit)
// We, perhaps unintuitively, need to check the Y overlap, not
// the x overlap
// if the y overlap exceeds a superficial value, that suggests
// we're in a state like
//
// G = Ground, C = Character
//
// GG C
// GG C
//
// moving to the left
if math.Abs(yover) > 1 {
// We add a buffer so this doesn't retrigger immediately
xbump := 1.0
if xover > 0 {
xbump = -1
}
char.SetX(oldX + xbump)
if char.Delta.Y() < 0 {
char.Delta[1] = 0
}
}
// If we're below what we hit and we have significant xoverlap, by contrast,
// then we're about to jump from below into the ground, and we
// should stop the character.
if !aboveGround && math.Abs(xover) > 1 {
// We add a buffer so this doesn't retrigger immediately
char.SetY(oldY + 1)
char.Delta[1] = fallSpeed
}
}
return 0
})
platforms := []floatgeom.Rect2{
floatgeom.NewRect2WH(0, 400, 300, 20),
floatgeom.NewRect2WH(100, 250, 30, 20),
floatgeom.NewRect2WH(340, 300, 100, 20),
}
for _, p := range platforms {
entities.New(ctx,
entities.WithRect(p),
entities.WithColor(color.RGBA{0, 0, 255, 255}),
entities.WithLabel(Ground),
)
}
}})
oak.Init("platformer")
}