Skip to content

Commit

Permalink
refactor(ecs): update mem-mapped component type handling
Browse files Browse the repository at this point in the history
BREAKING CHANGE: component buffer data type use string consts

- part of unified umbrella-wide changes to thi.ng/api Type alias
  (see a333d41)
  • Loading branch information
postspectacular committed Feb 2, 2021
1 parent 274dadf commit 3207200
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/ecs/src/ecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
INotify,
INotifyMixin,
Listener,
uintType,
uintTypeForSize,
} from "@thi.ng/api";
import { bitSize } from "@thi.ng/binary";
import { isArray, isString } from "@thi.ng/checks";
Expand Down Expand Up @@ -80,7 +80,7 @@ export class ECS<SPEC> implements INotify {
`component '${opts.id}' already existing`
);
const cap = this.idgen.capacity;
const utype = uintType(cap);
const utype = uintTypeForSize(cap);
const sparse = this.pool.mallocAs(utype, cap);
const dense = this.pool.mallocAs(utype, cap);
if (!(sparse && dense)) return;
Expand Down
15 changes: 7 additions & 8 deletions packages/ecs/test/component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Type } from "@thi.ng/api";
import { equiv } from "@thi.ng/equiv";
import * as assert from "assert";
import { ECS, MemMappedComponent } from "../src";
Expand All @@ -9,7 +8,7 @@ describe("component", () => {
beforeEach(() => (ecs = new ECS({ capacity: 16 })));

it("defComponent (minimal)", () => {
const a = ecs.defComponent({ id: "a", type: Type.F32 });
const a = ecs.defComponent({ id: "a", type: "f32" });
assert(a instanceof MemMappedComponent);
assert(a.dense instanceof Uint8Array);
assert(a.sparse instanceof Uint8Array);
Expand All @@ -22,7 +21,7 @@ describe("component", () => {
});

it("defComponent (w/ type)", () => {
const a = ecs.defComponent({ id: "a", type: Type.U8 })!;
const a = ecs.defComponent({ id: "a", type: "u8" })!;
assert(a.vals instanceof Uint8Array);
assert.strictEqual(a.dense.length, ecs.idgen.capacity);
assert.strictEqual(a.sparse.length, ecs.idgen.capacity);
Expand All @@ -32,14 +31,14 @@ describe("component", () => {
});

it("defComponent (w/ size)", () => {
const a = ecs.defComponent({ id: "a", type: Type.F32, size: 2 })!;
const a = ecs.defComponent({ id: "a", type: "f32", size: 2 })!;
assert(a.vals instanceof Float32Array);
assert.strictEqual(a.vals.length, ecs.idgen.capacity * 2);
assert.strictEqual(a.size, 2);
assert.strictEqual(a.stride, 2);
const b = ecs.defComponent({
id: "b",
type: Type.F32,
type: "f32",
size: 3,
stride: 4,
})!;
Expand All @@ -51,7 +50,7 @@ describe("component", () => {
it("add (w/ default val)", () => {
const a = ecs.defComponent({
id: "a",
type: Type.F32,
type: "f32",
size: 2,
default: [1, 2],
})!;
Expand All @@ -67,7 +66,7 @@ describe("component", () => {
it("values / packedValues", () => {
const a = ecs.defComponent({
id: "a",
type: Type.F32,
type: "f32",
size: 2,
default: [1, 2],
})!;
Expand All @@ -88,7 +87,7 @@ describe("component", () => {
it("resize", () => {
const a = ecs.defComponent({
id: "a",
type: Type.F32,
type: "f32",
size: 2,
default: [1, 2],
})!;
Expand Down
2 changes: 1 addition & 1 deletion packages/ecs/test/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("component", () => {

it("group", () => {
const a = ecs.defComponent({ id: "a", default: () => "a" })!;
const b = ecs.defComponent({ id: "b", type: 7, size: 2 })!;
const b = ecs.defComponent({ id: "b", type: "f32", size: 2 })!;
const g = ecs.defGroup([a, b]);
ecs.defEntity(["a", "b"]);
ecs.defEntity({ a: "aa", b: [1, 2] });
Expand Down

0 comments on commit 3207200

Please sign in to comment.