Skip to content

Commit 29c6d0e

Browse files
committed
split code + added tests
1 parent 4ba104b commit 29c6d0e

File tree

10 files changed

+678
-305
lines changed

10 files changed

+678
-305
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
"@typescript-eslint/explicit-module-boundary-types": "off",
1313
"@typescript-eslint/no-var-requires": "off",
1414
"@typescript-eslint/no-inferrable-types": "off",
15+
"@typescript-eslint/no-misused-new": "off",
1516
"no-debugger": "off",
1617
"no-fallthrough": "off",
1718
"no-constant-condition": "off",

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
**μECS** is an ECS library.
22

33
With the ECS paradigm, you can decouple your game's state from its logic, among other benefits,
4-
such as *extremely* fast performance. Most games, from ones built in the Unity or Unreal engines, all the way
5-
to custom-engine AAA titles, utilize some variation of the ECS paradigm.
4+
such as *extremely* fast performance. Most games, from ones built in the Unity or Unreal engines,
5+
all the way to custom-engine AAA titles, utilize some variation of the ECS paradigm.
66

77
This library's main goals are to be lightweight and user-friendly, but most importantly, to be fast.

bench/ecs.bench.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
2+
import { World } from "../src";
3+
import Benchmark from "benchmark";
4+
5+
class PositionComponent {
6+
x = 0.0;
7+
y = 0.0;
8+
}
9+
10+
class DirectionComponent {
11+
x = 0.0;
12+
y = 0.0;
13+
}
14+
15+
class ComflabulationComponent {
16+
thingy = 0.0
17+
dingy = 0
18+
mingy = false
19+
stringy = ""
20+
}
21+
22+
function MovementSystem(world: World, dt: number) {
23+
world.view(PositionComponent, DirectionComponent).each((entity, position, direction) => {
24+
position.x += direction.x * dt;
25+
position.y += direction.y * dt;
26+
});
27+
}
28+
29+
function ComflabSystem(world: World) {
30+
world.view(ComflabulationComponent).each((entity, comflab) => {
31+
comflab.thingy *= 1.00001;
32+
comflab.mingy = !comflab.mingy;
33+
comflab.dingy++;
34+
comflab.stringy = comflab.dingy.toString();
35+
});
36+
}
37+
38+
function num(min: number, max: number) {
39+
return min + 0.5 * (max - min);
40+
}
41+
42+
function MoreComplexSystem(world: World) {
43+
world.view(PositionComponent, DirectionComponent, ComflabulationComponent)
44+
.each((entity, position, direction, comflab) => {
45+
const nums: number[] = [];
46+
for (let i = 0; i < comflab.dingy && i < 100; i++) {
47+
nums.push(i * comflab.thingy);
48+
}
49+
50+
const sum = nums.reduce((acc, val) => acc + val);
51+
const product = nums.reduce((acc, val) => acc + val * 2);
52+
53+
comflab.stringy = comflab.dingy.toString();
54+
55+
if (comflab.dingy % 10000 == 0) {
56+
if (position.x > position.y) {
57+
direction.x = num(0, 5);
58+
direction.y = num(0, 10);
59+
} else {
60+
direction.x = num(0, 10);
61+
direction.y = num(0, 5);
62+
}
63+
}
64+
});
65+
}
66+
67+
function Update(world: World) {
68+
MovementSystem(world, 1);
69+
ComflabSystem(world);
70+
MoreComplexSystem(world);
71+
}
72+
73+
function Init() {
74+
const world = new World;
75+
for (let i = 0; i < 100; ++i) {
76+
const entity = world.create(new PositionComponent, new DirectionComponent);
77+
if (i % 3 === 0) world.emplace(entity, new ComflabulationComponent);
78+
}
79+
return world;
80+
}
81+
82+
const suite = new Benchmark.Suite;
83+
84+
interface Context {
85+
[key: string]: any;
86+
}
87+
88+
function bench<T extends Context>(
89+
name: string,
90+
setup: () => T,
91+
code: (context: T) => void
92+
) {
93+
suite.add(name, code.bind(undefined, setup()));
94+
}
95+
96+
bench(
97+
"big iteration test",
98+
function () {
99+
return {
100+
world: Init()
101+
}
102+
},
103+
function (context) {
104+
Update(context.world);
105+
}
106+
)

package-lock.json

Lines changed: 60 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"browser": "dist/index.umd.js",
2424
"types": "dist/index.d.ts",
2525
"files": [
26+
"/dist",
2627
"LICENSE",
2728
"README.md"
2829
],
@@ -36,16 +37,18 @@
3637
"@rollup/plugin-commonjs": "^15.1.0",
3738
"@rollup/plugin-json": "^4.1.0",
3839
"@rollup/plugin-node-resolve": "^9.0.0",
40+
"@types/benchmark": "^2.1.0",
3941
"@types/jest": "^26.0.20",
4042
"@typescript-eslint/eslint-plugin": "^4.14.1",
4143
"@typescript-eslint/parser": "^4.14.1",
44+
"benchmark": "^2.1.4",
4245
"eslint": "^7.18.0",
4346
"jest": "^26.6.3",
44-
"node-fetch": "^2.6.1",
4547
"rollup": "^2.38.0",
4648
"rollup-plugin-terser": "^7.0.2",
4749
"rollup-plugin-typescript2": "^0.27.3",
4850
"ts-jest": "^26.4.4",
51+
"ts-node": "^9.1.1",
4952
"tslib": "^2.1.0",
5053
"typedoc": "^0.20.19",
5154
"typescript": "^4.1.3"

0 commit comments

Comments
 (0)