2019-12-14 23:11:00 +00:00
|
|
|
import { Bind, Game } from "../Applet/Init";
|
|
|
|
import { KeyControl } from "../Applet/Keyboard";
|
|
|
|
import { Loop } from "../Applet/Loop";
|
2020-02-16 04:38:54 +00:00
|
|
|
import { DrawSet, Layer } from "../Applet/Render";
|
2019-12-14 23:11:00 +00:00
|
|
|
import { FindCollisions } from "./Collision";
|
2020-02-16 04:38:54 +00:00
|
|
|
import { CollisionClass, Data, Location, Polygon, RenderBounds } from "./Components";
|
|
|
|
import { Create, Join, Liveness, Lookup, Remove, SparseStore, Store } from "./Data";
|
2019-12-14 23:11:00 +00:00
|
|
|
import { DumbMotion } from "./Location";
|
|
|
|
import { RunRenderBounds } from "./Renderers";
|
|
|
|
|
2020-01-16 03:49:52 +00:00
|
|
|
interface Apple {}
|
|
|
|
interface Banana {
|
2019-12-14 23:11:00 +00:00
|
|
|
peeled: boolean
|
|
|
|
}
|
2020-01-16 03:49:52 +00:00
|
|
|
interface Carrot {
|
2019-12-14 23:11:00 +00:00
|
|
|
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},
|
|
|
|
];
|
2020-01-16 03:49:52 +00:00
|
|
|
apple: Store<Apple> = [
|
2019-12-14 23:11:00 +00:00
|
|
|
{generation: 5},
|
|
|
|
{generation: 5},
|
|
|
|
{generation: -1},
|
|
|
|
{generation: -1},
|
|
|
|
{generation: 5},
|
|
|
|
{generation: 5},
|
|
|
|
];
|
2020-01-16 03:49:52 +00:00
|
|
|
banana: SparseStore<Banana> = {
|
2019-12-14 23:11:00 +00:00
|
|
|
3: {generation: 5, peeled: false},
|
|
|
|
4: {generation: 5, peeled: true},
|
|
|
|
};
|
2020-01-16 03:49:52 +00:00
|
|
|
carrot: SparseStore<Carrot> = {
|
2019-12-14 23:11:00 +00:00
|
|
|
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();
|
2020-01-16 06:54:43 +00:00
|
|
|
const applesMaybeCarrots = Join(data, "apple", "id").map(([apple, id]) => ({
|
2019-12-14 23:11:00 +00:00
|
|
|
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();
|
2020-01-16 06:54:43 +00:00
|
|
|
const beforeDelete = Join(data, "apple", "carrot", "id",);
|
2019-12-14 23:11:00 +00:00
|
|
|
Remove(data, [0, 5]);
|
2020-01-16 06:54:43 +00:00
|
|
|
const afterDelete = Join(data, "apple", "carrot", "id");
|
2019-12-14 23:11:00 +00:00
|
|
|
pre.innerText = JSON.stringify({
|
|
|
|
beforeDelete,
|
|
|
|
afterDelete
|
|
|
|
}, null, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bind("#EcsCreateTest")
|
|
|
|
export class EcsCreateTest {
|
|
|
|
constructor(pre: HTMLElement) {
|
|
|
|
const data = new TestData();
|
2020-01-16 06:54:43 +00:00
|
|
|
const beforeCreate = Join(data, "apple", "banana", "carrot", "id");
|
2019-12-14 23:11:00 +00:00
|
|
|
const createdId = Create(data, {
|
|
|
|
apple: {},
|
|
|
|
banana: {peeled: false},
|
|
|
|
carrot: {cronch: 11}
|
|
|
|
});
|
2020-01-16 06:54:43 +00:00
|
|
|
const afterCreate = Join(data, "apple", "banana", "carrot", "id");
|
2019-12-14 23:11:00 +00:00
|
|
|
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 drawSet = new DrawSet();
|
|
|
|
|
2020-01-19 06:24:29 +00:00
|
|
|
// spinner box
|
|
|
|
Create(this.data, {
|
2019-12-14 23:11:00 +00:00
|
|
|
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"),
|
2020-02-16 04:38:54 +00:00
|
|
|
renderBounds: new RenderBounds("#0a0", 0),
|
2019-12-14 23:11:00 +00:00
|
|
|
});
|
|
|
|
|
2020-01-19 06:24:29 +00:00
|
|
|
// triangles
|
|
|
|
[0, 1, 2, 3, 4, 5].forEach(angle => Create(this.data, {
|
2019-12-14 23:11:00 +00:00
|
|
|
location: new Location({
|
|
|
|
X: 200,
|
|
|
|
Y: 200,
|
2020-01-19 06:24:29 +00:00
|
|
|
Angle: angle,
|
2019-12-14 23:11:00 +00:00
|
|
|
VAngle: -Math.PI/10
|
|
|
|
}),
|
|
|
|
bounds: new Polygon([70, 0, 55, 40, 85, 40]),
|
|
|
|
collisionSourceClass: new CollisionClass("tri"),
|
|
|
|
renderBounds: new RenderBounds(
|
|
|
|
"#d40",
|
2020-02-16 04:38:54 +00:00
|
|
|
0
|
2019-12-14 23:11:00 +00:00
|
|
|
)
|
2020-01-19 06:24:29 +00:00
|
|
|
}));
|
2019-12-14 23:11:00 +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";
|
|
|
|
}
|
|
|
|
});
|
2019-12-14 23:11:00 +00:00
|
|
|
|
2020-01-19 06:24:29 +00:00
|
|
|
FindCollisions(this.data, 500, (className, sourceId, _targetId) => {
|
2019-12-14 23:11:00 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|