]> git.localhorst.tv Git - blank.git/blob - src/block.hpp
allow face/turn selection of placed blocks
[blank.git] / src / block.hpp
1 #ifndef BLANK_BLOCK_HPP_
2 #define BLANK_BLOCK_HPP_
3
4 #include "geometry.hpp"
5 #include "model.hpp"
6 #include "shape.hpp"
7
8 #include <vector>
9 #include <glm/glm.hpp>
10
11
12 namespace blank {
13
14 /// single 1x1x1 cube
15 struct Block {
16
17         using Type = unsigned short;
18         using Pos = glm::vec3;
19
20         enum Face {
21                 FACE_UP,
22                 FACE_DOWN,
23                 FACE_RIGHT,
24                 FACE_LEFT,
25                 FACE_FRONT,
26                 FACE_BACK,
27                 FACE_COUNT,
28         };
29         enum Turn {
30                 TURN_NONE,
31                 TURN_LEFT,
32                 TURN_AROUND,
33                 TURN_RIGHT,
34                 TURN_COUNT,
35         };
36
37         Type type;
38         unsigned char orient;
39
40         constexpr explicit Block(Type type = 0, Face face = FACE_UP, Turn turn = TURN_NONE)
41         : type(type), orient(face * TURN_COUNT + turn) { }
42
43         const glm::mat4 &Transform() const;
44
45         Face GetFace() const { return Face(orient / 4); }
46         void SetFace(Face face) { orient = face * TURN_COUNT + GetTurn(); }
47         Turn GetTurn() const { return Turn(orient % 4); }
48         void SetTurn(Turn turn) { orient = GetFace() * TURN_COUNT + turn; }
49
50 };
51
52
53 /// attributes of a type of block
54 struct BlockType {
55
56         const Shape *shape;
57         glm::vec3 color;
58         glm::vec3 outline_color;
59
60         Block::Type id;
61
62         bool visible;
63
64         struct Faces {
65                 bool up;
66                 bool down;
67                 bool right;
68                 bool left;
69                 bool front;
70                 bool back;
71         } fill;
72
73         explicit BlockType(
74                 bool v = false,
75                 const glm::vec3 &color = { 1, 1, 1 },
76                 const Shape *shape = &DEFAULT_SHAPE
77         );
78
79         static const NullShape DEFAULT_SHAPE;
80
81
82         void FillModel(
83                 Model::Buffer &m,
84                 const glm::mat4 &transform = glm::mat4(1.0f),
85                 Model::Index idx_offset = 0
86         ) const;
87         void FillOutlineModel(
88                 OutlineModel &m,
89                 const glm::vec3 &pos_offset = { 0, 0, 0 },
90                 OutlineModel::Index idx_offset = 0
91         ) const;
92
93 };
94
95
96 class BlockTypeRegistry {
97
98 public:
99         BlockTypeRegistry();
100
101 public:
102         Block::Type Add(const BlockType &);
103
104         size_t Size() const { return types.size(); }
105
106         BlockType *operator [](Block::Type id) { return &types[id]; }
107         const BlockType *Get(Block::Type id) const { return &types[id]; }
108
109 private:
110         std::vector<BlockType> types;
111
112 };
113
114 }
115
116 #endif