]> git.localhorst.tv Git - blank.git/blob - src/world/block.cpp
entity/world collision response
[blank.git] / src / world / block.cpp
1 #include "Block.hpp"
2 #include "BlockType.hpp"
3 #include "BlockTypeRegistry.hpp"
4
5 #include "../model/geometry.hpp"
6
7 #include <ostream>
8 #include <glm/gtx/euler_angles.hpp>
9 #include <glm/gtx/transform.hpp>
10
11
12 namespace blank {
13
14 const NullShape BlockType::DEFAULT_SHAPE;
15
16
17 bool operator ==(const Block &a, const Block &b) {
18         return a.type == b.type && a.orient == b.orient;
19 }
20
21 std::ostream &operator <<(std::ostream &out, const Block &block) {
22         return out << "Block(" << block.type << ", " << block.GetFace() << ", " << block.GetTurn() << ')';
23 }
24
25 std::ostream &operator <<(std::ostream &out, const Block::Face &face) {
26         switch (face) {
27                 case Block::FACE_UP:
28                         out << "FACE_UP";
29                         break;
30                 case Block::FACE_DOWN:
31                         out << "FACE_DOWN";
32                         break;
33                 case Block::FACE_RIGHT:
34                         out << "FACE_RIGHT";
35                         break;
36                 case Block::FACE_LEFT:
37                         out << "FACE_LEFT";
38                         break;
39                 case Block::FACE_FRONT:
40                         out << "FACE_FRONT";
41                         break;
42                 case Block::FACE_BACK:
43                         out << "FACE_BACK";
44                         break;
45                 default:
46                 case Block::FACE_COUNT:
47                         out << "invalid Block::Face";
48                         break;
49         }
50         return out;
51 }
52
53 std::ostream &operator <<(std::ostream &out, const Block::Turn &turn) {
54         switch (turn) {
55                 case Block::TURN_NONE:
56                         out << "TURN_NONE";
57                         break;
58                 case Block::TURN_LEFT:
59                         out << "TURN_LEFT";
60                         break;
61                 case Block::TURN_AROUND:
62                         out << "TURN_AROUND";
63                         break;
64                 case Block::TURN_RIGHT:
65                         out << "TURN_RIGHT";
66                         break;
67                 default:
68                 case Block::TURN_COUNT:
69                         out << "invalid Block::Turn";
70                         break;
71         }
72         return out;
73 }
74
75
76 BlockType::BlockType(bool v, const glm::vec3 &col, const Shape *s) noexcept
77 : shape(s)
78 , color(col)
79 , outline_color(-1, -1, -1)
80 , id(0)
81 , luminosity(0)
82 , visible(v)
83 , block_light(false)
84 , collision(false)
85 , collide_block(false)
86 , fill({ false, false, false, false, false, false }) {
87
88 }
89
90 void BlockType::FillModel(
91         Model::Buffer &buf,
92         const glm::mat4 &transform,
93         Model::Index idx_offset
94 ) const noexcept {
95         shape->Vertices(buf.vertices, buf.normals, buf.indices, transform, idx_offset);
96         buf.colors.insert(buf.colors.end(), shape->VertexCount(), color);
97 }
98
99 void BlockType::FillBlockModel(
100         BlockModel::Buffer &buf,
101         const glm::mat4 &transform,
102         BlockModel::Index idx_offset
103 ) const noexcept {
104         shape->Vertices(buf.vertices, buf.indices, transform, idx_offset);
105         buf.colors.insert(buf.colors.end(), shape->VertexCount(), color);
106 }
107
108 void BlockType::FillOutlineModel(
109         OutlineModel &model,
110         const glm::vec3 &pos_offset,
111         OutlineModel::Index idx_offset
112 ) const noexcept {
113         shape->Outline(model.vertices, model.indices, pos_offset, idx_offset);
114         model.colors.insert(model.colors.end(), shape->OutlineCount(), outline_color);
115 }
116
117
118 BlockTypeRegistry::BlockTypeRegistry() {
119         Add(BlockType());
120 }
121
122 Block::Type BlockTypeRegistry::Add(const BlockType &t) {
123         int id = types.size();
124         types.push_back(t);
125         types.back().id = id;
126         return id;
127 }
128
129
130 const glm::mat4 Block::orient2transform[ORIENT_COUNT] = {
131         {  1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: up,    turn: none
132         {  0,  0, -1,  0,  0,  1,  0,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: up,    turn: left
133         { -1,  0,  0,  0,  0,  1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: up,    turn: around
134         {  0,  0,  1,  0,  0,  1,  0,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: up,    turn: right
135         {  1,  0,  0,  0,  0, -1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: down,  turn: none
136         {  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: down,  turn: left
137         { -1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: down,  turn: around
138         {  0,  0,  1,  0,  0, -1,  0,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: down,  turn: right
139         {  0, -1,  0,  0,  1,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: right, turn: none
140         {  0, -1,  0,  0,  0,  0, -1,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: right, turn: left
141         {  0, -1,  0,  0, -1,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: right, turn: around
142         {  0, -1,  0,  0,  0,  0,  1,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: right, turn: right
143         {  0,  1,  0,  0, -1,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: left,  turn: none
144         {  0,  1,  0,  0,  0,  0,  1,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: left,  turn: left
145         {  0,  1,  0,  0,  1,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: left,  turn: around
146         {  0,  1,  0,  0,  0,  0, -1,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: left,  turn: right
147         {  1,  0,  0,  0,  0,  0,  1,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: none
148         {  0,  0, -1,  0,  1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: left
149         { -1,  0,  0,  0,  0,  0, -1,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: around
150         {  0,  0,  1,  0, -1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: right
151         {  1,  0,  0,  0,  0,  0, -1,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: none
152         {  0,  0, -1,  0, -1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: left
153         { -1,  0,  0,  0,  0,  0,  1,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: around
154         {  0,  0,  1,  0,  1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: right
155 };
156
157 const glm::tvec3<int> Block::face2normal[FACE_COUNT] = {
158         {  0,  1,  0 },
159         {  0, -1,  0 },
160         {  1,  0,  0 },
161         { -1,  0,  0 },
162         {  0,  0,  1 },
163         {  0,  0, -1 },
164 };
165
166 const Block::Face Block::orient2face[ORIENT_COUNT][FACE_COUNT] = {
167         { FACE_UP,    FACE_DOWN,  FACE_RIGHT, FACE_LEFT,  FACE_FRONT, FACE_BACK,  }, // face: up,    turn: none
168         { FACE_UP,    FACE_DOWN,  FACE_FRONT, FACE_BACK,  FACE_LEFT,  FACE_RIGHT, }, // face: up,    turn: left
169         { FACE_UP,    FACE_DOWN,  FACE_LEFT,  FACE_RIGHT, FACE_BACK,  FACE_FRONT, }, // face: up,    turn: around
170         { FACE_UP,    FACE_DOWN,  FACE_BACK,  FACE_FRONT, FACE_RIGHT, FACE_LEFT,  }, // face: up,    turn: right
171         { FACE_DOWN,  FACE_UP,    FACE_RIGHT, FACE_LEFT,  FACE_BACK,  FACE_FRONT, }, // face: down,  turn: none
172         { FACE_DOWN,  FACE_UP,    FACE_BACK,  FACE_FRONT, FACE_LEFT,  FACE_RIGHT, }, // face: down,  turn: left
173         { FACE_DOWN,  FACE_UP,    FACE_LEFT,  FACE_RIGHT, FACE_FRONT, FACE_BACK,  }, // face: down,  turn: around
174         { FACE_DOWN,  FACE_UP,    FACE_FRONT, FACE_BACK,  FACE_RIGHT, FACE_LEFT,  }, // face: down,  turn: right
175         { FACE_LEFT,  FACE_RIGHT, FACE_UP,    FACE_DOWN,  FACE_FRONT, FACE_BACK,  }, // face: right, turn: none
176         { FACE_LEFT,  FACE_RIGHT, FACE_FRONT, FACE_BACK,  FACE_DOWN,  FACE_UP,    }, // face: right, turn: left
177         { FACE_LEFT,  FACE_RIGHT, FACE_DOWN,  FACE_UP,    FACE_BACK,  FACE_FRONT, }, // face: right, turn: around
178         { FACE_LEFT,  FACE_RIGHT, FACE_BACK,  FACE_FRONT, FACE_UP,    FACE_DOWN,  }, // face: right, turn: right
179         { FACE_RIGHT, FACE_LEFT,  FACE_DOWN,  FACE_UP,    FACE_FRONT, FACE_BACK,  }, // face: left,  turn: none
180         { FACE_RIGHT, FACE_LEFT,  FACE_FRONT, FACE_BACK,  FACE_UP,    FACE_DOWN,  }, // face: left,  turn: left
181         { FACE_RIGHT, FACE_LEFT,  FACE_UP,    FACE_DOWN,  FACE_BACK,  FACE_FRONT, }, // face: left,  turn: around
182         { FACE_RIGHT, FACE_LEFT,  FACE_BACK,  FACE_FRONT, FACE_DOWN,  FACE_UP,    }, // face: left,  turn: right
183         { FACE_BACK,  FACE_FRONT, FACE_RIGHT, FACE_LEFT,  FACE_UP,    FACE_DOWN,  }, // face: front, turn: none
184         { FACE_BACK,  FACE_FRONT, FACE_UP,    FACE_DOWN,  FACE_LEFT,  FACE_RIGHT, }, // face: front, turn: left
185         { FACE_BACK,  FACE_FRONT, FACE_LEFT,  FACE_RIGHT, FACE_DOWN,  FACE_UP,    }, // face: front, turn: around
186         { FACE_BACK,  FACE_FRONT, FACE_DOWN,  FACE_UP,    FACE_RIGHT, FACE_LEFT,  }, // face: front, turn: right
187         { FACE_FRONT, FACE_BACK,  FACE_RIGHT, FACE_LEFT,  FACE_DOWN,  FACE_UP,    }, // face: back,  turn: none
188         { FACE_FRONT, FACE_BACK,  FACE_DOWN,  FACE_UP,    FACE_LEFT,  FACE_RIGHT, }, // face: back,  turn: left
189         { FACE_FRONT, FACE_BACK,  FACE_LEFT,  FACE_RIGHT, FACE_UP,    FACE_DOWN,  }, // face: back,  turn: around
190         { FACE_FRONT, FACE_BACK,  FACE_UP,    FACE_DOWN,  FACE_RIGHT, FACE_LEFT,  }, // face: back,  turn: right
191 };
192
193 }