173564bab944afd5491a873666efd937d2a91428
[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','0',1);
47     this.playerBScoreText = this.game.add.bitmapText(this.world.width/this.world.scale.x - 2.1, -0.4, 'spaced-scorefont','0',1);
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("PLAYER A WINS");
58       }
59       else if (this.playerA.score < this.playerB.score) {
60         this.setVictoryText("PLAYER B WINS");
61       }
62       else {
63         this.setVictoryText("DRAW");
64       }
65
66       var self = this;
67       setTimeout(function() {
68         self.game.state.start('play');
69       }, 5000);
70     }
71
72     this.pollPlayerInput();
73   },
74   pollPlayerInput: function() {
75     if (this.game.input.gamepad.pad1.connected) {
76       this.pollAnalogStickForPlayer(this.game.input.gamepad.pad1, this.playerA);
77     }
78     if (this.game.input.gamepad.pad2.connected) {
79       this.pollAnalogStickForPlayer(this.game.input.gamepad.pad2, this.playerB);
80     }
81
82     if (this.game.input.keyboard.isDown(this.playerAControls.up)) {
83       this.movePlayer(this.playerA, 0, -1);
84     }
85     if (this.game.input.keyboard.isDown(this.playerAControls.down)) {
86       this.movePlayer(this.playerA, 0, 1);
87     }
88     if (this.game.input.keyboard.isDown(this.playerAControls.left)) {
89       this.movePlayer(this.playerA, -1, 0);
90     }
91     if (this.game.input.keyboard.isDown(this.playerAControls.right)) {
92       this.movePlayer(this.playerA, 1, 0);
93     }
94     if (this.game.input.keyboard.isDown(this.playerBControls.up)) {
95       this.movePlayer(this.playerB, 0, -1);
96     }
97     if (this.game.input.keyboard.isDown(this.playerBControls.down)) {
98       this.movePlayer(this.playerB, 0, 1);
99     }
100     if (this.game.input.keyboard.isDown(this.playerBControls.left)) {
101       this.movePlayer(this.playerB, -1, 0);
102     }
103     if (this.game.input.keyboard.isDown(this.playerBControls.right)) {
104       this.movePlayer(this.playerB, 1, 0);
105     }
106   },
107   pollAnalogStickForPlayer: function(pad, player) {
108     if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_LEFT) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) < -0.2) {
109       this.movePlayer(player, -1, 0);
110     }
111     if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_RIGHT) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) > 0.2) {
112       this.movePlayer(player, 1, 0);
113     }
114     if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_UP) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) < -0.2) {
115       this.movePlayer(player, 0, -1);
116     }
117     if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_DOWN) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) > 0.2) {
118       this.movePlayer(player, 0, 1);
119     }
120   },
121   readLevelFile: function() {
122     this.pills = this.game.add.group();
123     this.players = this.game.add.group();
124     this.walls = this.game.add.group();
125
126     var levelText = this.game.cache.getText('level');
127     var splitRows = levelText.split('\n');
128
129
130     for (var x=0; x<splitRows.length; x++) {
131       for (var y=0; y<splitRows[x].length; y++) {
132         switch(splitRows[x][y]) {
133           case '#':
134             this.walls.add(new Wall(this.game, x, y));
135             break;
136           case '.':
137             this.pills.add(new Pill(this.game, x, y));
138             break;
139           case '*':
140             this.pills.add(new BonusPill(this.game, x, y));
141             break;
142           case 'A':
143             this.playerA = new Player(this.game, x, y, 'player-a', 0);
144             this.players.add(this.playerA);
145             break;
146           case 'B':
147             this.playerB = new Player(this.game, x, y, 'player-b', 0);
148             this.players.add(this.playerB);
149             break;
150         }
151       }
152     }
153
154     this.walls.forEach(function(wall) {
155       this.addToMap(wall.x, wall.y);
156     }, this);
157     this.updatePlayerTurn(0);
158   },
159
160   setupPlayerControls: function() {
161     this.playerAControls = {
162       up: Phaser.Keyboard.W,
163       left: Phaser.Keyboard.A,
164       down: Phaser.Keyboard.S,
165       right: Phaser.Keyboard.D
166     };
167     this.playerBControls = {
168       up: Phaser.Keyboard.UP,
169       left: Phaser.Keyboard.LEFT,
170       down: Phaser.Keyboard.DOWN,
171       right: Phaser.Keyboard.RIGHT
172     };
173
174     function addKeyCaptures(controls, keyboard) {
175       for (var index in controls) {
176         if (controls.hasOwnProperty(index)) {
177           keyboard.addKeyCapture(controls[index]);
178         }
179       }
180     }
181     addKeyCaptures(this.playerAControls, this.game.input.keyboard);
182     addKeyCaptures(this.playerBControls, this.game.input.keyboard);
183
184     this.game.input.gamepad.start();
185   },
186   movePlayer: function(player, deltaX, deltaY) {
187     var newX = player.x + deltaX;
188     var newY = player.y + deltaY;
189
190     if (!this.checkMap(newX, newY) && player.isMyTurn && !player.moving) {
191       player.move(newX, newY);
192       this.togglePlayerTurn();
193     }
194   },
195   playerPillCollision: function(player, pill) {
196     player.score += pill.score;
197     pill.destroy();
198
199     this.playerAScoreText.setText(this.playerA.score+'');
200     this.playerBScoreText.setText(this.playerB.score+'');
201   },
202   togglePlayerTurn: function() {
203     this.updatePlayerTurn((this.playerTurn+1)%this.players.length);
204   },
205   updatePlayerTurn: function(newPlayerTurn) {
206     this.playerTurn = newPlayerTurn;
207     for (var i=0; i<this.players.children.length; ++i) {
208       this.players.children[i].isMyTurn = (i === this.playerTurn);
209     }
210     console.log("Player " + this.playerTurn + "'s turn");
211   },
212   setVictoryText: function(newText) {
213     this.victoryText = this.game.add.bitmapText(this.world.width/2/this.world.scale.x, 2, 'scorefont', newText, 1);
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;