/
gamepadDemoController.go
97 lines (86 loc) · 2.73 KB
/
gamepadDemoController.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
package scenes
import (
"github.com/gabz57/goledmatrix/canvas"
"github.com/gabz57/goledmatrix/components"
"github.com/gabz57/goledmatrix/controller"
"image"
"time"
)
type GamepadDemoController struct {
positionable canvas.Positionable
move *components.Movement
}
func NewGamepadDemoController(bounds image.Rectangle, positionable canvas.Positionable) *GamepadDemoController {
return &GamepadDemoController{
positionable: positionable,
move: components.NewBoundedMovement(
positionable.GetPosition().Floating(),
canvas.FloatingPoint{},
nil,
&bounds),
}
}
func (c *GamepadDemoController) HandleKeyboardEvent(event *controller.KeyboardEvent, projection *controller.KeyboardProjection) {
}
func (c *GamepadDemoController) HandleGamepadEvent(event *controller.GamepadEvent, projection *controller.GamepadProjection) {
if event.IsDPad() {
dpadMove(c.move, projection.DPadDirection(), 10.)
} else if event.Name == controller.EventTypeLeftStick {
stickMove(c.move, projection.LeftStick, 150.)
} else if event.Name == controller.EventTypeRightStick {
stickMove(c.move, projection.RightStick, 300.)
}
}
func (c *GamepadDemoController) Update(elapsedBetweenUpdate time.Duration) bool {
nextPosition, _ := c.move.NextPosition(elapsedBetweenUpdate)
next := nextPosition.Int()
current := c.positionable.GetPosition()
if current.X != next.X || current.Y != next.Y {
c.positionable.SetPosition(next)
return true
}
return false
}
func dpadMove(move *components.Movement, direction *float64, pixelPerSecond float64) {
if direction != nil {
directionFP := components.DirectionToFloatingPoint(*direction)
directionFPCorrected := canvas.FloatingPoint{}
if directionFP.X > 0.1 {
directionFPCorrected.X = 1
} else if directionFP.X < -0.1 {
directionFPCorrected.X = -1
}
if directionFP.Y > 0.1 {
directionFPCorrected.Y = 1
} else if directionFP.Y < -0.1 {
directionFPCorrected.Y = -1
}
move.SetVelocity(toVelocity(pixelPerSecond, directionFPCorrected))
} else {
move.SetVelocity(canvas.FloatingPoint{})
}
}
func stickMove(move *components.Movement, stick controller.Stick, pixelPerSecond float64) {
move.SetVelocity(toVelocity(pixelPerSecond, toDirection(stick)))
}
func toVelocity(pixelPerSecond float64, direction canvas.FloatingPoint) canvas.FloatingPoint {
return canvas.FloatingPoint{
X: pixelPerSecond * direction.X,
Y: pixelPerSecond * direction.Y,
}
}
func toDirection(stick controller.Stick) canvas.FloatingPoint {
// remove noise around (0,0)
stickX := int(stick.X) - 128
if stickX >= -10 && stickX < 10 {
stickX = 0
}
stickY := int(stick.Y) - 128
if stickY >= -10 && stickY < 10 {
stickY = 0
}
return canvas.FloatingPoint{
X: float64(stickX) / 255.0,
Y: float64(stickY) / 255.0,
}
}