Giter Site home page Giter Site logo

16patsle / phaser3-weapon-plugin Goto Github PK

View Code? Open in Web Editor NEW
23.0 5.0 9.0 3.51 MB

Weapon Plugin for Phaser 3, ported from the one included in Phaser CE

Home Page: https://16patsle.github.io/phaser3-weapon-plugin/

License: MIT License

JavaScript 5.84% TypeScript 94.16%
typescript phaser phaser3 phaser3-plugin hacktoberfest

phaser3-weapon-plugin's People

Contributors

16patsle avatar dependabot[bot] avatar jdotr avatar jdotrjs avatar jemiloii avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

phaser3-weapon-plugin's Issues

Callback for bullet shoot

Thank you for the port of this plugin. I used it for a game lately. I did not find a way to add a callback for when a bullet is shot / started, so that you can add a sound. Would be amazing to have this!

Refactor fire method

It's 200 lines long with minimal comments, there has got to be some cleanup we can do there

Possible memory leak in physics collisions?

Hey, using your plugin and wired together the basic 'on mouse pressed' example that fires some random bullets. IE:

if (this.input.activePointer.isDown) {
  this.weapon.fire();
}

so that works fine. However, if I include the overlap() Arcade Physics logic to track collisions between my other scene sprites & weapon bullets, I notice that the FPS collapses after a while. It goes from a smooth 60fps, to < 10fps. I keep the mouse button down and after about 10 seconds, the FPS starts dropping. Letting go of the mouse, but the FPS continues to drop.

this.physics.add.overlap(this.allSprites, this.weapon.bullets, (actor, bullet) => {
    bullet.kill();
});

So I don't have any particular debug info: the FPS drop for sure happens if the overlap() logic is included (this.allSprites contains a bunch of normal ArcadeSprite sprites).

Without that overlap logic, holding the mouse button down doesn't cause the FPS drop at all. I wonder is there something going on with the Bullets when they 'die' yet they're still being cached by the ArcadePhysics overlap() function?

The plugin is great and just what I need, but this FPS lag means I can't use it ATM. I probably could hot-patch some 'fake' physics overlap logic, but would be a shame not to use the code provided out-of-box.

extends WeaponPlugin.Weapon

throws an error:
Uncaught TypeError: Class constructor Weapon cannot be invoked without 'new'

what is the proper way to do this? I tried changing Phaser.Weapon / Phaser.Bullet to WeaponPlugin.Weapon / WeaponPlugin.Bullet with no luck ..

Here's the Phaser 2 Weapon code I am trying to migrate:

class Bullet extends Phaser.Bullet {

    kill() {}
    
    update() {
        this.angle++;
        this.tint   = Math.random() * 0xFFFFFF;
        super.update();
    }
}

export class PentagramBullet extends Phaser.Weapon {

    BLOWS_SHIT_UP   = true;
    hitPoints   = 8;
    skullBulletFireSound;
    
    constructor( player, game, parent = game.world, name = 'Pentagram Bullet', addToStage = false, enableBody = true, physicsBodyType = Phaser.Physics.ARCADE ) {
        super( game, parent, name, addToStage, enableBody, physicsBodyType );
        
        this.nextFire       = 0;
        this.bulletSpeed    = 600;
        this.fireRate       = 1600;
        this.bulletClass    = Bullet;

        this.createBullets( 32, 'pentagramBullet' );

        this.trackSprite( player, 60, 5, true );
    }
}

Import issues

Screen Shot 2022-03-09 at 8 39 14 PM

I have reset my node_modules and reinstalled the package and I still run into this issue. I am importing as the docs recommend

Screen Shot 2022-03-09 at 8 40 55 PM

Very weird behavior. I am in a create-react-app environment

All thrown errors should be moved to the plugin creation / setup step if possible

      if (this.bulletKillType === consts.KILL_LIFESPAN) {
        if (this.bulletLifespan <= 0) {
          throw new Error('Invalid bulletLifespan; must be > 0')
        }
        bullet.data.timeEvent = this.scene.time.addEvent({
          delay: this.bulletLifespan,
          callback: bullet.kill.bind(bullet),
        })
        bullet.lifespan = this.bulletLifespan;
      }

I added that throw because it doesn't make sense to have a lifespan < 0 but it's not great to have things throwing while the game runs either.

When we start polishing we should update the setup phase to hide all attribute updates behind setters with validation or use a builder pattern to do as much as we can to ensure that things which will throw will happen as early as possible.

Stop binding callback for each lifespan kill event

From Weapon.js

      if (this.bulletKillType === consts.KILL_LIFESPAN) {
        if (this.bulletLifespan <= 0) {
          throw new Error('Invalid bulletLifespan; must be > 0')
        }
        bullet.data.timeEvent = this.scene.time.addEvent({
          delay: this.bulletLifespan,
          callback: bullet.kill.bind(bullet),
        })
        bullet.lifespan = this.bulletLifespan;
      }

we can probably pass the bullet as a callback context instead of binding the kill on each fire (I would have guessed I tried this when I initially wrote it so maybe there is a problem? We should test to make sure it works before merging which is why I'm ticketing this as a separate thing.

Game crashes on scene restart when using this plugin

I've created a clean repository for showing this problem.
Important part of the code

Expected behavior: restart scene and continue firing the weapon.

Actual behavior: getting an error on restart
Uncaught TypeError: this.data.destroy is not a function
at Bullet.destroy (GameObject.js:555)
at DisplayList.shutdown (DisplayList.js:206)
at EventEmitter.emit (index.js:203)
at Systems.shutdown (Systems.js:708)
at SceneManager.stop (SceneManager.js:1164)
at SceneManager.processQueue (SceneManager.js:288)
at SceneManager.update (SceneManager.js:547)
at Game.step (Game.js:538)
at TimeStep.step (TimeStep.js:560)
at step (RequestAnimationFrame.js:104)

Also there is also warning: Scene Plugin key in use: null

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.