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) {
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;
105 std::cout << "looking at block " << aim_block
106 << " " << Chunk::ToCoords(aim_block)
107 << " of chunk " << aim_chunk->Position()
109 Print(aim_chunk->BlockAt(aim_block));
112 void Interface::PrintLightInfo() {
114 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
115 << " at position " << world.Player().Position()
119 void Interface::PrintSelectionInfo() {
120 std::cout << std::endl;
124 void Interface::Print(const Block &block) {
125 std::cout << "type: " << block.type
126 << ", face: " << block.GetFace()
127 << ", turn: " << block.GetTurn()
132 void Interface::Handle(const SDL_MouseMotionEvent &event) {
133 if (config.mouse_disabled) return;
134 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
135 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
138 void Interface::Handle(const SDL_MouseButtonEvent &event) {
139 if (config.mouse_disabled) return;
141 if (event.state != SDL_PRESSED) return;
143 if (event.button == 1) {
145 } else if (event.button == 2) {
147 } else if (event.button == 3) {
152 void Interface::PickBlock() {
153 if (!aim_chunk) return;
154 selection = aim_chunk->BlockAt(aim_block);
155 hud.Display(selection);
158 void Interface::PlaceBlock() {
159 if (!aim_chunk) return;
160 Chunk *mod_chunk = aim_chunk;
161 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
162 if (!Chunk::InBounds(next_pos)) {
163 mod_chunk = &world.Next(*aim_chunk, aim_normal);
164 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
166 mod_chunk->SetBlock(next_pos, selection);
167 mod_chunk->Invalidate();
170 void Interface::RemoveBlock() {
171 if (!aim_chunk) return;
172 aim_chunk->SetBlock(aim_block, remove);
173 aim_chunk->Invalidate();
177 void Interface::Handle(const SDL_MouseWheelEvent &event) {
178 if (config.mouse_disabled) return;
182 } else if (event.y > 0) {
187 void Interface::SelectNext() {
189 if (size_t(selection.type) >= world.BlockTypes().Size()) {
192 hud.Display(selection);
195 void Interface::SelectPrevious() {
197 if (selection.type <= 0) {
198 selection.type = world.BlockTypes().Size() - 1;
200 hud.Display(selection);
203 void Interface::Handle(const SDL_WindowEvent &event) {
204 if (event.event == SDL_WINDOWEVENT_RESIZED) {
205 hud.Viewport(event.data1, event.data2);
210 void Interface::Update(int dt) {
212 if (right && !left) {
213 vel.x = config.move_velocity;
214 } else if (left && !right) {
215 vel.x = -config.move_velocity;
218 vel.y = config.move_velocity;
219 } else if (down && !up) {
220 vel.y = -config.move_velocity;
222 if (back && !front) {
223 vel.z = config.move_velocity;
224 } else if (front && !back) {
225 vel.z = -config.move_velocity;
230 Ray aim = ctrl.Aim();
232 if (world.Intersection(aim, glm::mat4(1.0f), &aim_chunk, &aim_block, &dist, &aim_normal)) {
234 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
235 outline_transform = glm::scale(glm::mat4(1.0f), glm::vec3(1.0002f));
236 outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
237 outline_transform *= aim_chunk->ToTransform(aim_block);
245 void Interface::Render(DirectionalLighting &program) {
246 if (config.visual_disabled) return;
249 program.SetM(outline_transform);