1 #include "interface.hpp"
3 #include "geometry.hpp"
8 #include <glm/gtc/matrix_transform.hpp>
9 #include <glm/gtx/io.hpp>
14 Interface::Interface(World &world)
16 , ctrl(world.Player())
17 , hud(world.BlockTypes())
22 , outline_transform(1.0f)
23 , move_velocity(0.005f)
24 , pitch_sensitivity(-0.0025f)
25 , yaw_sensitivity(-0.001f)
34 hud.Viewport(960, 600);
35 hud.Display(selection);
39 void Interface::Handle(const SDL_KeyboardEvent &event) {
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) {
84 void Interface::FaceBlock() {
85 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
86 hud.Display(selection);
89 void Interface::TurnBlock() {
90 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
91 hud.Display(selection);
94 void Interface::PrintBlockInfo() {
95 std::cout << std::endl;
97 std::cout << "not looking at any block" << std::endl;
100 std::cout << "looking at block " << aim_block
101 << " " << Chunk::ToCoords(aim_block)
102 << " of chunk " << aim_chunk->Position()
104 Print(aim_chunk->BlockAt(aim_block));
107 void Interface::PrintSelectionInfo() {
108 std::cout << std::endl;
112 void Interface::Print(const Block &block) {
113 std::cout << "type: " << block.type
114 << ", face: " << block.GetFace()
115 << ", turn: " << block.GetTurn()
120 void Interface::Handle(const SDL_MouseMotionEvent &event) {
121 ctrl.RotateYaw(event.xrel * yaw_sensitivity);
122 ctrl.RotatePitch(event.yrel * pitch_sensitivity);
125 void Interface::Handle(const SDL_MouseButtonEvent &event) {
126 if (event.state != SDL_PRESSED) return;
128 if (event.button == 1) {
130 } else if (event.button == 2) {
132 } else if (event.button == 3) {
137 void Interface::PickBlock() {
138 if (!aim_chunk) return;
139 selection = aim_chunk->BlockAt(aim_block);
140 hud.Display(selection);
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());
151 mod_chunk->BlockAt(next_pos) = selection;
152 mod_chunk->Invalidate();
155 void Interface::RemoveBlock() {
156 if (!aim_chunk) return;
157 aim_chunk->BlockAt(aim_block) = remove;
158 aim_chunk->Invalidate();
162 void Interface::Handle(const SDL_MouseWheelEvent &event) {
165 } else if (event.y > 0) {
170 void Interface::SelectNext() {
172 if (size_t(selection.type) >= world.BlockTypes().Size()) {
175 hud.Display(selection);
178 void Interface::SelectPrevious() {
180 if (selection.type <= 0) {
181 selection.type = world.BlockTypes().Size() - 1;
183 hud.Display(selection);
186 void Interface::Handle(const SDL_WindowEvent &event) {
187 if (event.event == SDL_WINDOWEVENT_RESIZED) {
188 hud.Viewport(event.data1, event.data2);
193 void Interface::Update(int dt) {
195 if (right && !left) {
196 vel.x = move_velocity;
197 } else if (left && !right) {
198 vel.x = -move_velocity;
201 vel.y = move_velocity;
202 } else if (down && !up) {
203 vel.y = -move_velocity;
205 if (back && !front) {
206 vel.z = move_velocity;
207 } else if (front && !back) {
208 vel.z = -move_velocity;
213 Ray aim = ctrl.Aim();
215 if (world.Intersection(aim, glm::mat4(1.0f), &aim_chunk, &aim_block, &dist, &aim_normal)) {
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);
228 void Interface::Render(DirectionalLighting &program) {
230 program.SetM(outline_transform);