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) {
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 ctrl.RotateYaw(event.xrel * yaw_sensitivity);
134 ctrl.RotatePitch(event.yrel * pitch_sensitivity);
137 void Interface::Handle(const SDL_MouseButtonEvent &event) {
138 if (event.state != SDL_PRESSED) return;
140 if (event.button == 1) {
142 } else if (event.button == 2) {
144 } else if (event.button == 3) {
149 void Interface::PickBlock() {
150 if (!aim_chunk) return;
151 selection = aim_chunk->BlockAt(aim_block);
152 hud.Display(selection);
155 void Interface::PlaceBlock() {
156 if (!aim_chunk) return;
157 Chunk *mod_chunk = aim_chunk;
158 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
159 if (!Chunk::InBounds(next_pos)) {
160 mod_chunk = &world.Next(*aim_chunk, aim_normal);
161 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
163 mod_chunk->SetBlock(next_pos, selection);
164 mod_chunk->Invalidate();
167 void Interface::RemoveBlock() {
168 if (!aim_chunk) return;
169 aim_chunk->SetBlock(aim_block, remove);
170 aim_chunk->Invalidate();
174 void Interface::Handle(const SDL_MouseWheelEvent &event) {
177 } else if (event.y > 0) {
182 void Interface::SelectNext() {
184 if (size_t(selection.type) >= world.BlockTypes().Size()) {
187 hud.Display(selection);
190 void Interface::SelectPrevious() {
192 if (selection.type <= 0) {
193 selection.type = world.BlockTypes().Size() - 1;
195 hud.Display(selection);
198 void Interface::Handle(const SDL_WindowEvent &event) {
199 if (event.event == SDL_WINDOWEVENT_RESIZED) {
200 hud.Viewport(event.data1, event.data2);
205 void Interface::Update(int dt) {
207 if (right && !left) {
208 vel.x = move_velocity;
209 } else if (left && !right) {
210 vel.x = -move_velocity;
213 vel.y = move_velocity;
214 } else if (down && !up) {
215 vel.y = -move_velocity;
217 if (back && !front) {
218 vel.z = move_velocity;
219 } else if (front && !back) {
220 vel.z = -move_velocity;
225 Ray aim = ctrl.Aim();
227 if (world.Intersection(aim, glm::mat4(1.0f), &aim_chunk, &aim_block, &dist, &aim_normal)) {
229 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
230 outline_transform = glm::scale(glm::mat4(1.0f), glm::vec3(1.0002f));
231 outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
232 outline_transform *= aim_chunk->ToTransform(aim_block);
240 void Interface::Render(DirectionalLighting &program) {
242 program.SetM(outline_transform);