Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <raylib.h>
- #include <raymath.h>
- typedef struct
- {
- Rectangle rect, texture_rect;
- Texture2D texture;
- } Tile;
- enum TileID
- {
- GRASS_BLOCK,
- DIRT,
- STONE,
- GRASS,
- RED_FLOWER,
- BLUE_FLOWER,
- ESMERALD_ORE,
- COPPER_ORE,
- COAL_ORE,
- DIAMOND_ORE
- };
- Rectangle factoryTileRect(TileID id)
- {
- switch (id)
- {
- case GRASS_BLOCK: return Rectangle { 8.f * 1.f, 8.f * 0.f, 8.f, 8.f }; break;
- case DIRT: return Rectangle { 8.f * 1.f, 8.f * 1.f, 8.f, 8.f }; break;
- case STONE: return Rectangle { 8.f * 6.f, 8.f * 2.f, 8.f, 8.f }; break;
- case GRASS: return Rectangle { 8.f * 2.f, 8.f * 5.f, 8.f, 8.f }; break;
- case RED_FLOWER: return Rectangle { 8.f * 5.f, 8.f * 2.f, 8.f, 8.f }; break;
- case BLUE_FLOWER: return Rectangle { 8.f * 0.f, 8.f * 5.f, 8.f, 8.f }; break;
- case ESMERALD_ORE: return Rectangle { 8.f * 7.f, 8.f * 0.f, 8.f, 8.f }; break;
- case COPPER_ORE: return Rectangle { 8.f * 7.f, 8.f * 2.f, 8.f, 8.f }; break;
- case DIAMOND_ORE: return Rectangle { 8.f * 6.f, 8.f * 3.f, 8.f, 8.f }; break;
- case COAL_ORE: return Rectangle { 8.f * 4.f, 8.f * 5.f, 8.f, 8.f }; break;
- default: return Rectangle { 8.f, 0.f, 8.f, 8.f }; break;
- }
- }
- std::vector<Tile> generateChunk(int chunk_at, Texture2D tilemap)
- {
- std::vector<Tile> chunk;
- static ssize_t max_high = 64;
- for(ssize_t x = chunk_at; x < chunk_at + 64; x++)
- {
- for(ssize_t y = 0; y < max_high; y++)
- {
- Tile tile;
- tile.rect = {
- static_cast<float>(x) * 24.f,
- static_cast<float>(y) * 24.f - static_cast<float>(max_high) * 24.f + 512.f,
- 24.f, 24.f
- };
- tile.texture = tilemap;
- if(y == 0)
- {
- tile.texture_rect = factoryTileRect(TileID::GRASS_BLOCK);
- if(GetRandomValue(0, 2) == 0)
- {
- Tile extra = tile;
- int id = GetRandomValue(0, 2);
- extra.texture_rect = id == 0 ?
- factoryTileRect(TileID::GRASS) : id == 1 ?
- factoryTileRect(TileID::RED_FLOWER) :
- factoryTileRect(TileID::BLUE_FLOWER);
- extra.rect.y -= extra.rect.height;
- chunk.push_back(extra);
- }
- }
- else if(y > 8)
- {
- tile.texture_rect = factoryTileRect(TileID::STONE);
- if(y > 16)
- {
- if(GetRandomValue(0, 25) == 0)
- tile.texture_rect = factoryTileRect(TileID::COAL_ORE);
- else if(GetRandomValue(0, 50) == 0)
- tile.texture_rect = factoryTileRect(TileID::COPPER_ORE);
- else if(GetRandomValue(0, 100) == 0)
- tile.texture_rect = factoryTileRect(TileID::ESMERALD_ORE);
- else if(GetRandomValue(0, 75) == 0)
- tile.texture_rect = factoryTileRect(TileID::DIAMOND_ORE);
- }
- if(y == max_high - 1)
- {
- tile.texture_rect = Rectangle { 8.f * 5.f, 8.f * 5.f, 8.f, 8.f };
- }
- }
- else
- {
- tile.texture_rect = factoryTileRect(TileID::DIRT);
- }
- chunk.push_back(tile);
- }
- max_high += GetRandomValue(-1, 1);
- if(max_high < 4) max_high = 4;
- }
- return chunk;
- }
- int main()
- {
- InitWindow(720, 480, "World Gen");
- Texture2D default_tilemap = LoadTexture("data/tileset/default.png");
- SetTargetFPS(60);
- int region_x = 0;
- std::vector<Tile> world;
- world = generateChunk(region_x, default_tilemap);
- Vector2 camera_position = { 0.f, 0.f }, player_position = { 720.f / 2.f, 480.f / 2.f + 50.f * 24.f };
- while(!WindowShouldClose())
- {
- if(IsKeyDown(KEY_W)) player_position.y += 10.f;
- if(IsKeyDown(KEY_A)) player_position.x += 10.f;
- if(IsKeyDown(KEY_S)) player_position.y -= 10.f;
- if(IsKeyDown(KEY_D)) player_position.x -= 10.f;
- camera_position.x += (720.f / 2.f - player_position.x - camera_position.x) / 8.f;
- camera_position.y += (480.f / 2.f - player_position.y - camera_position.y) / 8.f;
- BeginDrawing();
- ClearBackground(Color {130, 200, 229});
- if(camera_position.x > region_x * 24.f)
- {
- region_x += 64;
- std::vector<Tile> new_chunk = generateChunk(region_x, default_tilemap);
- world.insert(world.end(), new_chunk.begin(), new_chunk.end());
- }
- for(Tile tile : world)
- {
- tile.rect.x -= camera_position.x;
- tile.rect.y -= camera_position.y;
- if(tile.rect.x > -tile.rect.width && tile.rect.x < 720.f && tile.rect.y > -tile.rect.height && tile.rect.y < 480.f)
- DrawTexturePro(tile.texture, tile.texture_rect, tile.rect, Vector2Zero(), 0.f, WHITE );
- }
- Vector2 player_scrolled = player_position;
- player_scrolled.x += camera_position.x;
- player_scrolled.y += camera_position.y;
- DrawRectangleV(player_scrolled, Vector2 { 32.f, 32.f }, RED);
- EndDrawing();
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment