Browse code

team controllers distribution

devnewton authored on 18/12/2016 at 11:30:02
Showing 3 changed files
... ...
@@ -7,8 +7,18 @@ import { Team } from "../entities/Team";
7 7
 import { Pathfinder } from "../ia/services/Pathfinder";
8 8
 import { DamageResolver } from "../utils/DamageResolver";
9 9
 
10
+import { ControllerType } from "../utils/Controls";
11
+
12
+export class LevelConfig {
13
+    bettyController: ControllerType;
14
+    betty2Controller: ControllerType;
15
+    georgeController: ControllerType;
16
+    george2Controller: ControllerType;
17
+}
18
+
10 19
 export class Level extends AbstractState {
11 20
 
21
+    config: LevelConfig;
12 22
     collisionSprites: Phaser.Group;
13 23
     pathfinder: Pathfinder;
14 24
     damageResolver: DamageResolver;
... ...
@@ -28,6 +38,10 @@ export class Level extends AbstractState {
28 28
         this.game.load.image('misc', 'sprites/opengameart/misc.png');
29 29
     }
30 30
 
31
+    init(config: LevelConfig) {
32
+        this.config = config;
33
+    }
34
+
31 35
     create() {
32 36
         super.create();
33 37
         this.game.physics.startSystem(Phaser.Physics.ARCADE);
... ...
@@ -57,27 +71,32 @@ export class Level extends AbstractState {
57 57
 
58 58
         this.pathfinder = new Pathfinder(map);
59 59
 
60
+        let controllers = (this.game as UnderthiefGame).controllers;
61
+
60 62
         this.leftTeam = new Team(this.game);
61 63
         let betty = new Player(this.game, 'betty');
62 64
         betty.x = 256;
63 65
         betty.y = 320;
64
-        betty.controls = (this.game as UnderthiefGame).controllers.getKeyboard();
66
+        betty.controls = controllers.getController(this.config.bettyController);
65 67
         this.leftTeam.add(betty);
66 68
 
67 69
         let betty2 = new Player(this.game, 'betty2');
68 70
         betty2.x = 256;
69 71
         betty2.y = 480;
72
+        betty2.controls = controllers.getController(this.config.betty2Controller);
70 73
         this.leftTeam.add(betty2);
71 74
 
72 75
         this.rightTeam = new Team(this.game);
73 76
         let george = new Player(this.game, 'george');
74 77
         george.x = 1024;
75 78
         george.y = 320;
79
+        george.controls = controllers.getController(this.config.georgeController);
76 80
         this.rightTeam.add(george);
77 81
 
78 82
         let george2 = new Player(this.game, 'george2');
79 83
         george2.x = 1024;
80 84
         george2.y = 480;
85
+        george2.controls = controllers.getController(this.config.george2Controller);
81 86
         this.rightTeam.add(george2);
82 87
         /*        leftTeam.getAt(0).controls = (this.game as UnderthiefGame).controllers.getKeyboard();
83 88
         leftTeam.getAt(1).controls = (this.game as UnderthiefGame).controllers.getPad(1);
... ...
@@ -1,5 +1,6 @@
1 1
 /// <reference path="../../typings/phaser.d.ts"/>
2 2
 import { AbstractState } from "./AbstractState";
3
+import { LevelConfig } from "./Level";
3 4
 import { MenuButton } from "../ui/MenuButton";
4 5
 import { MenuSelect, MenuSelectOption } from "../ui/MenuSelect";
5 6
 import { UnderthiefGame } from "../UnderthiefGame";
... ...
@@ -81,6 +82,13 @@ export class TeamSelectScreen extends AbstractState {
81 81
                 george2Select.setSelectedValue(ControllerType.PAD4);
82 82
                 break;
83 83
         }
84
-        new MenuButton(this.game, "Play", 200, 600, () => this.game.state.start('Level'));
84
+        new MenuButton(this.game, "Play", 200, 600, () => {
85
+            let config = new LevelConfig();
86
+            config.bettyController = bettySelect.getSelectedValue();
87
+            config.betty2Controller = betty2Select.getSelectedValue();
88
+                        config.georgeController = georgeSelect.getSelectedValue();
89
+                        config.george2Controller = george2Select.getSelectedValue();
90
+            this.game.state.start('Level', true, true, config)
91
+        });
85 92
     }
86 93
 }
... ...
@@ -22,6 +22,15 @@ export class Controllers {
22 22
         ];
23 23
     }
24 24
 
25
+    getController(type: ControllerType): AbstractControls {
26
+        switch (type) {
27
+            case ControllerType.CPU:
28
+                return null;
29
+            default:
30
+                return this.controllers[type];
31
+        }
32
+    }
33
+
25 34
     getKeyboard(): KeyboardControls {
26 35
         return this.controllers[0] as KeyboardControls;
27 36
     }