-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.go
160 lines (143 loc) · 3.92 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
package main
import (
"github.com/SolarLune/resolv/resolv"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/inpututil"
"image/color"
"math"
)
// For physics
const gravity = 100
type Player struct {
Object
// Number of times jumped
jumped int
// Last N positions; we could remember the timestamp, too for more independence of the frameCounter counter.
PreviousPosition []Vector2
}
func NewPlayer() *Player {
return &Player{
Object: Object{
Body: resolv.NewRectangle(0, height-20-borderWidth, 20, 20),
Velocity: Vector2{},
Acceleration: Vector2{X: 10.0, Y: 10.0},
},
jumped: 0,
PreviousPosition: nil,
}
}
func (player *Player) Update(g *GameState) {
if inpututil.IsKeyJustPressed(ebiten.KeyUp) || inpututil.IsGamepadButtonJustPressed(0, ebiten.GamepadButton0) {
player.jumped++
switch player.jumped {
case 1:
player.Velocity.Y -= float64(gravity) * 0.75
case 2:
player.Velocity.Y -= float64(gravity) * 0.50
}
}
// Check axes of first gamepad.
axes := []float64{
ebiten.GamepadAxis(0, 0),
ebiten.GamepadAxis(0, 1)}
gamepadAcceleration := 40.0
if math.Abs(axes[0]) > 0.15 {
player.Velocity.X += axes[0] * gamepadAcceleration
}
if math.Abs(axes[1]) > 0.15 {
player.Velocity.X -= axes[1] * gamepadAcceleration
}
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
player.Velocity.X -= 5
}
if ebiten.IsKeyPressed(ebiten.KeyRight) {
player.Velocity.X += 5
}
for math.Abs(player.Velocity.X) > 40 {
d := -0.01
if math.Signbit(player.Velocity.X) {
d *= -1
}
player.Velocity.X += d
}
// Basic physics.
delta := 1.0 / float64(ebiten.MaxTPS())
dx := int32(player.Velocity.X * delta * player.Acceleration.X)
dy := int32(player.Velocity.Y * delta * player.Acceleration.Y)
collision := walls.Resolve(player.Body, dx, 0)
if collision.Colliding() {
dx = collision.ResolveX
}
player.Body.X += dx
if collision.Colliding() {
player.Velocity.X = 0
} else {
if player.jumped > 0 {
player.Velocity.X *= 0.99
} else {
player.Velocity.X *= 0.9
}
if math.Abs(player.Velocity.X) < 0.01 && player.jumped == 0 {
player.Velocity.X = 0
}
}
collision = walls.Resolve(player.Body, 0, dy)
if collision.Colliding() {
dy = collision.ResolveY
}
player.Body.Y += dy
if collision.Colliding() {
player.Velocity.Y = 0
player.jumped = 0
} else {
player.Velocity.Y += gravity * delta
}
// Update historic positions.
if g.frameCounter%numHistoricFramesUpdate == 0 {
if len(player.PreviousPosition) < numHistoricFrames {
player.PreviousPosition = append(player.PreviousPosition, Vector2{float64(player.Body.X), float64(player.Body.Y)})
} else {
player.PreviousPosition = player.PreviousPosition[1:]
player.PreviousPosition = append(player.PreviousPosition, Vector2{float64(player.Body.X), float64(player.Body.Y)})
}
}
// Check if goal reached.
// BUG Check that we did not reach the goal already (as game state, i.e. later) to prevent sound on each collision.
if goals.IsColliding(player.Body) {
if gameState.timer.UpdateTime() {
PlayAudioTimes("goal", 2)
} else {
PlayAudio("goal")
}
}
}
func (player *Player) Draw(screen *ebiten.Image) {
x := getXTranslation()
// Trail
if len(player.PreviousPosition) > 0 {
r, g, b, _ := color.RGBA{
R: 255,
G: 255,
B: 255,
A: 255,
}.RGBA()
colorQuotient := 255.0 / float64(len(player.PreviousPosition))
for i, vec := range player.PreviousPosition {
boxColor := uint8(colorQuotient * float64(i))
drawRect(screen,
vec.X+float64(player.Body.W)/2-float64(player.Body.W)/8,
vec.Y+float64(player.Body.H)/2-float64(player.Body.H)/8,
float64(player.Body.W)/4,
float64(player.Body.H)/4,
color.RGBA{
R: uint8(r),
G: uint8(g),
B: uint8(b),
A: boxColor,
})
}
}
col := color.RGBA{255, 255, 0, 255}
ebitenutil.DrawRect(screen, x, float64(player.Body.Y), float64(player.Body.W), float64(player.Body.H), col)
}