|
| 1 | +import { Point } from '/src/lib/geometry/point' |
| 2 | +import { Rect } from '/src/lib/geometry/rect' |
| 3 | +import { Grid } from '/src/lib/grid' |
| 4 | + |
| 5 | + |
| 6 | +const SURFACE_NOISE_RATIO = .6 |
| 7 | +const REGION_WATER = false |
| 8 | +const REGION_LAND = true |
| 9 | + |
| 10 | + |
| 11 | +export class LandMaskZone { |
| 12 | + #landMaskGrid |
| 13 | + |
| 14 | + constructor(context) { |
| 15 | + this.size = context.zoneSize |
| 16 | + this.#landMaskGrid = buildModel(context) |
| 17 | + } |
| 18 | + |
| 19 | + get(zonePoint) { |
| 20 | + const isLand = this.#landMaskGrid.get(zonePoint) |
| 21 | + return isLand |
| 22 | + } |
| 23 | + |
| 24 | + draw(props, params) { |
| 25 | + const {canvas, canvasPoint, tileSize} = props |
| 26 | + const zoneSize = this.size |
| 27 | + const size = tileSize / zoneSize |
| 28 | + // render zone tiles |
| 29 | + for (let x=0; x < zoneSize; x++) { |
| 30 | + const xSize = x * size |
| 31 | + for (let y=0; y < zoneSize; y++) { |
| 32 | + const zonePoint = [y, x] |
| 33 | + const ySize = y * size |
| 34 | + const zoneCanvasPoint = Point.plus(canvasPoint, [ySize, xSize]) |
| 35 | + const color = this.get(zonePoint) ? '#71b13e' : '#1d2255' |
| 36 | + canvas.rect(zoneCanvasPoint, size, color) |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +function buildModel(context) { |
| 44 | + const hash = Point.hash(context.worldPoint) |
| 45 | + // cache de zone grid noise |
| 46 | + // if (SURFACE_GRID_CACHE.has(hash)) { |
| 47 | + // return SURFACE_GRID_CACHE.get(hash) |
| 48 | + // } |
| 49 | + // Generate a boolean grid (land or water) |
| 50 | + const {worldPoint, world, rect, zoneRect} = context |
| 51 | + const relativePoint = Point.multiplyScalar(worldPoint, zoneRect.width) |
| 52 | + const noiseRect = Rect.multiply(rect, zoneRect.width) |
| 53 | + const landMaskGrid = Grid.fromRect(zoneRect, zonePoint => { |
| 54 | + const noisePoint = Point.plus(relativePoint, zonePoint) |
| 55 | + const noise = world.noise.get4DZoneOutline(noiseRect, noisePoint) |
| 56 | + return noise > SURFACE_NOISE_RATIO ? REGION_LAND : REGION_WATER |
| 57 | + }) |
| 58 | + // SURFACE_GRID_CACHE.set(hash, landMaskGrid) |
| 59 | + return landMaskGrid |
| 60 | +} |
0 commit comments