@rpgjs/client 3.3.2 → 4.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/lib/Components/AbstractComponent.d.ts +3 -2
- package/lib/Components/AbstractComponent.js +17 -28
- package/lib/Components/AbstractComponent.js.map +1 -1
- package/lib/Components/BarComponent.d.ts +2 -1
- package/lib/Components/BarComponent.js +32 -33
- package/lib/Components/BarComponent.js.map +1 -1
- package/lib/Components/Component.d.ts +3 -3
- package/lib/Components/Component.js +84 -96
- package/lib/Components/Component.js.map +1 -1
- package/lib/Components/DebugComponent.d.ts +2 -1
- package/lib/Components/DebugComponent.js +9 -10
- package/lib/Components/DebugComponent.js.map +1 -1
- package/lib/Components/ImageComponent.d.ts +2 -1
- package/lib/Components/ImageComponent.js +5 -7
- package/lib/Components/ImageComponent.js.map +1 -1
- package/lib/Components/ShapeComponent.d.ts +2 -1
- package/lib/Components/ShapeComponent.js +14 -14
- package/lib/Components/ShapeComponent.js.map +1 -1
- package/lib/Components/TextComponent.d.ts +3 -2
- package/lib/Components/TextComponent.js +10 -9
- package/lib/Components/TextComponent.js.map +1 -1
- package/lib/Components/TileComponent.d.ts +2 -1
- package/lib/Components/TileComponent.js +12 -16
- package/lib/Components/TileComponent.js.map +1 -1
- package/lib/Effects/Animation.d.ts +10 -9
- package/lib/Effects/Animation.js +36 -36
- package/lib/Effects/Animation.js.map +1 -1
- package/lib/Effects/AnimationCharacter.js +2 -5
- package/lib/Effects/AnimationCharacter.js.map +1 -1
- package/lib/Effects/Spinner.d.ts +3 -2
- package/lib/Effects/Spinner.js +2 -5
- package/lib/Effects/Spinner.js.map +1 -1
- package/lib/Effects/Timeline.d.ts +1 -1
- package/lib/Effects/Timeline.js +6 -10
- package/lib/Effects/Timeline.js.map +1 -1
- package/lib/Effects/TransitionScene.d.ts +2 -1
- package/lib/Effects/TransitionScene.js +3 -7
- package/lib/Effects/TransitionScene.js.map +1 -1
- package/lib/GameEngine.js +51 -26
- package/lib/GameEngine.js.map +1 -1
- package/lib/Interfaces/Character.js +1 -2
- package/lib/Interfaces/Scene.js +1 -2
- package/lib/KeyboardControls.d.ts +2 -1
- package/lib/KeyboardControls.js +47 -45
- package/lib/KeyboardControls.js.map +1 -1
- package/lib/Logger.js +1 -5
- package/lib/Logger.js.map +1 -1
- package/lib/Presets/AnimationSpritesheet.js +9 -13
- package/lib/Presets/AnimationSpritesheet.js.map +1 -1
- package/lib/Presets/Scene.js +2 -5
- package/lib/Presets/Scene.js.map +1 -1
- package/lib/Renderer.d.ts +5 -3
- package/lib/Renderer.js +68 -60
- package/lib/Renderer.js.map +1 -1
- package/lib/Resources.js +1 -5
- package/lib/Resources.js.map +1 -1
- package/lib/RpgClient.d.ts +3 -2
- package/lib/RpgClient.js +1 -2
- package/lib/RpgClientEngine.d.ts +2 -1
- package/lib/RpgClientEngine.js +192 -176
- package/lib/RpgClientEngine.js.map +1 -1
- package/lib/RpgGui.js +40 -41
- package/lib/RpgGui.js.map +1 -1
- package/lib/Scene/EventLayer.d.ts +4 -0
- package/lib/Scene/EventLayer.js +8 -0
- package/lib/Scene/EventLayer.js.map +1 -0
- package/lib/Scene/Map.d.ts +17 -3
- package/lib/Scene/Map.js +157 -113
- package/lib/Scene/Map.js.map +1 -1
- package/lib/Scene/Scene.d.ts +3 -3
- package/lib/Scene/Scene.js +24 -20
- package/lib/Scene/Scene.js.map +1 -1
- package/lib/Scene/SceneData.js +1 -5
- package/lib/Scene/SceneData.js.map +1 -1
- package/lib/Sound/RpgSound.js +8 -11
- package/lib/Sound/RpgSound.js.map +1 -1
- package/lib/Sound/Sound.d.ts +2 -2
- package/lib/Sound/Sound.js +1 -5
- package/lib/Sound/Sound.js.map +1 -1
- package/lib/Sound/Sounds.js +4 -8
- package/lib/Sound/Sounds.js.map +1 -1
- package/lib/Sprite/Character.d.ts +3 -2
- package/lib/Sprite/Character.js +15 -16
- package/lib/Sprite/Character.js.map +1 -1
- package/lib/Sprite/Player.js +2 -9
- package/lib/Sprite/Player.js.map +1 -1
- package/lib/Sprite/Spritesheet.d.ts +4 -4
- package/lib/Sprite/Spritesheet.js +1 -5
- package/lib/Sprite/Spritesheet.js.map +1 -1
- package/lib/Sprite/Spritesheets.js +4 -8
- package/lib/Sprite/Spritesheets.js.map +1 -1
- package/lib/Tilemap/CommonLayer.d.ts +2 -1
- package/lib/Tilemap/CommonLayer.js +7 -11
- package/lib/Tilemap/CommonLayer.js.map +1 -1
- package/lib/Tilemap/ImageLayer.js +5 -7
- package/lib/Tilemap/ImageLayer.js.map +1 -1
- package/lib/Tilemap/Tile.d.ts +5 -5
- package/lib/Tilemap/Tile.js +19 -21
- package/lib/Tilemap/Tile.js.map +1 -1
- package/lib/Tilemap/TileLayer.d.ts +0 -7
- package/lib/Tilemap/TileLayer.js +27 -29
- package/lib/Tilemap/TileLayer.js.map +1 -1
- package/lib/Tilemap/TileSet.d.ts +2 -1
- package/lib/Tilemap/TileSet.js +9 -12
- package/lib/Tilemap/TileSet.js.map +1 -1
- package/lib/Tilemap/index.d.ts +3 -11
- package/lib/Tilemap/index.js +22 -61
- package/lib/Tilemap/index.js.map +1 -1
- package/lib/clientEntryPoint.js +26 -28
- package/lib/clientEntryPoint.js.map +1 -1
- package/lib/index.js +21 -75
- package/lib/index.js.map +1 -1
- package/package.json +19 -19
- package/src/Components/AbstractComponent.ts +120 -0
- package/src/Components/BarComponent.ts +179 -0
- package/src/Components/Component.ts +506 -0
- package/src/Components/DebugComponent.ts +36 -0
- package/src/Components/ImageComponent.ts +30 -0
- package/src/Components/ShapeComponent.ts +64 -0
- package/src/Components/TextComponent.ts +33 -0
- package/src/Components/TileComponent.ts +43 -0
- package/src/Effects/Animation.ts +297 -0
- package/src/Effects/AnimationCharacter.ts +7 -0
- package/src/Effects/Spinner.ts +19 -0
- package/src/Effects/Timeline.ts +294 -0
- package/src/Effects/TransitionScene.ts +57 -0
- package/src/GameEngine.ts +284 -0
- package/src/Interfaces/Character.ts +7 -0
- package/src/Interfaces/Scene.ts +9 -0
- package/src/KeyboardControls.ts +552 -0
- package/src/Logger.ts +3 -0
- package/src/Presets/AnimationSpritesheet.ts +36 -0
- package/src/Presets/Scene.ts +3 -0
- package/src/Renderer.ts +263 -0
- package/src/Resources.ts +40 -0
- package/src/RpgClient.ts +333 -0
- package/src/RpgClientEngine.ts +709 -0
- package/src/RpgGui.ts +553 -0
- package/src/RpgGuiCompiled.ts +43 -0
- package/src/Scene/EventLayer.ts +9 -0
- package/src/Scene/Map.ts +393 -0
- package/src/Scene/Scene.ts +270 -0
- package/src/Scene/SceneData.ts +13 -0
- package/src/Sound/RpgSound.ts +50 -0
- package/src/Sound/Sound.ts +91 -0
- package/src/Sound/Sounds.ts +7 -0
- package/src/Sprite/Character.ts +149 -0
- package/src/Sprite/Player.ts +3 -0
- package/src/Sprite/Spritesheet.ts +392 -0
- package/src/Sprite/Spritesheets.ts +8 -0
- package/src/Tilemap/CommonLayer.ts +20 -0
- package/src/Tilemap/ImageLayer.ts +20 -0
- package/src/Tilemap/Tile.ts +80 -0
- package/src/Tilemap/TileLayer.ts +142 -0
- package/src/Tilemap/TileSet.ts +40 -0
- package/src/Tilemap/index.ts +173 -0
- package/src/clientEntryPoint.ts +141 -0
- package/src/index.ts +25 -0
- package/src/types/howler.d.ts +73 -0
- package/tsconfig.json +30 -0
- package/lib/Components/ColorComponent.d.ts +0 -9
- package/lib/Components/ColorComponent.js +0 -18
- package/lib/Components/ColorComponent.js.map +0 -1
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
import { DefaultInput, Direction, Input, Utils } from '@rpgjs/common'
|
|
2
|
+
import { ControlOptions, Controls } from '@rpgjs/types';
|
|
3
|
+
import { RpgClientEngine } from './RpgClientEngine';
|
|
4
|
+
|
|
5
|
+
// keyboard handling
|
|
6
|
+
const keyCodeTable = {
|
|
7
|
+
3: 'break',
|
|
8
|
+
8: 'backspace', // backspace / delete
|
|
9
|
+
9: 'tab',
|
|
10
|
+
12: 'clear',
|
|
11
|
+
13: 'enter',
|
|
12
|
+
16: 'shift',
|
|
13
|
+
17: 'ctrl',
|
|
14
|
+
18: 'alt',
|
|
15
|
+
19: 'pause/break',
|
|
16
|
+
20: 'caps lock',
|
|
17
|
+
27: 'escape',
|
|
18
|
+
28: 'conversion',
|
|
19
|
+
29: 'non-conversion',
|
|
20
|
+
32: 'space',
|
|
21
|
+
33: 'page up',
|
|
22
|
+
34: 'page down',
|
|
23
|
+
35: 'end',
|
|
24
|
+
36: 'home',
|
|
25
|
+
37: Direction.Left,
|
|
26
|
+
38: Direction.Up,
|
|
27
|
+
39: Direction.Right,
|
|
28
|
+
40: Direction.Down,
|
|
29
|
+
41: 'select',
|
|
30
|
+
42: 'print',
|
|
31
|
+
43: 'execute',
|
|
32
|
+
44: 'Print Screen',
|
|
33
|
+
45: 'insert',
|
|
34
|
+
46: 'delete',
|
|
35
|
+
48: '0',
|
|
36
|
+
49: '1',
|
|
37
|
+
50: '2',
|
|
38
|
+
51: '3',
|
|
39
|
+
52: '4',
|
|
40
|
+
53: '5',
|
|
41
|
+
54: '6',
|
|
42
|
+
55: '7',
|
|
43
|
+
56: '8',
|
|
44
|
+
57: '9',
|
|
45
|
+
58: ':',
|
|
46
|
+
59: 'semicolon (firefox), equals',
|
|
47
|
+
60: '<',
|
|
48
|
+
61: 'equals (firefox)',
|
|
49
|
+
63: 'ß',
|
|
50
|
+
64: '@',
|
|
51
|
+
65: 'a',
|
|
52
|
+
66: 'b',
|
|
53
|
+
67: 'c',
|
|
54
|
+
68: 'd',
|
|
55
|
+
69: 'e',
|
|
56
|
+
70: 'f',
|
|
57
|
+
71: 'g',
|
|
58
|
+
72: 'h',
|
|
59
|
+
73: 'i',
|
|
60
|
+
74: 'j',
|
|
61
|
+
75: 'k',
|
|
62
|
+
76: 'l',
|
|
63
|
+
77: 'm',
|
|
64
|
+
78: 'n',
|
|
65
|
+
79: 'o',
|
|
66
|
+
80: 'p',
|
|
67
|
+
81: 'q',
|
|
68
|
+
82: 'r',
|
|
69
|
+
83: 's',
|
|
70
|
+
84: 't',
|
|
71
|
+
85: 'u',
|
|
72
|
+
86: 'v',
|
|
73
|
+
87: 'w',
|
|
74
|
+
88: 'x',
|
|
75
|
+
89: 'y',
|
|
76
|
+
90: 'z',
|
|
77
|
+
91: 'Windows Key / Left ⌘ / Chromebook Search key',
|
|
78
|
+
92: 'right window key',
|
|
79
|
+
93: 'Windows Menu / Right ⌘',
|
|
80
|
+
96: 'numpad 0',
|
|
81
|
+
97: 'numpad 1',
|
|
82
|
+
98: 'numpad 2',
|
|
83
|
+
99: 'numpad 3',
|
|
84
|
+
100: 'numpad 4',
|
|
85
|
+
101: 'numpad 5',
|
|
86
|
+
102: 'numpad 6',
|
|
87
|
+
103: 'numpad 7',
|
|
88
|
+
104: 'numpad 8',
|
|
89
|
+
105: 'numpad 9',
|
|
90
|
+
106: 'multiply',
|
|
91
|
+
107: 'add',
|
|
92
|
+
108: 'numpad period (firefox)',
|
|
93
|
+
109: 'subtract',
|
|
94
|
+
110: 'decimal point',
|
|
95
|
+
111: 'divide',
|
|
96
|
+
112: 'f1',
|
|
97
|
+
113: 'f2',
|
|
98
|
+
114: 'f3',
|
|
99
|
+
115: 'f4',
|
|
100
|
+
116: 'f5',
|
|
101
|
+
117: 'f6',
|
|
102
|
+
118: 'f7',
|
|
103
|
+
119: 'f8',
|
|
104
|
+
120: 'f9',
|
|
105
|
+
121: 'f10',
|
|
106
|
+
122: 'f11',
|
|
107
|
+
123: 'f12',
|
|
108
|
+
124: 'f13',
|
|
109
|
+
125: 'f14',
|
|
110
|
+
126: 'f15',
|
|
111
|
+
127: 'f16',
|
|
112
|
+
128: 'f17',
|
|
113
|
+
129: 'f18',
|
|
114
|
+
130: 'f19',
|
|
115
|
+
131: 'f20',
|
|
116
|
+
132: 'f21',
|
|
117
|
+
133: 'f22',
|
|
118
|
+
134: 'f23',
|
|
119
|
+
135: 'f24',
|
|
120
|
+
144: 'num lock',
|
|
121
|
+
145: 'scroll lock',
|
|
122
|
+
160: '^',
|
|
123
|
+
161: '!',
|
|
124
|
+
163: '#',
|
|
125
|
+
164: '$',
|
|
126
|
+
165: 'ù',
|
|
127
|
+
166: 'page backward',
|
|
128
|
+
167: 'page forward',
|
|
129
|
+
169: 'closing paren (AZERTY)',
|
|
130
|
+
170: '*',
|
|
131
|
+
171: '~ + * key',
|
|
132
|
+
173: 'minus (firefox), mute/unmute',
|
|
133
|
+
174: 'decrease volume level',
|
|
134
|
+
175: 'increase volume level',
|
|
135
|
+
176: 'next',
|
|
136
|
+
177: 'previous',
|
|
137
|
+
178: 'stop',
|
|
138
|
+
179: 'play/pause',
|
|
139
|
+
180: 'e-mail',
|
|
140
|
+
181: 'mute/unmute (firefox)',
|
|
141
|
+
182: 'decrease volume level (firefox)',
|
|
142
|
+
183: 'increase volume level (firefox)',
|
|
143
|
+
186: 'semi-colon / ñ',
|
|
144
|
+
187: 'equal sign',
|
|
145
|
+
188: 'comma',
|
|
146
|
+
189: 'dash',
|
|
147
|
+
190: 'period',
|
|
148
|
+
191: 'forward slash / ç',
|
|
149
|
+
192: 'grave accent / ñ / æ',
|
|
150
|
+
193: '?, / or °',
|
|
151
|
+
194: 'numpad period (chrome)',
|
|
152
|
+
219: 'open bracket',
|
|
153
|
+
220: 'back slash',
|
|
154
|
+
221: 'close bracket / å',
|
|
155
|
+
222: 'single quote / ø',
|
|
156
|
+
223: '`',
|
|
157
|
+
224: 'left or right ⌘ key (firefox)',
|
|
158
|
+
225: 'altgr',
|
|
159
|
+
226: '< /git >',
|
|
160
|
+
230: 'GNOME Compose Key',
|
|
161
|
+
231: 'ç',
|
|
162
|
+
233: 'XF86Forward',
|
|
163
|
+
234: 'XF86Back',
|
|
164
|
+
240: 'alphanumeric',
|
|
165
|
+
242: 'hiragana/katakana',
|
|
166
|
+
243: 'half-width/full-width',
|
|
167
|
+
244: 'kanji',
|
|
168
|
+
255: 'toggle touchpad'
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const directionCode = {
|
|
172
|
+
[Direction.Up]: 1,
|
|
173
|
+
[Direction.Right]: 2,
|
|
174
|
+
[Direction.Down]: 3,
|
|
175
|
+
[Direction.Left]: 4
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const { isArray } = Utils
|
|
179
|
+
|
|
180
|
+
const inverse = (obj) => {
|
|
181
|
+
const newObj = {}
|
|
182
|
+
for (let key in obj) {
|
|
183
|
+
const val = obj[key]
|
|
184
|
+
newObj[val] = key
|
|
185
|
+
}
|
|
186
|
+
return newObj
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const inverseKeyCodeTable = inverse(keyCodeTable)
|
|
190
|
+
|
|
191
|
+
type BoundKey = { actionName: string, options: ControlOptions, parameters?: any }
|
|
192
|
+
|
|
193
|
+
export class KeyboardControls {
|
|
194
|
+
private keyState: {
|
|
195
|
+
[keyName: string]: {
|
|
196
|
+
isDown: boolean,
|
|
197
|
+
count: number
|
|
198
|
+
} | null
|
|
199
|
+
} = {}
|
|
200
|
+
private boundKeys: {
|
|
201
|
+
[keyName: string]: BoundKey
|
|
202
|
+
} = {}
|
|
203
|
+
private stop: boolean = false
|
|
204
|
+
private lastKeyPressed: number | null = null
|
|
205
|
+
private _controlsOptions: Controls = {}
|
|
206
|
+
|
|
207
|
+
constructor(private clientEngine: RpgClientEngine) {
|
|
208
|
+
const { globalConfig } = clientEngine
|
|
209
|
+
this.setupListeners();
|
|
210
|
+
this.setInputs({
|
|
211
|
+
...DefaultInput,
|
|
212
|
+
...(globalConfig.inputs || {})
|
|
213
|
+
})
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** @internal */
|
|
217
|
+
preStep() {
|
|
218
|
+
//this.directionToAngle()
|
|
219
|
+
if (this.stop) return
|
|
220
|
+
const boundKeys = Object.keys(this.boundKeys)
|
|
221
|
+
const applyInput = (keyName: string) => {
|
|
222
|
+
const keyState = this.keyState[keyName]
|
|
223
|
+
if (!keyState) return
|
|
224
|
+
const { isDown, count } = keyState
|
|
225
|
+
if (isDown) {
|
|
226
|
+
const { repeat, method } = this.boundKeys[keyName].options
|
|
227
|
+
if ((repeat || count == 0)) {
|
|
228
|
+
let parameters = this.boundKeys[keyName].parameters
|
|
229
|
+
if (typeof parameters === "function") {
|
|
230
|
+
parameters = parameters();
|
|
231
|
+
}
|
|
232
|
+
if (method) {
|
|
233
|
+
method(this.boundKeys[keyName])
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
this.clientEngine.sendInput(this.boundKeys[keyName].actionName)
|
|
237
|
+
}
|
|
238
|
+
this.keyState[keyName]!.count++
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
for (let keyName of boundKeys) {
|
|
243
|
+
applyInput(keyName)
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// TODO, merge direction
|
|
248
|
+
private directionToAngle() {
|
|
249
|
+
let directionVal = 0
|
|
250
|
+
let nbFound = 0
|
|
251
|
+
for (let keyName of Object.keys(this.boundKeys)) {
|
|
252
|
+
if (this.keyState[keyName]?.isDown) {
|
|
253
|
+
if (directionCode[keyName]) {
|
|
254
|
+
this.keyState[keyName] = null
|
|
255
|
+
directionVal += directionCode[keyName]
|
|
256
|
+
nbFound++
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (!nbFound) return
|
|
261
|
+
|
|
262
|
+
const index = directionVal / nbFound
|
|
263
|
+
|
|
264
|
+
if (this.keyState[index] !== null) {
|
|
265
|
+
this.keyState[index] = {
|
|
266
|
+
count: 0,
|
|
267
|
+
isDown: true
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
this.keyState[index]!.isDown = true
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private setupListeners() {
|
|
275
|
+
document.addEventListener('keydown', (e) => { this.onKeyChange(e, true); });
|
|
276
|
+
document.addEventListener('keyup', (e) => { this.onKeyChange(e, false); });
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
private bindKey(keys: Input | Input[], actionName: string, options: ControlOptions, parameters?: object) {
|
|
280
|
+
if (!isArray(keys)) keys = [keys] as Input[]
|
|
281
|
+
const keyOptions = Object.assign({
|
|
282
|
+
repeat: false
|
|
283
|
+
}, options);
|
|
284
|
+
(keys as Input[]).forEach(keyName => {
|
|
285
|
+
this.boundKeys[keyName] = { actionName, options: keyOptions, parameters }
|
|
286
|
+
})
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private applyKeyDown(name: string) {
|
|
290
|
+
const code = inverseKeyCodeTable[name]
|
|
291
|
+
const e: any = new Event('keydown')
|
|
292
|
+
e.keyCode = code
|
|
293
|
+
this.onKeyChange(e, true)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private applyKeyUp(name: string) {
|
|
297
|
+
const code = inverseKeyCodeTable[name]
|
|
298
|
+
const e: any = new Event('keyup')
|
|
299
|
+
e.keyCode = code
|
|
300
|
+
this.onKeyChange(e, false)
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
private applyKeyPress(name: string): Promise<void> {
|
|
304
|
+
return new Promise((resolve: any) => {
|
|
305
|
+
this.applyKeyDown(name)
|
|
306
|
+
setTimeout(() => {
|
|
307
|
+
this.applyKeyUp(name)
|
|
308
|
+
resolve()
|
|
309
|
+
}, 200)
|
|
310
|
+
})
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
private onKeyChange(e: KeyboardEvent, isDown: boolean) {
|
|
314
|
+
e = e || window.event;
|
|
315
|
+
|
|
316
|
+
const keyName: string = keyCodeTable[e.keyCode];
|
|
317
|
+
|
|
318
|
+
if (keyName && this.boundKeys[keyName]) {
|
|
319
|
+
if (this.keyState[keyName] == null) {
|
|
320
|
+
this.keyState[keyName] = {
|
|
321
|
+
count: 0,
|
|
322
|
+
isDown: true
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
this.keyState[keyName]!.isDown = isDown;
|
|
326
|
+
|
|
327
|
+
// key up, reset press count
|
|
328
|
+
if (!isDown) {
|
|
329
|
+
this.keyState[keyName]!.count = 0
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// keep reference to the last key pressed to avoid duplicates
|
|
333
|
+
this.lastKeyPressed = isDown ? e.keyCode : null;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (isDown) this.clientEngine.keyChange.next(keyName)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* From the name of the entry, we retrieve the control information
|
|
341
|
+
*
|
|
342
|
+
* ```ts
|
|
343
|
+
* import { Input } from '@rpgjs/client'
|
|
344
|
+
*
|
|
345
|
+
* // In method hooks, client is RpgClientEngine
|
|
346
|
+
* client.controls.getControl(Input.Enter)
|
|
347
|
+
* if (control) {
|
|
348
|
+
* console.log(control.actionName) // action
|
|
349
|
+
* }
|
|
350
|
+
* ```
|
|
351
|
+
* @title Get Control
|
|
352
|
+
* @method getControl(inputName)
|
|
353
|
+
* @param {string} inputName
|
|
354
|
+
* @returns { { actionName: string, options: any } | undefined }
|
|
355
|
+
* @memberof KeyboardControls
|
|
356
|
+
*/
|
|
357
|
+
getControl(inputName: string): BoundKey | undefined {
|
|
358
|
+
return this.boundKeys[inputName]
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Triggers an input according to the name of the control
|
|
363
|
+
*
|
|
364
|
+
* ```ts
|
|
365
|
+
* import { Control } from '@rpgjs/client'
|
|
366
|
+
*
|
|
367
|
+
* // In method hooks, client is RpgClientEngine
|
|
368
|
+
* client.controls.applyControl(Control.Action)
|
|
369
|
+
* ```
|
|
370
|
+
*
|
|
371
|
+
* You can put a second parameter or indicate on whether the key is pressed or released
|
|
372
|
+
*
|
|
373
|
+
* ```ts
|
|
374
|
+
* import { Control } from '@rpgjs/client'
|
|
375
|
+
*
|
|
376
|
+
* client.controls.applyControl(Control.Up, true) // keydown
|
|
377
|
+
* client.controls.applyControl(Control.Up, false) // keyup
|
|
378
|
+
* ```
|
|
379
|
+
* @title Apply Control
|
|
380
|
+
* @method applyControl(controlName,isDown)
|
|
381
|
+
* @param {string} controlName
|
|
382
|
+
* @param {boolean} [isDown]
|
|
383
|
+
* @returns {Promise<void>}
|
|
384
|
+
* @memberof KeyboardControls
|
|
385
|
+
*/
|
|
386
|
+
async applyControl(controlName: string | number, isDown?: boolean | undefined): Promise<void> {
|
|
387
|
+
const control = this._controlsOptions[controlName]
|
|
388
|
+
if (control) {
|
|
389
|
+
const input = isArray(control.bind) ? control.bind[0] : control.bind
|
|
390
|
+
if (isDown === undefined) {
|
|
391
|
+
await this.applyKeyPress(input as string)
|
|
392
|
+
}
|
|
393
|
+
else if (isDown) {
|
|
394
|
+
this.applyKeyDown(input as string)
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
this.applyKeyUp(input as string)
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Stop listening to the inputs. Pressing a key won't do anything
|
|
404
|
+
*
|
|
405
|
+
* @title Stop Inputs
|
|
406
|
+
* @method stopInputs()
|
|
407
|
+
* @returns {void}
|
|
408
|
+
* @memberof KeyboardControls
|
|
409
|
+
*/
|
|
410
|
+
stopInputs() {
|
|
411
|
+
this.stop = true
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Listen to the inputs again
|
|
416
|
+
*
|
|
417
|
+
* @title Listen Inputs
|
|
418
|
+
* @method listenInputs()
|
|
419
|
+
* @returns {void}
|
|
420
|
+
* @memberof KeyboardControls
|
|
421
|
+
*/
|
|
422
|
+
listenInputs() {
|
|
423
|
+
this.stop = false
|
|
424
|
+
this.keyState = {}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Assign custom inputs to the scene
|
|
429
|
+
*
|
|
430
|
+
* The object is the following:
|
|
431
|
+
*
|
|
432
|
+
* * the key of the object is the name of the control. Either it is existing controls (Up, Dow, Left, Right, Action, Back) or customized controls
|
|
433
|
+
* * The value is an object representing control information:
|
|
434
|
+
* * repeat {boolean} The key can be held down to repeat the action. (false by default)
|
|
435
|
+
* * bind {string | string[]} To which key is linked the control
|
|
436
|
+
* * method {Function} Function to be triggered. If you do not set this property, the name of the control is sent directly to the server.
|
|
437
|
+
* * delay {object|number} (since v3.2.0) Indicates how long (in milliseconds) the player can press the key again to perform the action
|
|
438
|
+
* * delay.duration
|
|
439
|
+
* * delay.otherControls {string | string[]} Indicates the other controls that will also have the delay at the same time
|
|
440
|
+
*
|
|
441
|
+
* ```ts
|
|
442
|
+
* import { Control, Input } from '@rpgjs/client'
|
|
443
|
+
*
|
|
444
|
+
* // In method hooks, client is RpgClientEngine
|
|
445
|
+
* client.controls.setInputs({
|
|
446
|
+
[Control.Up]: {
|
|
447
|
+
repeat: true,
|
|
448
|
+
bind: Input.Up
|
|
449
|
+
},
|
|
450
|
+
[Control.Down]: {
|
|
451
|
+
repeat: true,
|
|
452
|
+
bind: Input.Down
|
|
453
|
+
},
|
|
454
|
+
[Control.Right]: {
|
|
455
|
+
repeat: true,
|
|
456
|
+
bind: Input.Right
|
|
457
|
+
},
|
|
458
|
+
[Control.Left]: {
|
|
459
|
+
repeat: true,
|
|
460
|
+
bind: Input.Left
|
|
461
|
+
},
|
|
462
|
+
[Control.Action]: {
|
|
463
|
+
bind: [Input.Space, Input.Enter]
|
|
464
|
+
},
|
|
465
|
+
[Control.Back]: {
|
|
466
|
+
bind: Input.Escape
|
|
467
|
+
},
|
|
468
|
+
|
|
469
|
+
// The myscustom1 control is sent to the server when the A key is pressed.
|
|
470
|
+
mycustom1: {
|
|
471
|
+
bind: Input.A
|
|
472
|
+
},
|
|
473
|
+
|
|
474
|
+
// the myAction method is executed when the B key is pressed
|
|
475
|
+
mycustom2: {
|
|
476
|
+
bind: Input.B,
|
|
477
|
+
method({ actionName }) {
|
|
478
|
+
console.log('cool', actionName)
|
|
479
|
+
}
|
|
480
|
+
},
|
|
481
|
+
|
|
482
|
+
// The player can redo the action after 400ms
|
|
483
|
+
mycustom3: {
|
|
484
|
+
bind: Input.C,
|
|
485
|
+
delay: 400 // ms
|
|
486
|
+
},
|
|
487
|
+
|
|
488
|
+
// The player can redo the action (mycustom4) and the directions after 400ms
|
|
489
|
+
mycustom4: {
|
|
490
|
+
bind: Input.C,
|
|
491
|
+
delay: {
|
|
492
|
+
duration: 400,
|
|
493
|
+
otherControls: [Control.Up, Control.Down, Control.Left, Control.Right]
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
})
|
|
497
|
+
*
|
|
498
|
+
* ```
|
|
499
|
+
* @enum {string} Control
|
|
500
|
+
*
|
|
501
|
+
* Control.Up | up
|
|
502
|
+
* Control.Down | down
|
|
503
|
+
* Control.Left | left
|
|
504
|
+
* Control.Right | right
|
|
505
|
+
* Control.Action | action
|
|
506
|
+
* Control.Back | back
|
|
507
|
+
* @title Set Inputs
|
|
508
|
+
* @method setInputs(inputs)
|
|
509
|
+
* @param {object} inputs
|
|
510
|
+
* @memberof KeyboardControls
|
|
511
|
+
*/
|
|
512
|
+
setInputs(inputs: Controls) {
|
|
513
|
+
if (!inputs) return
|
|
514
|
+
this.boundKeys = {}
|
|
515
|
+
let inputsTransformed: any = {}
|
|
516
|
+
for (let control in inputs) {
|
|
517
|
+
inputsTransformed[this.transformDirectionInNumber(control)] = {
|
|
518
|
+
...inputs[control],
|
|
519
|
+
bind: this.transformDirectionInNumber(inputs[control].bind)
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
for (let control in inputsTransformed) {
|
|
523
|
+
const option = inputsTransformed[control]
|
|
524
|
+
const { method, bind } = option
|
|
525
|
+
if (method) {
|
|
526
|
+
option.method = method
|
|
527
|
+
}
|
|
528
|
+
let inputsKey: any = bind
|
|
529
|
+
if (!isArray(inputsKey)) {
|
|
530
|
+
inputsKey = [bind]
|
|
531
|
+
}
|
|
532
|
+
for (let input of inputsKey) {
|
|
533
|
+
this.bindKey(input, control, option)
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
this._controlsOptions = inputsTransformed
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
get options(): Controls {
|
|
540
|
+
return this._controlsOptions
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
private transformDirectionInNumber(direction: any): any {
|
|
544
|
+
switch (direction) {
|
|
545
|
+
case 'up': return 1
|
|
546
|
+
case 'down': return 3
|
|
547
|
+
case 'left': return 4
|
|
548
|
+
case 'right': return 2
|
|
549
|
+
}
|
|
550
|
+
return direction
|
|
551
|
+
}
|
|
552
|
+
}
|
package/src/Logger.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Direction } from '@rpgjs/common'
|
|
2
|
+
import { Animation } from '../Effects/AnimationCharacter'
|
|
3
|
+
|
|
4
|
+
export const RMSpritesheet = (framesWidth, framesHeight, frameStand = 1) => {
|
|
5
|
+
|
|
6
|
+
const frameY = direction => {
|
|
7
|
+
return {
|
|
8
|
+
[Direction.Down]: 0,
|
|
9
|
+
[Direction.Left]: 1,
|
|
10
|
+
[Direction.Right]: 2,
|
|
11
|
+
[Direction.Up]: 3
|
|
12
|
+
}[direction]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const stand = (direction: number) => [{ time: 0, frameX: frameStand, frameY: frameY(direction) }]
|
|
16
|
+
const walk = direction => {
|
|
17
|
+
const array: any = []
|
|
18
|
+
for (let i=0 ; i < framesWidth ; i++) {
|
|
19
|
+
array.push({ time: i*10, frameX: i, frameY: frameY(direction) })
|
|
20
|
+
}
|
|
21
|
+
return array
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
textures: {
|
|
26
|
+
[Animation.Stand]: {
|
|
27
|
+
animations: direction => [stand(direction)]
|
|
28
|
+
},
|
|
29
|
+
[Animation.Walk]: {
|
|
30
|
+
animations: direction => [walk(direction)]
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
framesHeight,
|
|
34
|
+
framesWidth
|
|
35
|
+
}
|
|
36
|
+
}
|