Auxilinaut

Extend Weapon Plugin (TypeScript)

Recommended Posts

Hello,

I'm trying to make separate weapon classes in an Asteroids-type game, for example a Missile.ts, Laser.ts, etc.

What I have so far is this (Missile.ts):

module Spaceroni {

    export class Missile extends Phaser.Weapon {

        constructor(game: Phaser.Game) {

            super(game, game.plugins);
            
            this.bulletKillType = Phaser.Weapon.KILL_LIFESPAN;
            this.bulletSpeed = 1500;
            this.fireRate = 20;

        }

    }

}

In my Player.ts I have

weapon: Spaceroni.Missile;

up top and I'm adding a weapon in its constructor with

this.weapon = game.add.weapon(30, 'missile');

This isn't working (bulletSpeed and fireRate aren't changing), I think because game.add.weapon() creates a new weapon instance, separate from my Missile class. I would like to avoid putting all of this Missile-specific code directly into the Player class since I want to be able to change the weapon based on power-ups, as well as add the Missile weapon to enemies.

Any ideas on how to properly extend the weapon plugin?

Thanks in advance!

Share this post


Link to post
Share on other sites

Hi, you need a helper similar to game.add.weapon:

Phaser.GameObjectFactory.prototype.missile = function (quantity, key, frame, group) {
  var missile = this.game.plugins.add(Spaceroni.Missile);

  missile.createBullets(quantity, key, frame, group);

  return missile;
};

// …

this.weapon = game.add.missile(30, 'missile');

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.