Skip to content

Commit

Permalink
Add rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
sondresj committed Oct 9, 2021
1 parent 7a3bb7c commit 4f4f869
Show file tree
Hide file tree
Showing 17 changed files with 218 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"rules": {
"semi": ["error", "never"],
"@typescript-eslint/no-empty-function": "warn", // temporary lower from error to warn
"@typescript-eslint/no-empty-function": "warn",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-unused-vars": "off",
"function-paren-newline": "off"
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
.eslintcache
lib
build
dist
node_modules
.reports
*.log
*.cpuprofile
.rollup.cache
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Most of the concepts used in this ECS implementation is not new, but I couldn't
- Virtually no garbage collecting
- Prefabrication of archetypes
- Defering: Defer whatever you want to the end of an update cycle
- Events: Coming soon (tm)
- DX:
- Implement in Typescript
- Helpful error messages
Expand Down
33 changes: 33 additions & 0 deletions packages/piecs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# piecs
PIECS is an entity component system with some batteries included, some not.

This project is very much a work in progress, everything is subject to change.

You propably know what an ECS is, if not: https://en.wikipedia.org/wiki/Entity_component_system

Most of the concepts used in this ECS implementation is not new, but I couldn't find an ecs with the feature set, DX and performance I wanted, so here's my attempt at an ECS interpretation and implementation.

## Noteable Features

- Virtually no garbage collecting
- Prefabrication of archetypes
- Defering: Defer whatever you want to the end of an update cycle
- DX:
- Implement in Typescript
- Helpful error messages
- Minimal, easy to understand api
- Register systems: No need to invoke them explicitly
- Add systems/queries/prefabs/components whenever you want. The only requirement is that the world is initialized once before the first update.
- BYO (Bring Your Own) Component sets: Might add an optional component manager/store in the future, but you may keep track of component values however you choose. However, there's propably no clean abstraction to component sets that doesn't tank the performance in javascript and serves all use-cases.

## Api

TODO

### World.initialize()

`world.initialize(): void`

Initialize must be done once before the first update. Safe to call multiple times, but does nothing if the world is already initialized.
Initialize serves only one purpose: to ensure unique archetypes in each query by only testing each archetype vs each query once.
This enables a less complex and faster query system.
23 changes: 13 additions & 10 deletions packages/piecs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
"name": "piecs",
"version": "0.1.0",
"description": "PIECS is an entity component system with some batteries included",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"lib"
"dist"
],
"repository": "[email protected]:sondresj/piecs.git",
"author": "Sondre <[email protected]>",
"license": "MIT",
"private": true,
"private": false,
"keywords": [
"ecs"
],
"scripts": {
"clean": "rm -rf lib",
"clean": "rm -rf dist build .reports",
"test": "jest",
"build": "tsc",
"bench": "yarn build && cross-env NODE_ENV=production node performance/bench.mjs >> result.log",
"build": "tsc -p tsconfig.build.json && tsc -p tsconfig.declaration.json && rollup -c",
"bench": "yarn build && cross-env NODE_ENV=production node performance/bench.mjs >> result.log",
"profile": "yarn build && cross-env NODE_ENV=production node --cpu-prof --trace-deopt performance/bench.mjs >> result.log",
"prepare": "yarn clean && yarn build",
"prepublishOnly": "yarn test --no-color"
Expand All @@ -27,9 +29,6 @@
"preset": "ts-jest",
"testEnvironment": "node",
"coverageDirectory": ".reports",
"transformIgnorePatterns": [
"/node_modules/(?!asm-piecs)"
],
"testMatch": [
"**/__tests__/**/*.ts"
],
Expand All @@ -52,8 +51,12 @@
"eslint-plugin-import": "^2.24.2",
"jest": "^27.1.0",
"jest-junit": "^12.2.0",
"rollup": "^2.58.0",
"rollup-plugin-dts": "^4.0.0",
"rollup-plugin-terser": "^7.0.2",
"ts-jest": "^27.0.5",
"ts-node": "^10.2.1",
"tslib": "^2.3.1",
"typescript": "^4.4.2"
}
}
3 changes: 1 addition & 2 deletions packages/piecs/performance/add_remove.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { World } from '../lib/World.js'
import { prefab, query } from '../lib/Query.js'
import { World, prefab, query } from '../dist/index.mjs'

export default function createAddRemove(count) {
const world = new World()
Expand Down
3 changes: 1 addition & 2 deletions packages/piecs/performance/entity_cycle.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { World } from '../lib/World.js'
import { prefab, query } from '../lib/Query.js'
import { World, prefab, query } from '../dist/index.mjs'

export default function createEntityCycle(count) {
const world = new World()
Expand Down
3 changes: 1 addition & 2 deletions packages/piecs/performance/frag_iter.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { World } from '../lib/World.js'
import { all, query } from '../lib/Query.js'
import { World, all, query } from '../dist/index.mjs'

export default function createFragIter(count) {
const world = new World()
Expand Down
3 changes: 1 addition & 2 deletions packages/piecs/performance/packed_1.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { World } from '../lib/World.js'
import { prefab, query } from '../lib/Query.js'
import { World, prefab, query } from '../dist/index.mjs'

export default function createPacked1(count) {
const world = new World()
Expand Down
17 changes: 8 additions & 9 deletions packages/piecs/performance/packed_5.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { World } from '../lib/World.js'
import { all, query } from '../lib/Query.js'
import { World, prefab, query } from '../dist/index.mjs'

export default function createPacked5(count) {
const world = new World()
Expand All @@ -25,44 +24,44 @@ export default function createPacked5(count) {
arr: new Uint32Array(count).fill(1)
}

const prefab = world.prefabricate([A.id, B.id, C.id, D.id, E.id])
const p = world.prefabricate([A.id, B.id, C.id, D.id, E.id])

world
.registerSystem(function systemAp5(entities) {
const arr = A.arr
for (let i = 0, l = entities.length; i < l; i++) {
arr[entities[i]] *= 2
}
}, query(all(A.id, E.id)))
}, query(prefab(p)))
.registerSystem(function systemBp5(entities) {
const arr = B.arr
for (let i = 0, l = entities.length; i < l; i++) {
arr[entities[i]] *= 2
}
}, query(all(B.id, E.id)))
}, query(prefab(p)))
.registerSystem(function systemCp5(entities) {
const arr = C.arr
for (let i = 0, l = entities.length; i < l; i++) {
arr[entities[i]] *= 2
}
}, query(all(C.id, E.id)))
}, query(prefab(p)))
.registerSystem(function systemDp5(entities) {
const arr = D.arr
for (let i = 0, l = entities.length; i < l; i++) {
arr[entities[i]] *= 2
}
}, query(all(D.id, E.id)))
}, query(prefab(p)))
.registerSystem(function systemEp5(entities) {
const arr = E.arr
for (let i = 0, l = entities.length; i < l; i++) {
arr[entities[i]] *= 2
}
}, query(all(E.id)))
}, query(prefab(p)))
.initialize()

for (let i = 0; i < count; i++) {
const entity = world.createEntity()
world.transformEntity(entity, prefab)
world.transformEntity(entity, p)
}

return function packed5() {
Expand Down
3 changes: 1 addition & 2 deletions packages/piecs/performance/simple_iter.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { World } from '../lib/World.js'
import { prefab, query } from '../lib/Query.js'
import { World, prefab, query } from '../dist/index.mjs'

export default function createSimpleIter(count) {
const world = new World()
Expand Down
27 changes: 27 additions & 0 deletions packages/piecs/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { terser } from 'rollup-plugin-terser'
import dts from 'rollup-plugin-dts'

const outDir = 'dist'

export default [{
input: 'build/index.js',
output: [{
file: `${outDir}/index.mjs`,
format: 'esm',
}, {
file: `${outDir}/index.cjs`,
format: 'cjs',
generatedCode: { consBindings: true }
}, {
file: `${outDir}/index.min.js`,
format: 'es',
plugins: [terser()]
}]
}, {
input: 'build/index.d.ts',
output: {
file: `${outDir}/index.d.ts`,
format: 'es'
},
plugins: [dts()]
}]
9 changes: 3 additions & 6 deletions packages/piecs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { World } from './World'

export type { Query } from './Query'
export { and, or, not, any, all, prefab, query } from './Query'
export type { WorldStatistics } from './utils'
export { getStatistics } from './utils'
export type { Archetype } from './Archetype'
export type { System } from './types'
export { World }
export default World
export { World } from './World'
export { and, or, not, any, all, prefab, query } from './Query'
export { getStatistics } from './utils'
9 changes: 9 additions & 0 deletions packages/piecs/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"incremental": false,
"declaration": false,
"removeComments": true,
"sourceMap": false
}
}
10 changes: 10 additions & 0 deletions packages/piecs/tsconfig.declaration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"incremental": false,
"declaration": true,
"emitDeclarationOnly": true,
"removeComments": false,
"sourceMap": false
}
}
8 changes: 4 additions & 4 deletions packages/piecs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* Basic Options */
"incremental": true, /* Enable incremental compilation */
"target": "ESNEXT", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"module": "ESNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"lib": ["ESNext"], /* Specify library files to be included in the compilation. */
"allowJs": false, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
Expand All @@ -12,11 +12,11 @@
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./lib", /* Redirect output structure to the directory. */
"outDir": "./build", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
"removeComments": true, /* Do not emit comments to output. */
"removeComments": false, /* Do not emit comments to output. */
"noEmit": false, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
"downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
Expand Down Expand Up @@ -66,5 +66,5 @@
"noUncheckedIndexedAccess": true
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__", "lib"]
"exclude": ["node_modules", "**/__tests__", "lib", "build"]
}
Loading

0 comments on commit 4f4f869

Please sign in to comment.