summary refs log tree commit diff
path: root/src/World/Chunk.cpp
blob: eeaefa0587b7ba41f5c95503a813a3a9024f38b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Chunk.hpp"
#include "BlockSide.hpp"

namespace MC::World {

const Chunk::BlockData& Chunk::at(U32 x, U32 y, U32 z) const {
    return m_blocks.at(pos(x, y, z));
}

Chunk::BlockData& Chunk::at(U32 x, U32 y, U32 z) {
    return m_blocks.at(pos(x, y, z));
}

const Chunk::BlockData& Chunk::at(Position::BlockLocal pos) const {
    return at(pos.x(), pos.y(), pos.z());
}

Chunk::BlockData& Chunk::at(Position::BlockLocal pos) {
    return at(pos.x(), pos.y(), pos.z());
}

ChunkIndex Chunk::index() const {
    return m_index;
}

Vector<3> Chunk::position() const {
    return m_position;
}

Bool Chunk::is_damaged() const {
    return m_damaged;
}

void Chunk::damage() {
    m_damaged = true;
}

Bool Chunk::is_valid_position(Position::BlockLocal pos) {
    return pos.x() < Width && pos.y() < Height && pos.z() < Width;
}

U64 Chunk::pos(U32 const x, U32 const y, U32 const z) {
    return x + Width * y + Width * Height * z;
}

Position::BlockLocal Chunk::pos(U64 const i) {
    return {
        i % Width,
        i / Width % Height,
        i / (Width * Height)
    };
}

AABB Chunk::block_bounds(Position::BlockLocal pos) {
    return {
        {pos.x(), pos.y(), pos.z()},
        {pos.x() + 1, pos.y() + 1, pos.z() + 1},
    };
}

AABB Chunk::block_bounds(Position::BlockWorld pos) {
    auto chunk_position = ChunkIndex::from_position(pos).world_position();
    return block_bounds(pos.to_local()).offset(Vec3(chunk_position));
}

}