summary refs log tree commit diff
path: root/src/World/Chunk.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/World/Chunk.cpp')
-rw-r--r--src/World/Chunk.cpp64
1 files changed, 38 insertions, 26 deletions
diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp
index 1874950..9642fa9 100644
--- a/src/World/Chunk.cpp
+++ b/src/World/Chunk.cpp
@@ -4,11 +4,11 @@
 namespace MC::World {
 
 void Chunk::set(uint32_t x, uint32_t y, uint32_t z, BlockData data) {
-    m_blocks[pos(x, y, z)] = data;
+    m_blocks.at(pos(x, y, z)) = data;
 }
 
-Chunk::BlockData Chunk::get(uint32_t x, uint32_t y, uint32_t z) {
-    return m_blocks[pos(x, y, z)];
+Chunk::BlockData Chunk::get(uint32_t x, uint32_t y, uint32_t z) const {
+    return m_blocks.at(pos(x, y, z));
 }
 
 GFX::Mesh Chunk::mesh() {
@@ -17,9 +17,9 @@ GFX::Mesh Chunk::mesh() {
     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++) {
+    for (int x = 0; x < Width; x++) {
+        for (int y = 0; y < Height; y++) {
+            for (int z = 0; z < Width; z++) {
                 auto type = get(x, y, z).type;
                 if (type == BlockType::Air) {
                     continue;
@@ -31,8 +31,8 @@ GFX::Mesh Chunk::mesh() {
                     }
 
                     auto side_positions = side.face();
-                    auto side_normals = Chunk::face_normals(side);
-                    auto side_tex_coords = Chunk::face_tex_coords(type, side);
+                    auto side_normals = face_normals(side);
+                    auto side_tex_coords = face_tex_coords(type, side);
 
                     for (auto& position : side_positions) {
                         position = position + Vector<3>{static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)};
@@ -87,9 +87,9 @@ bool Chunk::is_face_visible(uint32_t x, uint32_t y, uint32_t z, BlockSide side)
     };
 
     if (
-        neighbor_pos.x() >= Chunk::Width || neighbor_pos.x() < 0 ||
-        neighbor_pos.y() >= Chunk::Height || neighbor_pos.y() < 0 ||
-        neighbor_pos.z() >= Chunk::Width || neighbor_pos.z() < 0
+        neighbor_pos.x() >= Width || neighbor_pos.x() < 0 ||
+        neighbor_pos.y() >= Height || neighbor_pos.y() < 0 ||
+        neighbor_pos.z() >= Width || neighbor_pos.z() < 0
     ) {
         return true;
     }
@@ -103,8 +103,8 @@ bool Chunk::is_face_visible(uint32_t x, uint32_t y, uint32_t z, BlockSide side)
 }
 
 std::array<Vector<2>, 4> Chunk::face_tex_coords(BlockType type, BlockSide side) {
-    uint8_t atlas_width = 2;
-    uint8_t atlas_height = 3;
+    uint8_t atlas_width = 4;
+    uint8_t atlas_height = 4;
 
     float width_step = 1.0f / atlas_width;
     float height_step = 1.0f / atlas_height;
@@ -125,22 +125,34 @@ std::array<Vector<2>, 4> Chunk::face_tex_coords(BlockType type, BlockSide side)
             return block_coords(1, 0);
         case BlockType::Grass:
             switch (side) {
-                case BlockSide::Front:
-                case BlockSide::Back:
-                case BlockSide::Left:
-                case BlockSide::Right:
-                    return block_coords(0, 1);
-                case BlockSide::Top:
-                    return block_coords(0, 0);
-                case BlockSide::Bottom:
-                    return block_coords(1, 0);
+            case BlockSide::Front:
+            case BlockSide::Back:
+            case BlockSide::Left:
+            case BlockSide::Right:
+                return block_coords(2, 0);
+            case BlockSide::Bottom:
+                return block_coords(1, 0);
+            case BlockSide::Top:
+                return block_coords(0, 0);
             }
         case BlockType::Stone:
-            return block_coords(1, 1);
+            return block_coords(3, 0);
         case BlockType::Sand:
-            return block_coords(0, 2);
+            return block_coords(0, 1);
         case BlockType::Water:
-            return block_coords(1, 2);
+            return block_coords(1, 1);
+        case BlockType::Snow:
+            switch (side) {
+            case BlockSide::Front:
+            case BlockSide::Back:
+            case BlockSide::Left:
+            case BlockSide::Right:
+                return block_coords(3, 1);
+            case BlockSide::Bottom:
+                return block_coords(1, 0);
+            case BlockSide::Top:
+                return block_coords(2, 1);
+            }
         case BlockType::Air:
             return {};
     }
@@ -159,7 +171,7 @@ std::array<Vector<3>, 4> Chunk::face_normals(BlockSide side) {
 }
 
 uint64_t Chunk::pos(uint32_t x, uint32_t y, uint32_t z) {
-    return x + Chunk::Width * y + Chunk::Width * Chunk::Height * z;
+    return x + Width * y + Width * Height * z;
 }
 
 }