From 507e4175e082fbc90b755b7c1c9f79278ae48798 Mon Sep 17 00:00:00 2001 From: MegaBrutal Date: Thu, 28 Dec 2017 00:05:00 +0100 Subject: [PATCH] Cleanup: Use Phaser.Bullet & Phaser.Weapon I realized I reinvented the wheel by implementing my custom projectile sprite derived from Phaser.Sprite. There are predefined classes in Phaser.js just for this purpose. Now the player's weapon is derived from Phaser classes. modified: ld40.js --- ld40.js | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/ld40.js b/ld40.js index 5c84a99..d32c5ff 100644 --- a/ld40.js +++ b/ld40.js @@ -45,7 +45,7 @@ this.lasttime_damage = 0; this.speed = 150; this.switchmode(MODE_DETERMINATION); - this.projectiles = game.add.group(); + this.weapon = new JusticeBlaster(game, this); this.healthBar = new HealthBar(60, game.world.height - (WALL_BORDERBOTTOM / 2)); } @@ -102,10 +102,7 @@ } if ((this.mode == MODE_JUSTICE) && game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR) && ((game.time.now - this.lasttime_shoot) > WAIT_SHOOT)) { - this.shoot(0, SPEED_PROJECTILE); - this.shoot(0, -SPEED_PROJECTILE); - this.shoot(SPEED_PROJECTILE, 0); - this.shoot(-SPEED_PROJECTILE, 0); + this.shoot(); } } } @@ -124,10 +121,11 @@ this.mode = soulmode; } - shoot(velocityx, velocityy) { - var projectile = new Justice(this.x, this.y, velocityx, velocityy); - projectile.enablePhysics(); - this.projectiles.add(projectile); + shoot() { + this.weapon.fire(this, 0, this.y); + this.weapon.fire(this, this.x, 0); + this.weapon.fire(this, this.x, game.world.height); + this.weapon.fire(this, game.world.width, this.y); this.lasttime_shoot = game.time.now; } @@ -135,6 +133,7 @@ super.update(); this.control(); this.healthBar.update(this.love, this.maxHealth, this.health); + game.physics.arcade.overlap(monsters, this.weapon.bullets, function(m,b) { b.overlap_monster(m); }); if ((game.time.now - this.lasttime_mode) > WAIT_SOULMODE) { this.switchmode(MODE_DETERMINATION); } } } @@ -187,26 +186,28 @@ } } - class Justice extends Phaser.Sprite { - constructor(x, y, velocityx, velocityy) { + class Justice extends Phaser.Bullet { + constructor(game, x, y, key, frame) { super(game, x, y, 'projectile'); this.anchor.setTo(0.5, 0.5); - this.velocityx = velocityx; - this.velocityy = velocityy; } - enablePhysics() { - game.physics.arcade.enable(this); - this.body.velocity.x = this.velocityx; - this.body.velocity.y = this.velocityy; - } - - overlap(entity) { + overlap_monster(entity) { entity.damage(player.love); this.kill(); } } + class JusticeBlaster extends Phaser.Weapon { + constructor(game, parent) { + super(game, parent); + this.bulletClass = Justice; + this.bulletSpeed = SPEED_PROJECTILE; + this.multiFire = true; + this.createBullets(100, null); + } + } + class HealthBar { constructor(x, y) { this.mhealth = game.add.tileSprite(x + 10, y, 0, HB_THICKNESS, 'wall'); @@ -276,7 +277,6 @@ game.physics.arcade.collide(player, walls); game.physics.arcade.overlap(player, determinations, function(p,d) { p.overlap_determination(d); }); game.physics.arcade.overlap(player, monsters, function(p,m) { m.overlap_player(p); }); - game.physics.arcade.overlap(monsters, player.projectiles, function(m,j) { j.overlap(m); }); if (game.time.now > nexttime_determination) { -- 2.34.1