npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@browndragon/phaser3-plugin

v0.0.13

Published

An es6-style phaser.io base plugin class for scene plugins with an update cycle (like: tweens, animations, constraints, etc).

Downloads

47

Readme

phaser3-plugin

Provides a phaser.io plugin basic framework.

Usage

On the command line, $ npm i @browndragon/phaser3-plugin and then:

// index.js
import Phaser from 'phaser';  // phaser3-plugin uses a peer dependency, so this MUST be your first line before importing it!
import phaser3Plugin from '@browndragon/phaser3-plugin';

class MyObject extends phaser3Plugin.Managed {
    update(cumulative, delta) {
        /* ... */
    }
    /* ... */
}
class MyPlugin extends phaser3Plugin.Plugin {
    create({param1, param2, /* ... */}) {
        return new MyObject(param1, param2, /* ... */);
    }
    /* ... */
}

// Standard phaser preamble.
const game = new Phaser.Game({
  type: Phaser.AUTO,
  parent: 'phaser-example',
  width: 800,
  height: 600,
  plugins: {
    scene: [
      {key:'SomeKey', mapping:'someKey', plugin:MyPlugin, start:true},
    ],
  },
  scene: class extends Phaser.Scene {
    create() {
      let myObject = this.someKey.create({param1:'foo', param2:'bar', /* ... */});
      // Can do things like myObject.play(), myObject.pause(), myObject.stop()...
      /* ... */
    }
  },
});

The MyObject will have its update called whenever it's in state ACTIVE and is added or existinged to an object which recursively matches these rules (phaser3Plugin.Plugin does so by definition).

You can use pause to temporarily stop calling update on an object and all of its children; you can use stop to permanently stop calling update on an object and all of its children.

You can also use foo.timeScale (and foo.setTimeScale() and plugin.setGlobalTimeScale()) to modify how time passes within these systems.

Parts Included

Plugin

A Phaser.Plugins.ScenePlugin base class which manages the other components. By default it also acts as a container for the other components mentioned here.

Manages the scene lifecycle for you.

Managed

The base class for your plugin components. It exposes an update method, as well as play/stop and pause/resume pairs.

Container

A Managed which additionally has the creation and component methods of the plugin class, and so can itself contain subclasses of Managed... including other Containers.

Note that you cannot currently move Managed between Containers (or Plugin), so once created in a container, the instance can be destroyed, but not (safely!) change parents.

TODO: This could be fixed. It would require some thought around how to tell the difference between "removed forever" and "removed to reparent" for observation purposes.

A note on time

Plugin, Managed, and Container all expose timeScale, which (hierarchically) multiplies each time tick delta they observe. This means that if you have some chain Plugin > Container 1 > Container 2 > ManagedFoo and each has a timeScale of 0.5 ("half speed" compared to wall time), then the actual observed speed at ManagedFoo is (0.5*0.5*0.5*0.5 =) 1/16th normal speed.

A note on state & observability

There are (currently) two permissible callbacks on a Managed (or theoretically Container....) instance. These are onActive and onComplete, and correspond to the active and complete object events.

Contrasting this with animation and tween, the additional events (start, stop, update, etc) represent an additional state machine which a subclass could provide.

Why?!

I was looking at animation and tween and realized how similar they were -- they both have a Manager, they both have individual atoms which can be play and pause and stop; they support looping (... I didn't end up doing this...)...

I was writing @browndragon/phaser3-constraint and realized I needed something very similar: the ability to have something run on every update. It needed constraints that could be modified, paused, etc; it might even need the same hierarchy that tween has with timeline.

So I generalized it.

While analyzing & generalizing tween, I found several bugs (well, by inspection anyway). I got worried enough that I wrote @browndragon/sm to implement the state machine I'd need to feel more confident that I was navigating the various states this could hold safely.