base2020/src/Ecs/test.ts

181 lines
5.5 KiB
TypeScript
Raw Normal View History

import { Bind, Game } from "../Applet/Init";
import { KeyControl } from "../Applet/Keyboard";
import { Loop } from "../Applet/Loop";
import { Layer, DrawSet } from "../Applet/Render";
import { Data, Location, Polygon, RenderBounds, CollisionClass } from "./Components";
import { FindCollisions } from "./Collision";
import { Join, Liveness, Remove, Create, Lookup, Store, SparseStore } from "./Data";
import { DumbMotion } from "./Location";
import { RunRenderBounds } from "./Renderers";
interface Apple {}
interface Banana {
peeled: boolean
}
interface Carrot {
cronch: number
}
class TestData extends Data {
entity = [
{generation: 5, alive: Liveness.ALIVE},
{generation: 5, alive: Liveness.DEAD},
{generation: 5, alive: Liveness.ALIVE},
{generation: 5, alive: Liveness.ALIVE},
{generation: 5, alive: Liveness.INACTIVE},
{generation: 5, alive: Liveness.ALIVE},
];
apple: Store<Apple> = [
{generation: 5},
{generation: 5},
{generation: -1},
{generation: -1},
{generation: 5},
{generation: 5},
];
banana: SparseStore<Banana> = {
3: {generation: 5, peeled: false},
4: {generation: 5, peeled: true},
};
carrot: SparseStore<Carrot> = {
0: {generation: 5, cronch: 1},
1: {generation: 5, cronch: 1},
2: {generation: 4, cronch: 10},
3: {generation: 5, cronch: 1},
};
}
@Bind("#EcsJoinTest")
export class EcsJoinTest {
constructor(pre: HTMLElement) {
const data = new TestData();
pre.innerText = JSON.stringify({
"apples": Join(data, "apple"),
"bananas": Join(data, "banana"),
"carrots": Join(data, "carrot"),
"apples+carrots": Join(data, "apple", "carrot"),
}, null, 2);
}
}
@Bind("#EcsLookupTest")
export class EcsLookupTest {
constructor(pre: HTMLElement) {
const data = new TestData();
const applesMaybeCarrots = Join(data, "apple", "id").map(([apple, id]) => ({
apple,
maybeCarrot: Lookup(data, id, "carrot")[0]
}));
pre.innerText = JSON.stringify(applesMaybeCarrots, null, 2);
}
}
@Bind("#EcsRemoveTest")
export class EcsRemoveTest {
constructor(pre: HTMLElement) {
const data = new TestData();
const beforeDelete = Join(data, "apple", "carrot", "id",);
Remove(data, [0, 5]);
const afterDelete = Join(data, "apple", "carrot", "id");
pre.innerText = JSON.stringify({
beforeDelete,
afterDelete
}, null, 2);
}
}
@Bind("#EcsCreateTest")
export class EcsCreateTest {
constructor(pre: HTMLElement) {
const data = new TestData();
const beforeCreate = Join(data, "apple", "banana", "carrot", "id");
const createdId = Create(data, {
apple: {},
banana: {peeled: false},
carrot: {cronch: 11}
});
const afterCreate = Join(data, "apple", "banana", "carrot", "id");
pre.innerText = JSON.stringify({
beforeCreate,
afterCreate,
createdId
}, null, 2);
}
}
@Game("#RenderTest")
export class LoopTest {
data = new Data();
constructor(public canvas: HTMLCanvasElement, cx: CanvasRenderingContext2D, keys: KeyControl) {
const layer = new Layer(0);
const drawSet = new DrawSet();
2020-01-19 06:24:29 +00:00
// spinner box
Create(this.data, {
location: new Location({
X: 200,
Y: 200,
VAngle: Math.PI
}),
bounds: new Polygon([-50, 50, -60, 250, 60, 250, 50, 50]),
collisionTargetClass: new CollisionClass("block"),
renderBounds: new RenderBounds(
"#0a0",
layer
)
});
2020-01-19 06:24:29 +00:00
// triangles
[0, 1, 2, 3, 4, 5].forEach(angle => Create(this.data, {
location: new Location({
X: 200,
Y: 200,
2020-01-19 06:24:29 +00:00
Angle: angle,
VAngle: -Math.PI/10
}),
bounds: new Polygon([70, 0, 55, 40, 85, 40]),
collisionSourceClass: new CollisionClass("tri"),
renderBounds: new RenderBounds(
"#d40",
layer
)
2020-01-19 06:24:29 +00:00
}));
const loop = new Loop(30,
interval => {
DumbMotion(this.data, interval);
2020-01-19 06:24:29 +00:00
Join(this.data, "collisionSourceClass", "renderBounds").forEach(([collisionSourceClass, renderBounds]) => {
if(collisionSourceClass.name === "tri") {
renderBounds.color = "#d40";
}
});
2020-01-19 06:24:29 +00:00
FindCollisions(this.data, 500, (className, sourceId, _targetId) => {
switch(className) {
case "tri>block":
const [debug] = Lookup(this.data, sourceId, "renderBounds");
if(debug) debug.color = "#0ff";
break;
}
});
},
dt => {
cx.fillStyle = "#848";
cx.fillRect(0, 0, canvas.width, canvas.height);
RunRenderBounds(this.data, drawSet);
drawSet.draw(cx, dt);
}
);
loop.start();
keys.setHandler({
press: key => {
if(key == "a") loop.start();
else if(key == "b") loop.stop();
}
});
}
}