11import { createEntities , createEntity } from './ecs/entity-manager' ;
2- import { HEIGHT_MAP_RESOLUTION , MAP_HEIGHT , MAP_WIDTH , TREE_RADIUS , TREE_SPAWN_THRESHOLD , TREE_SPAWN_DENSITY } from './constants/world-constants' ;
2+ import {
3+ HEIGHT_MAP_RESOLUTION ,
4+ MAP_HEIGHT ,
5+ MAP_WIDTH ,
6+ TREE_RADIUS ,
7+ TREE_SPAWN_DENSITY ,
8+ WATER_LEVEL ,
9+ SNOW_LEVEL ,
10+ ROCK_LEVEL ,
11+ GRASS_LEVEL ,
12+ SAND_LEVEL ,
13+ } from './constants/world-constants' ;
314import { Entities , EntityType , BiomeType , GameWorldState } from './types/world-types' ;
415import { createNoise2D } from './utils/noise-utils' ;
516
@@ -37,35 +48,63 @@ export function generateHeightMap(width: number, height: number, resolution: num
3748}
3849
3950/**
40- * Generates tree entities based on the heightmap.
41- * Trees spawn on land (above TREE_SPAWN_THRESHOLD) with a probability of TREE_SPAWN_DENSITY.
42- * @param entities The entity manager to add trees to.
51+ * Generates a biome map based on the height map.
4352 * @param heightMap The heightmap of the world.
44- * @param resolution The size of each height map cell in pixels .
53+ * @returns A 2D array of BiomeType enums .
4554 */
46- export function generateTrees (
47- entities : Entities ,
48- heightMap : number [ ] [ ] ,
49- resolution : number ,
50- ) : void {
55+ export function generateBiomeMap ( heightMap : number [ ] [ ] ) : BiomeType [ ] [ ] {
5156 const gridHeight = heightMap . length ;
5257 const gridWidth = heightMap [ 0 ] ?. length ?? 0 ;
58+ const biomeMap : BiomeType [ ] [ ] = Array . from ( { length : gridHeight } , ( ) => new Array ( gridWidth ) ) ;
5359
5460 for ( let y = 0 ; y < gridHeight ; y ++ ) {
5561 for ( let x = 0 ; x < gridWidth ; x ++ ) {
5662 const height = heightMap [ y ] [ x ] ;
57-
58- // Only spawn trees on land (above water level threshold)
59- if ( height > TREE_SPAWN_THRESHOLD ) {
63+ if ( height > SNOW_LEVEL ) {
64+ biomeMap [ y ] [ x ] = BiomeType . SNOW ;
65+ } else if ( height > ROCK_LEVEL ) {
66+ biomeMap [ y ] [ x ] = BiomeType . ROCK ;
67+ } else if ( height > GRASS_LEVEL ) {
68+ biomeMap [ y ] [ x ] = BiomeType . GRASS ;
69+ } else if ( height > SAND_LEVEL ) {
70+ biomeMap [ y ] [ x ] = BiomeType . SAND ;
71+ } else if ( height > WATER_LEVEL ) {
72+ biomeMap [ y ] [ x ] = BiomeType . GROUND ;
73+ } else {
74+ // Water is not a biome type, but could be handled here if needed
75+ biomeMap [ y ] [ x ] = BiomeType . GROUND ; // Default to ground for below-water cells
76+ }
77+ }
78+ }
79+
80+ return biomeMap ;
81+ }
82+
83+ /**
84+ * Generates tree entities based on the biome map.
85+ * Trees spawn on GRASS biome cells with a probability of TREE_SPAWN_DENSITY.
86+ * @param entities The entity manager to add trees to.
87+ * @param biomeMap The biome map of the world.
88+ * @param resolution The size of each map cell in pixels.
89+ */
90+ export function generateTrees ( entities : Entities , biomeMap : BiomeType [ ] [ ] , resolution : number ) : void {
91+ const gridHeight = biomeMap . length ;
92+ const gridWidth = biomeMap [ 0 ] ?. length ?? 0 ;
93+
94+ for ( let y = 0 ; y < gridHeight ; y ++ ) {
95+ for ( let x = 0 ; x < gridWidth ; x ++ ) {
96+ const biome = biomeMap [ y ] [ x ] ;
97+
98+ // Only spawn trees on grass
99+ if ( biome === BiomeType . GRASS ) {
60100 // Random chance to spawn a tree
61101 if ( Math . random ( ) < TREE_SPAWN_DENSITY ) {
62102 const worldX = x * resolution + Math . random ( ) * resolution ;
63103 const worldY = y * resolution + Math . random ( ) * resolution ;
64-
104+
65105 createEntity ( entities , EntityType . TREE , {
66106 position : { x : worldX , y : worldY } ,
67107 radius : TREE_RADIUS ,
68- biomeType : BiomeType . TREE ,
69108 } ) ;
70109 }
71110 }
@@ -80,9 +119,10 @@ export function generateTrees(
80119export function initWorld ( ) : GameWorldState {
81120 const entities = createEntities ( ) ;
82121 const heightMap = generateHeightMap ( MAP_WIDTH , MAP_HEIGHT , HEIGHT_MAP_RESOLUTION ) ;
122+ const biomeMap = generateBiomeMap ( heightMap ) ;
83123
84- // Generate trees based on heightmap
85- generateTrees ( entities , heightMap , HEIGHT_MAP_RESOLUTION ) ;
124+ // Generate trees based on the biome map
125+ generateTrees ( entities , biomeMap , HEIGHT_MAP_RESOLUTION ) ;
86126
87127 const initialWorldState : GameWorldState = {
88128 time : 0 ,
@@ -92,6 +132,7 @@ export function initWorld(): GameWorldState {
92132 height : MAP_HEIGHT ,
93133 } ,
94134 heightMap,
135+ biomeMap,
95136 viewportCenter : {
96137 x : MAP_WIDTH / 2 ,
97138 y : MAP_HEIGHT / 2 ,
0 commit comments