-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasdcontroller.go
More file actions
51 lines (43 loc) · 1.02 KB
/
wasdcontroller.go
File metadata and controls
51 lines (43 loc) · 1.02 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
package engine
import (
"engine/vec2"
"github.com/hajimehoshi/ebiten/v2"
)
type WASDController struct {
BaseNode
controlledPosition *vec2.T
Speed float64
WKey ebiten.Key
AKey ebiten.Key
SKey ebiten.Key
DKey ebiten.Key
}
func NewWASDController(speed float64, controlledPosition *vec2.T) *WASDController {
return &WASDController{
BaseNode: *NewNode("WASDController"),
controlledPosition: controlledPosition,
Speed: speed,
WKey: ebiten.KeyW,
AKey: ebiten.KeyA,
SKey: ebiten.KeyS,
DKey: ebiten.KeyD,
}
}
func (c *WASDController) Update() error {
direction := vec2.T{}
if ebiten.IsKeyPressed(c.AKey) {
direction.Add(vec2.UX().Invert())
}
if ebiten.IsKeyPressed(c.DKey) {
direction.Add(vec2.UX())
}
if ebiten.IsKeyPressed(c.WKey) {
direction.Add(vec2.UY().Invert())
}
if ebiten.IsKeyPressed(c.SKey) {
direction.Add(vec2.UY())
}
direction.Normalize()
c.controlledPosition.Add(direction.Mul(c.Speed))
return nil
}