# behavior.js

> Plugable Entity Component System for Games

Latest version **0.1.0** (published 2015-12-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install behavior.js
pnpm add behavior.js
yarn add behavior.js
bun add behavior.js
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2015-12-22 |
| First published | 2015-12-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 27 |
| Author | Endel Dreyer |
| Maintainers | endel |
| Keywords | entity, component, system, ecs, behaviour |

## Links

- npm: https://www.npmjs.com/package/behavior.js
- Repository: https://github.com/gamestdio/behaviour.js
- Homepage: https://github.com/gamestdio/behaviour.js#readme
- Issues: https://github.com/gamestdio/behaviour.js/issues
- npm.io page: https://npm.io/package/behavior.js

## Dependencies (1)

- [tiny-emitter](https://npm.io/package/tiny-emitter.md) ^1.0.2

## Recent versions

- 0.1.0 (latest) — 2015-12-22

## README

behaviour.js
===

[![Build Status](https://secure.travis-ci.org/gamestdio/behaviour.js.png?branch=master)](http://travis-ci.org/gamestdio/behaviour.js)

Plugable [Entity Component System](https://en.wikipedia.org/wiki/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**:

- [Plugged into THREE.js](http://gamestdio.github.io/behaviour.js/three.html)
- [Plugged into PIXI.js](http://gamestdio.github.io/behaviour.js/pixi.html)

Usage
---

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.

```javascript
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
  // ...
}
```

API
---

Behaviours and Entities have `emit`/`on`/`once`/`off` methods. They subclass
[EventEmitters](https://github.com/scottcorgan/tiny-emitter#instance-methods).
A handy way to communicate between behaviours is listening to `entity` events in
Behaviour's `onAttach` callback.

### Behaviour

**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 (through `destroy` method)*
- `update` - *called when your component system is updated*

**Methods**

- `destroy`

License
---

MIT

---
_Source: https://npm.io/package/behavior.js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
