Giter Site home page Giter Site logo

fowl's Introduction

fowl

A truly lightweight Javascript Entity Component System

Usage

Components

Components are contructors that create objects of related data. The method fowl.registerComponents(...) has to be called once before instantiating components.

// A constructor for the Position component
var Position = function(x, y) {
	this.x = x;
	this.y = y;
};
fowl.registerComponents(Position); // Register the Position component for use with fowl

Entities

Creating entities

An EntityManager is necessary for dealing with entities. EntityManager.createEntity() will create an entity with zero components.

var em = new fowl.EntityManager();
var entity = em.createEntity();

Assigning components to entities

To add a component to an entity call EntityManager.addComponent(entity, component) with the entity and the component as arguments. Likewise does EntityManager.removeComponent(entity, component) remove a component.

em.addComponent(entity, new Position(10, 0));

Querying entities and their components

To iterate over the entities with e.g. the Position component you must first create a mask for the component types with EntityManager.getMask(componentTypes). Attn. You should allocate your masks at initialization only since they are costly to create.

var movementSystemMask = em.getMask([Position]);

Then, iterate over all entities and check whether they match the mask:

for (var entity = 0, length = em.count; entity < length; ++entity) {
	if (em.matches(entity, movementSystemMask)) {
		// Do stuff with `entity`
	}
}

To retrieve a component associated with an entity use EntityManager.getComponent(entity, component).

var position = em.getComponent(entity, Position);

Removing all entities

EntityManager.clear() removes all entities in play.

fowl's People

Contributors

axelf4 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.