]> git.localhorst.tv Git - blank.git/blob - src/interface.cpp
f4b13b015d6d69fcbf6adbfc6e0a92d6c3d6454e
[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(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 , move_velocity(0.005f)
24 , pitch_sensitivity(-0.0025f)
25 , yaw_sensitivity(-0.001f)
26 , remove(0)
27 , selection(1)
28 , front(false)
29 , back(false)
30 , left(false)
31 , right(false)
32 , up(false)
33 , down(false) {
34         hud.Viewport(960, 600);
35         hud.Display(selection);
36 }
37
38
39 void Interface::Handle(const SDL_KeyboardEvent &event) {
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_l:
77                         if (event.state == SDL_PRESSED) {
78                                 PrintLightInfo();
79                         }
80                         break;
81                 case SDLK_p:
82                         if (event.state == SDL_PRESSED) {
83                                 PrintSelectionInfo();
84                         }
85                         break;
86         }
87 }
88
89 void Interface::FaceBlock() {
90         selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
91         hud.Display(selection);
92 }
93
94 void Interface::TurnBlock() {
95         selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
96         hud.Display(selection);
97 }
98
99 void Interface::PrintBlockInfo() {
100         std::cout << std::endl;
101         if (!aim_chunk) {
102                 std::cout << "not looking at any block" << std::endl;
103                 return;
104         }
105         std::cout << "looking at block " << aim_block
106                 << " " << Chunk::ToCoords(aim_block)
107                 << " of chunk " << aim_chunk->Position()
108                 << std::endl;
109         Print(aim_chunk->BlockAt(aim_block));
110 }
111
112 void Interface::PrintLightInfo() {
113         std::cout
114                 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
115                 << " at position " << world.Player().Position()
116                 << std::endl;
117 }
118
119 void Interface::PrintSelectionInfo() {
120         std::cout << std::endl;
121         Print(selection);
122 }
123
124 void Interface::Print(const Block &block) {
125         std::cout << "type: " << block.type
126                 << ", face: " << block.GetFace()
127                 << ", turn: " << block.GetTurn()
128                 << std::endl;
129 }
130
131
132 void Interface::Handle(const SDL_MouseMotionEvent &event) {
133         ctrl.RotateYaw(event.xrel * yaw_sensitivity);
134         ctrl.RotatePitch(event.yrel * pitch_sensitivity);
135 }
136
137 void Interface::Handle(const SDL_MouseButtonEvent &event) {
138         if (event.state != SDL_PRESSED) return;
139
140         if (event.button == 1) {
141                 RemoveBlock();
142         } else if (event.button == 2) {
143                 PickBlock();
144         } else if (event.button == 3) {
145                 PlaceBlock();
146         }
147 }
148
149 void Interface::PickBlock() {
150         if (!aim_chunk) return;
151         selection = aim_chunk->BlockAt(aim_block);
152         hud.Display(selection);
153 }
154
155 void Interface::PlaceBlock() {
156         if (!aim_chunk) return;
157         Chunk *mod_chunk = aim_chunk;
158         glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
159         if (!Chunk::InBounds(next_pos)) {
160                 mod_chunk = &world.Next(*aim_chunk, aim_normal);
161                 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
162         }
163         mod_chunk->SetBlock(next_pos, selection);
164         mod_chunk->Invalidate();
165 }
166
167 void Interface::RemoveBlock() {
168         if (!aim_chunk) return;
169         aim_chunk->SetBlock(aim_block, remove);
170         aim_chunk->Invalidate();
171 }
172
173
174 void Interface::Handle(const SDL_MouseWheelEvent &event) {
175         if (event.y < 0) {
176                 SelectNext();
177         } else if (event.y > 0) {
178                 SelectPrevious();
179         }
180 }
181
182 void Interface::SelectNext() {
183         ++selection.type;
184         if (size_t(selection.type) >= world.BlockTypes().Size()) {
185                 selection.type = 1;
186         }
187         hud.Display(selection);
188 }
189
190 void Interface::SelectPrevious() {
191         --selection.type;
192         if (selection.type <= 0) {
193                 selection.type = world.BlockTypes().Size() - 1;
194         }
195         hud.Display(selection);
196 }
197
198 void Interface::Handle(const SDL_WindowEvent &event) {
199         if (event.event == SDL_WINDOWEVENT_RESIZED) {
200                 hud.Viewport(event.data1, event.data2);
201         }
202 }
203
204
205 void Interface::Update(int dt) {
206         glm::vec3 vel;
207         if (right && !left) {
208                 vel.x = move_velocity;
209         } else if (left && !right) {
210                 vel.x = -move_velocity;
211         }
212         if (up && !down) {
213                 vel.y = move_velocity;
214         } else if (down && !up) {
215                 vel.y = -move_velocity;
216         }
217         if (back && !front) {
218                 vel.z = move_velocity;
219         } else if (front && !back) {
220                 vel.z = -move_velocity;
221         }
222         ctrl.Velocity(vel);
223         ctrl.Update(dt);
224
225         Ray aim = ctrl.Aim();
226         float dist;
227         if (world.Intersection(aim, glm::mat4(1.0f), &aim_chunk, &aim_block, &dist, &aim_normal)) {
228                 outline.Clear();
229                 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
230                 outline_transform = glm::scale(glm::mat4(1.0f), glm::vec3(1.0002f));
231                 outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
232                 outline_transform *= aim_chunk->ToTransform(aim_block);
233         } else {
234                 aim_chunk = nullptr;
235         }
236
237 }
238
239
240 void Interface::Render(DirectionalLighting &program) {
241         if (aim_chunk) {
242                 program.SetM(outline_transform);
243                 outline.Draw();
244         }
245
246         hud.Render(program);
247 }
248
249 }