Made fonts red and yellow for different players
[interactive-pacbot.git] / game / states / play.js
1 'use strict';
2
3 var Player = require('../prefabs/player');
4 var Pill = require('../prefabs/pill');
5 var BonusPill = require('../prefabs/bonusPill');
6 var Wall = require('../prefabs/wall');
7
8 function Play() {}
9
10 Play.prototype = {
11   addToMap: function(x, y) {
12     if (!this.map) {
13       this.map = [];
14     }
15     if (!this.map[x]) {
16       this.map[x] = [];
17     }
18
19     this.map[x][y] = true;
20   },
21   removeFromMap: function(x,y) {
22     if (!this.map || !this.map[x]) {
23       return;
24     }
25     this.map[x][y] = false;
26   },
27   checkMap: function(x,y) {
28     if (!this.map || !this.map[x]) {
29       return false;
30     }
31     return this.map[x][y];
32   },
33   preload: function() {
34   },
35   create: function() {
36     this.readLevelFile();
37
38     this.world.scale = {x:50, y:50};
39     this.world.bounds = {x: -25, y:-25, width: this.game.width, height: this.game.height};
40     this.world.camera.setBoundsToWorld();
41
42     this.setupPlayerControls();
43
44     this.game.physics.startSystem(Phaser.Physics.ARCADE);
45
46     this.playerAScoreText = this.game.add.bitmapText(-0.1, -0.4, 'spaced-scorefont-a','0',2);
47     this.playerBScoreText = this.game.add.bitmapText(this.world.width/this.world.scale.x - 3.1, -0.4, 'spaced-scorefont-b','0',2);
48
49     this.gameWon = false;
50   },
51   update: function() {
52     this.game.physics.arcade.overlap(this.players, this.pills, this.playerPillCollision, null, this);
53
54     if (!this.gameWon && this.pills.total === 0) {
55       this.gameWon = true;
56       if (this.playerA.score > this.playerB.score) {
57         this.setVictoryText('RED WINS', 'a');
58       }
59       else if (this.playerA.score < this.playerB.score) {
60         this.setVictoryText('YELLOW WINS', 'b');
61       }
62       else {
63         var victoryColor = this.playerA.isMyTurn ? 'b' : 'a';
64         this.setVictoryText('DRAW', victoryColor);
65       }
66
67       var self = this;
68       setTimeout(function() {
69         self.game.state.start('play');
70       }, 5000);
71     }
72
73     this.pollPlayerInput();
74   },
75   pollPlayerInput: function() {
76     if (this.game.input.gamepad.pad1.connected) {
77       this.pollAnalogStickForPlayer(this.game.input.gamepad.pad1, this.playerA);
78     }
79     if (this.game.input.gamepad.pad2.connected) {
80       this.pollAnalogStickForPlayer(this.game.input.gamepad.pad2, this.playerB);
81     }
82
83     if (this.game.input.keyboard.isDown(this.playerAControls.up)) {
84       this.movePlayer(this.playerA, 0, -1);
85     }
86     if (this.game.input.keyboard.isDown(this.playerAControls.down)) {
87       this.movePlayer(this.playerA, 0, 1);
88     }
89     if (this.game.input.keyboard.isDown(this.playerAControls.left)) {
90       this.movePlayer(this.playerA, -1, 0);
91     }
92     if (this.game.input.keyboard.isDown(this.playerAControls.right)) {
93       this.movePlayer(this.playerA, 1, 0);
94     }
95     if (this.game.input.keyboard.isDown(this.playerBControls.up)) {
96       this.movePlayer(this.playerB, 0, -1);
97     }
98     if (this.game.input.keyboard.isDown(this.playerBControls.down)) {
99       this.movePlayer(this.playerB, 0, 1);
100     }
101     if (this.game.input.keyboard.isDown(this.playerBControls.left)) {
102       this.movePlayer(this.playerB, -1, 0);
103     }
104     if (this.game.input.keyboard.isDown(this.playerBControls.right)) {
105       this.movePlayer(this.playerB, 1, 0);
106     }
107   },
108   pollAnalogStickForPlayer: function(pad, player) {
109     if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_LEFT) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) < -0.2) {
110       this.movePlayer(player, -1, 0);
111     }
112     if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_RIGHT) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) > 0.2) {
113       this.movePlayer(player, 1, 0);
114     }
115     if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_UP) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) < -0.2) {
116       this.movePlayer(player, 0, -1);
117     }
118     if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_DOWN) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) > 0.2) {
119       this.movePlayer(player, 0, 1);
120     }
121   },
122   readLevelFile: function() {
123     this.pills = this.game.add.group();
124     this.players = this.game.add.group();
125     this.walls = this.game.add.group();
126
127     var levelText = this.game.cache.getText('level');
128     var splitRows = levelText.split('\n');
129
130
131     for (var x=0; x<splitRows.length; x++) {
132       for (var y=0; y<splitRows[x].length; y++) {
133         switch(splitRows[x][y]) {
134           case '#':
135             this.walls.add(new Wall(this.game, x, y));
136             break;
137           case '.':
138             this.pills.add(new Pill(this.game, x, y));
139             break;
140           case '*':
141             this.pills.add(new BonusPill(this.game, x, y));
142             break;
143           case 'A':
144             this.playerA = new Player(this.game, x, y, 'player-a', 0);
145             this.players.add(this.playerA);
146             break;
147           case 'B':
148             this.playerB = new Player(this.game, x, y, 'player-b', 0);
149             this.players.add(this.playerB);
150             break;
151         }
152       }
153     }
154
155     this.walls.forEach(function(wall) {
156       this.addToMap(wall.x, wall.y);
157     }, this);
158     this.updatePlayerTurn(0);
159   },
160
161   setupPlayerControls: function() {
162     this.playerAControls = {
163       up: Phaser.Keyboard.W,
164       left: Phaser.Keyboard.A,
165       down: Phaser.Keyboard.S,
166       right: Phaser.Keyboard.D
167     };
168     this.playerBControls = {
169       up: Phaser.Keyboard.UP,
170       left: Phaser.Keyboard.LEFT,
171       down: Phaser.Keyboard.DOWN,
172       right: Phaser.Keyboard.RIGHT
173     };
174
175     function addKeyCaptures(controls, keyboard) {
176       for (var index in controls) {
177         if (controls.hasOwnProperty(index)) {
178           keyboard.addKeyCapture(controls[index]);
179         }
180       }
181     }
182     addKeyCaptures(this.playerAControls, this.game.input.keyboard);
183     addKeyCaptures(this.playerBControls, this.game.input.keyboard);
184
185     this.game.input.gamepad.start();
186   },
187   movePlayer: function(player, deltaX, deltaY) {
188     var newX = player.x + deltaX;
189     var newY = player.y + deltaY;
190
191     if (!this.checkMap(newX, newY) && player.isMyTurn && !player.moving) {
192       player.move(newX, newY);
193       this.togglePlayerTurn();
194     }
195   },
196   playerPillCollision: function(player, pill) {
197     player.score += pill.score;
198     pill.destroy();
199
200     this.playerAScoreText.setText(this.playerA.score+'');
201     this.playerBScoreText.setText(this.playerB.score+'');
202   },
203   togglePlayerTurn: function() {
204     this.updatePlayerTurn((this.playerTurn+1)%this.players.length);
205   },
206   updatePlayerTurn: function(newPlayerTurn) {
207     this.playerTurn = newPlayerTurn;
208     for (var i=0; i<this.players.children.length; ++i) {
209       this.players.children[i].isMyTurn = (i === this.playerTurn);
210     }
211   },
212   setVictoryText: function(newText, winnerLetter) {
213     this.victoryText = this.game.add.bitmapText(this.world.width/2/this.world.scale.x, 2, 'scorefont-'+winnerLetter, newText, 2);
214     this.victoryText.position.x = this.world.width/2/this.world.scale.x - this.victoryText.textWidth/2 - 0.5;
215   },
216   shutdown: function() {
217     this.game.input.keyboard.removeKey(this.playerAControls.up);
218     this.game.input.keyboard.removeKey(this.playerAControls.down);
219     this.game.input.keyboard.removeKey(this.playerAControls.left);
220     this.game.input.keyboard.removeKey(this.playerAControls.right);
221
222     this.game.input.keyboard.removeKey(this.playerBControls.up);
223     this.game.input.keyboard.removeKey(this.playerBControls.down);
224     this.game.input.keyboard.removeKey(this.playerBControls.left);
225     this.game.input.keyboard.removeKey(this.playerBControls.right);
226   }
227 };
228
229 module.exports = Play;