]> git.localhorst.tv Git - blank.git/blob - src/interface.cpp
45439da0e9a1de63c497063f192a6109c60434b9
[blank.git] / src / interface.cpp
1 #include "interface.hpp"
2
3 #include "geometry.hpp"
4 #include "world.hpp"
5
6 #include <iostream>
7 #include <glm/glm.hpp>
8 #include <glm/gtc/matrix_transform.hpp>
9 #include <glm/gtx/io.hpp>
10
11
12 namespace blank {
13
14 Interface::Interface(const Config &config, World &world)
15 : world(world)
16 , ctrl(world.Player())
17 , hud(world.BlockTypes())
18 , aim_chunk(nullptr)
19 , aim_block(0)
20 , aim_normal()
21 , outline()
22 , outline_transform(1.0f)
23 , config(config)
24 , remove(0)
25 , selection(1)
26 , front(false)
27 , back(false)
28 , left(false)
29 , right(false)
30 , up(false)
31 , down(false) {
32         hud.Viewport(960, 600);
33         hud.Display(selection);
34 }
35
36
37 void Interface::Handle(const SDL_KeyboardEvent &event) {
38         if (config.keyboard_disabled) return;
39
40         switch (event.keysym.sym) {
41                 case SDLK_w:
42                         front = event.state == SDL_PRESSED;
43                         break;
44                 case SDLK_s:
45                         back = event.state == SDL_PRESSED;
46                         break;
47                 case SDLK_a:
48                         left = event.state == SDL_PRESSED;
49                         break;
50                 case SDLK_d:
51                         right = event.state == SDL_PRESSED;
52                         break;
53                 case SDLK_SPACE:
54                         up = event.state == SDL_PRESSED;
55                         break;
56                 case SDLK_LSHIFT:
57                         down = event.state == SDL_PRESSED;
58                         break;
59
60                 case SDLK_q:
61                         if (event.state == SDL_PRESSED) {
62                                 FaceBlock();
63                         }
64                         break;
65                 case SDLK_e:
66                         if (event.state == SDL_PRESSED) {
67                                 TurnBlock();
68                         }
69                         break;
70
71                 case SDLK_b:
72                         if (event.state == SDL_PRESSED) {
73                                 PrintBlockInfo();
74                         }
75                         break;
76                 case SDLK_c:
77                         if (event.state == SDL_PRESSED) {
78                                 PrintChunkInfo();
79                         }
80                         break;
81                 case SDLK_l:
82                         if (event.state == SDL_PRESSED) {
83                                 PrintLightInfo();
84                         }
85                         break;
86                 case SDLK_p:
87                         if (event.state == SDL_PRESSED) {
88                                 PrintSelectionInfo();
89                         }
90                         break;
91         }
92 }
93
94 void Interface::FaceBlock() {
95         selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
96         hud.Display(selection);
97 }
98
99 void Interface::TurnBlock() {
100         selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
101         hud.Display(selection);
102 }
103
104 void Interface::PrintBlockInfo() {
105         std::cout << std::endl;
106         if (!aim_chunk) {
107                 std::cout << "not looking at any block" << std::endl;
108                 return;
109         }
110         std::cout << "looking at block " << aim_block
111                 << " " << Chunk::ToCoords(aim_block)
112                 << " of chunk " << aim_chunk->Position()
113                 << std::endl;
114         Print(aim_chunk->BlockAt(aim_block));
115 }
116
117 void Interface::PrintChunkInfo() {
118         std::cout << std::endl;
119         if (!aim_chunk) {
120                 std::cout << "not looking at any block" << std::endl;
121                 return;
122         }
123         std::cout << "looking at chunk "
124                 << aim_chunk->Position()
125                 << std::endl;
126
127         std::cout << "  neighbors:" << std::endl;
128         if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
129                 std::cout << " left  " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
130         }
131         if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
132                 std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
133         }
134         if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
135                 std::cout << " up    " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
136         }
137         if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
138                 std::cout << " down  " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
139         }
140         if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
141                 std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
142         }
143         if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
144                 std::cout << " back  " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
145         }
146         std::cout << std::endl;
147 }
148
149 void Interface::PrintLightInfo() {
150         std::cout
151                 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
152                 << " at position " << world.Player().Position()
153                 << std::endl;
154 }
155
156 void Interface::PrintSelectionInfo() {
157         std::cout << std::endl;
158         Print(selection);
159 }
160
161 void Interface::Print(const Block &block) {
162         std::cout << "type: " << block.type
163                 << ", face: " << block.GetFace()
164                 << ", turn: " << block.GetTurn()
165                 << std::endl;
166 }
167
168
169 void Interface::Handle(const SDL_MouseMotionEvent &event) {
170         if (config.mouse_disabled) return;
171         ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
172         ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
173 }
174
175 void Interface::Handle(const SDL_MouseButtonEvent &event) {
176         if (config.mouse_disabled) return;
177
178         if (event.state != SDL_PRESSED) return;
179
180         if (event.button == 1) {
181                 RemoveBlock();
182         } else if (event.button == 2) {
183                 PickBlock();
184         } else if (event.button == 3) {
185                 PlaceBlock();
186         }
187 }
188
189 void Interface::PickBlock() {
190         if (!aim_chunk) return;
191         selection = aim_chunk->BlockAt(aim_block);
192         hud.Display(selection);
193 }
194
195 void Interface::PlaceBlock() {
196         if (!aim_chunk) return;
197         Chunk *mod_chunk = aim_chunk;
198         glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
199         if (!Chunk::InBounds(next_pos)) {
200                 mod_chunk = &world.Next(*aim_chunk, aim_normal);
201                 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
202         }
203         mod_chunk->SetBlock(next_pos, selection);
204         mod_chunk->Invalidate();
205 }
206
207 void Interface::RemoveBlock() {
208         if (!aim_chunk) return;
209         aim_chunk->SetBlock(aim_block, remove);
210         aim_chunk->Invalidate();
211 }
212
213
214 void Interface::Handle(const SDL_MouseWheelEvent &event) {
215         if (config.mouse_disabled) return;
216
217         if (event.y < 0) {
218                 SelectNext();
219         } else if (event.y > 0) {
220                 SelectPrevious();
221         }
222 }
223
224 void Interface::SelectNext() {
225         ++selection.type;
226         if (size_t(selection.type) >= world.BlockTypes().Size()) {
227                 selection.type = 1;
228         }
229         hud.Display(selection);
230 }
231
232 void Interface::SelectPrevious() {
233         --selection.type;
234         if (selection.type <= 0) {
235                 selection.type = world.BlockTypes().Size() - 1;
236         }
237         hud.Display(selection);
238 }
239
240 void Interface::Handle(const SDL_WindowEvent &event) {
241         if (event.event == SDL_WINDOWEVENT_RESIZED) {
242                 hud.Viewport(event.data1, event.data2);
243         }
244 }
245
246
247 void Interface::Update(int dt) {
248         glm::vec3 vel;
249         if (right && !left) {
250                 vel.x = config.move_velocity;
251         } else if (left && !right) {
252                 vel.x = -config.move_velocity;
253         }
254         if (up && !down) {
255                 vel.y = config.move_velocity;
256         } else if (down && !up) {
257                 vel.y = -config.move_velocity;
258         }
259         if (back && !front) {
260                 vel.z = config.move_velocity;
261         } else if (front && !back) {
262                 vel.z = -config.move_velocity;
263         }
264         ctrl.Velocity(vel);
265         ctrl.Update(dt);
266
267         Ray aim = ctrl.Aim();
268         float dist;
269         if (world.Intersection(aim, glm::mat4(1.0f), &aim_chunk, &aim_block, &dist, &aim_normal)) {
270                 outline.Clear();
271                 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
272                 outline_transform = glm::scale(glm::mat4(1.0f), glm::vec3(1.0002f));
273                 outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
274                 outline_transform *= aim_chunk->ToTransform(aim_block);
275         } else {
276                 aim_chunk = nullptr;
277         }
278
279 }
280
281
282 void Interface::Render(DirectionalLighting &program) {
283         if (config.visual_disabled) return;
284
285         if (aim_chunk) {
286                 program.SetM(outline_transform);
287                 outline.Draw();
288         }
289
290         hud.Render(program);
291 }
292
293 }