Browse code

simpler keyboard handling

devnewton authored on 30/10/2016 at 09:08:30
Showing 2 changed files
... ...
@@ -21,14 +21,18 @@ export class KeyboardOptions extends AbstractState {
21 21
         logo.scale.y = 2;
22 22
         logo.anchor.setTo(0.5, 0);
23 23
 
24
-        new MenuButton(this.game, "Azerty zsqd + ikjl", 500, 300, () => {
24
+        new MenuButton(this.game, "Azerty zsqd", 500, 300, () => {
25 25
             (<ShmuprpgGame>this.game).controls.useAzertyLayout();
26 26
             this.game.state.start('Options');
27 27
         });
28
-        new MenuButton(this.game, "Qwerty wsad + ikjl", 500, 450, () => {
28
+        new MenuButton(this.game, "Qwerty wsad", 500, 450, () => {
29 29
             (<ShmuprpgGame>this.game).controls.useQwertyLayout();
30 30
             this.game.state.start('Options');
31 31
         });
32
-        new MenuButton(this.game, "Back", 500, 600, () => this.game.state.start('Options'));
32
+        new MenuButton(this.game, "Other ⬆⬇⬅➡", 500, 600, () => {
33
+            (<ShmuprpgGame>this.game).controls.useOtherLayout();
34
+            this.game.state.start('Options');
35
+        });
36
+        new MenuButton(this.game, "Back", 500, 750, () => this.game.state.start('Options'));
33 37
     }
34 38
 }
... ...
@@ -4,14 +4,10 @@ export class Controls {
4 4
     kb: Phaser.Keyboard;
5 5
     pad: Phaser.SinglePad;
6 6
     game: Phaser.Game;
7
-    keyCodeZ: number;
8
-    keyCodeS: number;
9
-    keyCodeQ: number;
10
-    keyCodeD: number;
11
-    keyCodeI: number;
12
-    keyCodeK: number;
13
-    keyCodeJ: number;
14
-    keyCodeL: number;
7
+    keyCodeUP: number;
8
+    keyCodeDOWN: number;
9
+    keyCodeLEFT: number;
10
+    keyCodeRIGHT: number;
15 11
 
16 12
     constructor(game: Phaser.Game) {
17 13
         this.game = game;
... ...
@@ -26,33 +22,32 @@ export class Controls {
26 26
     }
27 27
 
28 28
     useAzertyLayout() {
29
-        this.keyCodeZ = Phaser.KeyCode.Z;
30
-        this.keyCodeS = Phaser.KeyCode.S;
31
-        this.keyCodeQ = Phaser.KeyCode.Q;
32
-        this.keyCodeD = Phaser.KeyCode.D;
33
-        this.keyCodeI = Phaser.KeyCode.I;
34
-        this.keyCodeK = Phaser.KeyCode.K;
35
-        this.keyCodeJ = Phaser.KeyCode.J;
36
-        this.keyCodeL = Phaser.KeyCode.L;
29
+        this.keyCodeUP = Phaser.KeyCode.Z;
30
+        this.keyCodeDOWN = Phaser.KeyCode.S;
31
+        this.keyCodeLEFT = Phaser.KeyCode.Q;
32
+        this.keyCodeRIGHT = Phaser.KeyCode.D;
37 33
         localStorage.setItem('keyboard.layout', 'azerty');
38 34
     }
39 35
 
40 36
     useQwertyLayout() {
41
-        this.keyCodeZ = Phaser.KeyCode.W;
42
-        this.keyCodeS = Phaser.KeyCode.S;
43
-        this.keyCodeQ = Phaser.KeyCode.A;
44
-        this.keyCodeD = Phaser.KeyCode.D;
45
-        this.keyCodeI = Phaser.KeyCode.I;
46
-        this.keyCodeK = Phaser.KeyCode.K;
47
-        this.keyCodeJ = Phaser.KeyCode.J;
48
-        this.keyCodeL = Phaser.KeyCode.L;
37
+        this.keyCodeUP = Phaser.KeyCode.W;
38
+        this.keyCodeDOWN = Phaser.KeyCode.S;
39
+        this.keyCodeLEFT = Phaser.KeyCode.A;
40
+        this.keyCodeRIGHT = Phaser.KeyCode.D;
49 41
         localStorage.setItem('keyboard.layout', 'qwerty');
50 42
     }
51 43
 
44
+    useOtherLayout() {
45
+        this.keyCodeUP = Phaser.KeyCode.UP;
46
+        this.keyCodeDOWN = Phaser.KeyCode.DOWN;
47
+        this.keyCodeLEFT = Phaser.KeyCode.LEFT;
48
+        this.keyCodeRIGHT = Phaser.KeyCode.RIGHT;
49
+        localStorage.setItem('keyboard.layout', 'other');
50
+    }
51
+
52 52
     shootingAngle(shooterX: number, shooterY: number): number {
53 53
         return this.firstNonNull(this.shootingAngleFromPointer(shooterX, shooterY)
54
-            , this.shootingAngleFromPad(),
55
-            this.shootingFromKeyboard());
54
+            , this.shootingAngleFromPad());
56 55
     }
57 56
 
58 57
     private firstNonNull<T>(...values: T[]): T {
... ...
@@ -69,7 +64,7 @@ export class Controls {
69 69
         if (pointer.isDown) {
70 70
             return Phaser.Math.angleBetween(shooterX, shooterY, pointer.worldX, pointer.worldY);
71 71
         } else {
72
-            return this.shootingFromKeyboard();
72
+            return null;
73 73
         }
74 74
     }
75 75
 
... ...
@@ -85,50 +80,30 @@ export class Controls {
85 85
         }
86 86
     }
87 87
 
88
-    private shootingFromKeyboard(): number {
89
-        let dx = 0;
90
-        if (this.kb.isDown(this.keyCodeJ)) {
91
-            dx = -1;
92
-        } else if (this.kb.isDown(this.keyCodeL)) {
93
-            dx = 1;
94
-        }
95
-        let dy = 0;
96
-        if (this.kb.isDown(this.keyCodeI)) {
97
-            dy = -1;
98
-        } else if (this.kb.isDown(this.keyCodeK)) {
99
-            dy = 1;
100
-        }
101
-        if (dx != 0 || dy != 0) {
102
-            return Phaser.Math.angleBetween(0, 0, dx, dy);
103
-        } else {
104
-            return null;
105
-        }
106
-    }
107
-
108 88
     isGoingUp(): boolean {
109 89
         return this.pad.isDown(Phaser.Gamepad.XBOX360_DPAD_UP)
110 90
             || this.pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) < -this.pad.deadZone
111 91
             || this.kb.isDown(Phaser.KeyCode.UP)
112
-            || this.kb.isDown(this.keyCodeZ);
92
+            || this.kb.isDown(this.keyCodeUP);
113 93
     }
114 94
     isGoingDown(): boolean {
115 95
         return this.pad.isDown(Phaser.Gamepad.XBOX360_DPAD_DOWN)
116 96
             || this.pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) > this.pad.deadZone
117 97
             || this.kb.isDown(Phaser.KeyCode.DOWN)
118
-            || this.kb.isDown(this.keyCodeS);
98
+            || this.kb.isDown(this.keyCodeDOWN);
119 99
     }
120 100
 
121 101
     isGoingLeft(): boolean {
122 102
         return this.pad.isDown(Phaser.Gamepad.XBOX360_DPAD_LEFT)
123 103
             || this.pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) < -this.pad.deadZone
124 104
             || this.kb.isDown(Phaser.KeyCode.LEFT)
125
-            || this.kb.isDown(this.keyCodeQ);
105
+            || this.kb.isDown(this.keyCodeLEFT);
126 106
     }
127 107
 
128 108
     isGoingRight(): boolean {
129 109
         return this.pad.isDown(Phaser.Gamepad.XBOX360_DPAD_RIGHT)
130 110
             || this.pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) > this.pad.deadZone
131 111
             || this.kb.isDown(Phaser.KeyCode.RIGHT)
132
-            || this.kb.isDown(this.keyCodeD);
112
+            || this.kb.isDown(this.keyCodeRIGHT);
133 113
     }
134 114
 }
135 115
\ No newline at end of file