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