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
this.lasttime_damage = 0;
this.speed = 150;
this.switchmode(MODE_DETERMINATION);
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));
}
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))
{
}
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);
- 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;
}
this.lasttime_shoot = game.time.now;
}
super.update();
this.control();
this.healthBar.update(this.love, this.maxHealth, this.health);
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); }
}
}
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);
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();
}
}
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');
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.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)
{
if (game.time.now > nexttime_determination)
{