]> git.localhorst.tv Git - blank.git/blob - src/entity.cpp
some cleanup
[blank.git] / src / entity.cpp
1 #include "entity.hpp"
2
3 #include "chunk.hpp"
4
5 #include <cmath>
6 #include <glm/gtx/transform.hpp>
7
8
9 namespace blank {
10
11 Entity::Entity()
12 : velocity()
13 , position()
14 , rotation(1.0f) {
15
16 }
17
18
19 void Entity::Velocity(const glm::vec3 &vel) {
20         velocity = vel;
21 }
22
23 void Entity::Position(const Block::Pos &pos) {
24         position = pos;
25         while (position.x >= Chunk::Width()) {
26                 position.x -= Chunk::Width();
27                 ++chunk.x;
28         }
29         while (position.x < 0) {
30                 position.x += Chunk::Width();
31                 --chunk.x;
32         }
33         while (position.y >= Chunk::Height()) {
34                 position.y -= Chunk::Height();
35                 ++chunk.y;
36         }
37         while (position.y < 0) {
38                 position.y += Chunk::Height();
39                 --chunk.y;
40         }
41         while (position.z >= Chunk::Depth()) {
42                 position.z -= Chunk::Depth();
43                 ++chunk.z;
44         }
45         while (position.z < 0) {
46                 position.z += Chunk::Depth();
47                 --chunk.z;
48         }
49 }
50
51 void Entity::Move(const glm::vec3 &delta) {
52         Position(position + delta);
53 }
54
55 void Entity::Rotation(const glm::mat4 &rot) {
56         rotation = rot;
57 }
58
59 glm::mat4 Entity::Transform(const Chunk::Pos &chunk_offset) const {
60         const glm::vec3 chunk_pos = (chunk - chunk_offset) * Chunk::Extent();
61         return glm::translate(position + chunk_pos) * rotation;
62 }
63
64 Ray Entity::Aim(const Chunk::Pos &chunk_offset) const {
65         glm::mat4 transform = Transform(chunk_offset);
66         glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
67         from /= from.w;
68         glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
69         to /= to.w;
70         return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
71 }
72
73 void Entity::Update(int dt) {
74         Move(velocity * float(dt));
75 }
76
77 }