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) {
76 if (event.state == SDL_PRESSED) {
82 void Interface::FaceBlock() {
83 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
84 hud.Display(selection);
87 void Interface::TurnBlock() {
88 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
89 hud.Display(selection);
92 void Interface::PrintBlockInfo() {
93 std::cout << std::endl;
95 std::cout << "not looking at any block" << std::endl;
98 std::cout << "looking at block " << aim_block
99 << " " << Chunk::ToCoords(aim_block)
100 << " of chunk " << aim_chunk->Position()
102 Print(aim_chunk->BlockAt(aim_block));
105 void Interface::PrintSelectionInfo() {
106 std::cout << std::endl;
110 void Interface::Print(const Block &block) {
111 std::cout << "type: " << block.type
112 << ", face: " << block.GetFace()
113 << ", turn: " << block.GetTurn()
118 void Interface::Handle(const SDL_MouseMotionEvent &event) {
119 ctrl.RotateYaw(event.xrel * yaw_sensitivity);
120 ctrl.RotatePitch(event.yrel * pitch_sensitivity);
123 void Interface::Handle(const SDL_MouseButtonEvent &event) {
124 if (event.state != SDL_PRESSED) return;
126 if (event.button == 1) {
128 } else if (event.button == 2) {
130 } else if (event.button == 3) {
135 void Interface::PickBlock() {
136 if (!aim_chunk) return;
137 selection = aim_chunk->BlockAt(aim_block);
138 hud.Display(selection);
141 void Interface::PlaceBlock() {
142 if (!aim_chunk) return;
143 Chunk *mod_chunk = aim_chunk;
144 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
145 if (!Chunk::InBounds(next_pos)) {
146 mod_chunk = &world.Next(*aim_chunk, aim_normal);
147 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
149 mod_chunk->BlockAt(next_pos) = selection;
150 mod_chunk->Invalidate();
153 void Interface::RemoveBlock() {
154 if (!aim_chunk) return;
155 aim_chunk->BlockAt(aim_block) = remove;
156 aim_chunk->Invalidate();
160 void Interface::Handle(const SDL_MouseWheelEvent &event) {
163 } else if (event.y > 0) {
168 void Interface::SelectNext() {
170 if (size_t(selection.type) >= world.BlockTypes().Size()) {
173 hud.Display(selection);
176 void Interface::SelectPrevious() {
178 if (selection.type <= 0) {
179 selection.type = world.BlockTypes().Size() - 1;
181 hud.Display(selection);
184 void Interface::Handle(const SDL_WindowEvent &event) {
185 if (event.event == SDL_WINDOWEVENT_RESIZED) {
186 hud.Viewport(event.data1, event.data2);
191 void Interface::Update(int dt) {
193 if (right && !left) {
194 vel.x = move_velocity;
195 } else if (left && !right) {
196 vel.x = -move_velocity;
199 vel.y = move_velocity;
200 } else if (down && !up) {
201 vel.y = -move_velocity;
203 if (back && !front) {
204 vel.z = move_velocity;
205 } else if (front && !back) {
206 vel.z = -move_velocity;
211 Ray aim = ctrl.Aim();
213 if (world.Intersection(aim, glm::mat4(1.0f), &aim_chunk, &aim_block, &dist, &aim_normal)) {
215 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
216 outline_transform = glm::scale(glm::mat4(1.0f), glm::vec3(1.0002f));
217 outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
218 outline_transform *= aim_chunk->ToTransform(aim_block);
226 void Interface::Render(DirectionalLighting &program) {
228 program.SetM(outline_transform);