https://github.com/set001/ecsengine
Simple entity component system (ECS) engine for JavaScript
https://github.com/set001/ecsengine
entity entity-component
Last synced: 10 months ago
JSON representation
Simple entity component system (ECS) engine for JavaScript
- Host: GitHub
- URL: https://github.com/set001/ecsengine
- Owner: SET001
- Created: 2018-06-26T21:58:40.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-07-09T21:56:55.000Z (over 7 years ago)
- Last Synced: 2025-04-18T09:45:25.632Z (11 months ago)
- Topics: entity, entity-component
- Language: TypeScript
- Size: 147 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
*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
```TypeScript
import { Engine } from 'ecsengine'
const engine = new Engine()
```
2. 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.
```TypeScript
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.
3. Define component group for system. Each system interested in work with entity that have some set of components.
```TypeScript
class PhysicComponentGroup{
position: PositionComponent = new PositionComponent()
physic: PhysicComponent = new PhysicComponent()
}
```
4. Define system and describe your logic there:
```TypeScript
@componentsGroup(PhysicComponent)
class PhysicSystem extends System{
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`
5. Define class for your entity:
```TypeScript
class GameObject extends Entity{
constructor(){
super()
this.add(PhysicComponent, {})
this.add(PositionComponent, {})
}
}
```
6. Add system and entity to engine and run it:
```TypeScript
const gameObject = new GameObject()
engine.addSystem(PhysicSystem)
engine.addEntity(gameObject)
setInterval(()=>{
engine.update()
console.log(gameObject.components.get(PositionComponent))
}, 100)
```