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