/
controller.go
124 lines (106 loc) · 3.37 KB
/
controller.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
// (c) 2022 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)
package ebitengine
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/elgopher/pi"
)
func updateController() {
for player := 0; player < 8; player++ {
getController(player).update(player)
}
}
func getController(player int) *controller {
c := controller{&pi.Controllers[player]}
return &c
}
type controller struct {
*pi.Controller
}
func (c *controller) update(player int) {
c.updateDirections(player)
c.updateFireButtons(player)
}
func (c *controller) updateDirections(player int) {
gamepadID := ebiten.GamepadID(player)
axisX := ebiten.StandardGamepadAxisValue(gamepadID, ebiten.StandardGamepadAxisLeftStickHorizontal)
axisY := ebiten.StandardGamepadAxisValue(gamepadID, ebiten.StandardGamepadAxisLeftStickVertical)
if axisX < -0.5 ||
ebiten.IsStandardGamepadButtonPressed(gamepadID, ebiten.StandardGamepadButtonLeftLeft) ||
isKeyboardPressed(player, pi.Left) {
c.BtnDuration[pi.Left] += 1
c.BtnDuration[pi.Right] = 0
} else if axisX > 0.5 ||
ebiten.IsStandardGamepadButtonPressed(gamepadID, ebiten.StandardGamepadButtonLeftRight) ||
isKeyboardPressed(player, pi.Right) {
c.BtnDuration[pi.Right] += 1
c.BtnDuration[pi.Left] = 0
} else {
c.BtnDuration[pi.Right] = 0
c.BtnDuration[pi.Left] = 0
}
if axisY < -0.5 ||
ebiten.IsStandardGamepadButtonPressed(gamepadID, ebiten.StandardGamepadButtonLeftTop) ||
isKeyboardPressed(player, pi.Up) {
c.BtnDuration[pi.Up] += 1
c.BtnDuration[pi.Down] = 0
} else if axisY > 0.5 ||
ebiten.IsStandardGamepadButtonPressed(gamepadID, ebiten.StandardGamepadButtonLeftBottom) ||
isKeyboardPressed(player, pi.Down) {
c.BtnDuration[pi.Down] += 1
c.BtnDuration[pi.Up] = 0
} else {
c.BtnDuration[pi.Up] = 0
c.BtnDuration[pi.Down] = 0
}
}
func (c *controller) updateFireButtons(player int) {
gamepadID := ebiten.GamepadID(player)
if ebiten.IsStandardGamepadButtonPressed(gamepadID, ebiten.StandardGamepadButtonRightBottom) ||
ebiten.IsStandardGamepadButtonPressed(gamepadID, ebiten.StandardGamepadButtonRightTop) ||
isKeyboardPressed(player, pi.O) {
c.BtnDuration[pi.O] += 1
} else {
c.BtnDuration[pi.O] = 0
}
if ebiten.IsStandardGamepadButtonPressed(gamepadID, ebiten.StandardGamepadButtonRightRight) ||
ebiten.IsStandardGamepadButtonPressed(gamepadID, ebiten.StandardGamepadButtonRightLeft) ||
isKeyboardPressed(player, pi.X) {
c.BtnDuration[pi.X] += 1
} else {
c.BtnDuration[pi.X] = 0
}
}
// first array is player, then π key, then slice of Ebitengine keys.
var keyboardMapping = [...][6][]ebiten.Key{
// player0:
{
{ebiten.KeyLeft}, // left
{ebiten.KeyRight}, // right
{ebiten.KeyUp}, // up
{ebiten.KeyDown}, // down
{ebiten.KeyZ, ebiten.KeyC, ebiten.KeyN}, // o
{ebiten.KeyX, ebiten.KeyV, ebiten.KeyM}, // x
},
// player1:
{
{ebiten.KeyS}, // left
{ebiten.KeyF}, // right
{ebiten.KeyE}, // up
{ebiten.KeyD}, // down
{ebiten.KeyShiftLeft}, // o
{ebiten.KeyTab, ebiten.KeyW, ebiten.KeyQ, ebiten.KeyA}, // x
},
}
func isKeyboardPressed(player int, button pi.Button) bool {
if player >= len(keyboardMapping) {
return false
}
keys := keyboardMapping[player][button]
for _, k := range keys {
if ebiten.IsKeyPressed(k) {
return true
}
}
return false
}