|
| 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 = {})); |
0 commit comments