pbp
phaser-behavior-plugin
npm i phaser-behavior-plugin
Deprecated!
Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.
pbp

phaser-behavior-plugin

Behavior System for Phaser games (1.8KB min+gzip)

by Luiz "Bills"

1.1.0 (see all)License:MITTypeScript:Not Found
npm i phaser-behavior-plugin
Readme

Phaser Behavior Plugin

Behavior System wrapper for Phaser games

js-standard-style

LIVE DEMO

Install

NPM

npm install phaser-behavior-plugin --save

DIRECT DOWNLOAD

Download Here

CDN

<script scr="//unpkg.com/phaser-behavior-plugin/dist/phaser-behavior-plugin.js"></script>

Usage

This library is UMD-compatible

// create a behavior
var Behavior = {

  // settings of the behavior
  options: {
    key: 'value'
  },

  create: function(object, options) {
    // called when the behavior is ADDED to a game object
  },

  destroy: function(object, options) {
    // called when the behavior is REMOVED from a game object
  },

  preUpdate: function(object, options) {
    // called at the very start of the update cycle,
    // before any other subsystems have been updated (including Physics)
  },

  update: function(object, options) {
    // called after all the core subsystems (Input, Tweens, Sound, etc)
    // and the State have updated, but before the render
  },

  render: function(object, options) {
    // called right after the Game Renderer completes, but before the State.render
  },

  postRender: function(object, options) {
    // called after the Game Renderer and State.render have run
  }
}

// initialize the plugin
var behaviorPlugin = game.plugins.add(BehaviorPlugin)
/*
 * or (if Phaser is a global)
 * var behaviorPlugin = game.plugins.add(Phaser.Plugin.Behavior)
 */

// a dummy
var myObject = game.create.sprite(0, 0, 'dummy')

// add the behavior system to any object (not just sprites)
behaviorPlugin.enable(myObject)


// all behavior instances must have a identifier/key. This id just need to be unique.
var id = 'my awesome key'

// You can to override the default options a behavior instance, if you want/need (optional)
var customOptions = { key: 'another value' }

// add a behavior
myObject.behaviors.set(id, Behavior, customOptions) // `.create` is called

// checks if a object has a behavior instance
// returns true if the object has a behavior with the key
myObject.behaviors.has(id)

// get a behavior instance
// returns the behavior instance if the object has a behavior with the key, otherwise `undefined`
myObject.behaviors.get(id)

// remove a behavior instance corresponding to this key
myObject.behaviors.remove(id) // `.destroy` is called if a behavior instance is removed

// now be creative!
// create other behaviors...
myObject.behaviors.set('fire ball', behaviorSpell, { damage: 300 })
myObject.behaviors.set('frost nova', behaviorSpell, { damage: 100, slow: 0.4 })
myObject.behaviors.set('buff:invulnerability', behaviorImortal, { duration: 15 })
myObject.behaviors.set('weapon:main-hand', behaviorWeapon, { type: 'sword', damage: 120 })

LIVE DEMO

List of Behaviors

BehaviorDescription
Bulletmoves an game object forwards at an angle
Collision Handlerhandles collisions

License

MIT

Downloads/wk

2

GitHub Stars

34

LAST COMMIT

5yrs ago

MAINTAINERS

1

CONTRIBUTORS

2

OPEN ISSUES

0

OPEN PRs

0
VersionTagPublished
1.1.0
latest
5yrs ago
No alternatives found
No tutorials found
Add a tutorial

Rate & Review

100
No reviews found
Be the first to rate