ECS Entity
An entity is a lightweight container for Components. In IWSDK, entities may also carry a Three.js object3D.
const e = world.createEntity();
const t = world.createTransformEntity(); // adds an Object3D and a Transform component
Parenting options:
const child = world.createTransformEntity(undefined, { parent: t });
const persistent = world.createTransformEntity(undefined, { persistent: true }); // parented under scene
import { Mesh, BoxGeometry, MeshStandardMaterial } from '@iwsdk/core';
const geometry = new BoxGeometry(1, 1, 1);
const material = new MeshStandardMaterial({ color: 0x00ff00 });
const mesh = new Mesh(geometry, material);
const entity = world.createTransformEntity(mesh);
createTransformEntity is the recommended way to attach Three.js objects to entities. It ensures object3D is properly managed and detached when the entity is released.
e.addComponent(Health, { current: 100 });
e.removeComponent(Health);
e.has(Health); // boolean
Reading and writing data (in systems)
const v = e.getValue(Health, 'current'); // number | undefined
e.setValue(Health, 'current', 75);
// For vector fields (Types.Vec3) use a typed view:
const pos = e.getVectorView(Transform, 'position'); // Float32Array
pos[1] += 1; // move up