forked from quasilyte/ebitengine-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
191 lines (161 loc) · 4.75 KB
/
main.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
//go:build example
package main
import (
"fmt"
"image"
"log"
"os"
"strings"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
input "github.com/quasilyte/ebitengine-input"
)
// Note: this example won't work well in browsers (wasm builds)
// due to the fact that browsers don't "connect" the gamepads until
// the user presses any button.
// See _examples/gamepad_in_browser for an example that works around it.
// Define our list of actions as enum-like constants.
//
// Actions usually have more than one key associated with them.
// A key could be a keyboard key, a gamepad key, a mouse button, etc.
//
// When you want to check whether the player is pressing the "fire" key,
// instead of checking the left mouse button directly, you check whether ActionFire is active.
const (
ActionUnknown input.Action = iota
ActionDebug
ActionMoveLeft
ActionMoveRight
ActionExit
ActionTeleport
)
func main() {
ebiten.SetWindowSize(640, 480)
if err := ebiten.RunGame(newExampleGame()); err != nil {
log.Fatal(err)
}
}
type exampleGame struct {
started bool
players []*player
message string
inputHandlers []*input.Handler
inputSystem input.System
}
func newExampleGame() *exampleGame {
g := &exampleGame{}
// The System.Init() should be called exactly once.
g.inputSystem.Init(input.SystemConfig{
DevicesEnabled: input.AnyInput,
})
return g
}
func (g *exampleGame) Layout(outsideWidth, outsideHeight int) (int, int) {
return 640, 480
}
func (g *exampleGame) Draw(screen *ebiten.Image) {
ebitenutil.DebugPrint(screen, g.message)
for _, p := range g.players {
p.Draw(screen)
}
}
func (g *exampleGame) Update() error {
g.inputSystem.Update()
if !g.started {
g.Init()
g.started = true
}
// Treat the first input handler as the main one.
// Only the first player can exit the game.
if g.inputHandlers[0].ActionIsJustPressed(ActionExit) {
os.Exit(0)
}
if g.inputHandlers[0].ActionIsJustPressed(ActionDebug) {
fmt.Println("debug action is pressed")
}
for _, p := range g.players {
p.Update()
}
return nil
}
func (g *exampleGame) Init() {
// We're hardcoding the keymap here, but it could be read from the config file.
keymap := input.Keymap{
// Every action can have a list of keys that can activate it.
// KeyGamepadLStick<Direction> implements a D-pad like events for L/R sticks.
ActionMoveLeft: {input.KeyGamepadLeft, input.KeyGamepadLStickLeft, input.KeyLeft, input.KeyA},
ActionMoveRight: {input.KeyGamepadRight, input.KeyGamepadLStickRight, input.KeyRight, input.KeyD},
ActionExit: {
input.KeyGamepadStart,
input.KeyEscape,
input.KeyWithModifier(input.KeyC, input.ModControl),
},
ActionDebug: {input.KeyControlLeft},
}
// Player 1 will have a teleport ability activated by a mouse click or a touch screen tap.
keymap0 := keymap.Clone()
keymap0[ActionTeleport] = []input.Key{input.KeyMouseLeft, input.KeyTouchTap}
// Prepare the input handlers for all possible player slots.
numGamepads := 0
g.inputHandlers = make([]*input.Handler, 4)
for i := range g.inputHandlers {
m := keymap
if i == 0 {
m = keymap0
}
h := g.inputSystem.NewHandler(i, m)
if h.GamepadConnected() {
numGamepads++
}
g.inputHandlers[i] = h
}
// There can be only one player with keyboard.
// There can be up to 4 players with gamepads.
numPlayers := 1
inputDevice := input.KeyboardInput
if numGamepads != 0 {
inputDevice = input.GamepadInput
numPlayers = numGamepads
}
// Depending on the actual number of players, create
// player objects and give them associated input handlers.
g.players = make([]*player, numPlayers)
pos := image.Point{X: 256, Y: 128}
for i := range g.players {
g.players[i] = &player{
input: g.inputHandlers[i],
pos: pos,
label: fmt.Sprintf("[player%d]", i+1),
}
pos.Y += 64
}
// For the real-world games you will want to map these action key names to
// something more human-readable (you may also want to translate them).
// For simplicity, we'll use them here as is.
messageLines := []string{
"preferred input: " + inputDevice.String(),
"move left: " + strings.Join(g.inputHandlers[0].ActionKeyNames(ActionMoveLeft, inputDevice), " or "),
"move right: " + strings.Join(g.inputHandlers[0].ActionKeyNames(ActionMoveRight, inputDevice), " or "),
}
g.message = strings.Join(messageLines, "\n")
}
type player struct {
label string
input *input.Handler
pos image.Point
}
func (p *player) Update() {
if p.input.ActionIsPressed(ActionMoveLeft) {
p.pos.X -= 4
}
if p.input.ActionIsPressed(ActionMoveRight) {
p.pos.X += 4
}
if info, ok := p.input.JustPressedActionInfo(ActionTeleport); ok {
p.pos.X = int(info.Pos.X)
p.pos.Y = int(info.Pos.Y)
}
}
func (p *player) Draw(screen *ebiten.Image) {
ebitenutil.DebugPrintAt(screen, p.label, p.pos.X, p.pos.Y)
}