Browse code

fix some multi gamepad bugs

devnewton authored on 12/03/2018 20:36:58
Showing 5 changed files
... ...
@@ -2,6 +2,8 @@
2 2
 import {AbstractState} from "./AbstractState";
3 3
 import {Menu} from "../ui/Menu";
4 4
 import {MenuButton} from "../ui/MenuButton";
5
+import {GamepadUtils} from "../utils/GamepadUtils";
6
+
5 7
 
6 8
 export class GamepadOptions extends AbstractState {
7 9
 
... ...
@@ -24,16 +26,16 @@ export class GamepadOptions extends AbstractState {
24 26
         subtitle.y = this.game.world.height - subtitle.height;
25 27
 
26 28
         const menu = new Menu(this.game);
27
-        menu.addButton(new GamepadMenuButton(this.input.gamepad.pad1, 0xFF6666, "Gamepad 1", 200, 100, () => {
29
+        menu.addButton(new GamepadMenuButton(this.input.gamepad.pad1, "Gamepad 1", 200, 100, () => {
28 30
             this.game.state.start('GamepadOptionsLayout', true, false, 1);
29 31
         }));
30
-        menu.addButton(new GamepadMenuButton(this.input.gamepad.pad2, 0x66FF66, "Gamepad 2", 200, 200, () => {
32
+        menu.addButton(new GamepadMenuButton(this.input.gamepad.pad2, "Gamepad 2", 200, 200, () => {
31 33
             this.game.state.start('GamepadOptionsLayout', true, false, 2);
32 34
         }));
33
-        menu.addButton(new GamepadMenuButton(this.input.gamepad.pad3, 0x6666FF, "Gamepad 3", 200, 300, () => {
35
+        menu.addButton(new GamepadMenuButton(this.input.gamepad.pad3, "Gamepad 3", 200, 300, () => {
34 36
             this.game.state.start('GamepadOptionsLayout', true, false, 3);
35 37
         }));
36
-        menu.addButton(new GamepadMenuButton(this.input.gamepad.pad4, 0xFFFF66, "Gamepad 4", 200, 400, () => {
38
+        menu.addButton(new GamepadMenuButton(this.input.gamepad.pad4, "Gamepad 4", 200, 400, () => {
37 39
             this.game.state.start('GamepadOptionsLayout', true, false, 4);
38 40
         }));
39 41
         menu.button("Back", 200, 550, () => this.game.state.start('Options'));
... ...
@@ -42,20 +44,18 @@ export class GamepadOptions extends AbstractState {
42 44
 
43 45
 class GamepadMenuButton extends MenuButton {
44 46
     pad: Phaser.SinglePad;
45
-    activePadTint: number;
46
-    constructor(pad: Phaser.SinglePad, activePadTint: number, label: string, x: number, y: number, callback: Function) {
47
+    constructor(pad: Phaser.SinglePad, label: string, x: number, y: number, callback: Function) {
47 48
         super(pad.game, label, x, y, callback);
48 49
         this.pad = pad;
49
-        this.activePadTint = activePadTint;
50 50
     }
51 51
 
52 52
     update() {
53 53
         super.update();
54 54
 
55 55
         if (this.isPadActive()) {
56
-            this.tint = this.activePadTint;
56
+            this.labelText.tint = GamepadUtils.gamepadColor(this.pad);
57 57
         } else {
58
-            this.tint = 0xFFFFFF;
58
+            this.labelText.tint = 0xFFFFFF;
59 59
         }
60 60
     }
61 61
 
... ...
@@ -31,8 +31,7 @@ export class GamepadOptionsBindAxisOrButton extends AbstractState {
31 31
     }
32 32
 
33 33
     init(padIndex: number, binding: number = 0) {
34
-        padIndex = padIndex || 1;
35
-        this.padIndex = 1;
34
+        this.padIndex = padIndex || 1;
36 35
         this.pad = this.input.gamepad['pad' + this.padIndex];
37 36
         if (binding >= this.bindings.length) {
38 37
             this.currentBinding = 0;
... ...
@@ -116,9 +115,9 @@ class AxisButton extends MenuMiniButton {
116 115
         super.update();
117 116
 
118 117
         if (Math.abs(this.pad.axis(this.axisCode)) > this.pad.deadZone) {
119
-            this.tint = 0xFF6666;
118
+            this.labelText.tint = GamepadUtils.gamepadColor(this.pad);
120 119
         } else {
121
-            this.tint = 0xFFFFFF;
120
+            this.labelText.tint = 0xFFFFFF;
122 121
         }
123 122
     }
124 123
 
... ...
@@ -136,9 +135,9 @@ class ButtonButton extends MenuMiniButton {
136 135
         super.update();
137 136
 
138 137
         if (this.pad.isDown(this.buttonCode)) {
139
-            this.tint = 0xFF6666;
138
+            this.labelText.tint = GamepadUtils.gamepadColor(this.pad);
140 139
         } else {
141
-            this.tint = 0xFFFFFF;
140
+            this.labelText.tint = 0xFFFFFF;
142 141
         }
143 142
     }
144 143
 
... ...
@@ -51,7 +51,7 @@ export class TeamSelectScreen extends AbstractState {
51 51
             new MenuSelectOption<ControllerType>(ControllerType.KEYBOARD, 'Keyboard'),
52 52
             new MenuSelectOption<ControllerType>(ControllerType.PAD1, 'Pad 1'),
53 53
             new MenuSelectOption<ControllerType>(ControllerType.PAD2, 'Pad 2'),
54
-            new MenuSelectOption<ControllerType>(ControllerType.PAD4, 'Pad 3'),
54
+            new MenuSelectOption<ControllerType>(ControllerType.PAD3, 'Pad 3'),
55 55
             new MenuSelectOption<ControllerType>(ControllerType.PAD4, 'Pad 4')
56 56
         ];
57 57
 
... ...
@@ -2,11 +2,11 @@
2 2
 
3 3
 export enum ControllerType {
4 4
     CPU = -1,
5
-    KEYBOARD,
6
-    PAD1,
7
-    PAD2,
8
-    PAD3,
9
-    PAD4
5
+    KEYBOARD = 0,
6
+    PAD1 = 1,
7
+    PAD2 = 2,
8
+    PAD3 = 3,
9
+    PAD4 = 4
10 10
 }
11 11
 
12 12
 export class Controllers {
... ...
@@ -1,5 +1,18 @@
1 1
 /// <reference path="../../typings/phaser.d.ts"/>
2 2
 export class GamepadUtils {
3
-    static NB_BUTTONS=16;
4
-    static NB_AXIS=10;
3
+    static NB_BUTTONS = 16;
4
+    static NB_AXIS = 10;
5
+
6
+    static gamepadColor(pad: Phaser.SinglePad): number {
7
+        let gamepad = pad.game.input.gamepad;
8
+        if (pad == gamepad.pad4) {
9
+            return 0xFFFF00;
10
+        } else if (pad == gamepad.pad3) {
11
+            return 0x0000FF;
12
+        } else if (pad == gamepad.pad2) {
13
+            return 0x00FF00;
14
+        } else {
15
+            return 0xFF0000;
16
+        }
17
+    }
5 18
 }
6 19
\ No newline at end of file