Giter Club home page Giter Club logo

phaser-behavior-plugin's Introduction

Phaser Behavior Plugin

Behavior System wrapper for Phaser games

js-standard-style

Install

NPM

npm install phaser-behavior-plugin --save

DOWNLOAD

Download Here

CDN

<script scr="//unpkg.com/phaser-behavior-plugin@latest/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

Behavior Description
Bullet moves an game object forwards at an angle
Collision Handler handles collisions
Platformer Implements the platform games style movement

License

MIT

phaser-behavior-plugin's People

Contributors

jamesskemp avatar luizbills avatar npmcdn-to-unpkg-bot 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

phaser-behavior-plugin's Issues

myObject

// add the behavior system to any object (e.g. a sprite)
behaviorPlugin.enable(myObject)

It seems myObject not defined

Please provide Demo

Can you please provide a demo link for the working of this plugin?

Also behavior list is pending in the docs from long time ..

Another syntax

Any.behaviors.add :: (behavior, id)

// without id
entity.behaviors.add(Bullet)
var bullet = entity.behaviors.get(Bullet)

// with id
entity.behaviors.add(Spell, 'fireball')
var fireball = entity.behaviors.get(Spell, 'fireball')
var fireball = entity.behaviors.get(Spell) // => error

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.