1 #include "interface.hpp"
3 #include "geometry.hpp"
7 #include <glm/gtc/matrix_transform.hpp>
8 #include <glm/gtx/io.hpp>
13 Interface::Interface(const Config &config, World &world)
15 , ctrl(world.Player())
16 , hud(world.BlockTypes())
21 , outline_transform(1.0f)
27 hud.Viewport(960, 600);
28 hud.Display(selection);
32 void Interface::Handle(const SDL_KeyboardEvent &event) {
33 if (config.keyboard_disabled) return;
35 switch (event.keysym.sym) {
37 rev.z = event.state == SDL_PRESSED;
40 fwd.z = event.state == SDL_PRESSED;
43 rev.x = event.state == SDL_PRESSED;
46 fwd.x = event.state == SDL_PRESSED;
49 fwd.y = event.state == SDL_PRESSED;
52 rev.y = event.state == SDL_PRESSED;
56 if (event.state == SDL_PRESSED) {
61 if (event.state == SDL_PRESSED) {
67 if (event.state == SDL_PRESSED) {
72 if (event.state == SDL_PRESSED) {
77 if (event.state == SDL_PRESSED) {
82 if (event.state == SDL_PRESSED) {
89 void Interface::FaceBlock() {
90 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
91 hud.Display(selection);
94 void Interface::TurnBlock() {
95 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
96 hud.Display(selection);
99 void Interface::PrintBlockInfo() {
100 std::cout << std::endl;
102 std::cout << "not looking at any block" << std::endl;
103 Ray aim = ctrl.Aim();
104 std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
107 std::cout << "looking at block " << aim_block
108 << " " << Chunk::ToCoords(aim_block)
109 << " of chunk " << aim_chunk->Position()
111 Print(aim_chunk->BlockAt(aim_block));
114 void Interface::PrintChunkInfo() {
115 std::cout << std::endl;
117 std::cout << "not looking at any block" << std::endl;
120 std::cout << "looking at chunk "
121 << aim_chunk->Position()
124 std::cout << " neighbors:" << std::endl;
125 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
126 std::cout << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
128 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
129 std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
131 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
132 std::cout << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
134 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
135 std::cout << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
137 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
138 std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
140 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
141 std::cout << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
143 std::cout << std::endl;
146 void Interface::PrintLightInfo() {
148 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
149 << " at position " << world.Player().Position()
153 void Interface::PrintSelectionInfo() {
154 std::cout << std::endl;
158 void Interface::Print(const Block &block) {
159 std::cout << "type: " << block.type
160 << ", face: " << block.GetFace()
161 << ", turn: " << block.GetTurn()
166 void Interface::Handle(const SDL_MouseMotionEvent &event) {
167 if (config.mouse_disabled) return;
168 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
169 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
172 void Interface::Handle(const SDL_MouseButtonEvent &event) {
173 if (config.mouse_disabled) return;
175 if (event.state != SDL_PRESSED) return;
177 if (event.button == 1) {
179 } else if (event.button == 2) {
181 } else if (event.button == 3) {
186 void Interface::PickBlock() {
187 if (!aim_chunk) return;
188 selection = aim_chunk->BlockAt(aim_block);
189 hud.Display(selection);
192 void Interface::PlaceBlock() {
193 if (!aim_chunk) return;
194 Chunk *mod_chunk = aim_chunk;
195 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
196 if (!Chunk::InBounds(next_pos)) {
197 mod_chunk = &world.Next(*aim_chunk, aim_normal);
198 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
200 mod_chunk->SetBlock(next_pos, selection);
201 mod_chunk->Invalidate();
204 void Interface::RemoveBlock() noexcept {
205 if (!aim_chunk) return;
206 aim_chunk->SetBlock(aim_block, remove);
207 aim_chunk->Invalidate();
211 void Interface::Handle(const SDL_MouseWheelEvent &event) {
212 if (config.mouse_disabled) return;
216 } else if (event.y > 0) {
221 void Interface::SelectNext() {
223 if (size_t(selection.type) >= world.BlockTypes().Size()) {
226 hud.Display(selection);
229 void Interface::SelectPrevious() {
231 if (selection.type <= 0) {
232 selection.type = world.BlockTypes().Size() - 1;
234 hud.Display(selection);
237 void Interface::Handle(const SDL_WindowEvent &event) noexcept {
238 if (event.event == SDL_WINDOWEVENT_RESIZED) {
239 hud.Viewport(event.data1, event.data2);
244 void Interface::Update(int dt) {
245 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
248 Ray aim = ctrl.Aim();
250 if (world.Intersection(aim, glm::mat4(1.0f), &aim_chunk, &aim_block, &dist, &aim_normal)) {
252 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
253 outline_transform = glm::scale(glm::mat4(1.0f), glm::vec3(1.0002f));
254 outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
255 outline_transform *= aim_chunk->ToTransform(aim_block);
263 void Interface::Render(DirectionalLighting &program) noexcept {
264 if (config.visual_disabled) return;
267 program.SetM(outline_transform);