Skip to content

Commit 266b62c

Browse files
committed
adding the game state
1 parent 254bf67 commit 266b62c

2 files changed

Lines changed: 335 additions & 0 deletions

File tree

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/// <reference path="phaser.d.ts" />
2+
var __extends = this.__extends || function (d, b) {
3+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
4+
function __() { this.constructor = d; }
5+
__.prototype = b.prototype;
6+
d.prototype = new __();
7+
};
8+
var BasicGame;
9+
(function (BasicGame) {
10+
var GameState = (function (_super) {
11+
__extends(GameState, _super);
12+
function GameState() {
13+
_super.apply(this, arguments);
14+
}
15+
GameState.prototype.create = function () {
16+
this.game.world.height = 620;
17+
18+
this._ballOnPaddle = true;
19+
this._score = 0;
20+
this._lives = 3;
21+
22+
this._s = this.game.add.tileSprite(0, 0, 800, 600, "starfield");
23+
24+
var brick;
25+
this._bricks = this.game.add.group();
26+
27+
for (var y = 0; y < 4; y++) {
28+
for (var x = 0; x < 15; x++) {
29+
brick = this._bricks.create(120 + (x * 36), 100 + (y * 52), "breakout", "brick_" + (y + 1) + "_1.png");
30+
brick.body.bounce.setTo(1, 1);
31+
brick.body.immovable = true;
32+
}
33+
}
34+
35+
this._paddle = this.game.add.sprite(this.game.world.centerX, 500, "breakout", "paddle_big.png");
36+
this._paddle.anchor.setTo(0.5, 0.5);
37+
this._paddle.body.collideWorldBounds = true;
38+
this._paddle.body.bounce.setTo(1, 1);
39+
this._paddle.body.immovable = true;
40+
41+
this._ball = this.game.add.sprite(this.game.world.centerX, this._paddle.y - 16, "breakout", 'ball_1.png');
42+
this._ball.anchor.setTo(0.5, 0.5);
43+
this._ball.body.collideWorldBounds = true;
44+
45+
this._ball.body.bounce.setTo(1, 1);
46+
this._ball.animations.add("spin", ["ball_1.png", "ball_2.png", "ball_3.png", "ball_4.png", "ball_5.png"], 50, true, false);
47+
48+
this._scoreText = this.game.add.text(32, 550, "score: 0", { font: "20px Arial", fill: "#ffffff", align: "left" });
49+
this._livesText = this.game.add.text(680, 550, "lives: 3", { font: "20px Arial", fill: "#ffffff", align: "left" });
50+
this._introText = this.game.add.text(this.game.world.centerX, 400, "- click to start -", { font: "40px Arial", fill: "#ffffff", align: "center" });
51+
this._introText.anchor.setTo(0.5, 0.5);
52+
53+
this.game.input.onDown.add(this.releaseBall, this);
54+
55+
this._ballOnPaddle = true;
56+
};
57+
58+
GameState.prototype.update = function () {
59+
this._paddle.x = this.game.input.x;
60+
61+
if (this._paddle.x < 24) {
62+
this._paddle.x = 24;
63+
} else if (this._paddle.x > this.game.width - 24) {
64+
this._paddle.x = this.game.width - 24;
65+
}
66+
67+
if (this._ballOnPaddle) {
68+
this._ball.x = this._paddle.x;
69+
} else {
70+
this.game.physics.collide(this._ball, this._paddle, this.ballHitPaddle, null, this);
71+
this.game.physics.collide(this._ball, this._bricks, this.ballHitBrick, null, this);
72+
}
73+
74+
if (this._ball.y > 600 && this._ballOnPaddle == false) {
75+
this.ballLost();
76+
}
77+
};
78+
79+
GameState.prototype.quitGame = function (p_pointer) {
80+
this.game.state.start("MainMenu");
81+
};
82+
83+
GameState.prototype.releaseBall = function () {
84+
if (this._ballOnPaddle) {
85+
this._ballOnPaddle = false;
86+
this._ball.body.velocity.y = -300;
87+
this._ball.body.velocity.x = -75;
88+
this._ball.animations.play("spin");
89+
this._introText.visible = false;
90+
}
91+
};
92+
93+
GameState.prototype.ballLost = function () {
94+
this._lives--;
95+
96+
if (this._lives == 0) {
97+
this.gameOver();
98+
} else {
99+
this._livesText.content = "lives: " + this._lives;
100+
this._ballOnPaddle = true;
101+
this._ball.body.velocity.setTo(0, 0);
102+
this._ball.x = this._paddle.x + 16;
103+
this._ball.y = this._paddle.y - 16;
104+
this._ball.animations.stop();
105+
}
106+
};
107+
108+
GameState.prototype.gameOver = function () {
109+
this._ball.body.velocity.setTo(0, 0);
110+
111+
this.quitGame();
112+
};
113+
114+
GameState.prototype.ballHitBrick = function (p_ball, p_brick) {
115+
p_brick.kill();
116+
117+
this._score += 10;
118+
this._scoreText.content = "score: " + this._score;
119+
120+
if (this._bricks.countLiving() == 0) {
121+
this._score += 1000;
122+
this._scoreText.content = "score: " + this._score;
123+
this._introText.content = "- Next Level -";
124+
125+
this._ballOnPaddle = true;
126+
this._ball.body.velocity.setTo(0, 0);
127+
this._ball.x = this._paddle.x + 16;
128+
this._ball.y = this._paddle.y - 16;
129+
this._ball.animations.stop();
130+
131+
this._bricks.callAll('revive');
132+
}
133+
};
134+
135+
GameState.prototype.ballHitPaddle = function (p_ball, p_paddle) {
136+
var l_diff = 0;
137+
138+
if (p_ball.x < p_paddle.x) {
139+
l_diff = p_paddle.x - p_ball.x;
140+
p_ball.body.velocity.x = (-10 * l_diff);
141+
} else if (p_ball.x > p_paddle.x) {
142+
l_diff = p_ball.x - p_paddle.x;
143+
p_ball.body.velocity.x = (10 * l_diff);
144+
} else {
145+
p_ball.body.velocity.x = 2 + Math.random() * 8;
146+
}
147+
};
148+
return GameState;
149+
})(Phaser.State);
150+
BasicGame.GameState = GameState;
151+
})(BasicGame || (BasicGame = {}));
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/// <reference path="phaser.d.ts" />
2+
3+
module BasicGame
4+
{
5+
export class GameState extends Phaser.State {
6+
private _ball:Phaser.Sprite;
7+
private _paddle:Phaser.Sprite;
8+
private _bricks:Phaser.Group;
9+
10+
private _ballOnPaddle:boolean;
11+
12+
private _lives:number;
13+
private _score:number;
14+
15+
private _scoreText:Phaser.Text;
16+
private _livesText:Phaser.Text;
17+
private _introText:Phaser.Text;
18+
19+
private _s:Phaser.TileSprite;
20+
21+
create():void {
22+
this.game.world.height = 620;
23+
24+
this._ballOnPaddle = true;
25+
this._score = 0;
26+
this._lives = 3;
27+
28+
this._s = this.game.add.tileSprite(0,0,800,600,"starfield");
29+
30+
var brick:Phaser.Sprite;
31+
this._bricks = this.game.add.group();
32+
33+
for( var y=0; y < 4; y++ )
34+
{
35+
for( var x=0; x < 15; x++)
36+
{
37+
brick = this._bricks.create(120 + ( x * 36 ), 100 + ( y * 52 ),"breakout","brick_" + ( y+1 ) + "_1.png");
38+
brick.body.bounce.setTo(1, 1);
39+
brick.body.immovable = true;
40+
}
41+
}
42+
43+
this._paddle = this.game.add.sprite( this.game.world.centerX, 500, "breakout", "paddle_big.png");
44+
this._paddle.anchor.setTo(0.5,0.5);
45+
this._paddle.body.collideWorldBounds = true;
46+
this._paddle.body.bounce.setTo(1,1);
47+
this._paddle.body.immovable = true;
48+
49+
this._ball = this.game.add.sprite( this.game.world.centerX, this._paddle.y - 16, "breakout",'ball_1.png');
50+
this._ball.anchor.setTo(0.5,0.5);
51+
this._ball.body.collideWorldBounds = true;
52+
53+
this._ball.body.bounce.setTo(1,1);
54+
this._ball.animations.add("spin", ["ball_1.png", "ball_2.png", "ball_3.png", "ball_4.png", "ball_5.png" ], 50, true, false);
55+
56+
this._scoreText = this.game.add.text(32, 550, "score: 0", {font: "20px Arial", fill: "#ffffff", align: "left" });
57+
this._livesText = this.game.add.text(680, 550, "lives: 3", {font: "20px Arial", fill: "#ffffff", align: "left" });
58+
this._introText = this.game.add.text(this.game.world.centerX, 400, "- click to start -", {font: "40px Arial", fill: "#ffffff", align: "center" });
59+
this._introText.anchor.setTo(0.5,0.5);
60+
61+
this.game.input.onDown.add( this.releaseBall, this );
62+
63+
this._ballOnPaddle = true;
64+
65+
}
66+
67+
update():void
68+
{
69+
this._paddle.x = this.game.input.x;
70+
71+
if( this._paddle.x < 24 )
72+
{
73+
this._paddle.x = 24;
74+
}
75+
else if( this._paddle.x > this.game.width - 24 )
76+
{
77+
this._paddle.x = this.game.width - 24;
78+
}
79+
80+
if( this._ballOnPaddle )
81+
{
82+
this._ball.x = this._paddle.x;
83+
}
84+
else
85+
{
86+
this.game.physics.collide( this._ball, this._paddle, this.ballHitPaddle, null, this );
87+
this.game.physics.collide( this._ball, this._bricks, this.ballHitBrick, null, this );
88+
}
89+
90+
if( this._ball.y > 600 && this._ballOnPaddle == false )
91+
{
92+
this.ballLost();
93+
}
94+
}
95+
96+
private quitGame( p_pointer?:any ):void
97+
{
98+
this.game.state.start("MainMenu");
99+
}
100+
101+
private releaseBall():void
102+
{
103+
if( this._ballOnPaddle )
104+
{
105+
this._ballOnPaddle = false;
106+
this._ball.body.velocity.y = -300;
107+
this._ball.body.velocity.x = -75;
108+
this._ball.animations.play("spin");
109+
this._introText.visible = false;
110+
}
111+
}
112+
113+
private ballLost():void
114+
{
115+
this._lives--;
116+
117+
if( this._lives == 0)
118+
{
119+
this.gameOver();
120+
}
121+
else
122+
{
123+
this._livesText.content = "lives: " + this._lives;
124+
this._ballOnPaddle = true;
125+
this._ball.body.velocity.setTo(0,0);
126+
this._ball.x = this._paddle.x + 16;
127+
this._ball.y = this._paddle.y - 16;
128+
this._ball.animations.stop();
129+
}
130+
}
131+
132+
private gameOver():void
133+
{
134+
this._ball.body.velocity.setTo(0,0);
135+
136+
this.quitGame();
137+
}
138+
139+
private ballHitBrick( p_ball:Phaser.Sprite, p_brick:Phaser.Sprite ):void
140+
{
141+
p_brick.kill();
142+
143+
this._score += 10;
144+
this._scoreText.content = "score: " + this._score;
145+
146+
if( this._bricks.countLiving() == 0 )
147+
{
148+
this._score += 1000;
149+
this._scoreText.content = "score: " + this._score;
150+
this._introText.content = "- Next Level -";
151+
152+
this._ballOnPaddle = true;
153+
this._ball.body.velocity.setTo(0,0);
154+
this._ball.x = this._paddle.x + 16;
155+
this._ball.y = this._paddle.y - 16;
156+
this._ball.animations.stop();
157+
158+
this._bricks.callAll('revive');
159+
}
160+
161+
162+
}
163+
164+
private ballHitPaddle( p_ball:Phaser.Sprite, p_paddle:Phaser.Sprite ):void
165+
{
166+
var l_diff:number = 0;
167+
168+
if( p_ball.x < p_paddle.x )
169+
{
170+
l_diff = p_paddle.x - p_ball.x;
171+
p_ball.body.velocity.x = (-10 * l_diff);
172+
}
173+
else if( p_ball.x > p_paddle.x )
174+
{
175+
l_diff = p_ball.x - p_paddle.x;
176+
p_ball.body.velocity.x = (10 * l_diff);
177+
}
178+
else
179+
{
180+
p_ball.body.velocity.x = 2 + Math.random() * 8;
181+
}
182+
}
183+
}
184+
}

0 commit comments

Comments
 (0)