Browse code

auto select pad on connect/disconnect

devnewton authored on 18/03/2018 08:33:48
Showing 1 changed files
... ...
@@ -8,6 +8,11 @@ import { ControllerType } from "../utils/Controls";
8 8
 
9 9
 export class TeamSelectScreen extends AbstractState {
10 10
 
11
+    bettySelect: MenuSelect<ControllerType>;
12
+    betty2Select: MenuSelect<ControllerType>;
13
+    georgeSelect: MenuSelect<ControllerType>;
14
+    george2Select: MenuSelect<ControllerType>;
15
+
11 16
     constructor() {
12 17
         super();
13 18
     }
... ...
@@ -55,41 +60,60 @@ export class TeamSelectScreen extends AbstractState {
55 60
             new MenuSelectOption<ControllerType>(ControllerType.PAD4, 'Pad 4')
56 61
         ];
57 62
 
58
-        let bettySelect = menu.select(200, 150, playerOptions);
59
-        let betty2Select = menu.select(200, 250, playerOptions);
60
-        let georgeSelect = menu.select(200, 350, playerOptions);
61
-        let george2Select = menu.select(200, 450, playerOptions);
63
+        this.bettySelect = menu.select(200, 150, playerOptions);
64
+        this.betty2Select = menu.select(200, 250, playerOptions);
65
+        this.georgeSelect = menu.select(200, 350, playerOptions);
66
+        this.george2Select = menu.select(200, 450, playerOptions);
67
+
68
+        this.autoSelect();
69
+        menu.button("Play", 200, 600, () => {
70
+            let config = new LevelConfig();
71
+            config.bettyController = this.bettySelect.getSelectedValue();
72
+            config.betty2Controller = this.betty2Select.getSelectedValue();
73
+            config.georgeController = this.georgeSelect.getSelectedValue();
74
+            config.george2Controller = this.george2Select.getSelectedValue();
75
+            this.game.state.start('Level', true, false, config)
76
+        });
77
+
78
+        this.input.gamepad.onConnectCallback = this.input.gamepad.onDisconnectCallback = () => this.autoSelect();
79
+    }
80
+
81
+    shutdown() {
82
+        this.input.gamepad.onConnectCallback = this.input.gamepad.onDisconnectCallback = null;
83
+    }
62 84
 
85
+    autoSelect() {
63 86
         switch (this.input.gamepad.padsConnected) {
64 87
             case 0:
65
-                bettySelect.setSelectedValue(ControllerType.KEYBOARD);
88
+                this.bettySelect.setSelectedValue(ControllerType.KEYBOARD);
89
+                this.betty2Select.setSelectedValue(ControllerType.CPU);
90
+                this.georgeSelect.setSelectedValue(ControllerType.CPU);
91
+                this.george2Select.setSelectedValue(ControllerType.CPU);
66 92
                 break;
67 93
             case 1:
68
-                bettySelect.setSelectedValue(ControllerType.PAD1);
94
+                this.bettySelect.setSelectedValue(ControllerType.PAD1);
95
+                this.betty2Select.setSelectedValue(ControllerType.CPU);
96
+                this.georgeSelect.setSelectedValue(ControllerType.CPU);
97
+                this.george2Select.setSelectedValue(ControllerType.CPU);
69 98
                 break;
70 99
             case 2:
71
-                bettySelect.setSelectedValue(ControllerType.PAD1);
72
-                georgeSelect.setSelectedValue(ControllerType.PAD2);
100
+                this.bettySelect.setSelectedValue(ControllerType.PAD1);
101
+                this.betty2Select.setSelectedValue(ControllerType.CPU);
102
+                this.georgeSelect.setSelectedValue(ControllerType.PAD2);
103
+                this.george2Select.setSelectedValue(ControllerType.CPU);
73 104
                 break;
74 105
             case 3:
75
-                bettySelect.setSelectedValue(ControllerType.PAD1);
76
-                betty2Select.setSelectedValue(ControllerType.PAD2);
77
-                georgeSelect.setSelectedValue(ControllerType.PAD3);
106
+                this.bettySelect.setSelectedValue(ControllerType.PAD1);
107
+                this.betty2Select.setSelectedValue(ControllerType.PAD2);
108
+                this.georgeSelect.setSelectedValue(ControllerType.PAD3);
109
+                this.george2Select.setSelectedValue(ControllerType.CPU);
78 110
                 break;
79 111
             default:
80
-                bettySelect.setSelectedValue(ControllerType.PAD1);
81
-                betty2Select.setSelectedValue(ControllerType.PAD2);
82
-                georgeSelect.setSelectedValue(ControllerType.PAD3);
83
-                george2Select.setSelectedValue(ControllerType.PAD4);
112
+                this.bettySelect.setSelectedValue(ControllerType.PAD1);
113
+                this.betty2Select.setSelectedValue(ControllerType.PAD2);
114
+                this.georgeSelect.setSelectedValue(ControllerType.PAD3);
115
+                this.george2Select.setSelectedValue(ControllerType.PAD4);
84 116
                 break;
85 117
         }
86
-        menu.button("Play", 200, 600, () => {
87
-            let config = new LevelConfig();
88
-            config.bettyController = bettySelect.getSelectedValue();
89
-            config.betty2Controller = betty2Select.getSelectedValue();
90
-                        config.georgeController = georgeSelect.getSelectedValue();
91
-                        config.george2Controller = george2Select.getSelectedValue();
92
-            this.game.state.start('Level', true, false, config)
93
-        });
94 118
     }
95 119
 }