summary refs log tree commit diff
path: root/src/World
diff options
context:
space:
mode:
Diffstat (limited to 'src/World')
-rw-r--r--src/World/BlockSide.hpp66
-rw-r--r--src/World/BlockType.hpp11
-rw-r--r--src/World/Chunk.cpp72
-rw-r--r--src/World/Chunk.hpp30
-rw-r--r--src/World/Generator.cpp26
-rw-r--r--src/World/Generator.hpp15
6 files changed, 220 insertions, 0 deletions
diff --git a/src/World/BlockSide.hpp b/src/World/BlockSide.hpp
new file mode 100644
index 0000000..45efb11
--- /dev/null
+++ b/src/World/BlockSide.hpp
@@ -0,0 +1,66 @@
+#pragma once
+
+#include <cstdint>
+#include <vector>
+#include <array>
+#include "../Math/Vector.hpp"
+
+namespace MC {
+
+class BlockSide {
+public:
+    enum Value : uint8_t {
+        Front,
+        Back,
+
+        Top,
+        Bottom,
+
+        Left,
+        Right,
+    };
+
+    BlockSide() = default;
+    BlockSide(Value side) : m_side(side) {}
+
+    operator Value() const { return m_side; }
+
+    std::array<Vector<3>, 4> face() {
+        switch (m_side) {
+            case Front:
+                return {{
+                    {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 1.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 1.0f},
+                }};
+            case Back:
+                return {{
+                    {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 0.0f},
+                }};
+            case Top:
+                return {{
+                    {0.0f, 1.0f, 1.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f},
+                }};
+            case Bottom:
+                return {{
+                    {0.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f},
+                }};
+            case Left:
+                return {{
+                    {0.0f, 0.0f, 0.0f},{0.0f, 0.0f, 1.0f},{0.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 0.0f},
+                }};
+            case Right:
+                return {{
+                    {1.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 1.0f},
+                }};
+        }
+    }
+
+    static std::vector<BlockSide> all() {
+        return {
+            Front, Back, Top, Bottom, Left, Right,
+        };
+    }
+private:
+    Value m_side;
+};
+
+}
\ No newline at end of file
diff --git a/src/World/BlockType.hpp b/src/World/BlockType.hpp
new file mode 100644
index 0000000..e35daf8
--- /dev/null
+++ b/src/World/BlockType.hpp
@@ -0,0 +1,11 @@
+#pragma
+
+namespace MC {
+
+enum class BlockType : uint8_t {
+    Air,
+    Dirt,
+    Grass,
+};
+
+}
\ No newline at end of file
diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp
new file mode 100644
index 0000000..73aab3d
--- /dev/null
+++ b/src/World/Chunk.cpp
@@ -0,0 +1,72 @@
+#include "Chunk.hpp"
+#include "BlockSide.hpp"
+
+namespace MC {
+
+void Chunk::set(uint32_t x, uint32_t y, uint32_t z, BlockType type) {
+    m_blocks[x][y][z].type = type;
+}
+
+Mesh Chunk::mesh() {
+    std::vector<Vector<3>> positions{};
+    std::vector<Vector<2>> tex_coords{};
+    std::vector<uint32_t> indices{};
+
+    for (int x = 0; x < CHUNK_WIDTH; x++) {
+        for (int y = 0; y < CHUNK_HEIGHT; y++) {
+            for (int z = 0; z < CHUNK_WIDTH; z++) {
+                auto type = m_blocks[x][y][z].type;
+                if (type == BlockType::Air) {
+                    continue;
+                }
+
+                for (auto side: BlockSide::all()) {
+                    auto side_tex_coords = Chunk::face_tex_coords(type, side);
+                    auto side_positions = side.face();
+
+                    for (auto& position : side_positions) {
+                        position = position + Vector<3>{static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)};
+                    }
+
+                    uint32_t s = positions.size();
+
+                    positions.insert(positions.end(), side_positions.begin(), side_positions.end());
+                    tex_coords.insert(tex_coords.end(), side_tex_coords.begin(), side_tex_coords.end());
+                    indices.insert(indices.end(), {s, s + 1, s + 3, s + 1, s + 2, s + 3});
+                }
+            }
+        }
+    }
+
+    return {positions, tex_coords, indices};
+}
+
+std::array<Vector<2>, 4> Chunk::face_tex_coords(BlockType type, BlockSide side) {
+    switch (type) {
+        case BlockType::Dirt:
+            return {{
+                {0.5f, 0.0f}, {1.0f, 0.0f}, {1.0f, 0.5f}, {0.5f, 0.5f},
+            }};
+        case BlockType::Grass:
+            switch (side) {
+                case BlockSide::Front:
+                case BlockSide::Back:
+                case BlockSide::Left:
+                case BlockSide::Right:
+                    return {{
+                        {0.5f, 1.0f}, {0.0f, 1.0f},  {0.0f, 0.5f}, {0.5f, 0.5f},
+                    }};
+                case BlockSide::Top:
+                    return {{
+                        {0.0f, 0.0f}, {0.5f, 0.0f}, {0.5f, 0.5f}, {0.0f, 0.5f},
+                    }};
+                case BlockSide::Bottom:
+                    return {{
+                        {0.5f, 0.0f}, {1.0f, 0.0f}, {1.0f, 0.5f}, {0.5f, 0.5f},
+                    }};
+            }
+        default:
+            return {};
+    }}
+
+}
diff --git a/src/World/Chunk.hpp b/src/World/Chunk.hpp
new file mode 100644
index 0000000..eafc613
--- /dev/null
+++ b/src/World/Chunk.hpp
@@ -0,0 +1,30 @@
+#pragma once
+
+#include <cstdint>
+#include "BlockType.hpp"
+#include "../Mesh.hpp"
+#include "BlockSide.hpp"
+
+#define CHUNK_WIDTH 8
+#define CHUNK_HEIGHT 8
+
+namespace MC {
+
+class Chunk {
+public:
+    Chunk() : m_blocks{} {};
+
+    void set(uint32_t x, uint32_t y, uint32_t z, BlockType type);
+
+    Mesh mesh();
+private:
+    static std::array<Vector<2>, 4> face_tex_coords(BlockType type, BlockSide side);
+
+    struct BlockData {
+        BlockType type;
+    };
+
+    BlockData m_blocks[CHUNK_WIDTH][CHUNK_HEIGHT][CHUNK_WIDTH];
+};
+
+}
diff --git a/src/World/Generator.cpp b/src/World/Generator.cpp
new file mode 100644
index 0000000..5b8b9b5
--- /dev/null
+++ b/src/World/Generator.cpp
@@ -0,0 +1,26 @@
+#include "Generator.hpp"
+
+namespace MC {
+
+Chunk Generator::generate(uint32_t _x, uint32_t _y) {
+    Chunk chunk;
+
+    for (int y = 0; y < CHUNK_HEIGHT; y++) {
+        BlockType type = BlockType::Air;
+        if (y < CHUNK_HEIGHT / 2) {
+            type = BlockType::Dirt;
+        } else if (y == CHUNK_HEIGHT / 2) {
+            type = BlockType::Grass;
+        }
+
+        for (int x = 0; x < CHUNK_WIDTH; x++) {
+            for (int z = 0; z < CHUNK_WIDTH; z++) {
+                chunk.set(x, y, z, type);
+            }
+        }
+    }
+
+    return chunk;
+}
+
+}
\ No newline at end of file
diff --git a/src/World/Generator.hpp b/src/World/Generator.hpp
new file mode 100644
index 0000000..8e67ee1
--- /dev/null
+++ b/src/World/Generator.hpp
@@ -0,0 +1,15 @@
+#pragma once
+
+#include <cstdint>
+#include "Chunk.hpp"
+
+namespace MC {
+
+class Generator {
+public:
+    Generator() = default;
+
+    Chunk generate(uint32_t x, uint32_t y);
+};
+
+}