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/refactor #6

Merged
merged 4 commits into from
Apr 3, 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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
},
"keywords": [],
"dependencies": {
"@aresrpg/aresrpg-engine": "^1.1.0",
"alea": "^1.0.1",
"simplex-noise": "^4.0.1",
"sparse-octree": "^7.1.8",
Expand Down
10 changes: 5 additions & 5 deletions src/common/stats.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export class ProcGenStats {
export class GenStats {
// eslint-disable-next-line no-use-before-define
static singleton: ProcGenStats
static singleton: GenStats
static get instance() {
ProcGenStats.singleton = ProcGenStats.singleton || new ProcGenStats()
GenStats.singleton = GenStats.singleton || new GenStats()
// put in global scope for access from dev console
// window.aresrpg = {}
// window.aresrpg.procgen = {}
// window.aresrpg.procgen.stats = ProcGenStats.singleton.stats
return ProcGenStats.singleton
// window.aresrpg.procgen.stats = GenStats.singleton.stats
return GenStats.singleton
}

stats: any = {
Expand Down
46 changes: 35 additions & 11 deletions src/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
import { IVoxelMaterial } from '@aresrpg/aresrpg-engine'
import { Color } from 'three'
import { Vector3 } from 'three'

enum VoxelType {
export enum BlockType {
NONE,
ROCK,
GRASS,
SNOW,
WATER,
SAND,
}

const VOXEL_TYPE_COLORS: Record<VoxelType, IVoxelMaterial> = [
{ color: new Color('#ABABAB') },
{ color: new Color('#00B920') },
{ color: new Color('#E5E5E5') },
{ color: new Color('#0055E2') },
{ color: new Color('#DCBE28') },
]
export type Block = {
pos: Vector3
type: BlockType
}

export { VoxelType, VOXEL_TYPE_COLORS }
export enum BlockNeighbour {
xMyMzM,
xMyMz0,
xMyMzP,
xMy0zM,
xMy0z0,
xMy0zP,
xMyPzM,
xMyPz0,
xMyPzP,
x0yMzM,
x0yMz0,
x0yMzP,
x0y0zM,
x0y0zP,
x0yPzM,
x0yPz0,
x0yPzP,
xPyMzM,
xPyMz0,
xPyMzP,
xPy0zM,
xPy0z0,
xPy0zP,
xPyPzM,
xPyPz0,
xPyPzP,
}
112 changes: 45 additions & 67 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Vector2, Vector3 } from 'three'

import { ProcGenStats } from './stats'
import { GenStats } from './stats'
import { BlockNeighbour } from './types'

/**
* Removing out of range values
Expand All @@ -13,7 +14,7 @@ const sanitiseNoise = (noiseVal: number) => {
// correct and report noise anomaly
if (invalidNoiseRange) {
res = Math.round(noiseVal)
ProcGenStats.instance.noiseAnomaly(noiseVal)
GenStats.instance.noiseAnomaly(noiseVal)
}
return res
}
Expand All @@ -36,42 +37,19 @@ const interpolatePoints = (p1: Vector2, p2: Vector2, t: number) => {
const slope = range.x > 0 ? range.y / range.x : 0
return p1.y + slope * (t - p1.x)
}
enum NeighbourType {
xMyMzM,
xMyMz0,
xMyMzP,
xMy0zM,
xMy0z0,
xMy0zP,
xMyPzM,
xMyPz0,
xMyPzP,
x0yMzM,
x0yMz0,
x0yMzP,
x0y0zM,
x0y0zP,
x0yPzM,
x0yPz0,
x0yPzP,
xPyMzM,
xPyMz0,
xPyMzP,
xPy0zM,
xPy0z0,
xPy0zP,
xPyPzM,
xPyPz0,
xPyPzP,
}

const AjacentNeighbours = [
NeighbourType.xPy0z0,
NeighbourType.xMy0z0, // right, left
NeighbourType.x0yPz0,
NeighbourType.x0yMz0, // top, bottom
NeighbourType.x0y0zP,
NeighbourType.x0y0zM, // front, back
/**
* Direct neighbours e.g.
* - FRONT/BACK,
* - TOP/BOTTOM,
* - LEFT/RIGHT
*/
const AdjacentNeighbours = [
BlockNeighbour.xPy0z0,
BlockNeighbour.xMy0z0, // right, left
BlockNeighbour.x0yPz0,
BlockNeighbour.x0yMz0, // top, bottom
BlockNeighbour.x0y0zP,
BlockNeighbour.x0y0zM, // front, back
]

/**
Expand All @@ -80,65 +58,65 @@ const AjacentNeighbours = [
* @param dir neighbour identifier
* @returns
*/
const getNeighbour = (pos: Vector3, dir: NeighbourType): Vector3 => {
const getNeighbour = (pos: Vector3, dir: BlockNeighbour): Vector3 => {
switch (dir) {
case NeighbourType.xMyMzM:
case BlockNeighbour.xMyMzM:
return pos.clone().add(new Vector3(-1, -1, -1))
case NeighbourType.xMyMz0:
case BlockNeighbour.xMyMz0:
return pos.clone().add(new Vector3(-1, -1, 0))
case NeighbourType.xMyMzP:
case BlockNeighbour.xMyMzP:
return pos.clone().add(new Vector3(-1, -1, 1))
case NeighbourType.xMy0zM:
case BlockNeighbour.xMy0zM:
return pos.clone().add(new Vector3(-1, 0, -1))
case NeighbourType.xMy0z0:
case BlockNeighbour.xMy0z0:
return pos.clone().add(new Vector3(-1, 0, 0))
case NeighbourType.xMy0zP:
case BlockNeighbour.xMy0zP:
return pos.clone().add(new Vector3(-1, 0, 1))
case NeighbourType.xMyPzM:
case BlockNeighbour.xMyPzM:
return pos.clone().add(new Vector3(-1, 1, -1))
case NeighbourType.xMyPz0:
case BlockNeighbour.xMyPz0:
return pos.clone().add(new Vector3(-1, 1, 0))
case NeighbourType.xMyPzP:
case BlockNeighbour.xMyPzP:
return pos.clone().add(new Vector3(-1, 1, 1))
case NeighbourType.x0yMzM:
case BlockNeighbour.x0yMzM:
return pos.clone().add(new Vector3(0, -1, -1))
case NeighbourType.x0yMz0:
case BlockNeighbour.x0yMz0:
return pos.clone().add(new Vector3(0, -1, 0))
case NeighbourType.x0yMzP:
case BlockNeighbour.x0yMzP:
return pos.clone().add(new Vector3(0, -1, 1))
case NeighbourType.x0y0zM:
case BlockNeighbour.x0y0zM:
return pos.clone().add(new Vector3(0, 0, -1))
case NeighbourType.x0y0zP:
case BlockNeighbour.x0y0zP:
return pos.clone().add(new Vector3(0, 0, 1))
case NeighbourType.x0yPzM:
case BlockNeighbour.x0yPzM:
return pos.clone().add(new Vector3(0, 1, -1))
case NeighbourType.x0yPz0:
case BlockNeighbour.x0yPz0:
return pos.clone().add(new Vector3(0, 1, 0))
case NeighbourType.x0yPzP:
case BlockNeighbour.x0yPzP:
return pos.clone().add(new Vector3(0, 1, 1))
case NeighbourType.xPyMzM:
case BlockNeighbour.xPyMzM:
return pos.clone().add(new Vector3(1, -1, -1))
case NeighbourType.xPyMz0:
case BlockNeighbour.xPyMz0:
return pos.clone().add(new Vector3(1, -1, 0))
case NeighbourType.xPyMzP:
case BlockNeighbour.xPyMzP:
return pos.clone().add(new Vector3(1, -1, 1))
case NeighbourType.xPy0zM:
case BlockNeighbour.xPy0zM:
return pos.clone().add(new Vector3(1, 0, -1))
case NeighbourType.xPy0z0:
case BlockNeighbour.xPy0z0:
return pos.clone().add(new Vector3(1, 0, 0))
case NeighbourType.xPy0zP:
case BlockNeighbour.xPy0zP:
return pos.clone().add(new Vector3(1, 0, 1))
case NeighbourType.xPyPzM:
case BlockNeighbour.xPyPzM:
return pos.clone().add(new Vector3(1, 1, -1))
case NeighbourType.xPyPz0:
case BlockNeighbour.xPyPz0:
return pos.clone().add(new Vector3(1, 1, 0))
case NeighbourType.xPyPzP:
case BlockNeighbour.xPyPzP:
return pos.clone().add(new Vector3(1, 1, 1))
}
}

const getAllNeighbours = (pos: Vector3): Vector3[] => {
const neighbours = Object.values(NeighbourType)
const neighbours = Object.values(BlockNeighbour)
.filter(v => !isNaN(Number(v)))
.map(type => getNeighbour(pos, type as number))
return neighbours
Expand All @@ -149,6 +127,6 @@ export {
round2,
interpolatePoints,
getAllNeighbours,
AjacentNeighbours,
AdjacentNeighbours,
getNeighbour,
}
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export { VoxelMap } from './procgen/VoxelMap'
export { WorldGenerator } from './procgen/WorldGen'
export { SimplexNoiseSampler } from './procgen/NoiseSampler'
export { HeightProfiler } from './procgen/HeightProfiler'
export { BlendMode, ProcGenLayer, GenLayer } from './procgen/ProcGenLayer'
export { ProcGenStats } from './common/stats'
export { VoxelType } from './common/types'
export { GenStats } from './common/stats'
export { BlockType } from './common/types'
4 changes: 4 additions & 0 deletions src/procgen/ProcGenLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ export class ProcGenLayer extends GenLayer {
return vals * 255
}

static layerIndex(index: number) {
return `layer#${index}`
}

get config() {
return this.params
}
Expand Down
45 changes: 0 additions & 45 deletions src/procgen/VoxelMap.ts

This file was deleted.

Loading