55
66<script type="text/javascript">
77
8+ EnemyTank = function (index, game, player, bullets) {
9+
10+ var x = game.world.randomX;
11+ var y = game.world.randomY;
12+
13+ this.game = game;
14+ this.health = 3;
15+ this.player = player;
16+ this.bullets = bullets;
17+ this.fireRate = 1000;
18+ this.nextFire = 0;
19+ this.alive = true;
20+
21+ this.shadow = game.add.sprite(x, y, 'enemy', 'shadow');
22+ this.tank = game.add.sprite(x, y, 'enemy', 'tank1');
23+ this.turret = game.add.sprite(x, y, 'enemy', 'turret');
24+
25+ this.shadow.anchor.setTo(0.5, 0.5);
26+ this.tank.anchor.setTo(0.5, 0.5);
27+ this.turret.anchor.setTo(0.3, 0.5);
28+
29+ this.tank.name = index.toString();
30+ this.tank.body.immovable = true;
31+ this.tank.body.collideWorldBounds = true;
32+ this.tank.body.bounce.setTo(1, 1);
33+
34+ this.tank.angle = game.rnd.angle();
35+
36+ game.physics.velocityFromRotation(this.tank.rotation, 100, this.tank.body.velocity);
37+
38+ };
39+
40+ EnemyTank.prototype.damage = function() {
41+
42+ this.health -= 1;
43+
44+ if (this.health <= 0)
45+ {
46+ this.alive = false;
47+
48+ this.shadow.kill();
49+ this.tank.kill();
50+ this.turret.kill();
51+
52+ return true;
53+ }
54+
55+ return false;
56+
57+ }
58+
59+ EnemyTank.prototype.update = function() {
60+
61+ this.shadow.x = this.tank.x;
62+ this.shadow.y = this.tank.y;
63+ this.shadow.rotation = this.tank.rotation;
64+
65+ this.turret.x = this.tank.x;
66+ this.turret.y = this.tank.y;
67+ this.turret.rotation = this.game.physics.angleBetween(this.tank, this.player);
68+
69+ if (this.game.physics.distanceBetween(this.tank, this.player) < 300)
70+ {
71+ if (this.game.time.now > this.nextFire && this.bullets.countDead() > 0)
72+ {
73+ this.nextFire = this.game.time.now + this.fireRate;
74+
75+ var bullet = this.bullets.getFirstDead();
76+
77+ bullet.reset(this.turret.x, this.turret.y);
78+
79+ bullet.rotation = this.game.physics.moveToObject(bullet, this.player, 500);
80+ }
81+ }
82+
83+ };
84+
885 var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
986
10- function preload() {
87+ function preload () {
1188
1289 game.load.atlas('tank', 'assets/games/tanks/tanks.png', 'assets/games/tanks/tanks.json');
90+ game.load.atlas('enemy', 'assets/games/tanks/enemy-tanks.png', 'assets/games/tanks/tanks.json');
1391 game.load.image('bullet', 'assets/games/tanks/bullet.png');
1492 game.load.image('earth', 'assets/games/tanks/scorched_earth.png');
93+ game.load.spritesheet('explosion', 'assets/games/tanks/explosion.png', 64, 64, 23);
1594
1695 }
1796
@@ -21,7 +100,9 @@ function preload() {
21100 var tank;
22101 var turret;
23102
24- var enemy;
103+ var enemies;
104+ var enemyBullets;
105+ var explosions;
25106
26107 var currentSpeed = 0;
27108 var cursors;
@@ -30,26 +111,15 @@ function preload() {
30111 var fireRate = 100;
31112 var nextFire = 0;
32113
33- function create() {
114+ function create () {
34115
35- // Resize our game world to be a 2000x2000 square
116+ // Resize our game world to be a 2000 x 2000 square
36117 game.world.setBounds(-1000, -1000, 2000, 2000);
37118
38119 // Our tiled scrolling background
39120 land = game.add.tileSprite(0, 0, 800, 600, 'earth');
40121 land.fixedToCamera = true;
41122
42- // A shadow below our tank
43- shadow = game.add.sprite(0, 0, 'tank', 'shadow');
44- shadow.anchor.setTo(0.5, 0.5);
45-
46- // Our bullet group
47- bullets = game.add.group();
48- bullets.createMultiple(50, 'bullet');
49- bullets.setAll('anchor.x', 0.5);
50- bullets.setAll('anchor.y', 0.5);
51- bullets.setAll('outOfBoundsKill', true);
52-
53123 // The base of our tank
54124 tank = game.add.sprite(0, 0, 'tank', 'tank1');
55125 tank.anchor.setTo(0.5, 0.5);
@@ -61,15 +131,47 @@ function create() {
61131 tank.body.maxVelocity.setTo(400, 400);
62132 tank.body.collideWorldBounds = true;
63133
64-
65134 // Finally the turret that we place on-top of the tank body
66135 turret = game.add.sprite(0, 0, 'tank', 'turret');
67136 turret.anchor.setTo(0.3, 0.5);
68137
69- enemy = game.add.sprite(900, 400, 'tank', 'tank1');
70- enemy.anchor.setTo(0.5, 0.5);
71- enemy.body.immovable = true;
72- enemy.body.collideWorldBounds = true;
138+ // The enemies bullet group
139+ enemyBullets = game.add.group();
140+ enemyBullets.createMultiple(100, 'bullet');
141+ enemyBullets.setAll('anchor.x', 0.5);
142+ enemyBullets.setAll('anchor.y', 0.5);
143+ enemyBullets.setAll('outOfBoundsKill', true);
144+
145+ // Create some baddies to waste :)
146+ enemies = [];
147+
148+ for (var i = 0; i < 10; i++)
149+ {
150+ enemies.push(new EnemyTank(i, game, tank, enemyBullets));
151+ }
152+
153+ // A shadow below our tank
154+ shadow = game.add.sprite(0, 0, 'tank', 'shadow');
155+ shadow.anchor.setTo(0.5, 0.5);
156+
157+ // Our bullet group
158+ bullets = game.add.group();
159+ bullets.createMultiple(30, 'bullet');
160+ bullets.setAll('anchor.x', 0.5);
161+ bullets.setAll('anchor.y', 0.5);
162+ bullets.setAll('outOfBoundsKill', true);
163+
164+ // Explosion pool
165+ explosions = game.add.group();
166+
167+ for (var i = 0; i < 10; i++)
168+ {
169+ var e = explosions.create(0, 0, 'explosion', 0, false);
170+ e.animations.add('boom');
171+ }
172+
173+ tank.bringToTop();
174+ turret.bringToTop();
73175
74176 game.camera.follow(tank);
75177 game.camera.deadzone = new Phaser.Rectangle(100, 100, 600, 400);
@@ -79,9 +181,19 @@ function create() {
79181
80182 }
81183
82- function update() {
184+ function update () {
83185
84- game.physics.collide(tank, enemy);
186+ game.physics.collide(enemyBullets, tank, bulletHitPlayer, null, this);
187+
188+ for (var i = 0; i < enemies.length; i++)
189+ {
190+ if (enemies[i].alive)
191+ {
192+ enemies[i].update();
193+ game.physics.collide(tank, enemies[i].tank);
194+ game.physics.collide(bullets, enemies[i].tank, bulletHitEnemy, null, this);
195+ }
196+ }
85197
86198 if (cursors.left.isDown)
87199 {
@@ -108,10 +220,6 @@ function update() {
108220 if (currentSpeed > 0)
109221 {
110222 game.physics.velocityFromRotation(tank.rotation, currentSpeed, tank.body.velocity);
111-
112- // Scroll the background (note the negative offset to ensure it moves the direction we're facing, not coming from)
113- // land.tilePosition.x -= (tank.body.velocity.x / 50);
114- // land.tilePosition.y -= (tank.body.velocity.y / 50);
115223 }
116224
117225 land.tilePosition.x = -game.camera.x;
@@ -135,7 +243,29 @@ function update() {
135243
136244 }
137245
138- function fire() {
246+ function bulletHitPlayer (tank, bullet) {
247+
248+ bullet.kill();
249+
250+
251+ }
252+
253+ function bulletHitEnemy (tank, bullet) {
254+
255+ bullet.kill();
256+
257+ var destroyed = enemies[tank.name].damage();
258+
259+ if (destroyed)
260+ {
261+ var e = explosions.getFirstDead();
262+ e.reset(tank.x, tank.y);
263+ e.play('boom');
264+ }
265+
266+ }
267+
268+ function fire () {
139269
140270 if (game.time.now > nextFire && bullets.countDead() > 0)
141271 {
@@ -150,18 +280,10 @@ function fire() {
150280
151281 }
152282
153- function render() {
283+ function render () {
154284
155285 // game.debug.renderText('Active Bullets: ' + bullets.countLiving() + ' / ' + bullets.total, 32, 32);
156286
157- // game.debug.renderText('sr: ' + tank.body.right, 32, 100);
158- // game.debug.renderText('sb: ' + tank.body.bottom, 32, 132);
159-
160- // game.debug.renderSpriteCorners(tank, true, true);
161-
162- game.debug.renderCameraInfo(game.camera, 500, 32);
163- game.debug.renderSpriteInfo(tank, 32, 450);
164-
165287 }
166288
167289</script>
0 commit comments