1 #include "interface.hpp"
3 #include "geometry.hpp"
7 #include <glm/gtc/matrix_transform.hpp>
12 Interface::Interface(World &world)
14 , ctrl(world.Player())
20 , outline_transform(1.0f)
21 , move_velocity(0.005f)
22 , pitch_sensitivity(-0.0025f)
23 , yaw_sensitivity(-0.001f)
32 hud.Viewport(960, 600);
33 hud.Display(*world.BlockTypes()[selection.type]);
37 void Interface::Handle(const SDL_KeyboardEvent &event) {
38 switch (event.keysym.sym) {
40 front = event.state == SDL_PRESSED;
43 back = event.state == SDL_PRESSED;
46 left = event.state == SDL_PRESSED;
49 right = event.state == SDL_PRESSED;
53 up = event.state == SDL_PRESSED;
57 down = event.state == SDL_PRESSED;
62 void Interface::Handle(const SDL_MouseMotionEvent &event) {
63 ctrl.RotateYaw(event.xrel * yaw_sensitivity);
64 ctrl.RotatePitch(event.yrel * pitch_sensitivity);
67 void Interface::Handle(const SDL_MouseButtonEvent &event) {
68 if (event.state != SDL_PRESSED) return;
70 if (event.button == 1) {
72 } else if (event.button == 2) {
74 } else if (event.button == 3) {
79 void Interface::PickBlock() {
80 if (!aim_chunk) return;
81 selection = aim_chunk->BlockAt(aim_block);
82 hud.Display(*world.BlockTypes()[selection.type]);
85 void Interface::PlaceBlock() {
86 if (!aim_chunk) return;
87 Chunk *mod_chunk = aim_chunk;
88 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
89 if (!Chunk::InBounds(next_pos)) {
90 mod_chunk = &world.Next(*aim_chunk, aim_normal);
91 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
93 mod_chunk->BlockAt(next_pos) = selection;
94 mod_chunk->Invalidate();
97 void Interface::RemoveBlock() {
98 if (!aim_chunk) return;
99 aim_chunk->BlockAt(aim_block) = remove;
100 aim_chunk->Invalidate();
104 void Interface::Handle(const SDL_MouseWheelEvent &event) {
107 } else if (event.y > 0) {
112 void Interface::SelectNext() {
114 if (size_t(selection.type) >= world.BlockTypes().Size()) {
117 hud.Display(*world.BlockTypes()[selection.type]);
120 void Interface::SelectPrevious() {
122 if (selection.type <= 0) {
123 selection.type = world.BlockTypes().Size() - 1;
125 hud.Display(*world.BlockTypes()[selection.type]);
128 void Interface::Handle(const SDL_WindowEvent &event) {
129 if (event.event == SDL_WINDOWEVENT_RESIZED) {
130 hud.Viewport(event.data1, event.data2);
135 void Interface::Update(int dt) {
137 if (right && !left) {
138 vel.x = move_velocity;
139 } else if (left && !right) {
140 vel.x = -move_velocity;
143 vel.y = move_velocity;
144 } else if (down && !up) {
145 vel.y = -move_velocity;
147 if (back && !front) {
148 vel.z = move_velocity;
149 } else if (front && !back) {
150 vel.z = -move_velocity;
155 Ray aim = ctrl.Aim();
157 if (world.Intersection(aim, glm::mat4(1.0f), &aim_chunk, &aim_block, &dist, &aim_normal)) {
159 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
160 outline_transform = glm::scale(glm::mat4(1.0f), glm::vec3(1.0002f));
161 outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
162 outline_transform *= aim_chunk->ToTransform(aim_block);
170 void Interface::Render(DirectionalLighting &program) {
172 program.SetM(outline_transform);