Skip to content

Commit 4e31f1d

Browse files
committed
Refactor
1 parent 752f05e commit 4e31f1d

File tree

5 files changed

+61
-96
lines changed

5 files changed

+61
-96
lines changed

src/model/world/basin/model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class LandBasinFill extends ConcurrentFill {
184184
const target = world.surface.get(fillPoint)
185185
const parent = world.surface.get(parentPoint)
186186
// avoid fill if different types
187-
return target.isWater == parent.isWater
187+
return target.type.isWater == parent.type.isWater
188188
}
189189

190190
_fillBasin(fill, fillPoint, parentPoint) {

src/model/world/relief/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export class ReliefLayer {
3030
#detectWaterType(world, point) {
3131
const outlineNoise = world.noise.get4DOutline(this.rect, point)
3232
const grainedNoise = world.noise.get4DGrained(this.rect, point)
33-
if (world.surface.get(point).id == SeaSurface.id) {
33+
const surface = world.surface.get(point)
34+
if (surface.type.id == SeaSurface.id) {
3435
if (outlineNoise > OCEAN_RATIO) return Relief.OCEAN
3536
return Relief.PLATFORM
3637
}

src/model/world/surface/index.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,21 @@ export class SurfaceLayer {
1919

2020
get(point) {
2121
const bodyId = this.#model.body.get(point)
22-
return Surface.parse(this.#model.bodyType.get(bodyId))
22+
const borderId = this.#model.borderType.get(point)
23+
return {
24+
type: Surface.parse(this.#model.bodyType.get(bodyId)),
25+
borderType: Surface.parse(borderId),
26+
}
2327
}
2428

2529
getText(point) {
2630
const surface = this.get(point)
2731
const surfaceArea = this.getArea(point)
2832
const attrs = [
29-
`${surface.name}`,
33+
`${surface.type.name}`,
3034
`area=${surfaceArea}`,
31-
].join(' | ')
35+
`border=${surface.borderType.name}`,
36+
].join(', ')
3237
return `Surface(${attrs})`
3338
}
3439

@@ -43,40 +48,41 @@ export class SurfaceLayer {
4348
}
4449

4550
isWater(point) {
46-
return this.get(point).isWater
51+
return this.get(point).type.isWater
52+
}
53+
54+
isLand(point) {
55+
return ! this.isWater(point)
56+
}
57+
58+
isBorder(point) {
59+
return this.#model.borderType.get(point) != Surface.id
4760
}
4861

4962
isLake(point) {
50-
return this.get(point).id == LakeSurface.id
63+
return this.get(point).type.id == LakeSurface.id
5164
}
5265

5366
isOcean(point) {
54-
return this.get(point).id == OceanSurface.id
67+
return this.get(point).type.id == OceanSurface.id
5568
}
5669

5770
isSea(point) {
58-
return this.get(point).id == SeaSurface.id
71+
return this.get(point).type.id == SeaSurface.id
5972
}
6073

6174
isIsland(point) {
62-
return this.get(point).id == IslandSurface.id
75+
return this.get(point).type.id == IslandSurface.id
6376
}
6477

6578
isContinent(point) {
66-
return this.get(point).id == ContinentSurface.id
67-
}
68-
69-
isLand(point) {
70-
return ! this.get(point).isWater
71-
}
72-
73-
isBorder(point) {
74-
return this.#model.border.get(point) > 0
79+
return this.get(point).type.id == ContinentSurface.id
7580
}
7681

7782
draw(props, params) {
7883
const {canvas, canvasPoint, tileSize, tilePoint} = props
79-
let color = this.get(tilePoint).color
84+
const surface = this.get(tilePoint)
85+
let color = surface.type.color
8086
if (this.isBorder(tilePoint)) {
8187
color = color.darken(20)
8288
}

src/model/world/surface/model.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
} from './type'
1313

1414

15-
const EMPTY = null
1615
const LAND = 0
1716
const WATER = 1
1817
// Area ratios
@@ -21,20 +20,22 @@ const MINIMUN_OCEAN_RATIO = 2
2120
const MINIMUN_SEA_RATIO = .05
2221
const MINIMUN_CONTINENT_RATIO = 1
2322

23+
export const EMPTY = null
24+
2425

2526
export function buildSurfaceModel(context) {
2627
const {rect, world} = context
2728
// detect land or water tiles in the grid
2829
const landWaterGrid = detectLandWater(rect, world)
2930
// detect surface type by filling empty bodies
30-
const {bodyGrid, bodyTypeMap, bodyAreaMap} = detectSurfaceAreas(context, landWaterGrid)
31-
const borderGrid = detectBorders(context, landWaterGrid)
31+
const {bodyGrid, bodyTypeMap, bodyAreaMap} = detectBodies(context, landWaterGrid)
32+
const borderTypeGrid = detectBorderTypes(context, bodyTypeMap, bodyGrid)
3233
const waterArea = readWaterArea(bodyTypeMap, bodyAreaMap)
3334
return {
34-
border: borderGrid,
3535
body: bodyGrid,
3636
bodyType: bodyTypeMap,
3737
bodyArea: bodyAreaMap,
38+
borderType: borderTypeGrid,
3839
waterArea,
3940
}
4041
}
@@ -49,13 +50,13 @@ function detectLandWater(rect, world) {
4950
}
5051

5152

52-
function detectSurfaceAreas(baseContext, landWaterGrid) {
53+
function detectBodies(baseContext, landWaterGrid) {
5354
// flood fill "empty" points and determine body type by total area
5455
let bodyId = 1
5556
const bodyTypeMap = new Map()
5657
const bodyAreaMap = new Map()
5758
const bodyGrid = Grid.fromRect(baseContext.rect, () => EMPTY)
58-
const context = {...baseContext, bodyTypeMap, bodyGrid, landWaterGrid}
59+
const context = {...baseContext, bodyGrid, landWaterGrid}
5960

6061
landWaterGrid.forEach(originPoint => {
6162
if(bodyGrid.get(originPoint) != EMPTY) return
@@ -106,20 +107,21 @@ function fillBodyArea(context, originPoint, bodyId) {
106107
}
107108

108109

109-
function detectBorders(context, landWaterGrid) {
110-
// surface body matrix already defined, update it by setting
111-
// water/land borders as negative ids
112-
const borderGrid = Grid.fromRect(context.rect, point => {
113-
const isWater = landWaterGrid.get(point) == WATER
110+
function detectBorderTypes(context, bodyTypeMap, bodyGrid) {
111+
const getType = point => {
112+
const bodyId = bodyGrid.get(point)
113+
return Surface.parse(bodyTypeMap.get(bodyId))
114+
}
115+
return Grid.fromRect(context.rect, point => {
116+
let type = getType(point)
114117
for (let sidePoint of Point.adjacents(point)) {
115-
const isSideWater = landWaterGrid.get(sidePoint) == WATER
116-
if (isWater && ! isSideWater || ! isWater && isSideWater) {
117-
return 1
118+
const sideType = getType(sidePoint)
119+
if (type.isWater && ! sideType.isWater || ! type.isWater && sideType.isWater) {
120+
return sideType.id
118121
}
119122
}
120-
return 0
123+
return Surface.id
121124
})
122-
return borderGrid
123125
}
124126

125127

src/model/world/surface/type.js

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,101 +2,57 @@ import { Color } from '/src/lib/color'
22

33

44
export class Surface {
5+
static id = 0
6+
static name = 'Inner'
7+
static isWater = false
8+
static color = Color.BLACK
9+
510
static parse(id) {
611
return SURFACE_MAP[id]
712
}
813
}
914

1015
export class LakeSurface {
11-
static id = 0
16+
static id = 1
1217
static name = 'Lake'
1318
static isWater = true
14-
static isBorder = false
1519
static color = Color.fromHex('#218484')
1620
}
1721

1822
export class SeaSurface {
19-
static id = 1
23+
static id = 2
2024
static name = 'Sea'
2125
static isWater = true
22-
static isBorder = false
2326
static color = Color.fromHex('#216384')
2427
}
2528

2629
export class OceanSurface {
27-
static id = 2
30+
static id = 3
2831
static name = 'Ocean'
2932
static isWater = true
30-
static isBorder = false
3133
static color = Color.fromHex('#1d2255')
3234
}
3335

3436
export class IslandSurface {
35-
static id = 3
37+
static id = 4
3638
static name = 'Island'
3739
static isWater = false
38-
static isBorder = false
3940
static color = Color.fromHex('#c5ed7d')
4041
}
4142

4243
export class ContinentSurface {
43-
static id = 4
44+
static id = 5
4445
static name = 'Continent'
4546
static isWater = false
46-
static isBorder = false
4747
static color = Color.fromHex('#71b13e')
4848
}
4949

50-
export class LakeBorderSurface {
51-
static id = 5
52-
static name = 'Lake border'
53-
static isWater = true
54-
static isBorder = true
55-
static color = Color.fromHex('#0D7070')
56-
}
57-
58-
export class SeaBorderSurface {
59-
static id = 6
60-
static name = 'Sea border'
61-
static isWater = true
62-
static isBorder = true
63-
static color = Color.fromHex('#0D4F70')
64-
}
65-
66-
export class OceanBorderSurface {
67-
static id = 7
68-
static name = 'Ocean border'
69-
static isWater = true
70-
static isBorder = true
71-
static color = Color.fromHex('#090E41')
72-
}
73-
74-
export class IslandBorderSurface {
75-
static id = 8
76-
static name = 'Island border'
77-
static isWater = false
78-
static isBorder = true
79-
static color = Color.fromHex('#B1D969')
80-
}
81-
82-
export class ContinentBorderSurface {
83-
static id = 9
84-
static name = 'Continent border'
85-
static isWater = false
86-
static isBorder = true
87-
static color = Color.fromHex('#5D9D2A')
88-
}
89-
9050

9151
const SURFACE_MAP = {
92-
0: LakeSurface,
93-
1: SeaSurface,
94-
2: OceanSurface,
95-
3: IslandSurface,
96-
4: ContinentSurface,
97-
5: LakeBorderSurface,
98-
6: SeaBorderSurface,
99-
7: OceanBorderSurface,
100-
8: IslandBorderSurface,
101-
9: ContinentBorderSurface,
52+
0: Surface,
53+
1: LakeSurface,
54+
2: SeaSurface,
55+
3: OceanSurface,
56+
4: IslandSurface,
57+
5: ContinentSurface,
10258
}

0 commit comments

Comments
 (0)