-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.go
288 lines (266 loc) · 7.25 KB
/
input.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package input
import (
"github.com/lquesada/cavernal/lib/g3n/engine/util/application"
"github.com/lquesada/cavernal/lib/g3n/engine/window"
"github.com/lquesada/cavernal/lib/g3n/engine/gui"
"github.com/lquesada/cavernal/lib/g3n/engine/math32"
"github.com/lquesada/cavernal/entity"
"github.com/lquesada/cavernal/model"
)
type Input struct{
panel *gui.Panel
player entity.IPlayer
wantToAttack bool
jump bool
wantToAttackCurrent bool
wantToAttackAngle float32
waitingForAngle bool
mousePressed bool
padAttack bool
mouseAngle float32
isometricAngle float32
heightCenterFactor float32
worldRotateAngle float32
shiftAngle float32
hasShiftAngle bool
isoSin float32
isoCos float32
sequenceKeepLooking *model.Sequence
}
func New(panel *gui.Panel, player entity.IPlayer, isometricAngle, heightCenterFactor, minimumWaitAngleAttack float32, worldRotateAngle float32) *Input {
return &Input{
panel: panel,
player: player,
isometricAngle: isometricAngle,
heightCenterFactor: heightCenterFactor,
worldRotateAngle: worldRotateAngle,
isoSin: math32.Sin(isometricAngle),
isoCos: math32.Cos(isometricAngle),
sequenceKeepLooking: model.NewTimer(minimumWaitAngleAttack),
}
}
func (i *Input) Subscribe() {
app := application.Get()
i.panel.SubscribeID(window.OnMouseDown, i, i.mouseDown)
i.panel.SubscribeID(window.OnMouseUp, i, i.mouseUp)
i.panel.SubscribeID(window.OnCursor, i, i.mouseCursor)
app.Window().SubscribeID(window.OnKeyDown, i, i.keyDown)
}
func (i *Input) Unsubscribe() {
app := application.Get()
i.panel.UnsubscribeID(window.OnMouseDown, i)
i.panel.UnsubscribeID(window.OnMouseUp, i)
i.panel.UnsubscribeID(window.OnCursor, i)
app.Window().UnsubscribeID(window.OnKeyDown, i)
}
func (i* Input) mouseDown(name string, ev interface{}) {
e := ev.(*window.MouseEvent)
if e.Button == window.MouseButton1 {
i.wantToAttack = true
// This assumes GUI is at the right side.
i.wantToAttackAngle = i.mousePositionToAngle(e.Xpos, e.Ypos)
i.mousePressed = true
}
}
func (i* Input) mouseUp(name string, ev interface{}) {
e := ev.(*window.MouseEvent)
if e.Button == window.MouseButton1 {
i.mousePressed = false
}
}
func (i* Input) mouseCursor(name string, ev interface{}) {
e := ev.(*window.CursorEvent)
i.mouseAngle = i.mousePositionToAngle(e.Xpos, e.Ypos)
}
func (i *Input) mousePositionToAngle(x, y float32) float32 {
tx := (x - i.panel.Width()/2) / i.isoCos
ty := (y - (i.panel.Height()/2)*i.heightCenterFactor) / i.isoSin
return entity.NormalizeAngle(-math32.Atan2(ty, tx)-i.worldRotateAngle+math32.Pi/2)
}
func (i* Input) keyDown(name string, ev interface{}) {
e := ev.(*window.KeyEvent)
// L to attack in current direction
if e.Keycode == window.KeyL {
i.wantToAttackCurrent = true
return
}
if e.Keycode == window.KeySpace {
i.jump = true
}
if (e.Keycode == window.KeyKP8 ||
e.Keycode == window.KeyKP9 ||
e.Keycode == window.KeyKP6 ||
e.Keycode == window.KeyKP3 ||
e.Keycode == window.KeyKP2 ||
e.Keycode == window.KeyKP1 ||
e.Keycode == window.KeyKP4 ||
e.Keycode == window.KeyKP7 ||
e.Keycode == window.KeyU ||
e.Keycode == window.KeyK ||
e.Keycode == window.KeyJ ||
e.Keycode == window.KeyH) {
i.padAttack = true
}
}
func (i* Input) Apply(delta float32) {
app := application.Get()
shift := app.KeyState().Pressed(window.KeyLeftShift) || app.KeyState().Pressed(window.KeyRightShift)
// Movement is treated as continuous
var x, z float32
if app.KeyState().Pressed(window.KeyUp) || app.KeyState().Pressed(window.KeyW) {
z -= 1
}
if app.KeyState().Pressed(window.KeyRight) || app.KeyState().Pressed(window.KeyD) {
x += 1
}
if app.KeyState().Pressed(window.KeyDown) || app.KeyState().Pressed(window.KeyS) {
z += 1
}
if app.KeyState().Pressed(window.KeyLeft) || app.KeyState().Pressed(window.KeyA) {
x -= 1
}
i.player.MoveIntention().Set(0, 0, 0)
angle := entity.NormalizeAngle(-math32.Atan2(z, x)-i.worldRotateAngle+math32.Pi/2)
if x != 0 || z != 0 {
i.player.MoveIntention().X = math32.Sin(angle)
i.player.MoveIntention().Z = math32.Cos(angle)
i.player.MoveIntention().Normalize()
}
if !i.hasShiftAngle && shift {
if x == 0 && z == 0 {
angle = i.player.LookIntention()
}
i.shiftAngle = angle
i.hasShiftAngle = true
}
if !shift {
i.hasShiftAngle = false
}
// Look intention
if i.sequenceKeepLooking.Running() {
i.sequenceKeepLooking.Add(delta)
}
// Shift keeps look intention
if !shift {
if x != 0 || z != 0 && !i.player.IsAttacking() {
i.player.SetLookIntention(angle)
}
angleDeltaIntention := math32.Abs(entity.NormalizeAngle(i.player.LookAngle()-i.wantToAttackAngle))
tooFarAngle := angleDeltaIntention > 0.0001
if !tooFarAngle || !i.player.IsAttacking() {
i.waitingForAngle = false
}
if i.player.IsAttacking() || (i.waitingForAngle && tooFarAngle) || i.sequenceKeepLooking.Running() {
i.player.SetLookIntention(i.wantToAttackAngle)
}
} else {
i.player.SetLookIntention(i.shiftAngle)
}
// Attack (one-off)
// Input for one-off attack (i.e. KeyDown)
var attack bool
if i.wantToAttackCurrent {
attack = true
i.wantToAttackCurrent = false
if shift {
i.wantToAttackAngle = i.shiftAngle
} else {
i.wantToAttackAngle = angle
}
}
// When using pad
var pX, pZ float32
// Keypad to attack in 8 angles
if app.KeyState().Pressed(window.KeyKP8) {
pZ -= 1
}
if app.KeyState().Pressed(window.KeyKP9) {
pZ -= 1
pX += 1
}
if app.KeyState().Pressed(window.KeyKP6) {
pX += 1
}
if app.KeyState().Pressed(window.KeyKP3) {
pX += 1
pZ += 1
}
if app.KeyState().Pressed(window.KeyKP2) {
pZ += 1
}
if app.KeyState().Pressed(window.KeyKP1) {
pZ += 1
pX -= 1
}
if app.KeyState().Pressed(window.KeyKP4) {
pX -= 1
}
if app.KeyState().Pressed(window.KeyKP7) {
pX -= 1
pZ -= 1
}
// UHJK to attack in 4 angles
if app.KeyState().Pressed(window.KeyU) {
pZ -= 1
}
if app.KeyState().Pressed(window.KeyK) {
pX += 1
}
if app.KeyState().Pressed(window.KeyJ) {
pZ += 1
}
if app.KeyState().Pressed(window.KeyH) {
pX -= 1
}
if i.padAttack {
i.padAttack = false
if pX != 0 || pZ != 0 {
i.wantToAttackAngle = entity.NormalizeAngle(-math32.Atan2(pZ, pX)-i.worldRotateAngle+math32.Pi/2)
i.shiftAngle = i.wantToAttackAngle
i.wantToAttack = true
attack = true
}
}
if i.wantToAttack {
attack = true
i.wantToAttack = false
}
// Attack (repeat)
var repeatAttack bool
if app.KeyState().Pressed(window.KeyL) {
repeatAttack = true
if shift {
i.wantToAttackAngle = i.shiftAngle
} else {
i.wantToAttackAngle = angle
}
}
if pX != 0 || pZ != 0 {
i.wantToAttackAngle = entity.NormalizeAngle(-math32.Atan2(pZ, pX)-i.worldRotateAngle+math32.Pi/2)
repeatAttack = true
}
if i.mousePressed {
repeatAttack = true
i.wantToAttackAngle = i.mouseAngle
}
if attack {
if i.player.WantToAttack() {
i.waitingForAngle = true
i.sequenceKeepLooking.Reset()
i.sequenceKeepLooking.Start()
}
} else if repeatAttack {
if i.player.WantToRepeatAttack() {
i.waitingForAngle = true
i.sequenceKeepLooking.Reset()
i.sequenceKeepLooking.Start()
}
}
// Jump
if i.jump {
i.player.Jump()
i.jump = false
} else if app.KeyState().Pressed(window.KeySpace) {
i.player.RepeatJump()
}
}