]> git.localhorst.tv Git - blank.git/blob - src/ui/ui.cpp
test entities for world collision
[blank.git] / src / ui / ui.cpp
1 #include "HUD.hpp"
2 #include "Interface.hpp"
3
4 #include "../app/init.hpp"
5 #include "../graphics/DirectionalLighting.hpp"
6 #include "../model/shapes.hpp"
7 #include "../world/World.hpp"
8
9 #include <iostream>
10 #include <glm/gtc/matrix_transform.hpp>
11 #include <glm/gtx/io.hpp>
12
13
14 namespace blank {
15
16 HUD::HUD(const BlockTypeRegistry &types)
17 : types(types)
18 , block()
19 , block_buf()
20 , block_transform(1.0f)
21 , block_visible(false)
22 , crosshair()
23 , crosshair_transform(1.0f)
24 , near(100.0f)
25 , far(-100.0f)
26 , projection(glm::ortho(0.0f, 1.0f, 1.0f, 0.0f, near, far))
27 , view(glm::translate(glm::mat4(1.0f), glm::vec3(-0.5f, -0.5f, 0))) {
28         block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
29         block_transform = glm::scale(block_transform, glm::vec3(50.0f));
30         block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
31         block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
32
33         crosshair.vertices = std::vector<glm::vec3>({
34                 { -10.0f,   0.0f, 0.0f }, { 10.0f,  0.0f, 0.0f },
35                 {   0.0f, -10.0f, 0.0f }, {  0.0f, 10.0f, 0.0f },
36         });
37         crosshair.indices = std::vector<OutlineModel::Index>({
38                 0, 1, 2, 3
39         });
40         crosshair.colors.resize(4, { 10.0f, 10.0f, 10.0f });
41         crosshair.Invalidate();
42 }
43
44
45 void HUD::Viewport(float width, float height) noexcept {
46         Viewport(0, 0, width, height);
47 }
48
49 void HUD::Viewport(float x, float y, float width, float height) noexcept {
50         projection = glm::ortho(x, width, height, y, near, far);
51         crosshair_transform = glm::translate(glm::mat4(1.0f), glm::vec3(width * 0.5f, height * 0.5f, 0.0f));
52 }
53
54
55 void HUD::Display(const Block &b) {
56         const BlockType &type = types.Get(b.type);
57
58         block_buf.Clear();
59         type.FillModel(block_buf, b.Transform());
60         block.Update(block_buf);
61         block_visible = type.visible;
62 }
63
64
65 void HUD::Render(DirectionalLighting &program) noexcept {
66         if (block_visible) {
67                 program.SetLightDirection({ 1.0f, 3.0f, 5.0f });
68                 // disable distance fog
69                 program.SetFogDensity(0.0f);
70                 GLContext::ClearDepthBuffer();
71                 program.SetMVP(block_transform, view, projection);
72                 block.Draw();
73                 program.SetM(crosshair_transform);
74                 crosshair.Draw();
75         }
76 }
77
78
79 Interface::Interface(const Config &config, World &world)
80 : world(world)
81 , ctrl(world.Player())
82 , hud(world.BlockTypes())
83 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
84 , aim_chunk(nullptr)
85 , aim_block(0)
86 , aim_normal()
87 , outline()
88 , outline_transform(1.0f)
89 , config(config)
90 , place_timer(256)
91 , remove_timer(256)
92 , remove(0)
93 , selection(1)
94 , fwd(0)
95 , rev(0) {
96         hud.Viewport(960, 600);
97         hud.Display(selection);
98 }
99
100
101 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
102         if (config.keyboard_disabled) return;
103
104         switch (event.keysym.sym) {
105                 case SDLK_w:
106                         rev.z = 1;
107                         break;
108                 case SDLK_s:
109                         fwd.z = 1;
110                         break;
111                 case SDLK_a:
112                         rev.x = 1;
113                         break;
114                 case SDLK_d:
115                         fwd.x = 1;
116                         break;
117                 case SDLK_SPACE:
118                         fwd.y = 1;
119                         break;
120                 case SDLK_LSHIFT:
121                         rev.y = 1;
122                         break;
123
124                 case SDLK_q:
125                         FaceBlock();
126                         break;
127                 case SDLK_e:
128                         TurnBlock();
129                         break;
130
131                 case SDLK_b:
132                         PrintBlockInfo();
133                         break;
134                 case SDLK_c:
135                         PrintChunkInfo();
136                         break;
137                 case SDLK_l:
138                         PrintLightInfo();
139                         break;
140                 case SDLK_p:
141                         PrintSelectionInfo();
142                         break;
143         }
144 }
145
146 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
147         if (config.keyboard_disabled) return;
148
149         switch (event.keysym.sym) {
150                 case SDLK_w:
151                         rev.z = 0;
152                         break;
153                 case SDLK_s:
154                         fwd.z = 0;
155                         break;
156                 case SDLK_a:
157                         rev.x = 0;
158                         break;
159                 case SDLK_d:
160                         fwd.x = 0;
161                         break;
162                 case SDLK_SPACE:
163                         fwd.y = 0;
164                         break;
165                 case SDLK_LSHIFT:
166                         rev.y = 0;
167                         break;
168         }
169 }
170
171 void Interface::FaceBlock() {
172         selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
173         hud.Display(selection);
174 }
175
176 void Interface::TurnBlock() {
177         selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
178         hud.Display(selection);
179 }
180
181 void Interface::PrintBlockInfo() {
182         std::cout << std::endl;
183         if (!aim_chunk) {
184                 std::cout << "not looking at any block" << std::endl;
185                 Ray aim = ctrl.Aim();
186                 std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
187                 return;
188         }
189         std::cout << "looking at block " << aim_block
190                 << " " << Chunk::ToCoords(aim_block)
191                 << " of chunk " << aim_chunk->Position()
192                 << std::endl;
193         Print(aim_chunk->BlockAt(aim_block));
194 }
195
196 void Interface::PrintChunkInfo() {
197         std::cout << std::endl;
198         if (!aim_chunk) {
199                 std::cout << "not looking at any block" << std::endl;
200                 return;
201         }
202         std::cout << "looking at chunk "
203                 << aim_chunk->Position()
204                 << std::endl;
205
206         std::cout << "  neighbors:" << std::endl;
207         if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
208                 std::cout << " left  " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
209         }
210         if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
211                 std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
212         }
213         if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
214                 std::cout << " up    " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
215         }
216         if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
217                 std::cout << " down  " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
218         }
219         if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
220                 std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
221         }
222         if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
223                 std::cout << " back  " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
224         }
225         std::cout << std::endl;
226 }
227
228 void Interface::PrintLightInfo() {
229         std::cout
230                 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
231                 << " at position " << world.Player().Position()
232                 << std::endl;
233 }
234
235 void Interface::PrintSelectionInfo() {
236         std::cout << std::endl;
237         Print(selection);
238 }
239
240 void Interface::Print(const Block &block) {
241         std::cout << "type: " << block.type
242                 << ", face: " << block.GetFace()
243                 << ", turn: " << block.GetTurn()
244                 << std::endl;
245 }
246
247
248 void Interface::Handle(const SDL_MouseMotionEvent &event) {
249         if (config.mouse_disabled) return;
250         ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
251         ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
252 }
253
254 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
255         if (config.mouse_disabled) return;
256
257         if (event.button == SDL_BUTTON_LEFT) {
258                 RemoveBlock();
259                 remove_timer.Start();
260         } else if (event.button == SDL_BUTTON_MIDDLE) {
261                 PickBlock();
262         } else if (event.button == SDL_BUTTON_RIGHT) {
263                 PlaceBlock();
264                 place_timer.Start();
265         }
266 }
267
268 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
269         if (config.mouse_disabled) return;
270
271         if (event.button == SDL_BUTTON_LEFT) {
272                 remove_timer.Stop();
273         } else if (event.button == SDL_BUTTON_RIGHT) {
274                 place_timer.Stop();
275         }
276 }
277
278 void Interface::PickBlock() {
279         if (!aim_chunk) return;
280         selection = aim_chunk->BlockAt(aim_block);
281         hud.Display(selection);
282 }
283
284 void Interface::PlaceBlock() {
285         if (!aim_chunk) return;
286         Chunk *mod_chunk = aim_chunk;
287         glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
288         if (!Chunk::InBounds(next_pos)) {
289                 mod_chunk = &world.Next(*aim_chunk, aim_normal);
290                 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
291         }
292         mod_chunk->SetBlock(next_pos, selection);
293         mod_chunk->Invalidate();
294 }
295
296 void Interface::RemoveBlock() noexcept {
297         if (!aim_chunk) return;
298         aim_chunk->SetBlock(aim_block, remove);
299         aim_chunk->Invalidate();
300 }
301
302
303 void Interface::Handle(const SDL_MouseWheelEvent &event) {
304         if (config.mouse_disabled) return;
305
306         if (event.y < 0) {
307                 SelectNext();
308         } else if (event.y > 0) {
309                 SelectPrevious();
310         }
311 }
312
313 void Interface::SelectNext() {
314         ++selection.type;
315         if (size_t(selection.type) >= world.BlockTypes().Size()) {
316                 selection.type = 1;
317         }
318         hud.Display(selection);
319 }
320
321 void Interface::SelectPrevious() {
322         --selection.type;
323         if (selection.type <= 0) {
324                 selection.type = world.BlockTypes().Size() - 1;
325         }
326         hud.Display(selection);
327 }
328
329 void Interface::Handle(const SDL_WindowEvent &event) noexcept {
330         if (event.event == SDL_WINDOWEVENT_RESIZED) {
331                 hud.Viewport(event.data1, event.data2);
332         }
333 }
334
335
336 void Interface::Update(int dt) {
337         ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
338         ctrl.Update(dt);
339
340         place_timer.Update(dt);
341         remove_timer.Update(dt);
342
343         aim = ctrl.Aim();
344         CheckAim();
345
346         if (remove_timer.Hit()) {
347                 RemoveBlock();
348                 CheckAim();
349         }
350
351         if (place_timer.Hit()) {
352                 PlaceBlock();
353                 CheckAim();
354         }
355 }
356
357 void Interface::CheckAim() {
358         float dist;
359         if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
360                 outline.Clear();
361                 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
362                 outline_transform = glm::scale(glm::vec3(1.0002f));
363                 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
364                 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
365         } else {
366                 aim_chunk = nullptr;
367         }
368 }
369
370
371 void Interface::Render(DirectionalLighting &program) noexcept {
372         if (config.visual_disabled) return;
373
374         if (aim_chunk) {
375                 program.SetM(outline_transform);
376                 outline.Draw();
377         }
378
379         hud.Render(program);
380 }
381
382 }