]> git.localhorst.tv Git - blank.git/blob - src/block.hpp
added ability to get seperate information about block face obstruction
[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 / TURN_COUNT); }
46         void SetFace(Face face) { orient = face * TURN_COUNT + GetTurn(); }
47         Turn GetTurn() const { return Turn(orient % TURN_COUNT); }
48         void SetTurn(Turn turn) { orient = GetFace() * TURN_COUNT + turn; }
49
50         static Face Opposite(Face f) {
51                 return Face(f ^ 1);
52         }
53
54         static glm::tvec3<int> FaceNormal(Face face) {
55                 switch (face) {
56                         case FACE_UP:
57                                 return { 0, 1, 0 };
58                         case FACE_DOWN:
59                                 return { 0, -1, 0 };
60                         case FACE_RIGHT:
61                                 return { 1, 0, 0 };
62                         case FACE_LEFT:
63                                 return { -1, 0, 0 };
64                         case FACE_FRONT:
65                                 return { 0, 0, 1 };
66                         case FACE_BACK:
67                                 return { 0, 0, -1 };
68                         default:
69                                 return { 0, 0, 0 };
70                 }
71         }
72
73         static Face NormalFace(const glm::vec3 &norm) {
74                 const glm::vec3 anorm(abs(norm));
75                 if (anorm.x > anorm.y) {
76                         if (anorm.x > anorm.z) {
77                                 return norm.x > 0.0f ? FACE_RIGHT : FACE_LEFT;
78                         } else {
79                                 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
80                         }
81                 } else {
82                         if (anorm.y > anorm.z) {
83                                 return norm.y > 0.0f ? FACE_UP : FACE_DOWN;
84                         } else {
85                                 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
86                         }
87                 }
88         }
89
90         struct FaceSet {
91
92                 explicit FaceSet(unsigned char v = 0)
93                 : value(v) { }
94
95                 bool IsSet(Face f) const {
96                         return value & Mask(f);
97                 }
98                 void Set(Face f) {
99                         value |= Mask(f);
100                 }
101                 void Unset(Face f) {
102                         value |= ~Mask(f);
103                 }
104
105                 void Clear() {
106                         value = 0;
107                 }
108                 void Fill() {
109                         value = Mask(FACE_COUNT) - 1;
110                 }
111
112                 bool Empty() const {
113                         return value == 0;
114                 }
115                 bool All() const {
116                         return value == Mask(FACE_COUNT) - 1;
117                 }
118
119                 unsigned char Mask(Face f) const {
120                         return 1 << f;
121                 }
122
123                 unsigned char value;
124
125         };
126
127 };
128
129
130 /// attributes of a type of block
131 struct BlockType {
132
133         const Shape *shape;
134         glm::vec3 color;
135         glm::vec3 outline_color;
136
137         Block::Type id;
138
139         int luminosity;
140
141         bool visible;
142         bool block_light;
143
144         struct Faces {
145                 bool face[Block::FACE_COUNT];
146                 Faces &operator =(const Faces &other) {
147                         for (int i = 0; i < Block::FACE_COUNT; ++i) {
148                                 face[i] = other.face[i];
149                         }
150                         return *this;
151                 }
152                 bool operator [](Block::Face f) const {
153                         return face[f];
154                 }
155         } fill;
156
157         explicit BlockType(
158                 bool v = false,
159                 const glm::vec3 &color = { 1, 1, 1 },
160                 const Shape *shape = &DEFAULT_SHAPE
161         );
162
163         static const NullShape DEFAULT_SHAPE;
164
165         bool FaceFilled(const Block &, Block::Face) const;
166
167         void FillModel(
168                 Model::Buffer &m,
169                 const glm::mat4 &transform = glm::mat4(1.0f),
170                 Model::Index idx_offset = 0
171         ) const;
172         void FillBlockModel(
173                 BlockModel::Buffer &m,
174                 const glm::mat4 &transform = glm::mat4(1.0f),
175                 BlockModel::Index idx_offset = 0
176         ) const;
177         void FillOutlineModel(
178                 OutlineModel &m,
179                 const glm::vec3 &pos_offset = { 0, 0, 0 },
180                 OutlineModel::Index idx_offset = 0
181         ) const;
182
183 };
184
185
186 class BlockTypeRegistry {
187
188 public:
189         BlockTypeRegistry();
190
191 public:
192         Block::Type Add(const BlockType &);
193
194         size_t Size() const { return types.size(); }
195
196         BlockType *operator [](Block::Type id) { return &types[id]; }
197         const BlockType *Get(Block::Type id) const { return &types[id]; }
198
199 private:
200         std::vector<BlockType> types;
201
202 };
203
204 }
205
206 #endif