vdkuipb-ecs

Ecs is a simple Typescript library of a bare ecs implementation.

Usage no npm install needed!

<script type="module">
  import vdkuipbEcs from 'https://cdn.skypack.dev/vdkuipb-ecs';
</script>

README

ecs

Ecs is a simple Typescript library of a bare ecs implementation.

Installation

Use the package manager npm to install ecs.

npm install vdkuipb-ecs

Usage

import { Entity, System } from "vdkuipb-ecs"

export class RotationComponent {
    angle = 0;
}


class RotatorSystem extends System {

    protected getRequiredComponents() {
        return [ RotationComponent ]
    }

    protected entityInstantiated(entity: Entity): void {
        let rotation: RotationComponent = entity.getComponent(RotationComponent);
        rotation.angle = 50;
    }

    protected entityUpdate(entity: Entity, delta?: number): void {
        let rotation: RotationComponent = entity.getComponent(RotationComponent);
        rotation.angle += 1;

        console.log(rotation.angle);

        if (rotation.angle >= 100) {
            this.remove(entity);
        }
    }

    protected entityDestroyed(entity: Entity): void {
        console.log("removed: " + entity.id);
    }
}

var entity = new Entity();
entity.addComponent(PositionComponent);
entity.addComponent(RotationComponent);

var system = new RotatorSystem();
system.add(entity);

let prev = Date.now();
setInterval(() => {
    system.update(prev - Date.now());
    prev = Date.now();
}, 1000 / 10);

A better example can be found in the example folder.

Documentation

PlantUML model

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Lisence

MIT