Browse code

keyboard custom binding support

devnewton authored on 08/11/2016 15:05:46
Showing 4 changed files
... ...
@@ -5,6 +5,7 @@ import {DemoEnding} from "./states/DemoEnding";
5 5
 import {Help} from "./states/Help";
6 6
 import {Options} from "./states/Options";
7 7
 import {KeyboardOptions} from "./states/KeyboardOptions";
8
+import {KeyboardOptionsBindKey} from "./states/KeyboardOptionsBindKey";
8 9
 import {GamepadOptions} from "./states/GamepadOptions";
9 10
 import {Level} from "./states/Level";
10 11
 import {GameOver} from "./states/GameOver";
... ...
@@ -24,6 +25,7 @@ export class ShmuprpgGame extends Phaser.Game {
24 25
         this.state.add('Help', Help);
25 26
         this.state.add('Options', Options);
26 27
         this.state.add('KeyboardOptions', KeyboardOptions);
28
+        this.state.add('KeyboardOptionsBindKey', KeyboardOptionsBindKey);
27 29
         this.state.add('GamepadOptions', GamepadOptions);
28 30
         this.state.add('Level', Level);
29 31
         this.state.add('GameOver', GameOver);
... ...
@@ -33,6 +33,10 @@ export class KeyboardOptions extends AbstractState {
33 33
             (<ShmuprpgGame>this.game).controls.useOtherLayout();
34 34
             this.game.state.start('Options');
35 35
         });
36
-        new MenuButton(this.game, "Back", 500, 750, () => this.game.state.start('Options'));
36
+        new MenuButton(this.game, "Custom", 500, 750, () => {
37
+            (<ShmuprpgGame>this.game).controls.useOtherLayout();
38
+            this.game.state.start('KeyboardOptionsBindKey', true, false);
39
+        });
40
+        new MenuButton(this.game, "Back", 500, 900, () => this.game.state.start('Options'));
37 41
     }
38 42
 }
39 43
new file mode 100644
... ...
@@ -0,0 +1,59 @@
1
+/// <reference path="../../typings/phaser.d.ts"/>
2
+import {AbstractState} from "./AbstractState";
3
+import {MenuButton} from "../ui/MenuButton";
4
+import { ShmuprpgGame } from "../ShmuprpgGame";
5
+
6
+
7
+export class KeyboardOptionsBindKey extends AbstractState {
8
+
9
+    bindings = [
10
+        { label: 'Press move up key', localStorageKey: 'keyboard.layout.custom.moveUp' },
11
+        { label: 'Press move down key', localStorageKey: 'keyboard.layout.custom.moveDown' },
12
+        { label: 'Press move left key', localStorageKey: 'keyboard.layout.custom.moveLeft' },
13
+        { label: 'Press move right key', localStorageKey: 'keyboard.layout.custom.moveRight' },
14
+        { label: 'Press shoot up key', localStorageKey: 'keyboard.layout.custom.shootUp' },
15
+        { label: 'Press shoot down key', localStorageKey: 'keyboard.layout.custom.shootDown' },
16
+        { label: 'Press shoot left key', localStorageKey: 'keyboard.layout.custom.shootLeft' },
17
+        { label: 'Press shoot right key', localStorageKey: 'keyboard.layout.custom.shootRight' }
18
+     ];
19
+
20
+    currentBinding: number = 0;
21
+
22
+    constructor() {
23
+        super();
24
+    }
25
+
26
+    preload() {
27
+        MenuButton.preload(this.game);
28
+    }
29
+
30
+    init(binding: number = 0) {
31
+        if (binding >= this.bindings.length) {
32
+            this.currentBinding = 0;
33
+            (this.game as ShmuprpgGame).controls.useCustomLayout();
34
+            this.game.state.start('KeyboardOptions');
35
+        } else {
36
+            this.currentBinding = binding;
37
+        }
38
+    }
39
+
40
+    create() {
41
+        super.create();
42
+        let logo = this.game.add.text(this.game.world.centerX, 0, this.bindings[this.currentBinding].label, { font: "68px monospace", fill: 'white' });
43
+        logo.scale.x = 2;
44
+        logo.scale.y = 2;
45
+        logo.anchor.setTo(0.5, 0);
46
+        new MenuButton(this.game, "Back", 500, 900, () => this.game.state.start('KeyboardOptions'));
47
+    }
48
+
49
+    update() {
50
+        for (var k in Phaser.KeyCode) {
51
+            let keycode = Phaser.KeyCode[k];
52
+            if (this.input.keyboard.isDown(keycode)) {
53
+                localStorage.setItem(this.bindings[this.currentBinding].localStorageKey, keycode);
54
+                this.game.state.start('KeyboardOptionsBindKey', true, false, this.currentBinding + 1);
55
+                break;
56
+            }
57
+        }
58
+    }
59
+}
... ...
@@ -4,10 +4,10 @@ export class Controls {
4 4
     kb: Phaser.Keyboard;
5 5
     pad: Phaser.SinglePad;
6 6
     game: Phaser.Game;
7
-    keyCodeUP: number;
8
-    keyCodeDOWN: number;
9
-    keyCodeLEFT: number;
10
-    keyCodeRIGHT: number;
7
+    keyCodeMoveUp: number;
8
+    keyCodeMoveDown: number;
9
+    keyCodeMoveLeft: number;
10
+    keyCodeMoveRight: number;
11 11
     keyCodeShootUp: number;
12 12
     keyCodeShootDown: number;
13 13
     keyCodeShootLeft: number;
... ...
@@ -18,10 +18,19 @@ export class Controls {
18 18
         game.input.gamepad.start();
19 19
         this.kb = game.input.keyboard;
20 20
         this.pad = game.input.gamepad.pad1;
21
-        if (localStorage.getItem('keyboard.layout') == 'qwerty') {
22
-            this.useQwertyLayout();
23
-        } else {
21
+        this.setupKeyboardLayout();
22
+    }
23
+
24
+    setupKeyboardLayout() {
25
+        let layout = localStorage.getItem('keyboard.layout');
26
+        if (null == layout || layout == 'azerty') {
24 27
             this.useAzertyLayout();
28
+        } else if (layout == 'qwerty') {
29
+            this.useQwertyLayout();
30
+        } else if (layout == 'other') {
31
+            this.useOtherLayout();
32
+        } else if (layout == 'custom') {
33
+            this.useCustomLayout();
25 34
         }
26 35
     }
27 36
 
... ...
@@ -30,10 +39,10 @@ export class Controls {
30 39
     }
31 40
 
32 41
     useAzertyLayout() {
33
-        this.keyCodeUP = Phaser.KeyCode.Z;
34
-        this.keyCodeDOWN = Phaser.KeyCode.S;
35
-        this.keyCodeLEFT = Phaser.KeyCode.Q;
36
-        this.keyCodeRIGHT = Phaser.KeyCode.D;
42
+        this.keyCodeMoveUp = Phaser.KeyCode.Z;
43
+        this.keyCodeMoveDown = Phaser.KeyCode.S;
44
+        this.keyCodeMoveLeft = Phaser.KeyCode.Q;
45
+        this.keyCodeMoveRight = Phaser.KeyCode.D;
37 46
         this.keyCodeShootUp = Phaser.KeyCode.I;
38 47
         this.keyCodeShootDown = Phaser.KeyCode.K;
39 48
         this.keyCodeShootLeft = Phaser.KeyCode.J;
... ...
@@ -42,10 +51,10 @@ export class Controls {
42 51
     }
43 52
 
44 53
     useQwertyLayout() {
45
-        this.keyCodeUP = Phaser.KeyCode.W;
46
-        this.keyCodeDOWN = Phaser.KeyCode.S;
47
-        this.keyCodeLEFT = Phaser.KeyCode.A;
48
-        this.keyCodeRIGHT = Phaser.KeyCode.D;
54
+        this.keyCodeMoveUp = Phaser.KeyCode.W;
55
+        this.keyCodeMoveDown = Phaser.KeyCode.S;
56
+        this.keyCodeMoveLeft = Phaser.KeyCode.A;
57
+        this.keyCodeMoveRight = Phaser.KeyCode.D;
49 58
         this.keyCodeShootUp = Phaser.KeyCode.I;
50 59
         this.keyCodeShootDown = Phaser.KeyCode.K;
51 60
         this.keyCodeShootLeft = Phaser.KeyCode.J;
... ...
@@ -54,10 +63,10 @@ export class Controls {
54 63
     }
55 64
 
56 65
     useOtherLayout() {
57
-        this.keyCodeUP = Phaser.KeyCode.UP;
58
-        this.keyCodeDOWN = Phaser.KeyCode.DOWN;
59
-        this.keyCodeLEFT = Phaser.KeyCode.LEFT;
60
-        this.keyCodeRIGHT = Phaser.KeyCode.RIGHT;
66
+        this.keyCodeMoveUp = Phaser.KeyCode.UP;
67
+        this.keyCodeMoveDown = Phaser.KeyCode.DOWN;
68
+        this.keyCodeMoveLeft = Phaser.KeyCode.LEFT;
69
+        this.keyCodeMoveRight = Phaser.KeyCode.RIGHT;
61 70
         this.keyCodeShootUp = Phaser.KeyCode.I;
62 71
         this.keyCodeShootDown = Phaser.KeyCode.K;
63 72
         this.keyCodeShootLeft = Phaser.KeyCode.J;
... ...
@@ -65,6 +74,18 @@ export class Controls {
65 74
         localStorage.setItem('keyboard.layout', 'other');
66 75
     }
67 76
 
77
+    useCustomLayout() {
78
+        this.keyCodeMoveUp = parseInt(localStorage.getItem('keyboard.layout.custom.moveUp')) || Phaser.KeyCode.UP;
79
+        this.keyCodeMoveDown = parseInt(localStorage.getItem('keyboard.layout.custom.moveDown')) || Phaser.KeyCode.DOWN;
80
+        this.keyCodeMoveLeft = parseInt(localStorage.getItem('keyboard.layout.custom.moveLeft')) || Phaser.KeyCode.LEFT;
81
+        this.keyCodeMoveRight = parseInt(localStorage.getItem('keyboard.layout.custom.moveRight')) || Phaser.KeyCode.RIGHT;
82
+        this.keyCodeShootUp = parseInt(localStorage.getItem('keyboard.layout.custom.shootUp')) || Phaser.KeyCode.I;
83
+        this.keyCodeShootDown = parseInt(localStorage.getItem('keyboard.layout.custom.shootDown')) || Phaser.KeyCode.K;
84
+        this.keyCodeShootLeft = parseInt(localStorage.getItem('keyboard.layout.custom.shootLeft')) || Phaser.KeyCode.J;
85
+        this.keyCodeShootRight = parseInt(localStorage.getItem('keyboard.layout.custom.shootRight')) || Phaser.KeyCode.L;
86
+        localStorage.setItem('keyboard.layout', 'custom');
87
+    }
88
+
68 89
     shootingAngle(shooterX: number, shooterY: number): number {
69 90
         return this.firstNonNull(this.shootingAngleFromPointer(shooterX, shooterY)
70 91
             , this.shootingAngleFromPad(), this.shootingFromKeyboard());
... ...
@@ -123,24 +144,24 @@ export class Controls {
123 144
     isGoingUp(): boolean {
124 145
         return this.pad.isDown(Phaser.Gamepad.XBOX360_DPAD_UP)
125 146
             || this.pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) < -this.pad.deadZone
126
-            || this.kb.isDown(this.keyCodeUP);
147
+            || this.kb.isDown(this.keyCodeMoveUp);
127 148
     }
128 149
     isGoingDown(): boolean {
129 150
         return this.pad.isDown(Phaser.Gamepad.XBOX360_DPAD_DOWN)
130 151
             || this.pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) > this.pad.deadZone
131
-            || this.kb.isDown(this.keyCodeDOWN);
152
+            || this.kb.isDown(this.keyCodeMoveDown);
132 153
     }
133 154
 
134 155
     isGoingLeft(): boolean {
135 156
         return this.pad.isDown(Phaser.Gamepad.XBOX360_DPAD_LEFT)
136 157
             || this.pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) < -this.pad.deadZone
137
-            || this.kb.isDown(this.keyCodeLEFT);
158
+            || this.kb.isDown(this.keyCodeMoveLeft);
138 159
     }
139 160
 
140 161
     isGoingRight(): boolean {
141 162
         return this.pad.isDown(Phaser.Gamepad.XBOX360_DPAD_RIGHT)
142 163
             || this.pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) > this.pad.deadZone
143
-            || this.kb.isDown(this.keyCodeRIGHT);
164
+            || this.kb.isDown(this.keyCodeMoveRight);
144 165
     }
145 166
 
146 167
     isPassDialogButtonDown(): boolean {