1 #include "interface.hpp"
3 #include "geometry.hpp"
8 #include <glm/gtc/matrix_transform.hpp>
9 #include <glm/gtx/io.hpp>
14 Interface::Interface(const Config &config, World &world)
16 , ctrl(world.Player())
17 , hud(world.BlockTypes())
22 , outline_transform(1.0f)
32 hud.Viewport(960, 600);
33 hud.Display(selection);
37 void Interface::Handle(const SDL_KeyboardEvent &event) {
38 if (config.keyboard_disabled) return;
40 switch (event.keysym.sym) {
42 front = event.state == SDL_PRESSED;
45 back = event.state == SDL_PRESSED;
48 left = event.state == SDL_PRESSED;
51 right = event.state == SDL_PRESSED;
54 up = event.state == SDL_PRESSED;
57 down = event.state == SDL_PRESSED;
61 if (event.state == SDL_PRESSED) {
66 if (event.state == SDL_PRESSED) {
72 if (event.state == SDL_PRESSED) {
77 if (event.state == SDL_PRESSED) {
82 if (event.state == SDL_PRESSED) {
87 if (event.state == SDL_PRESSED) {
94 void Interface::FaceBlock() {
95 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
96 hud.Display(selection);
99 void Interface::TurnBlock() {
100 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
101 hud.Display(selection);
104 void Interface::PrintBlockInfo() {
105 std::cout << std::endl;
107 std::cout << "not looking at any block" << std::endl;
108 Ray aim = ctrl.Aim();
109 std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
112 std::cout << "looking at block " << aim_block
113 << " " << Chunk::ToCoords(aim_block)
114 << " of chunk " << aim_chunk->Position()
116 Print(aim_chunk->BlockAt(aim_block));
119 void Interface::PrintChunkInfo() {
120 std::cout << std::endl;
122 std::cout << "not looking at any block" << std::endl;
125 std::cout << "looking at chunk "
126 << aim_chunk->Position()
129 std::cout << " neighbors:" << std::endl;
130 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
131 std::cout << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
133 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
134 std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
136 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
137 std::cout << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
139 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
140 std::cout << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
142 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
143 std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
145 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
146 std::cout << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
148 std::cout << std::endl;
151 void Interface::PrintLightInfo() {
153 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
154 << " at position " << world.Player().Position()
158 void Interface::PrintSelectionInfo() {
159 std::cout << std::endl;
163 void Interface::Print(const Block &block) {
164 std::cout << "type: " << block.type
165 << ", face: " << block.GetFace()
166 << ", turn: " << block.GetTurn()
171 void Interface::Handle(const SDL_MouseMotionEvent &event) {
172 if (config.mouse_disabled) return;
173 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
174 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
177 void Interface::Handle(const SDL_MouseButtonEvent &event) {
178 if (config.mouse_disabled) return;
180 if (event.state != SDL_PRESSED) return;
182 if (event.button == 1) {
184 } else if (event.button == 2) {
186 } else if (event.button == 3) {
191 void Interface::PickBlock() {
192 if (!aim_chunk) return;
193 selection = aim_chunk->BlockAt(aim_block);
194 hud.Display(selection);
197 void Interface::PlaceBlock() {
198 if (!aim_chunk) return;
199 Chunk *mod_chunk = aim_chunk;
200 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
201 if (!Chunk::InBounds(next_pos)) {
202 mod_chunk = &world.Next(*aim_chunk, aim_normal);
203 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
205 mod_chunk->SetBlock(next_pos, selection);
206 mod_chunk->Invalidate();
209 void Interface::RemoveBlock() {
210 if (!aim_chunk) return;
211 aim_chunk->SetBlock(aim_block, remove);
212 aim_chunk->Invalidate();
216 void Interface::Handle(const SDL_MouseWheelEvent &event) {
217 if (config.mouse_disabled) return;
221 } else if (event.y > 0) {
226 void Interface::SelectNext() {
228 if (size_t(selection.type) >= world.BlockTypes().Size()) {
231 hud.Display(selection);
234 void Interface::SelectPrevious() {
236 if (selection.type <= 0) {
237 selection.type = world.BlockTypes().Size() - 1;
239 hud.Display(selection);
242 void Interface::Handle(const SDL_WindowEvent &event) {
243 if (event.event == SDL_WINDOWEVENT_RESIZED) {
244 hud.Viewport(event.data1, event.data2);
249 void Interface::Update(int dt) {
251 if (right && !left) {
252 vel.x = config.move_velocity;
253 } else if (left && !right) {
254 vel.x = -config.move_velocity;
257 vel.y = config.move_velocity;
258 } else if (down && !up) {
259 vel.y = -config.move_velocity;
261 if (back && !front) {
262 vel.z = config.move_velocity;
263 } else if (front && !back) {
264 vel.z = -config.move_velocity;
269 Ray aim = ctrl.Aim();
271 if (world.Intersection(aim, glm::mat4(1.0f), &aim_chunk, &aim_block, &dist, &aim_normal)) {
273 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
274 outline_transform = glm::scale(glm::mat4(1.0f), glm::vec3(1.0002f));
275 outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
276 outline_transform *= aim_chunk->ToTransform(aim_block);
284 void Interface::Render(DirectionalLighting &program) {
285 if (config.visual_disabled) return;
288 program.SetM(outline_transform);