phaser.load.spritesheet('kaboom', 'img/explosion.png', 64, 64, 23);
phaser.load.atlas('enemy', 'img/enemy-tanks.png', 'assets/tanks.json')
this.enemies = [];
this.enemiesTotal = 20;
this.enemiesAlive = 20;
this.ETbullets = [];
this.tank.health = 10;
this.tank.healthTotal = 10;
this.tank.alive = true;
for (var i = 0; i < this.enemiesTotal; i++)
var x = phaser.world.randomX;
var y = phaser.world.randomY;
var shadow = this.add.sprite(x, y, 'enemy', 'shadow');
var tank = this.add.sprite(x, y, 'enemy', 'tank');
var turret = this.add.sprite(x, y, 'enemy','turret');
var tank_group = phaser.add.group();
tank_group.add(tank);
tank_group.add(turret);
tank_group.add(shadow);
tank.bringToTop();
turret.bringToTop();
shadow.anchor.setTo(0.5);
tank.anchor.setTo(0.5);
turret.anchor.setTo(0.3, 0.5);
game.physics.enable(tank, Phaser.Physics.ARCADE);
tank.body.collideWorldBounds = true ;
tank.body.immovable = false ;
tank.body.bounce.setTo(1,1);
tank.angle = phaser.rnd.angle();
this.enemies.push({
tank: tank,
turret: turret,
tank_group: tank_group,
hasbullet : false,
shadow:shadow,
nextFire: 0,
health : 3,
alive : true
})
this.explosions = game.add.group();
for (var i = 0; i < 10; i++)
{
this.explosionAnimation = this.explosions.create
(0, 0, 'kaboom', [0], false);
this.explosionAnimation.anchor.setTo(0.5, 0.5);
this.explosionAnimation.animations.add('kaboom');
}
// tank angle change rate
var tacr=0;
var bullets = this.bullets;
var enemies = this.enemies;
var explosionAnimation = this.explosionAnimation;
var explosions = this.explosions;
var ETbullets = this.ETbullets;
game.physics.arcade.velocityFromRotation
(enemies[i].tank.angle, 100, enemies[i].tank.body.velocity);
phaser.physics.arcade.collide(enemies[i].tank, this.tank);
var collided = phaser.physics.arcade.collide
(enemies[i].tank, this.layer);
if(collided && tacr<=0){
enemies[i].tank.angle += 4;
tacr=5;
}
if (enemies[i].tank.x >= phaser.world.width-32 ||
enemies[i].tank.x <= 32 && tacr<=0){
this.enemies[i].tank.angle += 4;
tacr=15;
}
if (enemies[i].tank.y >= phaser.world.height-32 ||
enemies[i].tank.y <= 32 && tacr<=0){
enemies[i].tank.angle += 4;
tacr=15;
}
if(enemies[i].alive)
{
phaser.physics.arcade.overlap(bullets, enemies[i].tank,
function (bullet , tank) {
});
Text
bullet.kill();
enemies[i].health -=1;
if (enemies[i].health <=0)
{
}
Text
enemies[i].alive = false;
enemies[i].shadow.kill();
enemies[i].tank.kill();
enemies[i].turret.kill();
explosionAnimation = explosions.getFirstExists(false);
explosionAnimation.reset(enemies[i].tank.x, enemies[i].tank.y);
explosionAnimation.play('kaboom', 30, false, true);
Text
for(j = i+1;j < this.enemiesTotal ; j++)
{
if(this.enemies[j].alive)
{
if (phaser.physics.arcade.collide
(this.enemies[i].tank,this.enemies[j].tank))
{
this.enemies[i].tank.angle += 4;
tacr=10;
}
}
}
this.enemies[i].tank_group.setAll('x',this.enemies[i].tank.x);
this.enemies[i].tank_group.setAll('y',this.enemies[i].tank.y);
this.enemies[i].tank_group.setAll('angle',this.enemies[i].tank.angle);
this.enemies[i].turret.rotation = this.game.physics.arcade.angleBetween
(this.enemies[i].tank, this.tank);
if (phaser.physics.arcade.distanceBetween(enemies[i].tank, tank) < 300)
{
if (phaser.time.now > this.nextFire && enemies[i].alive)
{
}
}
var ETbullet = phaser.add.sprite
(enemies[i].turret.x, enemies[i].turret.y, 'bullet');
var ETbullets = this.ETbullets;
ETbullet.anchor.setTo(0.5, 0.5);
ETbullet.currentSpeed =300;
ETbullet.rotation = enemies[i].turret.rotation;
// bullet.scale.setTo(0.25,0.25);
phaser.physics.enable(ETbullet);
phaser.physics.arcade.velocityFromRotation
(ETbullet.rotation, ETbullet.currentSpeed, ETbullet.body.velocity);
ETbullet.outOfBoundsKill = true;
ETbullets.push(ETbullet);
this.nextFire = phaser.time.now + this.fireRate;
phaser.physics.arcade.collide(ETbullets, this.layer, this.bulletHits);
phaser.physics.arcade.overlap(ETbullets, tank, function (ETbullet , tank) {
tank.health -= 1;
ETbullet.kill();
if(tank.health <=0)
{
shadow.kill();
tank.kill();
turret.kill();
}
});
tacr-=1;
render: function () {
var phaser = this.phaser
phaser.debug.text('Health: ' + this.tank.health + ' / '
+ this.tank.healthTotal, 32, 32);
}
this.shot =0;
this.cd =0;
if (phaser.input.keyboard.isDown(Phaser.Keyboard.Q) && this.cd <= 0)
this.shot = 10;
if(this.shot > 0)
this.fire();
this.shot -=1;
this.cd -=1;