# aranna

> Entity Component System

Latest version **0.0.1** (published 2014-07-05) · MITISC license · 0 weekly downloads

## Install

```sh
npm install aranna
pnpm add aranna
yarn add aranna
bun add aranna
```

## 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.0.1 |
| Published | 2014-07-05 |
| First published | 2014-07-05 |
| Weekly downloads | 0 |
| License | MITISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Daniele Polencic |
| Maintainers | danielepolencic |

## Links

- npm: https://www.npmjs.com/package/aranna
- npm.io page: https://npm.io/package/aranna

## Dependencies (1)

- [es6-shim](https://npm.io/package/es6-shim.md) ^0.13.0

## Recent versions

- 0.0.1 (latest) — 2014-07-05

## README

# Aranna [![Build Status](https://travis-ci.org/danielepolencic/aranna.svg)](https://travis-ci.org/danielepolencic/aranna)
Aranna is a lightweight dependency-free [Entity Component
System](http://en.wikipedia.org/wiki/Entity_component_system) written in
JavaScript inspired by [ces.js](https://github.com/qiao/ces.js).

## Basic Usage
Components are javascript classes with a required `name` property. Note that
each component should have a unique `name` property.

```javascript
function Position (x, y) {
  this.name = 'position';
  this.x = x;
  this.y = y;
}

function Velocity (x, y) {
  this.name = 'velocity';
  this.x = x;
  this.y = y;
};

function Health (maxHealth) {
  this.name = 'health';
  this.health = this.maxHealth = maxHealth;
};

Health.prototype.isDead: function () {
  return this.health <= 0;
};

Health.prototype.receiveDamage: function (damage) {
 this.health -= damage;
}
```

An entity is essentially a container of one or more components.

```javascript
var hero = entity();
hero.addComponent(new Position(0, 0));
hero.addComponent(new Velocity(0, 0));
hero.addComponent(new Health(100));
```

The system is responsible for updating the entities.

```javascript
function PhysicSystem () {};

PhysicSystem.prototype.update = function (world, dt) {
    var entities, position, velocity;

    entities = world.getEntities('position', 'velocity');

    entities.forEach(function (entity) {
      position = entity.getComponent('position');
      velocity = entity.getComponent('velocity');
      position.x += velocity.x * dt;
      position.y += velocity.y * dt;
    });
  }
};
```

The world is the container of all the entities and systems. Calling the `update`
method will sequentially update all the systems, in the order they were added.

```javascript
var world = World();

world.addEntity(hero);
// ... add other entities

world.addSystem(new PhysicSystem());
// ... add other systems

requestAnimationFrame(function () {
    world.update(/* arguments are passed to the systems */);
})
```

A system is notified when it is added or removed from the world:

```javascript
function MySystem () {}

MySystem.prototype.addedToWorld = function (world)  {
  // Code to handle being added to world.
};

MySystem.prototype.removedFromWorld = function (world) {
  // Code to handle being removed from world.
}
```

The world emits signals when entities are added or removed. You can listen for
specific entities and handle the signal accordingly:

```javascript
function MySystem () {}

MySystem.prototype.addedToWorld = function (world)  {

  world.onEntityAdded('position', 'velocity')(function (entity) {
    /*
      This function is called whenever an entity with both 'position' and
      'velocity' components is added to the world.
    */
  });

  world.onEntityRemoved('position', 'velocity')(function (entity) {
    /*
       This function is called whenever an entity with both 'position' and
       'velocity' components is removed from the world.
     */
  });

  world.onComponentAddedToEntity('position', 'velocity')(function (entity) {
    /*
       This function is called whenever a component is added to an entity; for
       example, when an entity with only 'position' has 'velocity' added to it.
     */
  });

  world.onComponentRemovedFromEntity('position', 'velocity')(function (entity) {
    /*
       This function is called whenever a component is removed to an entity; for
       example, when an entity with both 'position' and 'velocity' has
       'velocity' removed from it.
     */
  });

};
```

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