Browse code

allow gamepad selection

devnewton authored on 30/10/2016 at 18:24:17
Showing 5 changed files
... ...
@@ -3,6 +3,7 @@ import {Intro} from "./states/Intro.ts";
3 3
 import {Title} from "./states/Title.ts";
4 4
 import {Options} from "./states/Options.ts";
5 5
 import {KeyboardOptions} from "./states/KeyboardOptions.ts";
6
+import {GamepadOptions} from "./states/GamepadOptions.ts";
6 7
 import {Level} from "./states/Level.ts";
7 8
 import {GameOver} from "./states/GameOver.ts";
8 9
 import {Controls} from "./utils/Controls.ts";
... ...
@@ -20,6 +21,7 @@ export class ShmuprpgGame extends Phaser.Game {
20 20
         this.state.add('Title', Title);
21 21
         this.state.add('Options', Options);
22 22
         this.state.add('KeyboardOptions', KeyboardOptions);
23
+        this.state.add('GamepadOptions', GamepadOptions);
23 24
         this.state.add('Level', Level);
24 25
         this.state.add('GameOver', GameOver);
25 26
     }
26 27
new file mode 100644
... ...
@@ -0,0 +1,81 @@
0
+/// <reference path="../../typings/phaser.d.ts"/>
1
+import {AbstractState} from "./AbstractState.ts";
2
+import {MenuButton} from "../ui/MenuButton.ts";
3
+import { ShmuprpgGame } from "../ShmuprpgGame.ts";
4
+
5
+
6
+export class GamepadOptions extends AbstractState {
7
+
8
+    constructor() {
9
+        super();
10
+    }
11
+
12
+    preload() {
13
+        MenuButton.preload(this.game);
14
+    }
15
+
16
+    create() {
17
+        super.create();
18
+        let title = this.game.add.text(this.game.world.centerX, 0, 'Choose gamepad', { font: "68px monospace", fill: 'white' });
19
+        title.scale.x = 2;
20
+        title.scale.y = 2;
21
+        title.anchor.setTo(0.5, 0);
22
+
23
+        let subtitle = this.game.add.text(0, 0, 'Move stick or press button to show gamepad number', { font: "60px monospace", fill: 'white' });
24
+        subtitle.y = this.game.world.height - subtitle.height;
25
+
26
+        new GamepadMenuButton(this.input.gamepad.pad1, 0xFF6666, "Gamepad 1", 500, 200, () => {
27
+            (<ShmuprpgGame>this.game).controls.usePad(this.input.gamepad.pad1);
28
+            this.game.state.start('Options');
29
+        });
30
+        new GamepadMenuButton(this.input.gamepad.pad2, 0x66FF66, "Gamepad 2", 500, 350, () => {
31
+            (<ShmuprpgGame>this.game).controls.usePad(this.input.gamepad.pad2);
32
+            this.game.state.start('Options');
33
+        });
34
+        new GamepadMenuButton(this.input.gamepad.pad3, 0x6666FF, "Gamepad 3", 500, 500, () => {
35
+            (<ShmuprpgGame>this.game).controls.usePad(this.input.gamepad.pad3);
36
+            this.game.state.start('Options');
37
+        });
38
+        new GamepadMenuButton(this.input.gamepad.pad4, 0xFFFF66, "Gamepad 4", 500, 650, () => {
39
+            (<ShmuprpgGame>this.game).controls.usePad(this.input.gamepad.pad4);
40
+            this.game.state.start('Options');
41
+        });
42
+        new MenuButton(this.game, "Back", 500, 800, () => this.game.state.start('Options'));
43
+    }
44
+}
45
+
46
+class GamepadMenuButton extends MenuButton {
47
+    pad: Phaser.SinglePad;
48
+    activePadTint: number;
49
+    constructor(pad: Phaser.SinglePad, activePadTint: number, label: string, x: number, y: number, callback: Function) {
50
+        super(pad.game, label, x, y, callback);
51
+        this.pad = pad;
52
+        this.activePadTint = activePadTint;
53
+    }
54
+
55
+    update() {
56
+        super.update();
57
+
58
+        if (this.isPadActive()) {
59
+            this.tint = this.activePadTint;
60
+        } else {
61
+            this.tint = 0xFFFFFF;
62
+        }
63
+    }
64
+
65
+    isPadActive(): boolean {
66
+        for (let b = 0; b < 16; ++b) {
67
+            let button = this.pad.getButton(b);
68
+            if (button && button.isDown) {
69
+                return true;
70
+            }
71
+        }
72
+        for (let a = 0; a < 16; ++a) {
73
+            if (Math.abs(this.pad.axis(a)) > this.pad.deadZone) {
74
+                return true;
75
+            }
76
+        }
77
+        return false;
78
+    }
79
+
80
+}
... ...
@@ -14,12 +14,16 @@ export class Options extends AbstractState {
14 14
 
15 15
     create() {
16 16
         super.create();
17
-        let logo = this.game.add.text(this.game.world.centerX, 0, 'Options',{font: "120px monospace", fill: 'white'});
17
+        let logo = this.game.add.text(this.game.world.centerX, 0, 'Options', { font: "120px monospace", fill: 'white' });
18 18
         logo.scale.x = 2;
19 19
         logo.scale.y = 2;
20 20
         logo.anchor.setTo(0.5, 0);
21 21
 
22
-        new MenuButton(this.game, "Keyboard", 500, 300, () => this.game.state.start('KeyboardOptions'));
23
-        new MenuButton(this.game, "Back", 500, 450, () => this.game.state.start('Title'));
22
+        let y = 150;
23
+        new MenuButton(this.game, "Keyboard", 500, y += 150, () => this.game.state.start('KeyboardOptions'));
24
+        if (this.input.gamepad.supported) {
25
+            new MenuButton(this.game, "Gamepad", 500, y += 150, () => this.game.state.start('GamepadOptions'));
26
+        }
27
+        new MenuButton(this.game, "Back", 500, y += 150, () => this.game.state.start('Title'));
24 28
     }
25 29
 }
... ...
@@ -1,13 +1,18 @@
1 1
 /// <reference path="../../typings/phaser.d.ts"/>
2 2
 export class MenuButton extends Phaser.Button{
3 3
 
4
+    labelText: Phaser.Text;
4 5
     constructor(game: Phaser.Game, label: string, x: number, y: number, callback: Function) {
5 6
         super(game, x, y, 'menu-buttons', callback, null, 'over', 'out', 'down');
6
-        let labelText = new Phaser.Text(this.game, 140, 15, label, {font: "64px monospace", fill: 'white'});
7
-        this.addChild(labelText);
7
+        this.labelText = new Phaser.Text(this.game, 140, 15, label, {font: "64px monospace", fill: 'white'});
8
+        this.addChild(this.labelText);
8 9
         game.add.existing(this);
9 10
     }
10 11
 
12
+    setText(text: string) {
13
+        this.labelText.setText(text);
14
+    }
15
+
11 16
     static preload(game: Phaser.Game) {
12 17
         game.load.atlasXML('menu-buttons', 'menu/buttons.png', 'menu/buttons.xml');
13 18
     }
... ...
@@ -21,6 +21,10 @@ export class Controls {
21 21
         }
22 22
     }
23 23
 
24
+    usePad(pad: Phaser.SinglePad) {
25
+        this.pad = pad;
26
+    }
27
+
24 28
     useAzertyLayout() {
25 29
         this.keyCodeUP = Phaser.KeyCode.Z;
26 30
         this.keyCodeDOWN = Phaser.KeyCode.S;