]> git.localhorst.tv Git - blank.git/blob - src/interface.cpp
move human I/O related stuff into separate file
[blank.git] / src / interface.cpp
1 #include "interface.hpp"
2
3 #include "geometry.hpp"
4 #include "world.hpp"
5
6 #include <glm/glm.hpp>
7 #include <glm/gtc/matrix_transform.hpp>
8
9
10 namespace blank {
11
12 Interface::Interface(World &world)
13 : world(world)
14 , ctrl(world.Player())
15 , hud()
16 , aim_chunk(nullptr)
17 , aim_block(0)
18 , aim_normal()
19 , outline()
20 , outline_transform(1.0f)
21 , move_velocity(0.005f)
22 , pitch_sensitivity(-0.0025f)
23 , yaw_sensitivity(-0.001f)
24 , remove(0)
25 , selection(1)
26 , front(false)
27 , back(false)
28 , left(false)
29 , right(false)
30 , up(false)
31 , down(false) {
32         hud.Viewport(960, 600);
33         hud.Display(*world.BlockTypes()[selection.type]);
34 }
35
36
37 void Interface::Handle(const SDL_KeyboardEvent &event) {
38         switch (event.keysym.sym) {
39                 case SDLK_w:
40                         front = event.state == SDL_PRESSED;
41                         break;
42                 case SDLK_s:
43                         back = event.state == SDL_PRESSED;
44                         break;
45                 case SDLK_a:
46                         left = event.state == SDL_PRESSED;
47                         break;
48                 case SDLK_d:
49                         right = event.state == SDL_PRESSED;
50                         break;
51                 case SDLK_q:
52                 case SDLK_SPACE:
53                         up = event.state == SDL_PRESSED;
54                         break;
55                 case SDLK_e:
56                 case SDLK_LSHIFT:
57                         down = event.state == SDL_PRESSED;
58                         break;
59         }
60 }
61
62 void Interface::Handle(const SDL_MouseMotionEvent &event) {
63         ctrl.RotateYaw(event.xrel * yaw_sensitivity);
64         ctrl.RotatePitch(event.yrel * pitch_sensitivity);
65 }
66
67 void Interface::Handle(const SDL_MouseButtonEvent &event) {
68         if (event.state != SDL_PRESSED) return;
69
70         if (event.button == 1) {
71                 RemoveBlock();
72         } else if (event.button == 2) {
73                 PickBlock();
74         } else if (event.button == 3) {
75                 PlaceBlock();
76         }
77 }
78
79 void Interface::PickBlock() {
80         if (!aim_chunk) return;
81         selection = aim_chunk->BlockAt(aim_block);
82         hud.Display(*world.BlockTypes()[selection.type]);
83 }
84
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());
92         }
93         mod_chunk->BlockAt(next_pos) = selection;
94         mod_chunk->Invalidate();
95 }
96
97 void Interface::RemoveBlock() {
98         if (!aim_chunk) return;
99         aim_chunk->BlockAt(aim_block) = remove;
100         aim_chunk->Invalidate();
101 }
102
103
104 void Interface::Handle(const SDL_MouseWheelEvent &event) {
105         if (event.y < 0) {
106                 SelectNext();
107         } else if (event.y > 0) {
108                 SelectPrevious();
109         }
110 }
111
112 void Interface::SelectNext() {
113         ++selection.type;
114         if (size_t(selection.type) >= world.BlockTypes().Size()) {
115                 selection.type = 1;
116         }
117         hud.Display(*world.BlockTypes()[selection.type]);
118 }
119
120 void Interface::SelectPrevious() {
121         --selection.type;
122         if (selection.type <= 0) {
123                 selection.type = world.BlockTypes().Size() - 1;
124         }
125         hud.Display(*world.BlockTypes()[selection.type]);
126 }
127
128 void Interface::Handle(const SDL_WindowEvent &event) {
129         if (event.event == SDL_WINDOWEVENT_RESIZED) {
130                 hud.Viewport(event.data1, event.data2);
131         }
132 }
133
134
135 void Interface::Update(int dt) {
136         glm::vec3 vel;
137         if (right && !left) {
138                 vel.x = move_velocity;
139         } else if (left && !right) {
140                 vel.x = -move_velocity;
141         }
142         if (up && !down) {
143                 vel.y = move_velocity;
144         } else if (down && !up) {
145                 vel.y = -move_velocity;
146         }
147         if (back && !front) {
148                 vel.z = move_velocity;
149         } else if (front && !back) {
150                 vel.z = -move_velocity;
151         }
152         ctrl.Velocity(vel);
153         ctrl.Update(dt);
154
155         Ray aim = ctrl.Aim();
156         float dist;
157         if (world.Intersection(aim, glm::mat4(1.0f), &aim_chunk, &aim_block, &dist, &aim_normal)) {
158                 outline.Clear();
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);
163         } else {
164                 aim_chunk = nullptr;
165         }
166
167 }
168
169
170 void Interface::Render(DirectionalLighting &program) {
171         if (aim_chunk) {
172                 program.SetM(outline_transform);
173                 outline.Draw();
174         }
175
176         hud.Render(program);
177 }
178
179 }