]> git.localhorst.tv Git - blank.git/blob - src/world/block.cpp
block sounds depending on block type
[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 std::ostream &operator <<(std::ostream &out, const Block &block) {
15         return out << "Block(" << block.type << ", " << block.GetFace() << ", " << block.GetTurn() << ')';
16 }
17
18 std::ostream &operator <<(std::ostream &out, const Block::Face &face) {
19         switch (face) {
20                 case Block::FACE_UP:
21                         out << "FACE_UP";
22                         break;
23                 case Block::FACE_DOWN:
24                         out << "FACE_DOWN";
25                         break;
26                 case Block::FACE_RIGHT:
27                         out << "FACE_RIGHT";
28                         break;
29                 case Block::FACE_LEFT:
30                         out << "FACE_LEFT";
31                         break;
32                 case Block::FACE_FRONT:
33                         out << "FACE_FRONT";
34                         break;
35                 case Block::FACE_BACK:
36                         out << "FACE_BACK";
37                         break;
38                 default:
39                 case Block::FACE_COUNT:
40                         out << "invalid Block::Face";
41                         break;
42         }
43         return out;
44 }
45
46 std::ostream &operator <<(std::ostream &out, const Block::Turn &turn) {
47         switch (turn) {
48                 case Block::TURN_NONE:
49                         out << "TURN_NONE";
50                         break;
51                 case Block::TURN_LEFT:
52                         out << "TURN_LEFT";
53                         break;
54                 case Block::TURN_AROUND:
55                         out << "TURN_AROUND";
56                         break;
57                 case Block::TURN_RIGHT:
58                         out << "TURN_RIGHT";
59                         break;
60                 default:
61                 case Block::TURN_COUNT:
62                         out << "invalid Block::Turn";
63                         break;
64         }
65         return out;
66 }
67
68
69 BlockType::BlockType() noexcept
70 : shape(nullptr)
71 , textures()
72 , hsl_mod(0.0f, 1.0f, 1.0f)
73 , rgb_mod(1.0f, 1.0f, 1.0f)
74 , outline_color(-1, -1, -1)
75 , label("some block")
76 , place_sound(-1)
77 , remove_sound(-1)
78 , id(0)
79 , luminosity(0)
80 , visible(true)
81 , block_light(true)
82 , collision(true)
83 , collide_block(true)
84 , generate(false)
85 , min_solidity(0.5f)
86 , mid_solidity(0.75f)
87 , max_solidity(1.0f)
88 , min_humidity(-1.0f)
89 , mid_humidity(0.0f)
90 , max_humidity(1.0f)
91 , min_temperature(-1.0f)
92 , mid_temperature(0.0f)
93 , max_temperature(1.0f)
94 , min_richness(-1.0f)
95 , mid_richness(0.0f)
96 , max_richness(1.0f)
97 , commonness(1.0f) {
98
99 }
100
101 void BlockType::FillEntityMesh(
102         EntityMesh::Buffer &buf,
103         const glm::mat4 &transform
104 ) const noexcept {
105         if (!shape) return;
106         shape->Fill(buf, transform, textures);
107         buf.hsl_mods.insert(buf.hsl_mods.end(), shape->VertexCount(), hsl_mod);
108         buf.rgb_mods.insert(buf.rgb_mods.end(), shape->VertexCount(), rgb_mod);
109 }
110
111 void BlockType::FillBlockMesh(
112         BlockMesh::Buffer &buf,
113         const glm::mat4 &transform,
114         BlockMesh::Index idx_offset
115 ) const noexcept {
116         if (!shape) return;
117         shape->Fill(buf, transform, textures, idx_offset);
118         buf.hsl_mods.insert(buf.hsl_mods.end(), shape->VertexCount(), hsl_mod);
119         buf.rgb_mods.insert(buf.rgb_mods.end(), shape->VertexCount(), rgb_mod);
120 }
121
122 void BlockType::FillOutlineMesh(OutlineMesh::Buffer &buf) const noexcept {
123         if (!shape) return;
124         shape->Outline(buf);
125         buf.colors.insert(buf.colors.end(), shape->OutlineCount(), outline_color);
126 }
127
128
129 BlockTypeRegistry::BlockTypeRegistry() {
130         BlockType air;
131         air.visible = false;
132         air.block_light = false;
133         air.collision = false;
134         air.collide_block = false;
135         Add(air);
136 }
137
138 Block::Type BlockTypeRegistry::Add(const BlockType &t) {
139         int id = types.size();
140         types.push_back(t);
141         types.back().id = id;
142         return id;
143 }
144
145
146 const glm::mat4 Block::orient2transform[ORIENT_COUNT] = {
147         {  1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: up,    turn: none
148         {  0,  0, -1,  0,  0,  1,  0,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: up,    turn: left
149         { -1,  0,  0,  0,  0,  1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: up,    turn: around
150         {  0,  0,  1,  0,  0,  1,  0,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: up,    turn: right
151         {  1,  0,  0,  0,  0, -1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: down,  turn: none
152         {  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: down,  turn: left
153         { -1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: down,  turn: around
154         {  0,  0,  1,  0,  0, -1,  0,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: down,  turn: right
155         {  0, -1,  0,  0,  1,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: right, turn: none
156         {  0, -1,  0,  0,  0,  0, -1,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: right, turn: left
157         {  0, -1,  0,  0, -1,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: right, turn: around
158         {  0, -1,  0,  0,  0,  0,  1,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: right, turn: right
159         {  0,  1,  0,  0, -1,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  1, }, // face: left,  turn: none
160         {  0,  1,  0,  0,  0,  0,  1,  0,  1,  0,  0,  0,  0,  0,  0,  1, }, // face: left,  turn: left
161         {  0,  1,  0,  0,  1,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  1, }, // face: left,  turn: around
162         {  0,  1,  0,  0,  0,  0, -1,  0, -1,  0,  0,  0,  0,  0,  0,  1, }, // face: left,  turn: right
163         {  1,  0,  0,  0,  0,  0,  1,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: none
164         {  0,  0, -1,  0,  1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: left
165         { -1,  0,  0,  0,  0,  0, -1,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: around
166         {  0,  0,  1,  0, -1,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0,  1, }, // face: front, turn: right
167         {  1,  0,  0,  0,  0,  0, -1,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: none
168         {  0,  0, -1,  0, -1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: left
169         { -1,  0,  0,  0,  0,  0,  1,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: around
170         {  0,  0,  1,  0,  1,  0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  1, }, // face: back,  turn: right
171 };
172
173 const glm::ivec3 Block::face2normal[FACE_COUNT] = {
174         {  0,  1,  0 },
175         {  0, -1,  0 },
176         {  1,  0,  0 },
177         { -1,  0,  0 },
178         {  0,  0,  1 },
179         {  0,  0, -1 },
180 };
181
182 const Block::Face Block::orient2face[ORIENT_COUNT][FACE_COUNT] = {
183         { FACE_UP,    FACE_DOWN,  FACE_RIGHT, FACE_LEFT,  FACE_FRONT, FACE_BACK,  }, // face: up,    turn: none
184         { FACE_UP,    FACE_DOWN,  FACE_FRONT, FACE_BACK,  FACE_LEFT,  FACE_RIGHT, }, // face: up,    turn: left
185         { FACE_UP,    FACE_DOWN,  FACE_LEFT,  FACE_RIGHT, FACE_BACK,  FACE_FRONT, }, // face: up,    turn: around
186         { FACE_UP,    FACE_DOWN,  FACE_BACK,  FACE_FRONT, FACE_RIGHT, FACE_LEFT,  }, // face: up,    turn: right
187         { FACE_DOWN,  FACE_UP,    FACE_RIGHT, FACE_LEFT,  FACE_BACK,  FACE_FRONT, }, // face: down,  turn: none
188         { FACE_DOWN,  FACE_UP,    FACE_BACK,  FACE_FRONT, FACE_LEFT,  FACE_RIGHT, }, // face: down,  turn: left
189         { FACE_DOWN,  FACE_UP,    FACE_LEFT,  FACE_RIGHT, FACE_FRONT, FACE_BACK,  }, // face: down,  turn: around
190         { FACE_DOWN,  FACE_UP,    FACE_FRONT, FACE_BACK,  FACE_RIGHT, FACE_LEFT,  }, // face: down,  turn: right
191         { FACE_LEFT,  FACE_RIGHT, FACE_UP,    FACE_DOWN,  FACE_FRONT, FACE_BACK,  }, // face: right, turn: none
192         { FACE_LEFT,  FACE_RIGHT, FACE_FRONT, FACE_BACK,  FACE_DOWN,  FACE_UP,    }, // face: right, turn: left
193         { FACE_LEFT,  FACE_RIGHT, FACE_DOWN,  FACE_UP,    FACE_BACK,  FACE_FRONT, }, // face: right, turn: around
194         { FACE_LEFT,  FACE_RIGHT, FACE_BACK,  FACE_FRONT, FACE_UP,    FACE_DOWN,  }, // face: right, turn: right
195         { FACE_RIGHT, FACE_LEFT,  FACE_DOWN,  FACE_UP,    FACE_FRONT, FACE_BACK,  }, // face: left,  turn: none
196         { FACE_RIGHT, FACE_LEFT,  FACE_FRONT, FACE_BACK,  FACE_UP,    FACE_DOWN,  }, // face: left,  turn: left
197         { FACE_RIGHT, FACE_LEFT,  FACE_UP,    FACE_DOWN,  FACE_BACK,  FACE_FRONT, }, // face: left,  turn: around
198         { FACE_RIGHT, FACE_LEFT,  FACE_BACK,  FACE_FRONT, FACE_DOWN,  FACE_UP,    }, // face: left,  turn: right
199         { FACE_BACK,  FACE_FRONT, FACE_RIGHT, FACE_LEFT,  FACE_UP,    FACE_DOWN,  }, // face: front, turn: none
200         { FACE_BACK,  FACE_FRONT, FACE_UP,    FACE_DOWN,  FACE_LEFT,  FACE_RIGHT, }, // face: front, turn: left
201         { FACE_BACK,  FACE_FRONT, FACE_LEFT,  FACE_RIGHT, FACE_DOWN,  FACE_UP,    }, // face: front, turn: around
202         { FACE_BACK,  FACE_FRONT, FACE_DOWN,  FACE_UP,    FACE_RIGHT, FACE_LEFT,  }, // face: front, turn: right
203         { FACE_FRONT, FACE_BACK,  FACE_RIGHT, FACE_LEFT,  FACE_DOWN,  FACE_UP,    }, // face: back,  turn: none
204         { FACE_FRONT, FACE_BACK,  FACE_DOWN,  FACE_UP,    FACE_LEFT,  FACE_RIGHT, }, // face: back,  turn: left
205         { FACE_FRONT, FACE_BACK,  FACE_LEFT,  FACE_RIGHT, FACE_UP,    FACE_DOWN,  }, // face: back,  turn: around
206         { FACE_FRONT, FACE_BACK,  FACE_UP,    FACE_DOWN,  FACE_RIGHT, FACE_LEFT,  }, // face: back,  turn: right
207 };
208
209 }