summary refs log tree commit diff
path: root/src/World/ChunkRegistry.hpp
blob: b6d1613fff209cb68d067322b9fc42f27fdcba23 (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
#pragma once

#include <optional>
#include <unordered_map>
#include "../Time.hpp"
#include "Chunk.hpp"
#include "ChunkIndex.hpp"
#include "Position.hpp"

namespace MC::World {

class ChunkRegistry {
public:
    enum class Status {
        Empty,
        WaitingForGeneration,
        NeedsReification,
        WaitingForReification,
        Damaged,
        Done
    };

    // I think a Chunk entity should just store all this by itself...
    struct Data {
        ChunkIndex index;
        Status status;
        Time::Tick generated_at_tick;
        std::optional<Chunk> chunk = {};

        std::optional<GFX::Mesh> land_mesh = {};
        std::optional<GFX::Mesh> water_mesh = {};

        Bool generated() const {
            return chunk.has_value();
        }

        Status get_status() const {
            if (status == Status::Done && chunk.value().is_damaged()) { return Status::Damaged; }
            return status;
        }

        void damage() {
            // if (status == Status::Done) chunk.value().damage();
            // TODO: Properly damage the chunk, notifying the nearby chunks, too.
            status = Status::NeedsReification;
        }
    };

    Data& get(ChunkIndex index);

    Data* find(Position::BlockWorld pos);
    Data* find(Position::World pos);
private:
    std::unordered_map<ChunkIndex, Data> m_chunks;
};

}