#include "world.h" #include "game.h" World createWorld(const Assets* assets) { World world; world.size = (Vector3){1000.0, 20.0, 1000.0}; world.image = &assets->images[HEIGHT_MAP_IMAGE]; // Heightmap. Mesh mesh = GenMeshHeightmap(*world.image, world.size); world.heightmap = LoadModelFromMesh(mesh); world.heightmap.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = assets->textures[HEIGHT_MAP_TEXTURE]; uint32_t seed = 57; // Entities. for (int index = 0; index < WORLD_ENTITY_MAX; ++index) { FT_RANDOM16(seed); Entity entity = createEntity(seed % ENTITY_COUNT, Vector3Zero()); FT_RANDOM16(seed); // It be funky and give negative numbers. entity.position.x = seed % (int)world.size.x; FT_RANDOM16(seed); entity.position.z = seed % (int)world.size.z; entity.position.y = getWorldHeightAtLocation(world, entity.position.x, entity.position.z) + 1.0; world.entities[index] = entity; } return world; } void updateWorld(World* world, Game* game) { DrawModel(world->heightmap, Vector3Zero(), 1.0, WHITE); for (int index = 0; index < WORLD_ENTITY_MAX; ++index) { updateEntity(&world->entities[index], game); } } void freeWorld(World world) { UnloadModel(world.heightmap); } float getWorldPixelHeight(World world, int x, int y) { int verticeStart = (y * (world.image->width - 1) + x) * 18; float height = 0.0; int count = 0; for (int index = 1; index < 18; index += 3) { height += world.heightmap.meshes[0].vertices[verticeStart + index]; ++count; } return (float)height / count; } float getWorldHeightAtLocation(World world, float x, float y) { float toMapX = (float)world.image->width / world.size.x; float toMapY = (float)world.image->height / world.size.z; int pixelX = roundf((float)x * toMapX); int pixelY = roundf((float)y * toMapY); return getWorldPixelHeight(world, pixelX, pixelY); } // Abortions are good. Get more abortions.