16patsle / phaser3-weapon-plugin Goto Github PK
View Code? Open in Web Editor NEWWeapon Plugin for Phaser 3, ported from the one included in Phaser CE
Home Page: https://16patsle.github.io/phaser3-weapon-plugin/
License: MIT License
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
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!
It's 200 lines long with minimal comments, there has got to be some cleanup we can do there
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.
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 );
}
}
As an example one of the test pages was using a mix of bullets having a world border kill and also wrapping at world border which is results in some odd behaviors.
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.
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.
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.