Skip to content

Commit

Permalink
Merge branch 'dev' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
daneren2005 committed Apr 10, 2024
2 parents 941f81a + 1eeb998 commit 94a8b14
Show file tree
Hide file tree
Showing 8 changed files with 1,047 additions and 1,258 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ https://daneren2005.github.io/ecs-sharedarraybuffer-playground/#/bitecs
## Custom ES backend by a shared memory pool
This is another version backed by @daneren2005/shared-memory-objects to allow allocating chunks of memory for each entity and then mapping entity properties to it. That library allows allocating/freeing memory backed by a SharedArrayBuffer and some simple data structures like an array backed by TypedArrays. We are only passing the heap (which wraps the SharedArrayBuffers) and the world's memory pointer to each thread and then each thread can iterate over the entities to run updates on them from that.

https://daneren2005.github.io/ecs-sharedarraybuffer-playground/#/shared-memory-objects

ie:
```
class Entity {
Expand Down
2,163 changes: 968 additions & 1,195 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,27 @@
"start": "npm run serve",
"build": "vite build",
"lint": "eslint src/ --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --ignore-path .gitignore",
"preview": "vite preview"
"preview": "vite preview",
"update": "ncu",
"update:commit": "ncu -u && npm install"
},
"dependencies": {
"@daneren2005/shared-memory-objects": "^0.0.2",
"@daneren2005/shared-memory-objects": "^0.0.3",
"bitecs": "^0.3.40",
"core-js": "^3.36.0",
"core-js": "^3.36.1",
"phaser": "^3.80.1",
"vue": "^3.4.21",
"vue-router": "^4.3.0"
},
"devDependencies": {
"@timohausmann/quadtree-ts": "^2.2.2",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"@typescript-eslint/eslint-plugin": "^7.6.0",
"@typescript-eslint/parser": "^7.6.0",
"@vitejs/plugin-vue": "^5.0.4",
"@vue/eslint-config-typescript": "^12.0.0",
"eslint": "^8.57.0",
"eslint-plugin-vue": "^9.22.0",
"typescript": "~5.3.3",
"vite": "^5.1.4"
"@vue/eslint-config-typescript": "^13.0.0",
"eslint": "^9.0.0",
"eslint-plugin-vue": "^9.24.1",
"typescript": "~5.4.4",
"vite": "^5.2.8"
}
}
4 changes: 2 additions & 2 deletions src/shared-memory-objects/SharedMemoryObjects.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, Ref } from 'vue';
import Phaser from 'phaser';
import World from './entities/world';
import generateScene from '@/data/generate-scene';
import Station from './entities/station';
import Ship from './entities/ship';
import MainWorld from './entities/main-world';
let world = new World();
let world = new MainWorld();
const startupTime = ref(0);
const minUpdateTime = ref(0);
const maxUpdateTime = ref(0);
Expand Down
60 changes: 60 additions & 0 deletions src/shared-memory-objects/entities/main-world.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import World from './world';
import System from '../systems/system';
import WorkerSystem from '../systems/worker-system';

import VelocitySystemWorker from '../systems/velocity-system?worker';
import UpdateHealthTimersSystemWorker from '../systems/update-health-timers-system?worker';
import SpawnShipSystemWorker from '../systems/spawn-ship-system?worker';
import CollisionSystemWorker from '../systems/collision-system?worker';
import TargetEnemySystemWorker from '../systems/target-enemy-system?worker';
import MoveToTargetSystem from '../systems/move-to-target-system?worker';

export default class MainWorld extends World {
systems: Array<System> = [];

constructor() {
super();

this.initSystems();
}

update(delta: number) {
this.systems.forEach(system => {
system.update(delta);
});

super.update(delta);
}

private initSystems() {
this.systems.push(new WorkerSystem(this, {
name: 'velocitySystem',
worker: new VelocitySystemWorker()
}));
this.systems.push(new WorkerSystem(this, {
name: 'updateHealthTimersSystemWorker',
worker: new UpdateHealthTimersSystemWorker()
}));
this.systems.push(new WorkerSystem(this, {
name: 'spawnShipSystem',
worker: new SpawnShipSystemWorker()
}));
this.systems.push(new WorkerSystem(this, {
name: 'collisionSystem',
worker: new CollisionSystemWorker()
}));
this.systems.push(new WorkerSystem(this, {
name: 'targetEnemySystem',
worker: new TargetEnemySystemWorker()
}));
this.systems.push(new WorkerSystem(this, {
name: 'moveToTargetSystem',
worker: new MoveToTargetSystem()
}));
}

destroy() {
this.systems.forEach(system => system.destroy());
this.systems = [];
}
}
48 changes: 0 additions & 48 deletions src/shared-memory-objects/entities/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ import Entity from './entity';
import Station from './station';
import { AllocatedMemory, MAX_BYTE_OFFSET_LENGTH, MemoryHeap, MemoryHeapMemory, SharedAllocatedMemory, SharedList, getPointer } from '@daneren2005/shared-memory-objects';
import EntityList from './entity-list';
import System from '../systems/system';
import WorkerSystem from '../systems/worker-system';

import VelocitySystemWorker from '../systems/velocity-system?worker';
import UpdateHealthTimersSystemWorker from '../systems/update-health-timers-system?worker';
import SpawnShipSystemWorker from '../systems/spawn-ship-system?worker';
import CollisionSystemWorker from '../systems/collision-system?worker';
import TargetEnemySystemWorker from '../systems/target-enemy-system?worker';
import MoveToTargetSystem from '../systems/move-to-target-system?worker';

import { ENTITY_TYPES } from './types';
import Ship from './ship';
Expand All @@ -28,7 +19,6 @@ export default class World {
width: number,
height: number
};
systems: Array<System> = [];

readonly heap: MemoryHeap;
protected readonly memory: AllocatedMemory;
Expand All @@ -54,8 +44,6 @@ export default class World {
bufferByteOffset: this.memory.data.byteOffset + ENTITIES_LIST_INDEX * this.memory.data.BYTES_PER_ELEMENT
}
});

this.initSystems();
}

let memory = this.memory;
Expand Down Expand Up @@ -131,10 +119,6 @@ export default class World {
}

update(delta: number) {
this.systems.forEach(system => {
system.update(delta);
});

this.garbageCollect();
}

Expand All @@ -147,33 +131,6 @@ export default class World {
});
}

private initSystems() {
this.systems.push(new WorkerSystem(this, {
name: 'velocitySystem',
worker: new VelocitySystemWorker()
}));
this.systems.push(new WorkerSystem(this, {
name: 'updateHealthTimersSystemWorker',
worker: new UpdateHealthTimersSystemWorker()
}));
this.systems.push(new WorkerSystem(this, {
name: 'spawnShipSystem',
worker: new SpawnShipSystemWorker()
}));
this.systems.push(new WorkerSystem(this, {
name: 'collisionSystem',
worker: new CollisionSystemWorker()
}));
this.systems.push(new WorkerSystem(this, {
name: 'targetEnemySystem',
worker: new TargetEnemySystemWorker()
}));
this.systems.push(new WorkerSystem(this, {
name: 'moveToTargetSystem',
worker: new MoveToTargetSystem()
}));
}

getId() {
return Atomics.add(this.memory.data, ID_INDEX, 1);
}
Expand All @@ -184,11 +141,6 @@ export default class World {
world: this.memory.getSharedMemory()
};
}

destroy() {
this.systems.forEach(system => system.destroy());
this.systems = [];
}
}

interface WorldMemory {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import World from '../entities/world';
import createWorkerSystem from './create-worker-system';

class VelocitySystem {
class UpdateHealthTimerSystem {
world: World;

constructor(world: World) {
Expand All @@ -23,4 +23,4 @@ class VelocitySystem {
}
}

createWorkerSystem((world: World) => new VelocitySystem(world));
createWorkerSystem((world: World) => new UpdateHealthTimerSystem(world));
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig, loadEnv } from 'vite';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { fileURLToPath, URL } from 'node:url';

Expand Down

0 comments on commit 94a8b14

Please sign in to comment.