Created
April 10, 2015 09:56
-
-
Save p9436/4373d0cfc9db5a19f801 to your computer and use it in GitHub Desktop.
Phaser player sandbox 20x20 Phaser player sandbox 20x20 // source http://jsbin.com/toneni
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta name="description" content="Phaser player sandbox 20x20" /> | |
| <script src="https://cdn.jsdelivr.net/phaser/2.2.2/phaser.min.js"></script> | |
| <meta charset="utf-8"> | |
| <title>Phaser player sandbox 20x20</title> | |
| </head> | |
| <body> | |
| <script id="jsbin-javascript"> | |
| // var SHOT_DELAY = 150, // milliseconds (10 bullets/second) | |
| // BULLET_SPEED = 300, // pixels/second | |
| // NUMBER_OF_BULLETS = 10, | |
| // lastBulletShotAt = 0; | |
| var cursors; | |
| var debug_text=''; | |
| var enemies; | |
| var game = new Phaser.Game(320, 200, Phaser.CANVAS); | |
| //------------------------------------------------------------- | |
| var Weapon = function (parent, ammunition) { | |
| this.parent = parent; | |
| this.shotDelay = 550; // milliseconds (bullets/second) | |
| this.Limit = 10; | |
| this.lastShotAt = 0; | |
| this.ammunitions = parent.game.add.group(); | |
| this.ammunition = ammunition; | |
| }; | |
| Weapon.prototype.shot = function() { | |
| if (this.ammunitions.total >= this.Limit) return; | |
| if (game.time.now - this.lastShotAt < this.shotDelay) return; | |
| this.lastShotAt = game.time.now; | |
| if (this.parent.face == -1) { | |
| new this.ammunition(game, this.parent.body.x-5, this.parent.body.y+8, -1, this.ammunitions); | |
| } else { | |
| new this.ammunition(game, this.parent.body.x+19, this.parent.body.y+8, 1, this.ammunitions); | |
| } | |
| }; | |
| Weapon.prototype.machinegun = function() { | |
| this.shotDelay = 300; | |
| return this; | |
| }; | |
| Weapon.prototype.rocketlauncher = function() { | |
| return this; | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Hero = function (game,x,y) { | |
| this.game = game; | |
| this.face = 1; | |
| this.jumps = 0; | |
| this.jumping = false; | |
| this.onLadder = null; | |
| this.locked = false; | |
| this.lockedTo = null; | |
| this.standing = false; | |
| this.weapons = {rocketlauncher: new Weapon(this, Missile).rocketlauncher(), | |
| machinegun: new Weapon(this, Bullet).machinegun()}; | |
| this.availableWeapons = ['machinegun', 'rocketlauncher']; | |
| this.currentWeapon = this.weapons.rocketlauncher; | |
| this.currentWeaponIndex = 0; | |
| Phaser.Sprite.call(this, game, x, y, 'hero'); | |
| this.animations.add('right', [0, 1, 2, 3], 10, true); | |
| this.animations.add('left', [5, 6, 7, 8], 10, true); | |
| // this.animations.add('turn', [4], 20, true); | |
| this.anchor.setTo(0.5); | |
| game.physics.enable(this, Phaser.Physics.ARCADE); | |
| this.body.collideWorldBounds = true; | |
| this.body.bounce.set(0.1); | |
| // this.body.allowGravity = true; | |
| this.dust = 0; | |
| // dust | |
| bmd = game.add.bitmapData(8, 8); | |
| bmd.context.fillStyle = '#999999'; | |
| bmd.context.fillRect(0, 0, 8, 8); | |
| this.emitter_run = game.add.emitter(0, 0, 10); | |
| this.emitter_run.makeParticles(bmd, 0, 20, true, true); | |
| this.emitter_run.setXSpeed(0,0); | |
| this.emitter_run.setYSpeed(0,0); | |
| this.emitter_run.setAlpha(1, 0, 1000, Phaser.Easing.Linear.InOut); | |
| this.emitter_run.setScale(0, 1, 0, 1, 1000); | |
| this.emitter_run.gravity = -900; | |
| // more dust | |
| bmd = game.add.bitmapData(10, 10); | |
| bmd.context.fillStyle = '#999999'; | |
| bmd.context.fillRect(0, 0, 10, 10); | |
| this.emitter_jump = game.add.emitter(0, 0, 10); | |
| this.emitter_jump.makeParticles(bmd, 0, 20, true, true); | |
| this.emitter_jump.setXSpeed(-20,20); | |
| this.emitter_jump.setYSpeed(0,0); | |
| this.emitter_jump.setAlpha(1, 0, 1000, Phaser.Easing.Linear.InOut); | |
| this.emitter_jump.gravity = -900; | |
| game.add.existing(this); | |
| }; | |
| Hero.prototype = Object.create(Phaser.Sprite.prototype); | |
| Hero.prototype.constructor = Hero; | |
| Hero.prototype.updates = function () { | |
| this.standing = this.body.blocked.down || this.body.touching.down || this.locked; | |
| this.body.velocity.x = 0; | |
| debug_text = this.locked; | |
| if (this.onLadder){ | |
| this.body.allowGravity = false; | |
| if (cursors.left.isDown) { | |
| this.body.velocity.x = -150; | |
| this.face = -1; | |
| this.animations.play('left'); | |
| } else if (cursors.right.isDown) { | |
| this.body.velocity.x = 150; | |
| this.face = 1; | |
| this.animations.play('right'); | |
| } | |
| if (cursors.up.isDown) { | |
| this.body.velocity.y = -150; | |
| } else if (cursors.down.isDown) { | |
| this.body.velocity.y = 150; | |
| } else { | |
| this.animations.stop(); | |
| } | |
| } else { | |
| this.body.allowGravity = true; | |
| if (this.standing) { | |
| this.jumps = 2; | |
| this.jumping = false; | |
| } | |
| if (this.jumps > 0 && upInputIsActive(5)) { | |
| this.body.velocity.y = -300; | |
| this.jumping = true; | |
| this.emitter_jump.x = this.x; | |
| this.emitter_jump.y = this.y; | |
| this.emitter_jump.start(true, 1000, null, 10); | |
| this.unlock(); | |
| } | |
| if (this.jumping && upInputReleased()) { | |
| this.jumps--; | |
| this.jumping = false; | |
| } | |
| if (this.locked) | |
| { | |
| this.checkLock(); | |
| } | |
| if (cursors.left.isDown) { | |
| this.body.velocity.x = -150; | |
| this.face = -1; | |
| this.animations.play('left'); | |
| } else if (cursors.right.isDown) { | |
| this.body.velocity.x = 150; | |
| this.face = 1; | |
| this.animations.play('right'); | |
| } else { | |
| this.animations.stop(); | |
| } | |
| if (this.standing && this.deltaX!==0 && game.time.now - this.dust > 200) { | |
| this.dust = game.time.now; | |
| this.emitter_run.x = this.x; | |
| this.emitter_run.y = this.y+8; | |
| this.emitter_run.start(true, 500, null, 1); | |
| } | |
| } | |
| this.onLadder = null; | |
| if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR)) { | |
| this.currentWeapon.shot(); | |
| } | |
| }; | |
| Hero.prototype.checkLock = function () { | |
| this.body.velocity.y = 0; | |
| if (this.body.right < this.lockedTo.body.x || this.body.x > this.lockedTo.body.right) { | |
| this.unlock(); | |
| } | |
| }; | |
| Hero.prototype.unlock = function () { | |
| this.locked = false; | |
| this.lockedTo = false; | |
| }; | |
| Hero.prototype.switchWeapon = function () { | |
| this.currentWeaponIndex += 1; | |
| if (this.currentWeaponIndex >= this.availableWeapons.length) | |
| this.currentWeaponIndex = 0; | |
| console.log(this.currentWeaponIndex); | |
| this.currentWeapon = this.weapons[this.availableWeapons[this.currentWeaponIndex]]; | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Ladder = function(game, x, y, height, group) { | |
| this.game = game; | |
| if (typeof group === 'undefined') { group = game.world; } | |
| Phaser.TileSprite.call(this, game, x, y, 16, height, 'bricks', 120); | |
| this.game.physics.arcade.enableBody(this); | |
| // this.autoScroll(0,-10); | |
| this.anchor.setTo(0, 0); | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| game.physics.arcade.enableBody(this); | |
| this.body.allowGravity = false; | |
| this.body.immovable = true; | |
| this.game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Ladder.prototype = Object.create(Phaser.TileSprite.prototype); | |
| Ladder.prototype.constructor = Ladder; | |
| // ------------------------------------------------------------------ | |
| var Coin = function(game, x, y, group) { | |
| this.game = game; | |
| if (typeof group === 'undefined') { group = game.world; } | |
| Phaser.TileSprite.call(this, game, x, y, 16, 16, 'bricks', 121); | |
| this.game.physics.arcade.enableBody(this); | |
| this.anchor.setTo(0, 0); | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| this.game.physics.arcade.enableBody(this); | |
| this.body.allowGravity = false; | |
| this.body.immovable = true; | |
| this.game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Coin.prototype = Object.create(Phaser.TileSprite.prototype); | |
| Coin.prototype.constructor = Coin; | |
| // ------------------------------------------------------------------ | |
| // Missile constructor | |
| var Missile = function(game, x, y, direction, group) { | |
| this.SPEED = 150; // missile speed pixels/second | |
| this.direction = direction; | |
| this.game = game; | |
| var bmd = game.add.bitmapData(10, 4); | |
| bmd.context.fillStyle = '#FFDDEE'; | |
| bmd.context.fillRect(0, 0, 10, 4); | |
| Phaser.Sprite.call(this, game, x, y, bmd); | |
| // Set the pivot point for this sprite to the center | |
| this.anchor.setTo(0.5, 0.5); | |
| // Enable physics on the missile | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| game.physics.enable(this); | |
| game.physics.arcade.enableBody(this); | |
| this.checkWorldBounds = true; | |
| this.outOfBoundsKill = true; | |
| // Define constants that affect motion | |
| this.SMOKE_LIFETIME = 2000; // milliseconds | |
| this.distance = 0; | |
| this.ddist = game.rnd.normal()+1; | |
| this.trajectory = game.rnd.integerInRange(50, 80); | |
| this.amp = 1; | |
| bmd = game.add.bitmapData(8, 8); | |
| bmd.context.fillStyle = '#666666'; | |
| bmd.context.fillRect(0, 0, 8, 8); | |
| this.smokeEmitter = this.game.add.emitter(0, 0, 100); | |
| // this.smokeEmitter.gravity = -800; | |
| this.smokeEmitter.setXSpeed(0, 0); | |
| this.smokeEmitter.setYSpeed(-50, -80); // make smoke drift upwards | |
| this.smokeEmitter.setAlpha(1, 0, this.SMOKE_LIFETIME, Phaser.Easing.Linear.InOut); | |
| this.smokeEmitter.setScale(1, 2, 1, 2, this.SMOKE_LIFETIME); | |
| this.smokeEmitter.makeParticles(bmd); | |
| this.smokeEmitter.start(false, this.SMOKE_LIFETIME, 50); | |
| this.smokeEmitter.gravity = -800; | |
| this.game.add.existing(this); | |
| group.add(this); | |
| }; | |
| // Missiles are a type of Phaser.Sprite | |
| Missile.prototype = Object.create(Phaser.Sprite.prototype); | |
| Missile.prototype.constructor = Missile; | |
| Missile.prototype.update = function() { | |
| // Position the smoke emitter at the center of the missile | |
| this.smokeEmitter.x = this.x; | |
| this.smokeEmitter.y = this.y; | |
| // Calculate velocity vector based on this.rotation and this.SPEED | |
| this.body.velocity.x = this.direction * this.SPEED; | |
| this.body.velocity.y = this.trajectory * this.amp * Math.sin(-this.distance/30); | |
| this.rotation = Math.asin(this.direction * this.body.velocity.y / this.SPEED); | |
| this.distance += this.ddist; | |
| this.amp = this.amp / 1.007; | |
| if (!this.alive) { | |
| this.smokeEmitter.on = false; | |
| this.destroy(); | |
| } | |
| }; | |
| Missile.prototype.explode = function() { | |
| // Create BitmapData | |
| var bmd = game.add.bitmapData(100,100); | |
| // Draw circle | |
| bmd.ctx.fillStyle = '#ff9900'; | |
| bmd.ctx.beginPath(); | |
| bmd.ctx.arc(50, 50, 10, 0, Math.PI*2, true); | |
| bmd.ctx.closePath(); | |
| bmd.ctx.fill(); | |
| this.explodeEmitter = this.game.add.emitter(0, 0, 100); | |
| this.explodeEmitter.setXSpeed(-100, 100); | |
| this.explodeEmitter.setYSpeed(-100, 100); | |
| this.explodeEmitter.setAlpha(0.5, 0, 500); | |
| this.explodeEmitter.setScale(0, 4,0,4,200); | |
| this.explodeEmitter.blendMode = 1; | |
| this.explodeEmitter.x = this.x; | |
| this.explodeEmitter.y = this.y; | |
| this.explodeEmitter.makeParticles(bmd, 0, 50, false, false); | |
| this.explodeEmitter.start(true, 200, null, 10); | |
| this.kill(); | |
| }; | |
| Missile.prototype.bang = function() { | |
| // Draw circle | |
| var bmd = game.add.bitmapData(100,100); | |
| bmd.ctx.fillStyle = '#ff9900'; | |
| bmd.ctx.beginPath(); | |
| bmd.ctx.arc(50, 50, 10, 0, Math.PI*2, true); | |
| bmd.ctx.closePath(); | |
| bmd.ctx.fill(); | |
| var boom = this.game.add.sprite(this.x,this.y,bmd); | |
| boom.anchor.setTo(0.5, 0.5); | |
| boom.scale.x = 0; | |
| boom.scale.y = 0; | |
| var tween = this.game.add.tween(boom.scale).to({x: 5, y:5 }, 100, Phaser.Easing.Linear.None); | |
| tween.onComplete.addOnce(function(){boom.kill();}); | |
| tween.start(); | |
| this.kill(); | |
| this.game.enemies.forEach(function(subgroup){ | |
| subgroup.forEach(function(object){ | |
| distance = this.game.physics.arcade.distanceBetween(boom,object); | |
| radius = 80; | |
| mass = 0.05; | |
| if (distance < radius) { | |
| damage = (radius - distance)/10; | |
| // popUp(this.popUps, object, '-'+~~damage); | |
| angle = this.game.physics.arcade.angleBetween(boom, object); | |
| object.body.velocity.x += Math.cos(angle) * ((radius - distance) / mass); | |
| object.body.velocity.y += Math.sin(angle) * ((radius - distance) / mass); | |
| object.health -= damage/100; | |
| // object.healthStatus.setText(~~(object.health*100)) | |
| // if (object.health < 0.5) { | |
| // object.healthStatus.addColor('#900',0); | |
| // } | |
| } | |
| }); | |
| }); | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Bullet = function (game, x, y, direction, group) { | |
| this.SPEED = 500; | |
| this.direction = direction; | |
| var bmd = game.add.bitmapData(20, 1); | |
| bmd.context.fillStyle = '#FFFF00'; | |
| bmd.context.fillRect(0, 0, 20, 1); | |
| Phaser.Sprite.call(this, game, x, y, bmd); | |
| this.anchor.set(0.5,0.5); | |
| game.physics.enable(this); | |
| game.physics.arcade.enableBody(this); | |
| this.body.allowGravity = false; | |
| this.checkWorldBounds = true; | |
| this.outOfBoundsKill = true; | |
| this.body.velocity.x = this.direction * this.SPEED; | |
| game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Bullet.prototype = Object.create(Phaser.Sprite.prototype); | |
| Bullet.prototype.constructor = Bullet; | |
| // ------------------------------------------------------------------ | |
| var Bat = function(game,x,y, group) { | |
| if (typeof group === 'undefined') { group = game.world; } | |
| this.game = game; | |
| Phaser.Sprite.call(this, game, x, y, 'bat'); | |
| this.anchor.setTo(0.5, 0.5); | |
| this.anchor.set(0.5); | |
| this.animations.add('fly', [0,1,2,1 ], 20, true); | |
| this.play('fly'); | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| this.game.physics.arcade.enableBody(this); | |
| this.checkWorldBounds = true; | |
| this.outOfBoundsKill = true; | |
| this.body.collideWorldBounds = true; | |
| this.body.bounce.set(0.1); | |
| this.body.allowGravity = false; | |
| this.changeDirection = 0; | |
| this.changeDirectionDelay = this.game.rnd.between(1000,2000); | |
| this.game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Bat.prototype = Object.create(Phaser.Sprite.prototype); | |
| Bat.prototype.constructor = Bat; | |
| Bat.prototype.update = function() { | |
| if (this.game.time.now - this.changeDirection > this.changeDirectionDelay ) { | |
| this.body.velocity.x = this.game.rnd.normal()*50; | |
| this.body.velocity.y = this.game.rnd.normal()*50; | |
| this.changeDirection = this.game.time.now; | |
| } | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Ball = function(game,x,y,group) { | |
| if (typeof group === 'undefined') { group = game.world; } | |
| this.game = game; | |
| var bmd = game.add.bitmapData(20,20); | |
| bmd.ctx.fillStyle = '#999999'; | |
| bmd.ctx.beginPath(); | |
| bmd.ctx.arc(10, 10, 10, 0, Math.PI*2, true); | |
| bmd.ctx.closePath(); | |
| bmd.ctx.fill(); | |
| Phaser.Sprite.call(this, game, x, y, bmd); | |
| this.game.physics.arcade.enable(this, Phaser.Physics.ARCADE); | |
| this.body.collideWorldBounds = true; | |
| this.body.bounce.set(0.1); | |
| this.anchor.setTo(0.5, 0.5); | |
| this.healthStatus = game.add.text(0, -10, '100', {font: "9px Arial", fill: "#888888" }); | |
| this.addChild(this.healthStatus); | |
| game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Ball.prototype = Object.create(Phaser.Sprite.prototype); | |
| Ball.prototype.constructor = Ball; | |
| Ball.prototype.update = function() { | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Ghost = function(game,x,y,group) { | |
| if (typeof group === 'undefined') { group = game.world; } | |
| this.game = game; | |
| Phaser.Sprite.call(this, game, 200, 140, 'enemy'); | |
| this.anchor.setTo(0.5, 0.5); | |
| var randomScale = 0.8 + Math.random(); | |
| this.scale.setTo(randomScale, randomScale); | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| this.game.physics.arcade.enableBody(this); | |
| this.body.allowGravity = true; | |
| this.checkWorldBounds = true; | |
| this.outOfBoundsKill = true; | |
| this.body.velocity.x = 0; | |
| this.body.velocity.y = 0; | |
| this.dx = 10; | |
| game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Ghost.prototype = Object.create(Phaser.Sprite.prototype); | |
| Ghost.prototype.constructor = Ghost; | |
| Ghost.prototype.update = function() { | |
| if (this.body.velocity.x === 0) this.dx = -this.dx; | |
| // this.body.velocity.x = this.dx; | |
| }; | |
| // ------------------------------------------------------------------ | |
| CloudPlatform = function (game, x, y, key, group) { | |
| if (typeof group === 'undefined') { group = game.world; } | |
| Phaser.Sprite.call(this, game, x, y, key); | |
| game.physics.arcade.enable(this); | |
| this.anchor.x = 0.5; | |
| this.body.customSeparateX = true; | |
| this.body.customSeparateY = true; | |
| this.body.allowGravity = false; | |
| this.body.immovable = true; | |
| this.playerLocked = false; | |
| group.add(this); | |
| // game.add.existing(this); | |
| }; | |
| CloudPlatform.prototype = Object.create(Phaser.Sprite.prototype); | |
| CloudPlatform.prototype.constructor = CloudPlatform; | |
| CloudPlatform.prototype.addMotionPath = function (motionPath) { | |
| this.tweenX = this.game.add.tween(this.body); | |
| this.tweenY = this.game.add.tween(this.body); | |
| for (var i = 0; i < motionPath.length; i++) { | |
| this.tweenX.to( { x: motionPath[i].x }, motionPath[i].xSpeed, motionPath[i].xEase); | |
| this.tweenY.to( { y: motionPath[i].y }, motionPath[i].ySpeed, motionPath[i].yEase); | |
| } | |
| this.tweenX.loop(); | |
| this.tweenY.loop(); | |
| }; | |
| CloudPlatform.prototype.start = function () { | |
| this.tweenX.start(); | |
| this.tweenY.start(); | |
| }; | |
| CloudPlatform.prototype.stop = function () { | |
| this.tweenX.stop(); | |
| this.tweenY.stop(); | |
| }; | |
| // ------------------------------------------------------------------ | |
| //**************************************************************************************** | |
| //**************************************************************************************** | |
| //**************************************************************************************** | |
| //**************************************************************************************** | |
| var Game = function (game) { | |
| this.map = null; | |
| this.coins = null; | |
| this.layer = null; | |
| this.enemies = null; | |
| this.bats = null; | |
| this.ghost = null; | |
| this.player = null; | |
| this.clouds = null; | |
| this.ladders = null; | |
| this.bloodsplashEmitter = null; | |
| this.fragsEmitter = null; | |
| this.bulletSparksEmitter = null; | |
| }; | |
| Game.prototype = { | |
| preload: function () { | |
| game.stage.backgroundColor = 0x666666; | |
| game.time.advancedTiming = true; | |
| game.load.baseURL = 'http://9436.assets.s3.amazonaws.com/'; | |
| game.load.crossOrigin = ''; | |
| game.load.image('background', 'background.png'); | |
| game.load.image('enemy', 'player2.png'); | |
| game.load.image('platform', 'platform.png'); | |
| game.load.image('tiles', 'bricks2.png?'); | |
| game.load.tilemap('sandbox', 'sandbox20x20.json?1', null, Phaser.Tilemap.TILED_JSON); | |
| game.load.spritesheet('hero', 'hero11.png', 16, 16); | |
| this.load.spritesheet('bat', 'flying8.png', 8,8); | |
| game.load.spritesheet('bricks', 'bricks2.png?', 16, 16); | |
| }, | |
| create: function () { | |
| game.objects = {}; | |
| game.physics.startSystem(Phaser.Physics.ARCADE); | |
| game.physics.arcade.gravity.y = 800; | |
| // this.background = game.add.sprite(0, 0, 'background'); | |
| // this.background.fixedToCamera = true; | |
| this.map = game.add.tilemap('sandbox'); | |
| this.map.addTilesetImage('bricks', 'tiles'); | |
| this.layer = this.map.createLayer('walls'); | |
| this.layer.resizeWorld(); | |
| // this.layer.debug = true; | |
| this.map.setCollisionBetween(1,2000); | |
| // this.map.setTileIndexCallback([1], hit, this); | |
| this.popUps = initPopUps(); | |
| game.renderer.renderSession.roundPixels = false; | |
| this.clouds = this.add.physicsGroup(); | |
| new CloudPlatform(this.game, 80, 96, 'platform', this.clouds).addMotionPath([ | |
| { x: "+0", xSpeed: 1000, xEase: "Linear", | |
| y: "+70", ySpeed: 1000, yEase: "Sine.easeIn" }, | |
| { x: "-0", xSpeed: 1000, xEase: "Linear", | |
| y: "-70", ySpeed: 1000, yEase: "Sine.easeOut" } | |
| ]); | |
| this.clouds.callAll('start'); | |
| this.ladders = this.add.physicsGroup(); | |
| this.player = new Hero(this.game,30,80); | |
| game.camera.follow(this.player); | |
| for (var i=0, ic=this.map.objects.ladders.length; i<ic; i++) { | |
| var li = this.map.objects.ladders[i]; | |
| new Ladder(game, li.x, li.y, li.heihgt, this.ladders); | |
| } | |
| this.coins = this.add.group(); | |
| for (var i=0, ic=this.map.objects.coins.length; i<ic; i++) { | |
| var li = this.map.objects.coins[i]; | |
| new Coin(game, li.x, li.y, this.coins); | |
| } | |
| this.bats = this.add.physicsGroup(); | |
| for(var i=0;i<10;i++) | |
| new Bat(this.game, game.rnd.between(30,300), game.rnd.between(30,300), this.bats); | |
| this.ghost = this.add.group(); | |
| for(var i=0;i<1;i++) | |
| new Ghost(this.game, 1,1, this.ghost); | |
| this.balls = game.add.group(); | |
| for(var i=0;i<10;i++) { | |
| new Ball(this.game, game.rnd.between(30,300), game.rnd.between(30,300), this.balls); | |
| } | |
| // game.objects.balls = this.balls; | |
| game.enemies = this.add.group(); | |
| game.enemies.add(this.ghost); | |
| game.enemies.add(this.bats); | |
| game.enemies.add(this.balls); | |
| var bmd; | |
| bmd = game.add.bitmapData(2, 2); | |
| bmd.context.fillStyle = '#0099FF'; | |
| bmd.context.fillRect(0, 0, 2, 2); | |
| this.bulletSparksEmitter = game.add.emitter(0, 0, 100); | |
| this.bulletSparksEmitter.makeParticles(bmd, 0, 200, true, true); | |
| this.bulletSparksEmitter.setXSpeed(-300,300); | |
| this.bulletSparksEmitter.setYSpeed(-300,300); | |
| this.bulletSparksEmitter.bounce.setTo(0.5, 0.5); | |
| this.bulletSparksEmitter.gravity = 50; | |
| bmd = game.add.bitmapData(2, 2); | |
| bmd.context.fillStyle = '#FF0000'; | |
| bmd.context.fillRect(0, 0, 2, 2); | |
| this.bloodsplashEmitter = game.add.emitter(0, 0, 100); | |
| this.bloodsplashEmitter.makeParticles(bmd, 0, 200, true, true); | |
| this.bloodsplashEmitter.setYSpeed(-150,150); | |
| this.bloodsplashEmitter.setXSpeed(-150,150); | |
| this.bloodsplashEmitter.setRotation(0,0); | |
| this.bloodsplashEmitter.setAlpha(1, 0, 5000); | |
| this.bloodsplashEmitter.bounce.setTo(0,0); | |
| this.bloodsplashEmitter.gravity = 150; | |
| cursors = game.input.keyboard.createCursorKeys(); | |
| bmd = game.add.bitmapData(3, 3); | |
| bmd.context.fillStyle = '#FF0000'; | |
| bmd.context.fillRect(0, 0, 3, 3); | |
| this.fragsEmitter = game.add.emitter(0, 0, 100); | |
| this.fragsEmitter.makeParticles(bmd, 0, 200, true, true); | |
| this.fragsEmitter.setXSpeed(-300,300); | |
| this.fragsEmitter.setYSpeed(-300,300); | |
| this.fragsEmitter.setRotation(0,0); | |
| this.fragsEmitter.particleDrag.x = 100; | |
| this.fragsEmitter.setAlpha(1, 0, 50000); | |
| this.fragsEmitter.bounce.setTo(0.5,0.2); | |
| this.fragsEmitter.gravity = 150; | |
| }, | |
| preRender: function () { | |
| if (this.game.paused){ | |
| debub_text = 'paused'; | |
| // Because preRender still runs even if your game pauses! | |
| return; | |
| } | |
| if (this.player) { | |
| if (this.player.locked && this.player.lockedTo) { | |
| this.player.x += this.player.lockedTo.deltaX; | |
| this.player.y = this.player.lockedTo.y-10; | |
| if (this.player.body.velocity.x !== 0) { | |
| this.player.body.velocity.y = 0; | |
| } | |
| } | |
| } | |
| }, | |
| update: function () { | |
| game.physics.arcade.collide(this.player, this.layer, playerLayerCollide, null, this); | |
| game.physics.arcade.collide(this.player.weapons.machinegun.ammunitions, this.layer, bulletSparks, null, this); | |
| game.physics.arcade.collide(this.player.weapons.rocketlauncher.ammunitions, this.layer, missileBang, null, this); | |
| game.physics.arcade.collide(this.player.weapons.machinegun.ammunitions, this.bats, bulletBatCollide, null, this ); | |
| game.physics.arcade.collide(this.player.weapons.machinegun.ammunitions, this.ghost, bulletBatCollide, null, this ); | |
| game.physics.arcade.collide(this.player, this.clouds, customSep, null, this); | |
| game.physics.arcade.overlap(this.player, this.ladders, playerLadderCollide, null, this); | |
| game.physics.arcade.overlap(this.player, this.coins, playerCoinCollide, null, this); | |
| game.physics.arcade.collide(this.bloodsplashEmitter, this.layer, bloodsplashLayerCollide, null, this); | |
| game.physics.arcade.collide(this.fragsEmitter, this.layer); | |
| game.physics.arcade.collide(this.ghost, this.layer ); | |
| game.physics.arcade.collide(this.bats, this.layer); | |
| game.physics.arcade.collide(this.balls, this.layer); | |
| game.physics.arcade.overlap(this.player, this.bats, playerBatCollide, null, this); | |
| if (game.input.keyboard.downDuration(Phaser.Keyboard.Q,1)){ | |
| this.player.switchWeapon(); | |
| } | |
| game.input.onDown.add(bang, this); | |
| this.player.updates(); | |
| debug_text = 'Health: ' + this.player.health*100; | |
| }, | |
| render: function () { | |
| game.debug.text('FPS:'+game.time.fps ,16,16); | |
| game.debug.text(debug_text,86,16); | |
| } | |
| }; | |
| game.state.add('Game', Game, true); | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| function bang(pointer) { | |
| var boom = game.add.graphics(game.world.centerX, game.world.centerY); | |
| boom.lineStyle(1, 0xffffff); | |
| boom.drawCircle(0, 0, 80); | |
| boom.x = pointer.x; | |
| boom.y = pointer.y; | |
| boom.scale.x = 0; | |
| boom.scale.y = 0; | |
| var tween = game.add.tween(boom.scale).to({x: 1.5, y:1.5}, 100, Phaser.Easing.Linear.None); | |
| tween.onComplete.addOnce(function(){boom.scale.x =0; }); | |
| tween.start(); | |
| game.enemies.forEach(function(e) { | |
| e.forEach(function(object){ | |
| distance = game.physics.arcade.distanceBetween(boom,object); | |
| radius = 80; | |
| mass = 0.1; | |
| if (distance < radius) { | |
| damage = (radius - distance)/10; | |
| // popUp(popUpAlerts, object, '-'+~~damage); | |
| angle = game.physics.arcade.angleBetween(boom, object); | |
| object.body.velocity.x += Math.cos(angle) * ((radius - distance) / mass); | |
| object.body.velocity.y += Math.sin(angle) * ((radius - distance) / mass); | |
| // object.health -= damage/100; | |
| // object.healthStatus.setText(~~(object.health*100)) | |
| // if (object.health < 0.5) { | |
| // object.healthStatus.addColor('#900',0); | |
| // } | |
| } | |
| }); | |
| }); | |
| } | |
| function customSep(player, platform) { | |
| if (!player.locked && player.body.velocity.y > 0) { | |
| player.locked = true; | |
| player.lockedTo = platform; | |
| platform.playerLocked = true; | |
| player.body.velocity.y = 0; | |
| } | |
| } | |
| function upInputIsActive(duration) { | |
| return game.input.keyboard.downDuration(Phaser.Keyboard.UP, duration); | |
| } | |
| function upInputReleased() { | |
| return game.input.keyboard.upDuration(Phaser.Keyboard.UP); | |
| } | |
| function hit(sprite,tile) { | |
| // if (tile.alpha > 0.3) { | |
| // tile.alpha -= 0.01; | |
| // layer.dirty = true; | |
| // } | |
| return true; | |
| } | |
| function enemyTurn(enemy, layer) { | |
| if(enemy.body.blocked.right){ | |
| enemy.body.velocity.x = -speed; | |
| } else if (enemy.body.blocked.left) { | |
| enemy.body.velocity.x = speed; | |
| } | |
| } | |
| function playerCoinCollide(player, coin) { | |
| coin.destroy(); | |
| } | |
| function playerBatCollide(player, bat) { | |
| bat.kill(); | |
| player.damage(0.1); | |
| } | |
| function playerLadderCollide(player, ladder) { | |
| player.onLadder = ladder; | |
| player.body.velocity.y = 0; | |
| } | |
| function bulletBatCollide(bullet,bat) { | |
| this.bloodsplashEmitter.x = bat.x; | |
| this.bloodsplashEmitter.y = bat.y; | |
| this.bloodsplashEmitter.start(true, 5000, null, 10); | |
| var damage = 1; | |
| popUp(this.popUps, bat, '-'+damage); | |
| bat.damage(damage/10); | |
| if (bat.health<=0) { | |
| this.fragsEmitter.x = bat.x; | |
| this.fragsEmitter.y = bat.y; | |
| this.fragsEmitter.start(true, 0, null, 10); | |
| } | |
| bullet.kill(); | |
| } | |
| function bloodsplashLayerCollide(a,b) { | |
| a.body.velocity.x=0; | |
| a.body.velocity.y=0; | |
| // a.body.allowGravity = false; | |
| } | |
| function playerLayerCollide(player, tile) { | |
| // if (tile.index > 280) { | |
| // if (tile.alpha < 0.2) { | |
| // map.removeTile(tile.x,tile.y); | |
| // } else { | |
| // tile.alpha -= 0.1; | |
| // layer.dirty = true; | |
| // } | |
| // debug_text = tile.alpha; | |
| // } | |
| } | |
| function missileBang(a, b) { | |
| // a.explode(); | |
| a.bang(); | |
| } | |
| function bulletSparks(bullet, tile) { | |
| this.bulletSparksEmitter.y = tile.worldY+8; | |
| if (bullet.direction>0) { | |
| this.bulletSparksEmitter.x = tile.worldX; | |
| } else { | |
| this.bulletSparksEmitter.x = tile.worldX+16; | |
| } | |
| this.bulletSparksEmitter.start(true, 50, null, 5); | |
| bullet.kill(); | |
| } | |
| function initPopUps(){ | |
| var popUp, popUps = game.add.group(); | |
| for(var i=0;i<10;i++) { | |
| popUp = game.add.sprite(0,0); | |
| popUp.text = game.add.text(0, -10, '', {font: "9px Arial", fill: "#fff" }); | |
| popUp.addChild(popUp.text); | |
| popUps.add(popUp); | |
| popUp.kill(); | |
| } | |
| return popUps; | |
| } | |
| function popUp(popUps, object, text){ | |
| var a = popUps.getFirstDead(); | |
| if (a) { | |
| a.alpha = 1; | |
| a.reset(object.x, object.y-20); | |
| a.text.setText(text); //'-'+~~damage | |
| var tween = game.add.tween(a).to({y:a.y-60, alpha:0}, 1000, Phaser.Easing.Linear.None); | |
| tween.onComplete.addOnce(function(){ a.kill(); },this); | |
| tween.start(); | |
| } | |
| } | |
| </script> | |
| <script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta name="description" content="Phaser player sandbox 20x20" /> | |
| <script src="//cdn.jsdelivr.net/phaser/2.2.2/phaser.min.js"><\/script> | |
| <meta charset="utf-8"> | |
| <title>Phaser player sandbox 20x20</title> | |
| </head> | |
| <body> | |
| </body> | |
| </html></script> | |
| <script id="jsbin-source-javascript" type="text/javascript"> | |
| // var SHOT_DELAY = 150, // milliseconds (10 bullets/second) | |
| // BULLET_SPEED = 300, // pixels/second | |
| // NUMBER_OF_BULLETS = 10, | |
| // lastBulletShotAt = 0; | |
| var cursors; | |
| var debug_text=''; | |
| var enemies; | |
| var game = new Phaser.Game(320, 200, Phaser.CANVAS); | |
| //------------------------------------------------------------- | |
| var Weapon = function (parent, ammunition) { | |
| this.parent = parent; | |
| this.shotDelay = 550; // milliseconds (bullets/second) | |
| this.Limit = 10; | |
| this.lastShotAt = 0; | |
| this.ammunitions = parent.game.add.group(); | |
| this.ammunition = ammunition; | |
| }; | |
| Weapon.prototype.shot = function() { | |
| if (this.ammunitions.total >= this.Limit) return; | |
| if (game.time.now - this.lastShotAt < this.shotDelay) return; | |
| this.lastShotAt = game.time.now; | |
| if (this.parent.face == -1) { | |
| new this.ammunition(game, this.parent.body.x-5, this.parent.body.y+8, -1, this.ammunitions); | |
| } else { | |
| new this.ammunition(game, this.parent.body.x+19, this.parent.body.y+8, 1, this.ammunitions); | |
| } | |
| }; | |
| Weapon.prototype.machinegun = function() { | |
| this.shotDelay = 300; | |
| return this; | |
| }; | |
| Weapon.prototype.rocketlauncher = function() { | |
| return this; | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Hero = function (game,x,y) { | |
| this.game = game; | |
| this.face = 1; | |
| this.jumps = 0; | |
| this.jumping = false; | |
| this.onLadder = null; | |
| this.locked = false; | |
| this.lockedTo = null; | |
| this.standing = false; | |
| this.weapons = {rocketlauncher: new Weapon(this, Missile).rocketlauncher(), | |
| machinegun: new Weapon(this, Bullet).machinegun()}; | |
| this.availableWeapons = ['machinegun', 'rocketlauncher']; | |
| this.currentWeapon = this.weapons.rocketlauncher; | |
| this.currentWeaponIndex = 0; | |
| Phaser.Sprite.call(this, game, x, y, 'hero'); | |
| this.animations.add('right', [0, 1, 2, 3], 10, true); | |
| this.animations.add('left', [5, 6, 7, 8], 10, true); | |
| // this.animations.add('turn', [4], 20, true); | |
| this.anchor.setTo(0.5); | |
| game.physics.enable(this, Phaser.Physics.ARCADE); | |
| this.body.collideWorldBounds = true; | |
| this.body.bounce.set(0.1); | |
| // this.body.allowGravity = true; | |
| this.dust = 0; | |
| // dust | |
| bmd = game.add.bitmapData(8, 8); | |
| bmd.context.fillStyle = '#999999'; | |
| bmd.context.fillRect(0, 0, 8, 8); | |
| this.emitter_run = game.add.emitter(0, 0, 10); | |
| this.emitter_run.makeParticles(bmd, 0, 20, true, true); | |
| this.emitter_run.setXSpeed(0,0); | |
| this.emitter_run.setYSpeed(0,0); | |
| this.emitter_run.setAlpha(1, 0, 1000, Phaser.Easing.Linear.InOut); | |
| this.emitter_run.setScale(0, 1, 0, 1, 1000); | |
| this.emitter_run.gravity = -900; | |
| // more dust | |
| bmd = game.add.bitmapData(10, 10); | |
| bmd.context.fillStyle = '#999999'; | |
| bmd.context.fillRect(0, 0, 10, 10); | |
| this.emitter_jump = game.add.emitter(0, 0, 10); | |
| this.emitter_jump.makeParticles(bmd, 0, 20, true, true); | |
| this.emitter_jump.setXSpeed(-20,20); | |
| this.emitter_jump.setYSpeed(0,0); | |
| this.emitter_jump.setAlpha(1, 0, 1000, Phaser.Easing.Linear.InOut); | |
| this.emitter_jump.gravity = -900; | |
| game.add.existing(this); | |
| }; | |
| Hero.prototype = Object.create(Phaser.Sprite.prototype); | |
| Hero.prototype.constructor = Hero; | |
| Hero.prototype.updates = function () { | |
| this.standing = this.body.blocked.down || this.body.touching.down || this.locked; | |
| this.body.velocity.x = 0; | |
| debug_text = this.locked; | |
| if (this.onLadder){ | |
| this.body.allowGravity = false; | |
| if (cursors.left.isDown) { | |
| this.body.velocity.x = -150; | |
| this.face = -1; | |
| this.animations.play('left'); | |
| } else if (cursors.right.isDown) { | |
| this.body.velocity.x = 150; | |
| this.face = 1; | |
| this.animations.play('right'); | |
| } | |
| if (cursors.up.isDown) { | |
| this.body.velocity.y = -150; | |
| } else if (cursors.down.isDown) { | |
| this.body.velocity.y = 150; | |
| } else { | |
| this.animations.stop(); | |
| } | |
| } else { | |
| this.body.allowGravity = true; | |
| if (this.standing) { | |
| this.jumps = 2; | |
| this.jumping = false; | |
| } | |
| if (this.jumps > 0 && upInputIsActive(5)) { | |
| this.body.velocity.y = -300; | |
| this.jumping = true; | |
| this.emitter_jump.x = this.x; | |
| this.emitter_jump.y = this.y; | |
| this.emitter_jump.start(true, 1000, null, 10); | |
| this.unlock(); | |
| } | |
| if (this.jumping && upInputReleased()) { | |
| this.jumps--; | |
| this.jumping = false; | |
| } | |
| if (this.locked) | |
| { | |
| this.checkLock(); | |
| } | |
| if (cursors.left.isDown) { | |
| this.body.velocity.x = -150; | |
| this.face = -1; | |
| this.animations.play('left'); | |
| } else if (cursors.right.isDown) { | |
| this.body.velocity.x = 150; | |
| this.face = 1; | |
| this.animations.play('right'); | |
| } else { | |
| this.animations.stop(); | |
| } | |
| if (this.standing && this.deltaX!==0 && game.time.now - this.dust > 200) { | |
| this.dust = game.time.now; | |
| this.emitter_run.x = this.x; | |
| this.emitter_run.y = this.y+8; | |
| this.emitter_run.start(true, 500, null, 1); | |
| } | |
| } | |
| this.onLadder = null; | |
| if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR)) { | |
| this.currentWeapon.shot(); | |
| } | |
| }; | |
| Hero.prototype.checkLock = function () { | |
| this.body.velocity.y = 0; | |
| if (this.body.right < this.lockedTo.body.x || this.body.x > this.lockedTo.body.right) { | |
| this.unlock(); | |
| } | |
| }; | |
| Hero.prototype.unlock = function () { | |
| this.locked = false; | |
| this.lockedTo = false; | |
| }; | |
| Hero.prototype.switchWeapon = function () { | |
| this.currentWeaponIndex += 1; | |
| if (this.currentWeaponIndex >= this.availableWeapons.length) | |
| this.currentWeaponIndex = 0; | |
| console.log(this.currentWeaponIndex); | |
| this.currentWeapon = this.weapons[this.availableWeapons[this.currentWeaponIndex]]; | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Ladder = function(game, x, y, height, group) { | |
| this.game = game; | |
| if (typeof group === 'undefined') { group = game.world; } | |
| Phaser.TileSprite.call(this, game, x, y, 16, height, 'bricks', 120); | |
| this.game.physics.arcade.enableBody(this); | |
| // this.autoScroll(0,-10); | |
| this.anchor.setTo(0, 0); | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| game.physics.arcade.enableBody(this); | |
| this.body.allowGravity = false; | |
| this.body.immovable = true; | |
| this.game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Ladder.prototype = Object.create(Phaser.TileSprite.prototype); | |
| Ladder.prototype.constructor = Ladder; | |
| // ------------------------------------------------------------------ | |
| var Coin = function(game, x, y, group) { | |
| this.game = game; | |
| if (typeof group === 'undefined') { group = game.world; } | |
| Phaser.TileSprite.call(this, game, x, y, 16, 16, 'bricks', 121); | |
| this.game.physics.arcade.enableBody(this); | |
| this.anchor.setTo(0, 0); | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| this.game.physics.arcade.enableBody(this); | |
| this.body.allowGravity = false; | |
| this.body.immovable = true; | |
| this.game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Coin.prototype = Object.create(Phaser.TileSprite.prototype); | |
| Coin.prototype.constructor = Coin; | |
| // ------------------------------------------------------------------ | |
| // Missile constructor | |
| var Missile = function(game, x, y, direction, group) { | |
| this.SPEED = 150; // missile speed pixels/second | |
| this.direction = direction; | |
| this.game = game; | |
| var bmd = game.add.bitmapData(10, 4); | |
| bmd.context.fillStyle = '#FFDDEE'; | |
| bmd.context.fillRect(0, 0, 10, 4); | |
| Phaser.Sprite.call(this, game, x, y, bmd); | |
| // Set the pivot point for this sprite to the center | |
| this.anchor.setTo(0.5, 0.5); | |
| // Enable physics on the missile | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| game.physics.enable(this); | |
| game.physics.arcade.enableBody(this); | |
| this.checkWorldBounds = true; | |
| this.outOfBoundsKill = true; | |
| // Define constants that affect motion | |
| this.SMOKE_LIFETIME = 2000; // milliseconds | |
| this.distance = 0; | |
| this.ddist = game.rnd.normal()+1; | |
| this.trajectory = game.rnd.integerInRange(50, 80); | |
| this.amp = 1; | |
| bmd = game.add.bitmapData(8, 8); | |
| bmd.context.fillStyle = '#666666'; | |
| bmd.context.fillRect(0, 0, 8, 8); | |
| this.smokeEmitter = this.game.add.emitter(0, 0, 100); | |
| // this.smokeEmitter.gravity = -800; | |
| this.smokeEmitter.setXSpeed(0, 0); | |
| this.smokeEmitter.setYSpeed(-50, -80); // make smoke drift upwards | |
| this.smokeEmitter.setAlpha(1, 0, this.SMOKE_LIFETIME, Phaser.Easing.Linear.InOut); | |
| this.smokeEmitter.setScale(1, 2, 1, 2, this.SMOKE_LIFETIME); | |
| this.smokeEmitter.makeParticles(bmd); | |
| this.smokeEmitter.start(false, this.SMOKE_LIFETIME, 50); | |
| this.smokeEmitter.gravity = -800; | |
| this.game.add.existing(this); | |
| group.add(this); | |
| }; | |
| // Missiles are a type of Phaser.Sprite | |
| Missile.prototype = Object.create(Phaser.Sprite.prototype); | |
| Missile.prototype.constructor = Missile; | |
| Missile.prototype.update = function() { | |
| // Position the smoke emitter at the center of the missile | |
| this.smokeEmitter.x = this.x; | |
| this.smokeEmitter.y = this.y; | |
| // Calculate velocity vector based on this.rotation and this.SPEED | |
| this.body.velocity.x = this.direction * this.SPEED; | |
| this.body.velocity.y = this.trajectory * this.amp * Math.sin(-this.distance/30); | |
| this.rotation = Math.asin(this.direction * this.body.velocity.y / this.SPEED); | |
| this.distance += this.ddist; | |
| this.amp = this.amp / 1.007; | |
| if (!this.alive) { | |
| this.smokeEmitter.on = false; | |
| this.destroy(); | |
| } | |
| }; | |
| Missile.prototype.explode = function() { | |
| // Create BitmapData | |
| var bmd = game.add.bitmapData(100,100); | |
| // Draw circle | |
| bmd.ctx.fillStyle = '#ff9900'; | |
| bmd.ctx.beginPath(); | |
| bmd.ctx.arc(50, 50, 10, 0, Math.PI*2, true); | |
| bmd.ctx.closePath(); | |
| bmd.ctx.fill(); | |
| this.explodeEmitter = this.game.add.emitter(0, 0, 100); | |
| this.explodeEmitter.setXSpeed(-100, 100); | |
| this.explodeEmitter.setYSpeed(-100, 100); | |
| this.explodeEmitter.setAlpha(0.5, 0, 500); | |
| this.explodeEmitter.setScale(0, 4,0,4,200); | |
| this.explodeEmitter.blendMode = 1; | |
| this.explodeEmitter.x = this.x; | |
| this.explodeEmitter.y = this.y; | |
| this.explodeEmitter.makeParticles(bmd, 0, 50, false, false); | |
| this.explodeEmitter.start(true, 200, null, 10); | |
| this.kill(); | |
| }; | |
| Missile.prototype.bang = function() { | |
| // Draw circle | |
| var bmd = game.add.bitmapData(100,100); | |
| bmd.ctx.fillStyle = '#ff9900'; | |
| bmd.ctx.beginPath(); | |
| bmd.ctx.arc(50, 50, 10, 0, Math.PI*2, true); | |
| bmd.ctx.closePath(); | |
| bmd.ctx.fill(); | |
| var boom = this.game.add.sprite(this.x,this.y,bmd); | |
| boom.anchor.setTo(0.5, 0.5); | |
| boom.scale.x = 0; | |
| boom.scale.y = 0; | |
| var tween = this.game.add.tween(boom.scale).to({x: 5, y:5 }, 100, Phaser.Easing.Linear.None); | |
| tween.onComplete.addOnce(function(){boom.kill();}); | |
| tween.start(); | |
| this.kill(); | |
| this.game.enemies.forEach(function(subgroup){ | |
| subgroup.forEach(function(object){ | |
| distance = this.game.physics.arcade.distanceBetween(boom,object); | |
| radius = 80; | |
| mass = 0.05; | |
| if (distance < radius) { | |
| damage = (radius - distance)/10; | |
| // popUp(this.popUps, object, '-'+~~damage); | |
| angle = this.game.physics.arcade.angleBetween(boom, object); | |
| object.body.velocity.x += Math.cos(angle) * ((radius - distance) / mass); | |
| object.body.velocity.y += Math.sin(angle) * ((radius - distance) / mass); | |
| object.health -= damage/100; | |
| // object.healthStatus.setText(~~(object.health*100)) | |
| // if (object.health < 0.5) { | |
| // object.healthStatus.addColor('#900',0); | |
| // } | |
| } | |
| }); | |
| }); | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Bullet = function (game, x, y, direction, group) { | |
| this.SPEED = 500; | |
| this.direction = direction; | |
| var bmd = game.add.bitmapData(20, 1); | |
| bmd.context.fillStyle = '#FFFF00'; | |
| bmd.context.fillRect(0, 0, 20, 1); | |
| Phaser.Sprite.call(this, game, x, y, bmd); | |
| this.anchor.set(0.5,0.5); | |
| game.physics.enable(this); | |
| game.physics.arcade.enableBody(this); | |
| this.body.allowGravity = false; | |
| this.checkWorldBounds = true; | |
| this.outOfBoundsKill = true; | |
| this.body.velocity.x = this.direction * this.SPEED; | |
| game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Bullet.prototype = Object.create(Phaser.Sprite.prototype); | |
| Bullet.prototype.constructor = Bullet; | |
| // ------------------------------------------------------------------ | |
| var Bat = function(game,x,y, group) { | |
| if (typeof group === 'undefined') { group = game.world; } | |
| this.game = game; | |
| Phaser.Sprite.call(this, game, x, y, 'bat'); | |
| this.anchor.setTo(0.5, 0.5); | |
| this.anchor.set(0.5); | |
| this.animations.add('fly', [0,1,2,1 ], 20, true); | |
| this.play('fly'); | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| this.game.physics.arcade.enableBody(this); | |
| this.checkWorldBounds = true; | |
| this.outOfBoundsKill = true; | |
| this.body.collideWorldBounds = true; | |
| this.body.bounce.set(0.1); | |
| this.body.allowGravity = false; | |
| this.changeDirection = 0; | |
| this.changeDirectionDelay = this.game.rnd.between(1000,2000); | |
| this.game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Bat.prototype = Object.create(Phaser.Sprite.prototype); | |
| Bat.prototype.constructor = Bat; | |
| Bat.prototype.update = function() { | |
| if (this.game.time.now - this.changeDirection > this.changeDirectionDelay ) { | |
| this.body.velocity.x = this.game.rnd.normal()*50; | |
| this.body.velocity.y = this.game.rnd.normal()*50; | |
| this.changeDirection = this.game.time.now; | |
| } | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Ball = function(game,x,y,group) { | |
| if (typeof group === 'undefined') { group = game.world; } | |
| this.game = game; | |
| var bmd = game.add.bitmapData(20,20); | |
| bmd.ctx.fillStyle = '#999999'; | |
| bmd.ctx.beginPath(); | |
| bmd.ctx.arc(10, 10, 10, 0, Math.PI*2, true); | |
| bmd.ctx.closePath(); | |
| bmd.ctx.fill(); | |
| Phaser.Sprite.call(this, game, x, y, bmd); | |
| this.game.physics.arcade.enable(this, Phaser.Physics.ARCADE); | |
| this.body.collideWorldBounds = true; | |
| this.body.bounce.set(0.1); | |
| this.anchor.setTo(0.5, 0.5); | |
| this.healthStatus = game.add.text(0, -10, '100', {font: "9px Arial", fill: "#888888" }); | |
| this.addChild(this.healthStatus); | |
| game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Ball.prototype = Object.create(Phaser.Sprite.prototype); | |
| Ball.prototype.constructor = Ball; | |
| Ball.prototype.update = function() { | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Ghost = function(game,x,y,group) { | |
| if (typeof group === 'undefined') { group = game.world; } | |
| this.game = game; | |
| Phaser.Sprite.call(this, game, 200, 140, 'enemy'); | |
| this.anchor.setTo(0.5, 0.5); | |
| var randomScale = 0.8 + Math.random(); | |
| this.scale.setTo(randomScale, randomScale); | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| this.game.physics.arcade.enableBody(this); | |
| this.body.allowGravity = true; | |
| this.checkWorldBounds = true; | |
| this.outOfBoundsKill = true; | |
| this.body.velocity.x = 0; | |
| this.body.velocity.y = 0; | |
| this.dx = 10; | |
| game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Ghost.prototype = Object.create(Phaser.Sprite.prototype); | |
| Ghost.prototype.constructor = Ghost; | |
| Ghost.prototype.update = function() { | |
| if (this.body.velocity.x === 0) this.dx = -this.dx; | |
| // this.body.velocity.x = this.dx; | |
| }; | |
| // ------------------------------------------------------------------ | |
| CloudPlatform = function (game, x, y, key, group) { | |
| if (typeof group === 'undefined') { group = game.world; } | |
| Phaser.Sprite.call(this, game, x, y, key); | |
| game.physics.arcade.enable(this); | |
| this.anchor.x = 0.5; | |
| this.body.customSeparateX = true; | |
| this.body.customSeparateY = true; | |
| this.body.allowGravity = false; | |
| this.body.immovable = true; | |
| this.playerLocked = false; | |
| group.add(this); | |
| // game.add.existing(this); | |
| }; | |
| CloudPlatform.prototype = Object.create(Phaser.Sprite.prototype); | |
| CloudPlatform.prototype.constructor = CloudPlatform; | |
| CloudPlatform.prototype.addMotionPath = function (motionPath) { | |
| this.tweenX = this.game.add.tween(this.body); | |
| this.tweenY = this.game.add.tween(this.body); | |
| for (var i = 0; i < motionPath.length; i++) { | |
| this.tweenX.to( { x: motionPath[i].x }, motionPath[i].xSpeed, motionPath[i].xEase); | |
| this.tweenY.to( { y: motionPath[i].y }, motionPath[i].ySpeed, motionPath[i].yEase); | |
| } | |
| this.tweenX.loop(); | |
| this.tweenY.loop(); | |
| }; | |
| CloudPlatform.prototype.start = function () { | |
| this.tweenX.start(); | |
| this.tweenY.start(); | |
| }; | |
| CloudPlatform.prototype.stop = function () { | |
| this.tweenX.stop(); | |
| this.tweenY.stop(); | |
| }; | |
| // ------------------------------------------------------------------ | |
| //**************************************************************************************** | |
| //**************************************************************************************** | |
| //**************************************************************************************** | |
| //**************************************************************************************** | |
| var Game = function (game) { | |
| this.map = null; | |
| this.coins = null; | |
| this.layer = null; | |
| this.enemies = null; | |
| this.bats = null; | |
| this.ghost = null; | |
| this.player = null; | |
| this.clouds = null; | |
| this.ladders = null; | |
| this.bloodsplashEmitter = null; | |
| this.fragsEmitter = null; | |
| this.bulletSparksEmitter = null; | |
| }; | |
| Game.prototype = { | |
| preload: function () { | |
| game.stage.backgroundColor = 0x666666; | |
| game.time.advancedTiming = true; | |
| game.load.baseURL = 'http://9436.assets.s3.amazonaws.com/'; | |
| game.load.crossOrigin = ''; | |
| game.load.image('background', 'background.png'); | |
| game.load.image('enemy', 'player2.png'); | |
| game.load.image('platform', 'platform.png'); | |
| game.load.image('tiles', 'bricks2.png?'); | |
| game.load.tilemap('sandbox', 'sandbox20x20.json?1', null, Phaser.Tilemap.TILED_JSON); | |
| game.load.spritesheet('hero', 'hero11.png', 16, 16); | |
| this.load.spritesheet('bat', 'flying8.png', 8,8); | |
| game.load.spritesheet('bricks', 'bricks2.png?', 16, 16); | |
| }, | |
| create: function () { | |
| game.objects = {}; | |
| game.physics.startSystem(Phaser.Physics.ARCADE); | |
| game.physics.arcade.gravity.y = 800; | |
| // this.background = game.add.sprite(0, 0, 'background'); | |
| // this.background.fixedToCamera = true; | |
| this.map = game.add.tilemap('sandbox'); | |
| this.map.addTilesetImage('bricks', 'tiles'); | |
| this.layer = this.map.createLayer('walls'); | |
| this.layer.resizeWorld(); | |
| // this.layer.debug = true; | |
| this.map.setCollisionBetween(1,2000); | |
| // this.map.setTileIndexCallback([1], hit, this); | |
| this.popUps = initPopUps(); | |
| game.renderer.renderSession.roundPixels = false; | |
| this.clouds = this.add.physicsGroup(); | |
| new CloudPlatform(this.game, 80, 96, 'platform', this.clouds).addMotionPath([ | |
| { x: "+0", xSpeed: 1000, xEase: "Linear", | |
| y: "+70", ySpeed: 1000, yEase: "Sine.easeIn" }, | |
| { x: "-0", xSpeed: 1000, xEase: "Linear", | |
| y: "-70", ySpeed: 1000, yEase: "Sine.easeOut" } | |
| ]); | |
| this.clouds.callAll('start'); | |
| this.ladders = this.add.physicsGroup(); | |
| this.player = new Hero(this.game,30,80); | |
| game.camera.follow(this.player); | |
| for (var i=0, ic=this.map.objects.ladders.length; i<ic; i++) { | |
| var li = this.map.objects.ladders[i]; | |
| new Ladder(game, li.x, li.y, li.heihgt, this.ladders); | |
| } | |
| this.coins = this.add.group(); | |
| for (var i=0, ic=this.map.objects.coins.length; i<ic; i++) { | |
| var li = this.map.objects.coins[i]; | |
| new Coin(game, li.x, li.y, this.coins); | |
| } | |
| this.bats = this.add.physicsGroup(); | |
| for(var i=0;i<10;i++) | |
| new Bat(this.game, game.rnd.between(30,300), game.rnd.between(30,300), this.bats); | |
| this.ghost = this.add.group(); | |
| for(var i=0;i<1;i++) | |
| new Ghost(this.game, 1,1, this.ghost); | |
| this.balls = game.add.group(); | |
| for(var i=0;i<10;i++) { | |
| new Ball(this.game, game.rnd.between(30,300), game.rnd.between(30,300), this.balls); | |
| } | |
| // game.objects.balls = this.balls; | |
| game.enemies = this.add.group(); | |
| game.enemies.add(this.ghost); | |
| game.enemies.add(this.bats); | |
| game.enemies.add(this.balls); | |
| var bmd; | |
| bmd = game.add.bitmapData(2, 2); | |
| bmd.context.fillStyle = '#0099FF'; | |
| bmd.context.fillRect(0, 0, 2, 2); | |
| this.bulletSparksEmitter = game.add.emitter(0, 0, 100); | |
| this.bulletSparksEmitter.makeParticles(bmd, 0, 200, true, true); | |
| this.bulletSparksEmitter.setXSpeed(-300,300); | |
| this.bulletSparksEmitter.setYSpeed(-300,300); | |
| this.bulletSparksEmitter.bounce.setTo(0.5, 0.5); | |
| this.bulletSparksEmitter.gravity = 50; | |
| bmd = game.add.bitmapData(2, 2); | |
| bmd.context.fillStyle = '#FF0000'; | |
| bmd.context.fillRect(0, 0, 2, 2); | |
| this.bloodsplashEmitter = game.add.emitter(0, 0, 100); | |
| this.bloodsplashEmitter.makeParticles(bmd, 0, 200, true, true); | |
| this.bloodsplashEmitter.setYSpeed(-150,150); | |
| this.bloodsplashEmitter.setXSpeed(-150,150); | |
| this.bloodsplashEmitter.setRotation(0,0); | |
| this.bloodsplashEmitter.setAlpha(1, 0, 5000); | |
| this.bloodsplashEmitter.bounce.setTo(0,0); | |
| this.bloodsplashEmitter.gravity = 150; | |
| cursors = game.input.keyboard.createCursorKeys(); | |
| bmd = game.add.bitmapData(3, 3); | |
| bmd.context.fillStyle = '#FF0000'; | |
| bmd.context.fillRect(0, 0, 3, 3); | |
| this.fragsEmitter = game.add.emitter(0, 0, 100); | |
| this.fragsEmitter.makeParticles(bmd, 0, 200, true, true); | |
| this.fragsEmitter.setXSpeed(-300,300); | |
| this.fragsEmitter.setYSpeed(-300,300); | |
| this.fragsEmitter.setRotation(0,0); | |
| this.fragsEmitter.particleDrag.x = 100; | |
| this.fragsEmitter.setAlpha(1, 0, 50000); | |
| this.fragsEmitter.bounce.setTo(0.5,0.2); | |
| this.fragsEmitter.gravity = 150; | |
| }, | |
| preRender: function () { | |
| if (this.game.paused){ | |
| debub_text = 'paused'; | |
| // Because preRender still runs even if your game pauses! | |
| return; | |
| } | |
| if (this.player) { | |
| if (this.player.locked && this.player.lockedTo) { | |
| this.player.x += this.player.lockedTo.deltaX; | |
| this.player.y = this.player.lockedTo.y-10; | |
| if (this.player.body.velocity.x !== 0) { | |
| this.player.body.velocity.y = 0; | |
| } | |
| } | |
| } | |
| }, | |
| update: function () { | |
| game.physics.arcade.collide(this.player, this.layer, playerLayerCollide, null, this); | |
| game.physics.arcade.collide(this.player.weapons.machinegun.ammunitions, this.layer, bulletSparks, null, this); | |
| game.physics.arcade.collide(this.player.weapons.rocketlauncher.ammunitions, this.layer, missileBang, null, this); | |
| game.physics.arcade.collide(this.player.weapons.machinegun.ammunitions, this.bats, bulletBatCollide, null, this ); | |
| game.physics.arcade.collide(this.player.weapons.machinegun.ammunitions, this.ghost, bulletBatCollide, null, this ); | |
| game.physics.arcade.collide(this.player, this.clouds, customSep, null, this); | |
| game.physics.arcade.overlap(this.player, this.ladders, playerLadderCollide, null, this); | |
| game.physics.arcade.overlap(this.player, this.coins, playerCoinCollide, null, this); | |
| game.physics.arcade.collide(this.bloodsplashEmitter, this.layer, bloodsplashLayerCollide, null, this); | |
| game.physics.arcade.collide(this.fragsEmitter, this.layer); | |
| game.physics.arcade.collide(this.ghost, this.layer ); | |
| game.physics.arcade.collide(this.bats, this.layer); | |
| game.physics.arcade.collide(this.balls, this.layer); | |
| game.physics.arcade.overlap(this.player, this.bats, playerBatCollide, null, this); | |
| if (game.input.keyboard.downDuration(Phaser.Keyboard.Q,1)){ | |
| this.player.switchWeapon(); | |
| } | |
| game.input.onDown.add(bang, this); | |
| this.player.updates(); | |
| debug_text = 'Health: ' + this.player.health*100; | |
| }, | |
| render: function () { | |
| game.debug.text('FPS:'+game.time.fps ,16,16); | |
| game.debug.text(debug_text,86,16); | |
| } | |
| }; | |
| game.state.add('Game', Game, true); | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| function bang(pointer) { | |
| var boom = game.add.graphics(game.world.centerX, game.world.centerY); | |
| boom.lineStyle(1, 0xffffff); | |
| boom.drawCircle(0, 0, 80); | |
| boom.x = pointer.x; | |
| boom.y = pointer.y; | |
| boom.scale.x = 0; | |
| boom.scale.y = 0; | |
| var tween = game.add.tween(boom.scale).to({x: 1.5, y:1.5}, 100, Phaser.Easing.Linear.None); | |
| tween.onComplete.addOnce(function(){boom.scale.x =0; }); | |
| tween.start(); | |
| game.enemies.forEach(function(e) { | |
| e.forEach(function(object){ | |
| distance = game.physics.arcade.distanceBetween(boom,object); | |
| radius = 80; | |
| mass = 0.1; | |
| if (distance < radius) { | |
| damage = (radius - distance)/10; | |
| // popUp(popUpAlerts, object, '-'+~~damage); | |
| angle = game.physics.arcade.angleBetween(boom, object); | |
| object.body.velocity.x += Math.cos(angle) * ((radius - distance) / mass); | |
| object.body.velocity.y += Math.sin(angle) * ((radius - distance) / mass); | |
| // object.health -= damage/100; | |
| // object.healthStatus.setText(~~(object.health*100)) | |
| // if (object.health < 0.5) { | |
| // object.healthStatus.addColor('#900',0); | |
| // } | |
| } | |
| }); | |
| }); | |
| } | |
| function customSep(player, platform) { | |
| if (!player.locked && player.body.velocity.y > 0) { | |
| player.locked = true; | |
| player.lockedTo = platform; | |
| platform.playerLocked = true; | |
| player.body.velocity.y = 0; | |
| } | |
| } | |
| function upInputIsActive(duration) { | |
| return game.input.keyboard.downDuration(Phaser.Keyboard.UP, duration); | |
| } | |
| function upInputReleased() { | |
| return game.input.keyboard.upDuration(Phaser.Keyboard.UP); | |
| } | |
| function hit(sprite,tile) { | |
| // if (tile.alpha > 0.3) { | |
| // tile.alpha -= 0.01; | |
| // layer.dirty = true; | |
| // } | |
| return true; | |
| } | |
| function enemyTurn(enemy, layer) { | |
| if(enemy.body.blocked.right){ | |
| enemy.body.velocity.x = -speed; | |
| } else if (enemy.body.blocked.left) { | |
| enemy.body.velocity.x = speed; | |
| } | |
| } | |
| function playerCoinCollide(player, coin) { | |
| coin.destroy(); | |
| } | |
| function playerBatCollide(player, bat) { | |
| bat.kill(); | |
| player.damage(0.1); | |
| } | |
| function playerLadderCollide(player, ladder) { | |
| player.onLadder = ladder; | |
| player.body.velocity.y = 0; | |
| } | |
| function bulletBatCollide(bullet,bat) { | |
| this.bloodsplashEmitter.x = bat.x; | |
| this.bloodsplashEmitter.y = bat.y; | |
| this.bloodsplashEmitter.start(true, 5000, null, 10); | |
| var damage = 1; | |
| popUp(this.popUps, bat, '-'+damage); | |
| bat.damage(damage/10); | |
| if (bat.health<=0) { | |
| this.fragsEmitter.x = bat.x; | |
| this.fragsEmitter.y = bat.y; | |
| this.fragsEmitter.start(true, 0, null, 10); | |
| } | |
| bullet.kill(); | |
| } | |
| function bloodsplashLayerCollide(a,b) { | |
| a.body.velocity.x=0; | |
| a.body.velocity.y=0; | |
| // a.body.allowGravity = false; | |
| } | |
| function playerLayerCollide(player, tile) { | |
| // if (tile.index > 280) { | |
| // if (tile.alpha < 0.2) { | |
| // map.removeTile(tile.x,tile.y); | |
| // } else { | |
| // tile.alpha -= 0.1; | |
| // layer.dirty = true; | |
| // } | |
| // debug_text = tile.alpha; | |
| // } | |
| } | |
| function missileBang(a, b) { | |
| // a.explode(); | |
| a.bang(); | |
| } | |
| function bulletSparks(bullet, tile) { | |
| this.bulletSparksEmitter.y = tile.worldY+8; | |
| if (bullet.direction>0) { | |
| this.bulletSparksEmitter.x = tile.worldX; | |
| } else { | |
| this.bulletSparksEmitter.x = tile.worldX+16; | |
| } | |
| this.bulletSparksEmitter.start(true, 50, null, 5); | |
| bullet.kill(); | |
| } | |
| function initPopUps(){ | |
| var popUp, popUps = game.add.group(); | |
| for(var i=0;i<10;i++) { | |
| popUp = game.add.sprite(0,0); | |
| popUp.text = game.add.text(0, -10, '', {font: "9px Arial", fill: "#fff" }); | |
| popUp.addChild(popUp.text); | |
| popUps.add(popUp); | |
| popUp.kill(); | |
| } | |
| return popUps; | |
| } | |
| function popUp(popUps, object, text){ | |
| var a = popUps.getFirstDead(); | |
| if (a) { | |
| a.alpha = 1; | |
| a.reset(object.x, object.y-20); | |
| a.text.setText(text); //'-'+~~damage | |
| var tween = game.add.tween(a).to({y:a.y-60, alpha:0}, 1000, Phaser.Easing.Linear.None); | |
| tween.onComplete.addOnce(function(){ a.kill(); },this); | |
| tween.start(); | |
| } | |
| } | |
| </script></body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // var SHOT_DELAY = 150, // milliseconds (10 bullets/second) | |
| // BULLET_SPEED = 300, // pixels/second | |
| // NUMBER_OF_BULLETS = 10, | |
| // lastBulletShotAt = 0; | |
| var cursors; | |
| var debug_text=''; | |
| var enemies; | |
| var game = new Phaser.Game(320, 200, Phaser.CANVAS); | |
| //------------------------------------------------------------- | |
| var Weapon = function (parent, ammunition) { | |
| this.parent = parent; | |
| this.shotDelay = 550; // milliseconds (bullets/second) | |
| this.Limit = 10; | |
| this.lastShotAt = 0; | |
| this.ammunitions = parent.game.add.group(); | |
| this.ammunition = ammunition; | |
| }; | |
| Weapon.prototype.shot = function() { | |
| if (this.ammunitions.total >= this.Limit) return; | |
| if (game.time.now - this.lastShotAt < this.shotDelay) return; | |
| this.lastShotAt = game.time.now; | |
| if (this.parent.face == -1) { | |
| new this.ammunition(game, this.parent.body.x-5, this.parent.body.y+8, -1, this.ammunitions); | |
| } else { | |
| new this.ammunition(game, this.parent.body.x+19, this.parent.body.y+8, 1, this.ammunitions); | |
| } | |
| }; | |
| Weapon.prototype.machinegun = function() { | |
| this.shotDelay = 300; | |
| return this; | |
| }; | |
| Weapon.prototype.rocketlauncher = function() { | |
| return this; | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Hero = function (game,x,y) { | |
| this.game = game; | |
| this.face = 1; | |
| this.jumps = 0; | |
| this.jumping = false; | |
| this.onLadder = null; | |
| this.locked = false; | |
| this.lockedTo = null; | |
| this.standing = false; | |
| this.weapons = {rocketlauncher: new Weapon(this, Missile).rocketlauncher(), | |
| machinegun: new Weapon(this, Bullet).machinegun()}; | |
| this.availableWeapons = ['machinegun', 'rocketlauncher']; | |
| this.currentWeapon = this.weapons.rocketlauncher; | |
| this.currentWeaponIndex = 0; | |
| Phaser.Sprite.call(this, game, x, y, 'hero'); | |
| this.animations.add('right', [0, 1, 2, 3], 10, true); | |
| this.animations.add('left', [5, 6, 7, 8], 10, true); | |
| // this.animations.add('turn', [4], 20, true); | |
| this.anchor.setTo(0.5); | |
| game.physics.enable(this, Phaser.Physics.ARCADE); | |
| this.body.collideWorldBounds = true; | |
| this.body.bounce.set(0.1); | |
| // this.body.allowGravity = true; | |
| this.dust = 0; | |
| // dust | |
| bmd = game.add.bitmapData(8, 8); | |
| bmd.context.fillStyle = '#999999'; | |
| bmd.context.fillRect(0, 0, 8, 8); | |
| this.emitter_run = game.add.emitter(0, 0, 10); | |
| this.emitter_run.makeParticles(bmd, 0, 20, true, true); | |
| this.emitter_run.setXSpeed(0,0); | |
| this.emitter_run.setYSpeed(0,0); | |
| this.emitter_run.setAlpha(1, 0, 1000, Phaser.Easing.Linear.InOut); | |
| this.emitter_run.setScale(0, 1, 0, 1, 1000); | |
| this.emitter_run.gravity = -900; | |
| // more dust | |
| bmd = game.add.bitmapData(10, 10); | |
| bmd.context.fillStyle = '#999999'; | |
| bmd.context.fillRect(0, 0, 10, 10); | |
| this.emitter_jump = game.add.emitter(0, 0, 10); | |
| this.emitter_jump.makeParticles(bmd, 0, 20, true, true); | |
| this.emitter_jump.setXSpeed(-20,20); | |
| this.emitter_jump.setYSpeed(0,0); | |
| this.emitter_jump.setAlpha(1, 0, 1000, Phaser.Easing.Linear.InOut); | |
| this.emitter_jump.gravity = -900; | |
| game.add.existing(this); | |
| }; | |
| Hero.prototype = Object.create(Phaser.Sprite.prototype); | |
| Hero.prototype.constructor = Hero; | |
| Hero.prototype.updates = function () { | |
| this.standing = this.body.blocked.down || this.body.touching.down || this.locked; | |
| this.body.velocity.x = 0; | |
| debug_text = this.locked; | |
| if (this.onLadder){ | |
| this.body.allowGravity = false; | |
| if (cursors.left.isDown) { | |
| this.body.velocity.x = -150; | |
| this.face = -1; | |
| this.animations.play('left'); | |
| } else if (cursors.right.isDown) { | |
| this.body.velocity.x = 150; | |
| this.face = 1; | |
| this.animations.play('right'); | |
| } | |
| if (cursors.up.isDown) { | |
| this.body.velocity.y = -150; | |
| } else if (cursors.down.isDown) { | |
| this.body.velocity.y = 150; | |
| } else { | |
| this.animations.stop(); | |
| } | |
| } else { | |
| this.body.allowGravity = true; | |
| if (this.standing) { | |
| this.jumps = 2; | |
| this.jumping = false; | |
| } | |
| if (this.jumps > 0 && upInputIsActive(5)) { | |
| this.body.velocity.y = -300; | |
| this.jumping = true; | |
| this.emitter_jump.x = this.x; | |
| this.emitter_jump.y = this.y; | |
| this.emitter_jump.start(true, 1000, null, 10); | |
| this.unlock(); | |
| } | |
| if (this.jumping && upInputReleased()) { | |
| this.jumps--; | |
| this.jumping = false; | |
| } | |
| if (this.locked) | |
| { | |
| this.checkLock(); | |
| } | |
| if (cursors.left.isDown) { | |
| this.body.velocity.x = -150; | |
| this.face = -1; | |
| this.animations.play('left'); | |
| } else if (cursors.right.isDown) { | |
| this.body.velocity.x = 150; | |
| this.face = 1; | |
| this.animations.play('right'); | |
| } else { | |
| this.animations.stop(); | |
| } | |
| if (this.standing && this.deltaX!==0 && game.time.now - this.dust > 200) { | |
| this.dust = game.time.now; | |
| this.emitter_run.x = this.x; | |
| this.emitter_run.y = this.y+8; | |
| this.emitter_run.start(true, 500, null, 1); | |
| } | |
| } | |
| this.onLadder = null; | |
| if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR)) { | |
| this.currentWeapon.shot(); | |
| } | |
| }; | |
| Hero.prototype.checkLock = function () { | |
| this.body.velocity.y = 0; | |
| if (this.body.right < this.lockedTo.body.x || this.body.x > this.lockedTo.body.right) { | |
| this.unlock(); | |
| } | |
| }; | |
| Hero.prototype.unlock = function () { | |
| this.locked = false; | |
| this.lockedTo = false; | |
| }; | |
| Hero.prototype.switchWeapon = function () { | |
| this.currentWeaponIndex += 1; | |
| if (this.currentWeaponIndex >= this.availableWeapons.length) | |
| this.currentWeaponIndex = 0; | |
| console.log(this.currentWeaponIndex); | |
| this.currentWeapon = this.weapons[this.availableWeapons[this.currentWeaponIndex]]; | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Ladder = function(game, x, y, height, group) { | |
| this.game = game; | |
| if (typeof group === 'undefined') { group = game.world; } | |
| Phaser.TileSprite.call(this, game, x, y, 16, height, 'bricks', 120); | |
| this.game.physics.arcade.enableBody(this); | |
| // this.autoScroll(0,-10); | |
| this.anchor.setTo(0, 0); | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| game.physics.arcade.enableBody(this); | |
| this.body.allowGravity = false; | |
| this.body.immovable = true; | |
| this.game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Ladder.prototype = Object.create(Phaser.TileSprite.prototype); | |
| Ladder.prototype.constructor = Ladder; | |
| // ------------------------------------------------------------------ | |
| var Coin = function(game, x, y, group) { | |
| this.game = game; | |
| if (typeof group === 'undefined') { group = game.world; } | |
| Phaser.TileSprite.call(this, game, x, y, 16, 16, 'bricks', 121); | |
| this.game.physics.arcade.enableBody(this); | |
| this.anchor.setTo(0, 0); | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| this.game.physics.arcade.enableBody(this); | |
| this.body.allowGravity = false; | |
| this.body.immovable = true; | |
| this.game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Coin.prototype = Object.create(Phaser.TileSprite.prototype); | |
| Coin.prototype.constructor = Coin; | |
| // ------------------------------------------------------------------ | |
| // Missile constructor | |
| var Missile = function(game, x, y, direction, group) { | |
| this.SPEED = 150; // missile speed pixels/second | |
| this.direction = direction; | |
| this.game = game; | |
| var bmd = game.add.bitmapData(10, 4); | |
| bmd.context.fillStyle = '#FFDDEE'; | |
| bmd.context.fillRect(0, 0, 10, 4); | |
| Phaser.Sprite.call(this, game, x, y, bmd); | |
| // Set the pivot point for this sprite to the center | |
| this.anchor.setTo(0.5, 0.5); | |
| // Enable physics on the missile | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| game.physics.enable(this); | |
| game.physics.arcade.enableBody(this); | |
| this.checkWorldBounds = true; | |
| this.outOfBoundsKill = true; | |
| // Define constants that affect motion | |
| this.SMOKE_LIFETIME = 2000; // milliseconds | |
| this.distance = 0; | |
| this.ddist = game.rnd.normal()+1; | |
| this.trajectory = game.rnd.integerInRange(50, 80); | |
| this.amp = 1; | |
| bmd = game.add.bitmapData(8, 8); | |
| bmd.context.fillStyle = '#666666'; | |
| bmd.context.fillRect(0, 0, 8, 8); | |
| this.smokeEmitter = this.game.add.emitter(0, 0, 100); | |
| // this.smokeEmitter.gravity = -800; | |
| this.smokeEmitter.setXSpeed(0, 0); | |
| this.smokeEmitter.setYSpeed(-50, -80); // make smoke drift upwards | |
| this.smokeEmitter.setAlpha(1, 0, this.SMOKE_LIFETIME, Phaser.Easing.Linear.InOut); | |
| this.smokeEmitter.setScale(1, 2, 1, 2, this.SMOKE_LIFETIME); | |
| this.smokeEmitter.makeParticles(bmd); | |
| this.smokeEmitter.start(false, this.SMOKE_LIFETIME, 50); | |
| this.smokeEmitter.gravity = -800; | |
| this.game.add.existing(this); | |
| group.add(this); | |
| }; | |
| // Missiles are a type of Phaser.Sprite | |
| Missile.prototype = Object.create(Phaser.Sprite.prototype); | |
| Missile.prototype.constructor = Missile; | |
| Missile.prototype.update = function() { | |
| // Position the smoke emitter at the center of the missile | |
| this.smokeEmitter.x = this.x; | |
| this.smokeEmitter.y = this.y; | |
| // Calculate velocity vector based on this.rotation and this.SPEED | |
| this.body.velocity.x = this.direction * this.SPEED; | |
| this.body.velocity.y = this.trajectory * this.amp * Math.sin(-this.distance/30); | |
| this.rotation = Math.asin(this.direction * this.body.velocity.y / this.SPEED); | |
| this.distance += this.ddist; | |
| this.amp = this.amp / 1.007; | |
| if (!this.alive) { | |
| this.smokeEmitter.on = false; | |
| this.destroy(); | |
| } | |
| }; | |
| Missile.prototype.explode = function() { | |
| // Create BitmapData | |
| var bmd = game.add.bitmapData(100,100); | |
| // Draw circle | |
| bmd.ctx.fillStyle = '#ff9900'; | |
| bmd.ctx.beginPath(); | |
| bmd.ctx.arc(50, 50, 10, 0, Math.PI*2, true); | |
| bmd.ctx.closePath(); | |
| bmd.ctx.fill(); | |
| this.explodeEmitter = this.game.add.emitter(0, 0, 100); | |
| this.explodeEmitter.setXSpeed(-100, 100); | |
| this.explodeEmitter.setYSpeed(-100, 100); | |
| this.explodeEmitter.setAlpha(0.5, 0, 500); | |
| this.explodeEmitter.setScale(0, 4,0,4,200); | |
| this.explodeEmitter.blendMode = 1; | |
| this.explodeEmitter.x = this.x; | |
| this.explodeEmitter.y = this.y; | |
| this.explodeEmitter.makeParticles(bmd, 0, 50, false, false); | |
| this.explodeEmitter.start(true, 200, null, 10); | |
| this.kill(); | |
| }; | |
| Missile.prototype.bang = function() { | |
| // Draw circle | |
| var bmd = game.add.bitmapData(100,100); | |
| bmd.ctx.fillStyle = '#ff9900'; | |
| bmd.ctx.beginPath(); | |
| bmd.ctx.arc(50, 50, 10, 0, Math.PI*2, true); | |
| bmd.ctx.closePath(); | |
| bmd.ctx.fill(); | |
| var boom = this.game.add.sprite(this.x,this.y,bmd); | |
| boom.anchor.setTo(0.5, 0.5); | |
| boom.scale.x = 0; | |
| boom.scale.y = 0; | |
| var tween = this.game.add.tween(boom.scale).to({x: 5, y:5 }, 100, Phaser.Easing.Linear.None); | |
| tween.onComplete.addOnce(function(){boom.kill();}); | |
| tween.start(); | |
| this.kill(); | |
| this.game.enemies.forEach(function(subgroup){ | |
| subgroup.forEach(function(object){ | |
| distance = this.game.physics.arcade.distanceBetween(boom,object); | |
| radius = 80; | |
| mass = 0.05; | |
| if (distance < radius) { | |
| damage = (radius - distance)/10; | |
| // popUp(this.popUps, object, '-'+~~damage); | |
| angle = this.game.physics.arcade.angleBetween(boom, object); | |
| object.body.velocity.x += Math.cos(angle) * ((radius - distance) / mass); | |
| object.body.velocity.y += Math.sin(angle) * ((radius - distance) / mass); | |
| object.health -= damage/100; | |
| // object.healthStatus.setText(~~(object.health*100)) | |
| // if (object.health < 0.5) { | |
| // object.healthStatus.addColor('#900',0); | |
| // } | |
| } | |
| }); | |
| }); | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Bullet = function (game, x, y, direction, group) { | |
| this.SPEED = 500; | |
| this.direction = direction; | |
| var bmd = game.add.bitmapData(20, 1); | |
| bmd.context.fillStyle = '#FFFF00'; | |
| bmd.context.fillRect(0, 0, 20, 1); | |
| Phaser.Sprite.call(this, game, x, y, bmd); | |
| this.anchor.set(0.5,0.5); | |
| game.physics.enable(this); | |
| game.physics.arcade.enableBody(this); | |
| this.body.allowGravity = false; | |
| this.checkWorldBounds = true; | |
| this.outOfBoundsKill = true; | |
| this.body.velocity.x = this.direction * this.SPEED; | |
| game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Bullet.prototype = Object.create(Phaser.Sprite.prototype); | |
| Bullet.prototype.constructor = Bullet; | |
| // ------------------------------------------------------------------ | |
| var Bat = function(game,x,y, group) { | |
| if (typeof group === 'undefined') { group = game.world; } | |
| this.game = game; | |
| Phaser.Sprite.call(this, game, x, y, 'bat'); | |
| this.anchor.setTo(0.5, 0.5); | |
| this.anchor.set(0.5); | |
| this.animations.add('fly', [0,1,2,1 ], 20, true); | |
| this.play('fly'); | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| this.game.physics.arcade.enableBody(this); | |
| this.checkWorldBounds = true; | |
| this.outOfBoundsKill = true; | |
| this.body.collideWorldBounds = true; | |
| this.body.bounce.set(0.1); | |
| this.body.allowGravity = false; | |
| this.changeDirection = 0; | |
| this.changeDirectionDelay = this.game.rnd.between(1000,2000); | |
| this.game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Bat.prototype = Object.create(Phaser.Sprite.prototype); | |
| Bat.prototype.constructor = Bat; | |
| Bat.prototype.update = function() { | |
| if (this.game.time.now - this.changeDirection > this.changeDirectionDelay ) { | |
| this.body.velocity.x = this.game.rnd.normal()*50; | |
| this.body.velocity.y = this.game.rnd.normal()*50; | |
| this.changeDirection = this.game.time.now; | |
| } | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Ball = function(game,x,y,group) { | |
| if (typeof group === 'undefined') { group = game.world; } | |
| this.game = game; | |
| var bmd = game.add.bitmapData(20,20); | |
| bmd.ctx.fillStyle = '#999999'; | |
| bmd.ctx.beginPath(); | |
| bmd.ctx.arc(10, 10, 10, 0, Math.PI*2, true); | |
| bmd.ctx.closePath(); | |
| bmd.ctx.fill(); | |
| Phaser.Sprite.call(this, game, x, y, bmd); | |
| this.game.physics.arcade.enable(this, Phaser.Physics.ARCADE); | |
| this.body.collideWorldBounds = true; | |
| this.body.bounce.set(0.1); | |
| this.anchor.setTo(0.5, 0.5); | |
| this.healthStatus = game.add.text(0, -10, '100', {font: "9px Arial", fill: "#888888" }); | |
| this.addChild(this.healthStatus); | |
| game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Ball.prototype = Object.create(Phaser.Sprite.prototype); | |
| Ball.prototype.constructor = Ball; | |
| Ball.prototype.update = function() { | |
| }; | |
| // ------------------------------------------------------------------ | |
| var Ghost = function(game,x,y,group) { | |
| if (typeof group === 'undefined') { group = game.world; } | |
| this.game = game; | |
| Phaser.Sprite.call(this, game, 200, 140, 'enemy'); | |
| this.anchor.setTo(0.5, 0.5); | |
| var randomScale = 0.8 + Math.random(); | |
| this.scale.setTo(randomScale, randomScale); | |
| this.game.physics.enable(this, Phaser.Physics.ARCADE); | |
| this.game.physics.arcade.enableBody(this); | |
| this.body.allowGravity = true; | |
| this.checkWorldBounds = true; | |
| this.outOfBoundsKill = true; | |
| this.body.velocity.x = 0; | |
| this.body.velocity.y = 0; | |
| this.dx = 10; | |
| game.add.existing(this); | |
| group.add(this); | |
| }; | |
| Ghost.prototype = Object.create(Phaser.Sprite.prototype); | |
| Ghost.prototype.constructor = Ghost; | |
| Ghost.prototype.update = function() { | |
| if (this.body.velocity.x === 0) this.dx = -this.dx; | |
| // this.body.velocity.x = this.dx; | |
| }; | |
| // ------------------------------------------------------------------ | |
| CloudPlatform = function (game, x, y, key, group) { | |
| if (typeof group === 'undefined') { group = game.world; } | |
| Phaser.Sprite.call(this, game, x, y, key); | |
| game.physics.arcade.enable(this); | |
| this.anchor.x = 0.5; | |
| this.body.customSeparateX = true; | |
| this.body.customSeparateY = true; | |
| this.body.allowGravity = false; | |
| this.body.immovable = true; | |
| this.playerLocked = false; | |
| group.add(this); | |
| // game.add.existing(this); | |
| }; | |
| CloudPlatform.prototype = Object.create(Phaser.Sprite.prototype); | |
| CloudPlatform.prototype.constructor = CloudPlatform; | |
| CloudPlatform.prototype.addMotionPath = function (motionPath) { | |
| this.tweenX = this.game.add.tween(this.body); | |
| this.tweenY = this.game.add.tween(this.body); | |
| for (var i = 0; i < motionPath.length; i++) { | |
| this.tweenX.to( { x: motionPath[i].x }, motionPath[i].xSpeed, motionPath[i].xEase); | |
| this.tweenY.to( { y: motionPath[i].y }, motionPath[i].ySpeed, motionPath[i].yEase); | |
| } | |
| this.tweenX.loop(); | |
| this.tweenY.loop(); | |
| }; | |
| CloudPlatform.prototype.start = function () { | |
| this.tweenX.start(); | |
| this.tweenY.start(); | |
| }; | |
| CloudPlatform.prototype.stop = function () { | |
| this.tweenX.stop(); | |
| this.tweenY.stop(); | |
| }; | |
| // ------------------------------------------------------------------ | |
| //**************************************************************************************** | |
| //**************************************************************************************** | |
| //**************************************************************************************** | |
| //**************************************************************************************** | |
| var Game = function (game) { | |
| this.map = null; | |
| this.coins = null; | |
| this.layer = null; | |
| this.enemies = null; | |
| this.bats = null; | |
| this.ghost = null; | |
| this.player = null; | |
| this.clouds = null; | |
| this.ladders = null; | |
| this.bloodsplashEmitter = null; | |
| this.fragsEmitter = null; | |
| this.bulletSparksEmitter = null; | |
| }; | |
| Game.prototype = { | |
| preload: function () { | |
| game.stage.backgroundColor = 0x666666; | |
| game.time.advancedTiming = true; | |
| game.load.baseURL = 'http://9436.assets.s3.amazonaws.com/'; | |
| game.load.crossOrigin = ''; | |
| game.load.image('background', 'background.png'); | |
| game.load.image('enemy', 'player2.png'); | |
| game.load.image('platform', 'platform.png'); | |
| game.load.image('tiles', 'bricks2.png?'); | |
| game.load.tilemap('sandbox', 'sandbox20x20.json?1', null, Phaser.Tilemap.TILED_JSON); | |
| game.load.spritesheet('hero', 'hero11.png', 16, 16); | |
| this.load.spritesheet('bat', 'flying8.png', 8,8); | |
| game.load.spritesheet('bricks', 'bricks2.png?', 16, 16); | |
| }, | |
| create: function () { | |
| game.objects = {}; | |
| game.physics.startSystem(Phaser.Physics.ARCADE); | |
| game.physics.arcade.gravity.y = 800; | |
| // this.background = game.add.sprite(0, 0, 'background'); | |
| // this.background.fixedToCamera = true; | |
| this.map = game.add.tilemap('sandbox'); | |
| this.map.addTilesetImage('bricks', 'tiles'); | |
| this.layer = this.map.createLayer('walls'); | |
| this.layer.resizeWorld(); | |
| // this.layer.debug = true; | |
| this.map.setCollisionBetween(1,2000); | |
| // this.map.setTileIndexCallback([1], hit, this); | |
| this.popUps = initPopUps(); | |
| game.renderer.renderSession.roundPixels = false; | |
| this.clouds = this.add.physicsGroup(); | |
| new CloudPlatform(this.game, 80, 96, 'platform', this.clouds).addMotionPath([ | |
| { x: "+0", xSpeed: 1000, xEase: "Linear", | |
| y: "+70", ySpeed: 1000, yEase: "Sine.easeIn" }, | |
| { x: "-0", xSpeed: 1000, xEase: "Linear", | |
| y: "-70", ySpeed: 1000, yEase: "Sine.easeOut" } | |
| ]); | |
| this.clouds.callAll('start'); | |
| this.ladders = this.add.physicsGroup(); | |
| this.player = new Hero(this.game,30,80); | |
| game.camera.follow(this.player); | |
| for (var i=0, ic=this.map.objects.ladders.length; i<ic; i++) { | |
| var li = this.map.objects.ladders[i]; | |
| new Ladder(game, li.x, li.y, li.heihgt, this.ladders); | |
| } | |
| this.coins = this.add.group(); | |
| for (var i=0, ic=this.map.objects.coins.length; i<ic; i++) { | |
| var li = this.map.objects.coins[i]; | |
| new Coin(game, li.x, li.y, this.coins); | |
| } | |
| this.bats = this.add.physicsGroup(); | |
| for(var i=0;i<10;i++) | |
| new Bat(this.game, game.rnd.between(30,300), game.rnd.between(30,300), this.bats); | |
| this.ghost = this.add.group(); | |
| for(var i=0;i<1;i++) | |
| new Ghost(this.game, 1,1, this.ghost); | |
| this.balls = game.add.group(); | |
| for(var i=0;i<10;i++) { | |
| new Ball(this.game, game.rnd.between(30,300), game.rnd.between(30,300), this.balls); | |
| } | |
| // game.objects.balls = this.balls; | |
| game.enemies = this.add.group(); | |
| game.enemies.add(this.ghost); | |
| game.enemies.add(this.bats); | |
| game.enemies.add(this.balls); | |
| var bmd; | |
| bmd = game.add.bitmapData(2, 2); | |
| bmd.context.fillStyle = '#0099FF'; | |
| bmd.context.fillRect(0, 0, 2, 2); | |
| this.bulletSparksEmitter = game.add.emitter(0, 0, 100); | |
| this.bulletSparksEmitter.makeParticles(bmd, 0, 200, true, true); | |
| this.bulletSparksEmitter.setXSpeed(-300,300); | |
| this.bulletSparksEmitter.setYSpeed(-300,300); | |
| this.bulletSparksEmitter.bounce.setTo(0.5, 0.5); | |
| this.bulletSparksEmitter.gravity = 50; | |
| bmd = game.add.bitmapData(2, 2); | |
| bmd.context.fillStyle = '#FF0000'; | |
| bmd.context.fillRect(0, 0, 2, 2); | |
| this.bloodsplashEmitter = game.add.emitter(0, 0, 100); | |
| this.bloodsplashEmitter.makeParticles(bmd, 0, 200, true, true); | |
| this.bloodsplashEmitter.setYSpeed(-150,150); | |
| this.bloodsplashEmitter.setXSpeed(-150,150); | |
| this.bloodsplashEmitter.setRotation(0,0); | |
| this.bloodsplashEmitter.setAlpha(1, 0, 5000); | |
| this.bloodsplashEmitter.bounce.setTo(0,0); | |
| this.bloodsplashEmitter.gravity = 150; | |
| cursors = game.input.keyboard.createCursorKeys(); | |
| bmd = game.add.bitmapData(3, 3); | |
| bmd.context.fillStyle = '#FF0000'; | |
| bmd.context.fillRect(0, 0, 3, 3); | |
| this.fragsEmitter = game.add.emitter(0, 0, 100); | |
| this.fragsEmitter.makeParticles(bmd, 0, 200, true, true); | |
| this.fragsEmitter.setXSpeed(-300,300); | |
| this.fragsEmitter.setYSpeed(-300,300); | |
| this.fragsEmitter.setRotation(0,0); | |
| this.fragsEmitter.particleDrag.x = 100; | |
| this.fragsEmitter.setAlpha(1, 0, 50000); | |
| this.fragsEmitter.bounce.setTo(0.5,0.2); | |
| this.fragsEmitter.gravity = 150; | |
| }, | |
| preRender: function () { | |
| if (this.game.paused){ | |
| debub_text = 'paused'; | |
| // Because preRender still runs even if your game pauses! | |
| return; | |
| } | |
| if (this.player) { | |
| if (this.player.locked && this.player.lockedTo) { | |
| this.player.x += this.player.lockedTo.deltaX; | |
| this.player.y = this.player.lockedTo.y-10; | |
| if (this.player.body.velocity.x !== 0) { | |
| this.player.body.velocity.y = 0; | |
| } | |
| } | |
| } | |
| }, | |
| update: function () { | |
| game.physics.arcade.collide(this.player, this.layer, playerLayerCollide, null, this); | |
| game.physics.arcade.collide(this.player.weapons.machinegun.ammunitions, this.layer, bulletSparks, null, this); | |
| game.physics.arcade.collide(this.player.weapons.rocketlauncher.ammunitions, this.layer, missileBang, null, this); | |
| game.physics.arcade.collide(this.player.weapons.machinegun.ammunitions, this.bats, bulletBatCollide, null, this ); | |
| game.physics.arcade.collide(this.player.weapons.machinegun.ammunitions, this.ghost, bulletBatCollide, null, this ); | |
| game.physics.arcade.collide(this.player, this.clouds, customSep, null, this); | |
| game.physics.arcade.overlap(this.player, this.ladders, playerLadderCollide, null, this); | |
| game.physics.arcade.overlap(this.player, this.coins, playerCoinCollide, null, this); | |
| game.physics.arcade.collide(this.bloodsplashEmitter, this.layer, bloodsplashLayerCollide, null, this); | |
| game.physics.arcade.collide(this.fragsEmitter, this.layer); | |
| game.physics.arcade.collide(this.ghost, this.layer ); | |
| game.physics.arcade.collide(this.bats, this.layer); | |
| game.physics.arcade.collide(this.balls, this.layer); | |
| game.physics.arcade.overlap(this.player, this.bats, playerBatCollide, null, this); | |
| if (game.input.keyboard.downDuration(Phaser.Keyboard.Q,1)){ | |
| this.player.switchWeapon(); | |
| } | |
| game.input.onDown.add(bang, this); | |
| this.player.updates(); | |
| debug_text = 'Health: ' + this.player.health*100; | |
| }, | |
| render: function () { | |
| game.debug.text('FPS:'+game.time.fps ,16,16); | |
| game.debug.text(debug_text,86,16); | |
| } | |
| }; | |
| game.state.add('Game', Game, true); | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| // ------------------------------------------------------------------ | |
| function bang(pointer) { | |
| var boom = game.add.graphics(game.world.centerX, game.world.centerY); | |
| boom.lineStyle(1, 0xffffff); | |
| boom.drawCircle(0, 0, 80); | |
| boom.x = pointer.x; | |
| boom.y = pointer.y; | |
| boom.scale.x = 0; | |
| boom.scale.y = 0; | |
| var tween = game.add.tween(boom.scale).to({x: 1.5, y:1.5}, 100, Phaser.Easing.Linear.None); | |
| tween.onComplete.addOnce(function(){boom.scale.x =0; }); | |
| tween.start(); | |
| game.enemies.forEach(function(e) { | |
| e.forEach(function(object){ | |
| distance = game.physics.arcade.distanceBetween(boom,object); | |
| radius = 80; | |
| mass = 0.1; | |
| if (distance < radius) { | |
| damage = (radius - distance)/10; | |
| // popUp(popUpAlerts, object, '-'+~~damage); | |
| angle = game.physics.arcade.angleBetween(boom, object); | |
| object.body.velocity.x += Math.cos(angle) * ((radius - distance) / mass); | |
| object.body.velocity.y += Math.sin(angle) * ((radius - distance) / mass); | |
| // object.health -= damage/100; | |
| // object.healthStatus.setText(~~(object.health*100)) | |
| // if (object.health < 0.5) { | |
| // object.healthStatus.addColor('#900',0); | |
| // } | |
| } | |
| }); | |
| }); | |
| } | |
| function customSep(player, platform) { | |
| if (!player.locked && player.body.velocity.y > 0) { | |
| player.locked = true; | |
| player.lockedTo = platform; | |
| platform.playerLocked = true; | |
| player.body.velocity.y = 0; | |
| } | |
| } | |
| function upInputIsActive(duration) { | |
| return game.input.keyboard.downDuration(Phaser.Keyboard.UP, duration); | |
| } | |
| function upInputReleased() { | |
| return game.input.keyboard.upDuration(Phaser.Keyboard.UP); | |
| } | |
| function hit(sprite,tile) { | |
| // if (tile.alpha > 0.3) { | |
| // tile.alpha -= 0.01; | |
| // layer.dirty = true; | |
| // } | |
| return true; | |
| } | |
| function enemyTurn(enemy, layer) { | |
| if(enemy.body.blocked.right){ | |
| enemy.body.velocity.x = -speed; | |
| } else if (enemy.body.blocked.left) { | |
| enemy.body.velocity.x = speed; | |
| } | |
| } | |
| function playerCoinCollide(player, coin) { | |
| coin.destroy(); | |
| } | |
| function playerBatCollide(player, bat) { | |
| bat.kill(); | |
| player.damage(0.1); | |
| } | |
| function playerLadderCollide(player, ladder) { | |
| player.onLadder = ladder; | |
| player.body.velocity.y = 0; | |
| } | |
| function bulletBatCollide(bullet,bat) { | |
| this.bloodsplashEmitter.x = bat.x; | |
| this.bloodsplashEmitter.y = bat.y; | |
| this.bloodsplashEmitter.start(true, 5000, null, 10); | |
| var damage = 1; | |
| popUp(this.popUps, bat, '-'+damage); | |
| bat.damage(damage/10); | |
| if (bat.health<=0) { | |
| this.fragsEmitter.x = bat.x; | |
| this.fragsEmitter.y = bat.y; | |
| this.fragsEmitter.start(true, 0, null, 10); | |
| } | |
| bullet.kill(); | |
| } | |
| function bloodsplashLayerCollide(a,b) { | |
| a.body.velocity.x=0; | |
| a.body.velocity.y=0; | |
| // a.body.allowGravity = false; | |
| } | |
| function playerLayerCollide(player, tile) { | |
| // if (tile.index > 280) { | |
| // if (tile.alpha < 0.2) { | |
| // map.removeTile(tile.x,tile.y); | |
| // } else { | |
| // tile.alpha -= 0.1; | |
| // layer.dirty = true; | |
| // } | |
| // debug_text = tile.alpha; | |
| // } | |
| } | |
| function missileBang(a, b) { | |
| // a.explode(); | |
| a.bang(); | |
| } | |
| function bulletSparks(bullet, tile) { | |
| this.bulletSparksEmitter.y = tile.worldY+8; | |
| if (bullet.direction>0) { | |
| this.bulletSparksEmitter.x = tile.worldX; | |
| } else { | |
| this.bulletSparksEmitter.x = tile.worldX+16; | |
| } | |
| this.bulletSparksEmitter.start(true, 50, null, 5); | |
| bullet.kill(); | |
| } | |
| function initPopUps(){ | |
| var popUp, popUps = game.add.group(); | |
| for(var i=0;i<10;i++) { | |
| popUp = game.add.sprite(0,0); | |
| popUp.text = game.add.text(0, -10, '', {font: "9px Arial", fill: "#fff" }); | |
| popUp.addChild(popUp.text); | |
| popUps.add(popUp); | |
| popUp.kill(); | |
| } | |
| return popUps; | |
| } | |
| function popUp(popUps, object, text){ | |
| var a = popUps.getFirstDead(); | |
| if (a) { | |
| a.alpha = 1; | |
| a.reset(object.x, object.y-20); | |
| a.text.setText(text); //'-'+~~damage | |
| var tween = game.add.tween(a).to({y:a.y-60, alpha:0}, 1000, Phaser.Easing.Linear.None); | |
| tween.onComplete.addOnce(function(){ a.kill(); },this); | |
| tween.start(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment