generated from sionleroux/ebitengine-game-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplayer.go
260 lines (224 loc) · 6.8 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
// Use of this source code is subject to an MIT-style
// licence which can be found in the LICENSE file.
package main
import (
"image"
"math"
"github.com/hajimehoshi/ebiten/v2"
"github.com/solarlune/resolv"
)
// playerSpeed is the distance the player moves per update cycle
var playerSpeed float64 = 1.2
// amount to change speed by when the player is reversing backwards
// walking backwards is very slow
var playerSpeedFactorReverse float64 = 0.2
// amount to change speed by when the player is strafing sideways
var playerSpeedFactorSideways float64 = 0.6
var playerSpeedFactorSprint float64 = 2.4
var playerAmmoClipMax int = 7
// states of the player
// It would be great to map them to the frameTag.Name from JSON
type playerState int
const (
playerIdle playerState = iota // Waiting for input from the player
playerWalking // Walking in some direction
playerReady // Readying the gun to shoot
playerShooting // Shooting the gun
playerDryFire // Trying to shoot with no ammo
playerReload // Reloading the gun
playerUnready // Unreadying the gun after shooting
)
// Player is the player character in the game
type Player struct {
Object *resolv.Object // Used for collision detection with other objects
Angle float64 // The angle the player is facing at
Frame int // The current animation frame
State playerState // The current animation state
PrevState playerState // The previous animation state
Sprinting bool // Whether the player is sprinting or not
Sprite *SpriteSheet // Used for player animations
Range float64 // How far you can shoot with the gun
Ammo int // How many shots you have left in the gun
TempSpeed float64 // Temporary speed multiplier
}
// NewPlayer constructs a new Player object at the provided location and size
func NewPlayer(position []int, sprites *SpriteSheet) *Player {
// the head and shoulders are about 4px from the middle
const collisionBoxSize float64 = 8
dimensions := sprites.Sprite[0].Position
object := resolv.NewObject(
float64(position[0]), float64(position[1]),
float64(dimensions.W), float64(dimensions.H),
tagPlayer,
)
object.SetShape(resolv.NewRectangle(
0, 0, // origin
collisionBoxSize, collisionBoxSize,
))
object.Shape.(*resolv.ConvexPolygon).RecenterPoints()
player := &Player{
State: playerIdle,
Object: object,
Angle: 0,
Sprite: sprites,
Range: 200,
Ammo: playerAmmoClipMax,
TempSpeed: 1,
}
return player
}
// Reload reloads the ammo
func (p *Player) Reload(g *GameScreen) {
p.State = playerReload
g.Sounds[soundGunReload].Play()
}
// Update updates the state of the player
func (p *Player) Update(g *GameScreen) {
p.PrevState = p.State
p.Sprinting = false
if p.State == playerIdle || p.State == playerWalking {
p.State = playerIdle
p.handleControls()
}
if p.Frame == p.Sprite.Meta.FrameTags[p.State].To {
p.animationBasedStateChanges(g)
}
// Player gun rotation
cx, cy := g.Camera.GetCursorCoords()
adjacent := float64(cx) - p.Object.Position.X
opposite := float64(cy) - p.Object.Position.Y
p.Angle = math.Atan2(opposite, adjacent)
p.Frame = Animate(p.Frame, g.Tick, p.Sprite.Meta.FrameTags[p.State])
p.Object.Shape.SetRotation(-p.Angle)
p.Object.Update()
}
// Animation-trigged state changes
func (p *Player) animationBasedStateChanges(g *GameScreen) {
switch p.State {
case playerShooting: // Back to idle after shooting animation
p.State = playerIdle
if p.Ammo < 1 {
p.Reload(g) // Automatic reload if out of ammo
}
case playerReload: // Back to idle after reload animation
p.Ammo = playerAmmoClipMax
p.State = playerIdle
case playerDryFire: // Back to idle after reload animation
p.State = playerIdle
}
}
// MoveLeft moves the player left
func (p *Player) MoveLeft() {
speed := playerSpeed * playerSpeedFactorSideways * p.TempSpeed
p.move(
math.Sin(p.Angle)*speed,
-math.Cos(p.Angle)*speed,
)
}
// MoveRight moves the player right
func (p *Player) MoveRight() {
speed := playerSpeed * playerSpeedFactorSideways * p.TempSpeed
p.move(
-math.Sin(p.Angle)*speed,
math.Cos(p.Angle)*speed,
)
}
// MoveForward moves the player forward towards the pointer
func (p *Player) MoveForward() {
speed := playerSpeed
if p.Sprinting && p.TempSpeed >= 1 {
speed = speed * playerSpeedFactorSprint
} else {
speed = speed * p.TempSpeed
}
p.move(
math.Cos(p.Angle)*speed,
math.Sin(p.Angle)*speed,
)
}
// MoveBackward moves the player backward away from the pointer
func (p *Player) MoveBackward() {
speed := playerSpeed * playerSpeedFactorReverse * p.TempSpeed
p.move(
-math.Cos(p.Angle)*speed,
-math.Sin(p.Angle)*speed,
)
}
// Move the Player by the given vector if it is possible to do so
func (p *Player) move(dx, dy float64) {
p.State = playerWalking
// Collision detection and response between sand trap and player
p.TempSpeed = 1
if collision := p.Object.Check(0, 0, tagSandTrap); collision != nil {
if p.Object.Shape.Intersection(0, 0, collision.Objects[0].Shape) != nil {
p.TempSpeed = sandTrapSpeedMultiplier
}
}
if collision := p.Object.Check(dx, 0, tagWall, tagDog); collision != nil {
for _, o := range collision.Objects {
if p.Object.Shape.Intersection(dx, 0, o.Shape) != nil {
dx = 0
}
}
}
p.Object.Position.X += dx
if collision := p.Object.Check(0, dy, tagWall, tagDog); collision != nil {
for _, o := range collision.Objects {
if p.Object.Shape.Intersection(0, dy, o.Shape) != nil {
dy = 0
}
}
}
p.Object.Position.Y += dy
}
// Draw draws the Player to the screen
func (p *Player) Draw(g *GameScreen) {
// the centre of the player's head is 2px down from the middle
const centerOffset float64 = -2
s := p.Sprite
frame := s.Sprite[p.Frame]
op := &ebiten.DrawImageOptions{}
// Centre and rotate
op.GeoM.Translate(
float64(-frame.Position.W/2),
float64(-frame.Position.H/2)+centerOffset/2,
)
op.GeoM.Rotate(p.Angle + math.Pi/2)
g.Camera.Surface.DrawImage(
s.Image.SubImage(image.Rect(
frame.Position.X,
frame.Position.Y,
frame.Position.X+frame.Position.W,
frame.Position.Y+frame.Position.H,
)).(*ebiten.Image),
g.Camera.GetTranslation(
op,
float64(p.Object.Position.X),
float64(p.Object.Position.Y),
),
)
}
func (p *Player) handleControls() {
if ebiten.IsKeyPressed(ebiten.KeyShift) {
p.Sprinting = true
}
if ebiten.IsKeyPressed(ebiten.KeyW) {
p.MoveForward()
}
if ebiten.IsKeyPressed(ebiten.KeyA) {
p.MoveLeft()
}
if ebiten.IsKeyPressed(ebiten.KeyS) {
p.MoveBackward()
}
if ebiten.IsKeyPressed(ebiten.KeyD) {
p.MoveRight()
}
}
// Position returns the Player's current coordinates
func (p *Player) Position() *Coord {
return &Coord{
X: p.Object.Position.X,
Y: p.Object.Position.Y,
}
}