Git Product home page Git Product logo

ecsengine's Introduction

THIS LIBRARY IS CURRENTLY IN ACTIVE DEVELOPMENET, THINGS WILL SIGNIFICANTLY CHANGE IN NEAREST FUTURE!

ecsengine

Simple Entity-Component-System (ECS) engine for JavaScript written in TypeScript.

Install

npm install ecsengine --save

Usage

  1. Create an instance of engine
import { Engine } from 'ecsengine'
const engine = new Engine()
  1. Define your components. The components are just to store data. It does not have to contain any logic. According to ECS paradigm - you store all your logic in System's code.
class PositionComponent extends Component{
  x: number = 0
  y: number = 0
  z: number = 0
}

class PhysicComponent extends Component{}

Component may be an empty class - just to point that entity with that component have some behavior.

  1. Define component group for system. Each system interested in work with entity that have some set of components.
class PhysicComponentGroup{
  position: PositionComponent = new PositionComponent()
  physic: PhysicComponent = new PhysicComponent()
}
  1. Define system and describe your logic there:
@componentsGroup(PhysicComponent)
class PhysicSystem extends System<PhysicComponentGroup>{
  execute(content: PhysicComponentGroup){
    content.position.x -= 9.8
  }
}

Also note that this code use decorators so you must have "experimentalDecorators": true, in your tsconfig.json

  1. Define class for your entity:
class GameObject extends Entity{
  constructor(){
    super()
    this.add(PhysicComponent, {})
    this.add(PositionComponent, {})
  }
}
  1. Add system and entity to engine and run it:
const gameObject = new GameObject()
engine.addSystem(PhysicSystem)
engine.addEntity(gameObject)

setInterval(()=>{
  engine.update()
  console.log(gameObject.components.get(PositionComponent))
}, 100)

ecsengine's People

Contributors

set001 avatar

Stargazers

Andrejs Agejevs avatar

Watchers

 avatar James Cloos 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.