Cleanup: Use Phaser.Bullet & Phaser.Weapon
authorMegaBrutal <code+git@megabrutal.com>
Wed, 27 Dec 2017 23:05:00 +0000 (00:05 +0100)
committerMegaBrutal <code+git@megabrutal.com>
Wed, 27 Dec 2017 23:05:00 +0000 (00:05 +0100)
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

diff --git a/ld40.js b/ld40.js
index 5c84a99..d32c5ff 100644 (file)
--- 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));
             }
 
                     }
                     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();
                     }
                 }
             }
                 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;
             }
 
                 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); }
             }
         }
             }
         }
 
-        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');
             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)
             {