Plugable Entity Component System for Games.
behaviour.js is a Unity-like Entity Component System that you can plug on any JavaScript library or engine.
Examples:
Attach the entity component system into your target library's base class. Then you'll be able to attach custom behaviours directly into your objects.
import { createComponentSystem, Behaviour } from 'behaviour.js'
var componentSystem = createComponentSystem(THREE.Object3D, 'behave')
// Define your custom behaviour
class CustomBehaviour extends Behaviour {
onAttach () {
console.log("CustomBehaviour has been attached into ", this.object)
}
update() {
console.log("Let's do something with this object", this.object)
this.object.rotation += 0.1
}
}
// Attach the behaviour into your objects
var object = new THREE.Object3D()
object.behave(new CustomBehaviour())
function animate() {
componentSystem.update()
// render your application
// ...
}
Behaviours and Entities have emit
/on
/once
/off
methods. They subclass
EventEmitters.
A handy way to communicate between behaviours is listening to entity
events in
Behaviour's onAttach
callback.
Properties
-
entity
- Entity instance -
object
- Object which this behaviour was attached
Callbacks
-
onAttach
- called after being attached in a target object -
onDestroy
- called after being manually destroyed (throughdestroy
method) -
update
- called when your component system is updated
Methods
destroy
MIT