Skip to content

Repository files navigation

czpg-ecs

Entity Component System architecture in javascript

Usage

npm i --save czpg-ecs

Struct

Context// Top level container for ECSEntity// Entity objectComponent// Component objectSystem// System base class

Inject Component type

import{Component}from'czpg-ecs';classPosition{constructor(x,y){this.x=x;this.y=y;}}constpositionComId=Component.inject(Position);

Create Entity

import{Entity}from'czpg-ecs';letentity=newEntity();// add componentletpositionCom=entity.addComponent(positionComId,1,2);// or use contructor insteadletpositionCom=entity.addComponent(Position,1,2);// positionCom = { x: 1, y: 2 }// remove componententity.removeComponent(positionCom);// or use constructor insteadentity.removeComponent(Position);

Create System

import{System}from'czpg-ecs';classPositionSystemextendsSystem{constructor(){// priority is 10, enable is truesuper(10,true);}update(context){// get group of entities with Position componentletgroup=context.getGroup('Position');group.entities.forEach(entity=>{entity.com.Position.x+=1;entity.com.Position.y+=1;});}}letpositionSystem=newPositionSystem();

Create Context

import{Context}from'czpg-ecs'letcontext=newContext();// add entitycontext.addEntity(entity);// remove entitycontext.removeEntity(entity);// add systemconetxt.addSystem(positionSystem);// remove systemcontext.removeSystem(positionSystem);// run systemscontext.execute();

Run in local

// clone and move to directory
git clone https://github.com/PrincessGod/ecs.js.git
cd ecs.js
// install npm packages
npm i
// build
npm run build
// build minify version
npm run build-min
// develop
npm run dev
// lint
npm run lint
// test
npm test

About

Entity Component System architecture in javascript

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages