diff options
Diffstat (limited to 'src/world.c')
-rw-r--r-- | src/world.c | 72 |
1 files changed, 41 insertions, 31 deletions
diff --git a/src/world.c b/src/world.c index 82d0d5a..8547eef 100644 --- a/src/world.c +++ b/src/world.c @@ -342,6 +342,41 @@ Vector3 getRandomPositionFromCenter(const World* world, Seed* seed, return position; } +bool checkForPlaceOverlap(const World* world, BoundingBox box) +{ + for (int index = 0; index < WORLD_PLACE_COUNT; ++index) + { + if (CheckCollisionBoxes(world->entities[world->places[index]].box, box)) + { + return true; + } + } + + return false; +} + +// Ensures entity will not overlap with a place. +Seed putEntityInRandomPlace(const World* world, Seed seed, Entity* entity) +{ + while (true) + { + Vector3 position; + position.x = FT_RANDOM16(seed) % (int)world->size.x; + position.y = 0.0; + position.z = FT_RANDOM16(seed) % (int)world->size.z; + + setEntityPosition(entity, position); + placeEntityOnGround(entity, world); + + if (!checkForPlaceOverlap(world, entity->box)) + { + break; + } + } + + return seed; +} + Seed generateWorldPlants(World* world, Seed seed, int start, int end) { for (int index = start; index < end; ++index) @@ -353,15 +388,9 @@ Seed generateWorldPlants(World* world, Seed seed, int start, int end) size_t plantsSize = 3; EntityId id = plants[seed % plantsSize]; - // Get position. - Vector3 position; - position.x = FT_RANDOM16(seed) % (int)world->size.x; - position.y = 0.0; - position.z = FT_RANDOM16(seed) % (int)world->size.z; - // Create entity. - Entity entity = createEntity(id, position); - placeEntityOnGround(&entity, world); + Entity entity = createEntity(id, Vector3Zero()); + seed = putEntityInRandomPlace(world, seed, &entity); world->entities[index] = entity; } @@ -379,13 +408,8 @@ Seed generateWorldItems(World* world, Seed seed, int start, int end) size_t itemsSize = 2; EntityId id = items[seed % itemsSize]; - Vector3 position; - position.x = FT_RANDOM16(seed) % (int)world->size.x; - position.y = 0.0; - position.z = FT_RANDOM16(seed) % (int)world->size.z; - - Entity entity = createEntity(id, position); - placeEntityOnGround(&entity, world); + Entity entity = createEntity(id, Vector3Zero()); + seed = putEntityInRandomPlace(world, seed, &entity); world->entities[index] = entity; } @@ -393,21 +417,6 @@ Seed generateWorldItems(World* world, Seed seed, int start, int end) return seed; } -bool isValidPlaceForItem(const World* world, BoundingBox box) -{ - for (int index = 0; index < WORLD_PLACE_COUNT; ++index) - { - if (CheckCollisionBoxes(world->entities[world->places[index]].box, box)) - { - return false; - } - } - - return true; -} - -//Vector2 generateRandomItemPosition(const World* world, - Seed generatePond(World* world, Color* heightmap, WorldUID id, Seed seed) { // Create pond entity. @@ -440,7 +449,7 @@ Seed generatePond(World* world, Color* heightmap, WorldUID id, Seed seed) // Set new height. int area = PLACE_POND_OUTER_AREA; - int edge = PLACE_POND_DEPTH * (world->size.y / 255.0 * 2.0) / 2.0 + height; + int edge = PLACE_POND_DEPTH * (world->size.y / 255.0 * 2.0) / 4.0 + height; for (int y = start.y - area; y < end.y + area; ++y) { @@ -513,6 +522,7 @@ World createWorld(Seed seed) // Places. WorldUID placeId = 0; seed = generatePond(&world, colors, placeId, seed); + world.places[0] = placeId; // Heightmap model. image = (Image){ |