Core part of ash.ts - a Typescript port of Ash Framework, an entity framework for game development by Richard Lord
npm i @ash.ts/core
TypeDoc generated API docs
As this is a port to a different language there are some changes to the API.
Javascript which is a language that typescript is compiling to, is dynamic. Variables that are declared but not yet instantiated doesn't have a type. In AS3 when Node fields are null but they are declared as some type, that information is kept at runtime. Adding typescript to javascript gave us code completion and type checking, but information about type is dropped as soon as code is compiled to javascript and not available at runtime. To keep that information you have to define static property propTypes on a Node. Example:
import { Node, keep } from '@ash.ts/ash';
import { Motion, Position } from '../components';
export class MovementNode extends Node {
public position!:Position;
public motion!:Motion;
public static propTypes = {
position: Position,
motion: Motion,
};
}
There are also 2 helpers that simplifying this process:
- @keep that works as a decorator
- defineNode which is a Node class factory
Both of these tools and their documentation are available in the @ash.ts/tools package.
Base System class is declared as abstract class with these methods marked as required to be declared in an inherited class.
public abstract addToEngine(engine:Engine):void;
public abstract removeFromEngine(engine:Engine):void;
public abstract update(time:number):void;
Example usage:
import { Engine, NodeList, System } from '@ash.ts/ash';
import { RenderNode } from '../nodes';
export class RenderSystem extends System {
private nodes:NodeList<RenderNode> | null = null;
constructor(public container:HTMLElement) {
super();
}
public addToEngine(engine:Engine):void {
this.nodes = engine.getNodeList(RenderNode);
// some more logic
}
public update(time:number):void {
for (let node = this.nodes!.head; node; node = node.next) {
// update logic
}
}
public removeFromEngine(engine:Engine):void {
this.nodes = null;
// some more logic
}
}