Skip to content

Commit 29acf7f

Browse files
committed
Enemy tanks, now firing.
1 parent ca9321e commit 29acf7f

7 files changed

Lines changed: 182 additions & 43 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ Version 1.0.7 (in progress in the dev branch)
110110
* New angle functions: angleBetween, angleToXY, angleToPointer
111111
* velocityFromAngle and velocityFromRotation added with examples created.
112112
* Fixed the RandomDataGenerator.sow method so if you give in the same seed you'll now get the same results (thanks Hsaka)
113+
* World.randomX/Y now works with negative World.bounds values.
114+
113115

114116
* TODO: look at Sprite.crop (http://www.html5gamedevs.com/topic/1617-error-in-spritecrop/)
115117
* TODO: d-pad example (http://www.html5gamedevs.com/topic/1574-gameinputondown-question/)
43.8 KB
Loading
115 KB
Loading

examples/games/tanks.php

Lines changed: 158 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,92 @@
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>

src/core/World.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,16 @@ Object.defineProperty(Phaser.World.prototype, "centerY", {
223223
Object.defineProperty(Phaser.World.prototype, "randomX", {
224224

225225
get: function () {
226-
return this.game.rnd.integerInRange(this.bounds.x, this.bounds.width);
226+
227+
if (this.bounds.x < 0)
228+
{
229+
return this.game.rnd.integerInRange(this.bounds.x, (this.bounds.width - Math.abs(this.bounds.x)));
230+
}
231+
else
232+
{
233+
return this.game.rnd.integerInRange(this.bounds.x, this.bounds.width);
234+
}
235+
227236
}
228237

229238
});
@@ -236,7 +245,16 @@ Object.defineProperty(Phaser.World.prototype, "randomX", {
236245
Object.defineProperty(Phaser.World.prototype, "randomY", {
237246

238247
get: function () {
239-
return this.game.rnd.integerInRange(this.bounds.y, this.bounds.height);
248+
249+
if (this.bounds.y < 0)
250+
{
251+
return this.game.rnd.integerInRange(this.bounds.y, (this.bounds.height - Math.abs(this.bounds.y)));
252+
}
253+
else
254+
{
255+
return this.game.rnd.integerInRange(this.bounds.y, this.bounds.height);
256+
}
257+
240258
}
241259

242260
});

src/math/RandomDataGenerator.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,6 @@ Phaser.RandomDataGenerator.prototype = {
171171
*/
172172
realInRange: function (min, max) {
173173

174-
min = min || 0;
175-
max = max || 0;
176-
177174
return this.frac() * (max - min) + min;
178175

179176
},

src/physics/arcade/ArcadePhysics.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ Phaser.Physics.Arcade.prototype = {
984984
},
985985

986986
/**
987-
* Move the given display object towards the pointer at a steady velocity. If no pointer is given it will use Phaser.Input.activePointer.
987+
* Move the given display object towards the destination object at a steady velocity.
988988
* 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.
989989
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
990990
* 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 = {
10031003
speed = speed || 60;
10041004
maxTime = maxTime || 0;
10051005

1006-
this._angle = Math.atan2(destination.y - displayObject.y, destination.x - displaxObject.x);
1006+
this._angle = Math.atan2(destination.y - displayObject.y, destination.x - displayObject.x);
10071007

10081008
if (maxTime > 0)
10091009
{

0 commit comments

Comments
 (0)