Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/schematics #26

Merged
merged 13 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules
.env
types
dist/
.parcel-cache
.env
.discarded
.todo.md
487 changes: 219 additions & 268 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
},
"keywords": [],
"dependencies": {
"@types/pako": "^2.0.3",
"alea": "^1.0.1",
"pako": "^2.1.0",
"poisson-disk-sampling": "^2.3.1",
"simplex-noise": "^4.0.3"
},
Expand Down
65 changes: 19 additions & 46 deletions src/api/WorldComputeProxy.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { Box2, Vector3 } from 'three'
import { Box2, Vector2 } from 'three'

import { Block, EntityData, PatchKey } from '../common/types'
import { EntityChunk, EntityChunkStub } from '../datacontainers/EntityChunk'
import { GroundBlock, PatchKey } from '../common/types'
import { GroundPatch, WorldCompute, WorldUtils } from '../index'
import { parseThreeStub } from '../common/utils'

export enum ComputeApiCall {
PatchCompute = 'bakeGroundPatch',
PatchCompute = 'bakePatch',
BlocksBatchCompute = 'computeBlocksBatch',
OvergroundBufferCompute = 'computeOvergroundBuffer',
QueryEntities = 'queryEntities',
BakeEntities = 'queryBakeEntities',
OvergroundItemsQuery = 'retrieveOvergroundItems',
BattleBoardCompute = 'computeBoardData',
}

Expand All @@ -21,8 +17,9 @@ export type ComputeApiParams = Partial<{
}>

/**
* Exposing world compute api with ability to run inside optional worker
* When provided all request are proxied to worker instead of main thread
* World API frontend proxying requests to internal modules: world-compute, world-cache,
* When optional worker is provided all compute request are proxied to worker
* instead of main thread
*/
export class WorldComputeProxy {
// eslint-disable-next-line no-use-before-define
Expand Down Expand Up @@ -75,51 +72,39 @@ export class WorldComputeProxy {
}

async computeBlocksBatch(
blockPosBatch: Vector3[],
blockPosBatch: Vector2[],
params = { includeEntitiesBlocks: false },
) {
const blocks = !this.worker
? WorldCompute.computeBlocksBatch(blockPosBatch, params)
: ((await this.workerCall(ComputeApiCall.BlocksBatchCompute, [
blockPosBatch,
params,
])?.then((blocksStubs: Block[]) =>
])?.then((blocksStubs: GroundBlock[]) =>
// parse worker's data to recreate original objects
blocksStubs.map(blockStub => {
blockStub.pos = WorldUtils.parseThreeStub(blockStub.pos)
return blockStub
}),
)) as Block[])
)) as GroundBlock[])

return blocks
}

// *iterEntitiesBaking(entityKeys: EntityKey[]) {
// for (const entityKey of entityKeys) {
// const entityChunk = WorldCompute.bakeChunkEntity(entityKey)
// yield entityChunk
// }
// }

async queryEntities(queriedRegion: Box2) {
const entitiesData = !this.worker
? WorldCompute.queryEntities(queriedRegion)
: ((await this.workerCall(
ComputeApiCall.QueryEntities,
async queryOvergroundItems(queriedRegion: Box2) {
const overgroundItems = !this.worker
? WorldCompute.retrieveOvergroundItems(queriedRegion)
: await this.workerCall(
ComputeApiCall.OvergroundItemsQuery,
[queriedRegion], // [emptyPatch.bbox]
)?.then(stubs =>
stubs.map((stub: EntityData) => ({
...stub,
bbox: parseThreeStub(stub.bbox),
})),
)) as EntityData[])
return entitiesData
)
return overgroundItems
}

async *iterPatchCompute(patchKeysBatch: PatchKey[]) {
for (const patchKey of patchKeysBatch) {
const patch = !this.worker
? WorldCompute.bakeGroundPatch(patchKey)
? WorldCompute.bakePatch(patchKey)
: ((await this.workerCall(
ComputeApiCall.PatchCompute,
[patchKey], // [emptyPatch.bbox]
Expand All @@ -133,25 +118,13 @@ export class WorldComputeProxy {

async bakeGroundPatch(boundsOrPatchKey: Box2 | string) {
const patchStub = !this.worker
? WorldCompute.bakeGroundPatch(boundsOrPatchKey)
? WorldCompute.bakePatch(boundsOrPatchKey)
: await this.workerCall(ComputeApiCall.PatchCompute, [boundsOrPatchKey])
// ?.then(patchStub => new GroundPatch().fromStub(patchStub)) as GroundPatch

return patchStub
}

async bakeEntities(queriedRange: Box2) {
const entityChunks = !this.worker
? WorldCompute.queryBakeEntities(queriedRange)
: await this.workerCall(ComputeApiCall.BakeEntities, [
queriedRange,
])?.then((entityChunks: EntityChunkStub[]) =>
// parse worker's data to recreate original objects
entityChunks.map(chunkStub => EntityChunk.fromStub(chunkStub)),
)
return entityChunks
}

// async requestBattleBoard(boardCenter: Vector3, boardParams: BoardParams, lastBoardBounds: Box2) {
// const boardData = !this.worker ?
// WorldCompute.computeBoardData(boardCenter, boardParams, lastBoardBounds) :
Expand Down
Loading