]> git.localhorst.tv Git - blank.git/blob - src/world/BlockLookup.hpp
block type prototypability and new types
[blank.git] / src / world / BlockLookup.hpp
1 #ifndef BLANK_WORLD_BLOCKLOOKUP_HPP_
2 #define BLANK_WORLD_BLOCKLOOKUP_HPP_
3
4 #include "Block.hpp"
5 #include "BlockType.hpp"
6 #include "Chunk.hpp"
7
8
9 namespace blank {
10
11 class BlockLookup {
12
13 public:
14         /// resolve chunk/position from oob coordinates
15         BlockLookup(Chunk *c, const RoughLocation::Fine &p) noexcept;
16
17         /// resolve chunk/position from ib coordinates and direction
18         BlockLookup(Chunk *c, const RoughLocation::Fine &p, Block::Face dir) noexcept;
19
20         /// check if lookup was successful
21         operator bool() const { return chunk; }
22
23         // only valid if lookup was successful
24         Chunk &GetChunk() const noexcept { return *chunk; }
25         const RoughLocation::Fine &GetBlockPos() const noexcept { return pos; }
26         int GetBlockIndex() const noexcept { return Chunk::ToIndex(pos); }
27         ExactLocation::Fine GetBlockCoords() const noexcept { return Chunk::ToCoords(pos); }
28         const Block &GetBlock() const noexcept { return GetChunk().BlockAt(GetBlockPos()); }
29         const BlockType &GetType() const noexcept { return GetChunk().Type(GetBlock()); }
30         int GetLight() const noexcept { return GetChunk().GetLight(GetBlockPos()); }
31
32         bool FaceFilled(Block::Face f) const noexcept { return GetType().FaceFilled(GetBlock(), f); }
33
34         void SetBlock(const Block &b) noexcept { GetChunk().SetBlock(GetBlockPos(), b); }
35
36         // traverse in given direction
37         BlockLookup Next(Block::Face f) const { return BlockLookup(chunk, pos, f); }
38
39 private:
40         Chunk *chunk;
41         RoughLocation::Fine pos;
42
43 };
44
45 }
46
47 #endif