mirror of
https://github.com/wassname/phaser.git
synced 2026-06-27 16:10:15 +08:00
Enemy tanks, now firing.
This commit is contained in:
@@ -110,6 +110,8 @@ Version 1.0.7 (in progress in the dev branch)
|
||||
* New angle functions: angleBetween, angleToXY, angleToPointer
|
||||
* velocityFromAngle and velocityFromRotation added with examples created.
|
||||
* Fixed the RandomDataGenerator.sow method so if you give in the same seed you'll now get the same results (thanks Hsaka)
|
||||
* World.randomX/Y now works with negative World.bounds values.
|
||||
|
||||
|
||||
* TODO: look at Sprite.crop (http://www.html5gamedevs.com/topic/1617-error-in-spritecrop/)
|
||||
* TODO: d-pad example (http://www.html5gamedevs.com/topic/1574-gameinputondown-question/)
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 44 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 115 KiB |
+158
-36
@@ -5,13 +5,92 @@
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
EnemyTank = function (index, game, player, bullets) {
|
||||
|
||||
var x = game.world.randomX;
|
||||
var y = game.world.randomY;
|
||||
|
||||
this.game = game;
|
||||
this.health = 3;
|
||||
this.player = player;
|
||||
this.bullets = bullets;
|
||||
this.fireRate = 1000;
|
||||
this.nextFire = 0;
|
||||
this.alive = true;
|
||||
|
||||
this.shadow = game.add.sprite(x, y, 'enemy', 'shadow');
|
||||
this.tank = game.add.sprite(x, y, 'enemy', 'tank1');
|
||||
this.turret = game.add.sprite(x, y, 'enemy', 'turret');
|
||||
|
||||
this.shadow.anchor.setTo(0.5, 0.5);
|
||||
this.tank.anchor.setTo(0.5, 0.5);
|
||||
this.turret.anchor.setTo(0.3, 0.5);
|
||||
|
||||
this.tank.name = index.toString();
|
||||
this.tank.body.immovable = true;
|
||||
this.tank.body.collideWorldBounds = true;
|
||||
this.tank.body.bounce.setTo(1, 1);
|
||||
|
||||
this.tank.angle = game.rnd.angle();
|
||||
|
||||
game.physics.velocityFromRotation(this.tank.rotation, 100, this.tank.body.velocity);
|
||||
|
||||
};
|
||||
|
||||
EnemyTank.prototype.damage = function() {
|
||||
|
||||
this.health -= 1;
|
||||
|
||||
if (this.health <= 0)
|
||||
{
|
||||
this.alive = false;
|
||||
|
||||
this.shadow.kill();
|
||||
this.tank.kill();
|
||||
this.turret.kill();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
EnemyTank.prototype.update = function() {
|
||||
|
||||
this.shadow.x = this.tank.x;
|
||||
this.shadow.y = this.tank.y;
|
||||
this.shadow.rotation = this.tank.rotation;
|
||||
|
||||
this.turret.x = this.tank.x;
|
||||
this.turret.y = this.tank.y;
|
||||
this.turret.rotation = this.game.physics.angleBetween(this.tank, this.player);
|
||||
|
||||
if (this.game.physics.distanceBetween(this.tank, this.player) < 300)
|
||||
{
|
||||
if (this.game.time.now > this.nextFire && this.bullets.countDead() > 0)
|
||||
{
|
||||
this.nextFire = this.game.time.now + this.fireRate;
|
||||
|
||||
var bullet = this.bullets.getFirstDead();
|
||||
|
||||
bullet.reset(this.turret.x, this.turret.y);
|
||||
|
||||
bullet.rotation = this.game.physics.moveToObject(bullet, this.player, 500);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
function preload () {
|
||||
|
||||
game.load.atlas('tank', 'assets/games/tanks/tanks.png', 'assets/games/tanks/tanks.json');
|
||||
game.load.atlas('enemy', 'assets/games/tanks/enemy-tanks.png', 'assets/games/tanks/tanks.json');
|
||||
game.load.image('bullet', 'assets/games/tanks/bullet.png');
|
||||
game.load.image('earth', 'assets/games/tanks/scorched_earth.png');
|
||||
game.load.spritesheet('explosion', 'assets/games/tanks/explosion.png', 64, 64, 23);
|
||||
|
||||
}
|
||||
|
||||
@@ -21,7 +100,9 @@
|
||||
var tank;
|
||||
var turret;
|
||||
|
||||
var enemy;
|
||||
var enemies;
|
||||
var enemyBullets;
|
||||
var explosions;
|
||||
|
||||
var currentSpeed = 0;
|
||||
var cursors;
|
||||
@@ -30,26 +111,15 @@
|
||||
var fireRate = 100;
|
||||
var nextFire = 0;
|
||||
|
||||
function create() {
|
||||
function create () {
|
||||
|
||||
// Resize our game world to be a 2000x2000 square
|
||||
// Resize our game world to be a 2000 x 2000 square
|
||||
game.world.setBounds(-1000, -1000, 2000, 2000);
|
||||
|
||||
// Our tiled scrolling background
|
||||
land = game.add.tileSprite(0, 0, 800, 600, 'earth');
|
||||
land.fixedToCamera = true;
|
||||
|
||||
// A shadow below our tank
|
||||
shadow = game.add.sprite(0, 0, 'tank', 'shadow');
|
||||
shadow.anchor.setTo(0.5, 0.5);
|
||||
|
||||
// Our bullet group
|
||||
bullets = game.add.group();
|
||||
bullets.createMultiple(50, 'bullet');
|
||||
bullets.setAll('anchor.x', 0.5);
|
||||
bullets.setAll('anchor.y', 0.5);
|
||||
bullets.setAll('outOfBoundsKill', true);
|
||||
|
||||
// The base of our tank
|
||||
tank = game.add.sprite(0, 0, 'tank', 'tank1');
|
||||
tank.anchor.setTo(0.5, 0.5);
|
||||
@@ -61,15 +131,47 @@
|
||||
tank.body.maxVelocity.setTo(400, 400);
|
||||
tank.body.collideWorldBounds = true;
|
||||
|
||||
|
||||
// Finally the turret that we place on-top of the tank body
|
||||
turret = game.add.sprite(0, 0, 'tank', 'turret');
|
||||
turret.anchor.setTo(0.3, 0.5);
|
||||
|
||||
enemy = game.add.sprite(900, 400, 'tank', 'tank1');
|
||||
enemy.anchor.setTo(0.5, 0.5);
|
||||
enemy.body.immovable = true;
|
||||
enemy.body.collideWorldBounds = true;
|
||||
// The enemies bullet group
|
||||
enemyBullets = game.add.group();
|
||||
enemyBullets.createMultiple(100, 'bullet');
|
||||
enemyBullets.setAll('anchor.x', 0.5);
|
||||
enemyBullets.setAll('anchor.y', 0.5);
|
||||
enemyBullets.setAll('outOfBoundsKill', true);
|
||||
|
||||
// Create some baddies to waste :)
|
||||
enemies = [];
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
enemies.push(new EnemyTank(i, game, tank, enemyBullets));
|
||||
}
|
||||
|
||||
// A shadow below our tank
|
||||
shadow = game.add.sprite(0, 0, 'tank', 'shadow');
|
||||
shadow.anchor.setTo(0.5, 0.5);
|
||||
|
||||
// Our bullet group
|
||||
bullets = game.add.group();
|
||||
bullets.createMultiple(30, 'bullet');
|
||||
bullets.setAll('anchor.x', 0.5);
|
||||
bullets.setAll('anchor.y', 0.5);
|
||||
bullets.setAll('outOfBoundsKill', true);
|
||||
|
||||
// Explosion pool
|
||||
explosions = game.add.group();
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
var e = explosions.create(0, 0, 'explosion', 0, false);
|
||||
e.animations.add('boom');
|
||||
}
|
||||
|
||||
tank.bringToTop();
|
||||
turret.bringToTop();
|
||||
|
||||
game.camera.follow(tank);
|
||||
game.camera.deadzone = new Phaser.Rectangle(100, 100, 600, 400);
|
||||
@@ -79,9 +181,19 @@
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
function update () {
|
||||
|
||||
game.physics.collide(tank, enemy);
|
||||
game.physics.collide(enemyBullets, tank, bulletHitPlayer, null, this);
|
||||
|
||||
for (var i = 0; i < enemies.length; i++)
|
||||
{
|
||||
if (enemies[i].alive)
|
||||
{
|
||||
enemies[i].update();
|
||||
game.physics.collide(tank, enemies[i].tank);
|
||||
game.physics.collide(bullets, enemies[i].tank, bulletHitEnemy, null, this);
|
||||
}
|
||||
}
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
@@ -108,10 +220,6 @@
|
||||
if (currentSpeed > 0)
|
||||
{
|
||||
game.physics.velocityFromRotation(tank.rotation, currentSpeed, tank.body.velocity);
|
||||
|
||||
// Scroll the background (note the negative offset to ensure it moves the direction we're facing, not coming from)
|
||||
// land.tilePosition.x -= (tank.body.velocity.x / 50);
|
||||
// land.tilePosition.y -= (tank.body.velocity.y / 50);
|
||||
}
|
||||
|
||||
land.tilePosition.x = -game.camera.x;
|
||||
@@ -135,7 +243,29 @@
|
||||
|
||||
}
|
||||
|
||||
function fire() {
|
||||
function bulletHitPlayer (tank, bullet) {
|
||||
|
||||
bullet.kill();
|
||||
|
||||
|
||||
}
|
||||
|
||||
function bulletHitEnemy (tank, bullet) {
|
||||
|
||||
bullet.kill();
|
||||
|
||||
var destroyed = enemies[tank.name].damage();
|
||||
|
||||
if (destroyed)
|
||||
{
|
||||
var e = explosions.getFirstDead();
|
||||
e.reset(tank.x, tank.y);
|
||||
e.play('boom');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function fire () {
|
||||
|
||||
if (game.time.now > nextFire && bullets.countDead() > 0)
|
||||
{
|
||||
@@ -150,18 +280,10 @@
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
function render () {
|
||||
|
||||
// game.debug.renderText('Active Bullets: ' + bullets.countLiving() + ' / ' + bullets.total, 32, 32);
|
||||
|
||||
// game.debug.renderText('sr: ' + tank.body.right, 32, 100);
|
||||
// game.debug.renderText('sb: ' + tank.body.bottom, 32, 132);
|
||||
|
||||
// game.debug.renderSpriteCorners(tank, true, true);
|
||||
|
||||
game.debug.renderCameraInfo(game.camera, 500, 32);
|
||||
game.debug.renderSpriteInfo(tank, 32, 450);
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
+20
-2
@@ -223,7 +223,16 @@ Object.defineProperty(Phaser.World.prototype, "centerY", {
|
||||
Object.defineProperty(Phaser.World.prototype, "randomX", {
|
||||
|
||||
get: function () {
|
||||
return this.game.rnd.integerInRange(this.bounds.x, this.bounds.width);
|
||||
|
||||
if (this.bounds.x < 0)
|
||||
{
|
||||
return this.game.rnd.integerInRange(this.bounds.x, (this.bounds.width - Math.abs(this.bounds.x)));
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.game.rnd.integerInRange(this.bounds.x, this.bounds.width);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
@@ -236,7 +245,16 @@ Object.defineProperty(Phaser.World.prototype, "randomX", {
|
||||
Object.defineProperty(Phaser.World.prototype, "randomY", {
|
||||
|
||||
get: function () {
|
||||
return this.game.rnd.integerInRange(this.bounds.y, this.bounds.height);
|
||||
|
||||
if (this.bounds.y < 0)
|
||||
{
|
||||
return this.game.rnd.integerInRange(this.bounds.y, (this.bounds.height - Math.abs(this.bounds.y)));
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.game.rnd.integerInRange(this.bounds.y, this.bounds.height);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@@ -171,9 +171,6 @@ Phaser.RandomDataGenerator.prototype = {
|
||||
*/
|
||||
realInRange: function (min, max) {
|
||||
|
||||
min = min || 0;
|
||||
max = max || 0;
|
||||
|
||||
return this.frac() * (max - min) + min;
|
||||
|
||||
},
|
||||
|
||||
@@ -984,7 +984,7 @@ Phaser.Physics.Arcade.prototype = {
|
||||
},
|
||||
|
||||
/**
|
||||
* Move the given display object towards the pointer at a steady velocity. If no pointer is given it will use Phaser.Input.activePointer.
|
||||
* Move the given display object towards the destination object at a steady velocity.
|
||||
* If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds.
|
||||
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
|
||||
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
||||
@@ -1003,7 +1003,7 @@ Phaser.Physics.Arcade.prototype = {
|
||||
speed = speed || 60;
|
||||
maxTime = maxTime || 0;
|
||||
|
||||
this._angle = Math.atan2(destination.y - displayObject.y, destination.x - displaxObject.x);
|
||||
this._angle = Math.atan2(destination.y - displayObject.y, destination.x - displayObject.x);
|
||||
|
||||
if (maxTime > 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user