]> git.localhorst.tv Git - blank.git/blob - src/world/Entity.cpp
some code reorganization
[blank.git] / src / world / Entity.cpp
1 #include "Entity.hpp"
2
3 #include "../model/geometry.hpp"
4 #include "../model/Shape.hpp"
5
6 #include <cmath>
7 #include <glm/gtx/transform.hpp>
8
9 namespace {
10
11 blank::Model::Buffer model_buffer;
12
13 }
14
15 namespace blank {
16
17 Entity::Entity() noexcept
18 : shape(nullptr)
19 , model()
20 , velocity(0, 0, 0)
21 , position(0, 0, 0)
22 , chunk(0, 0, 0)
23 , angular_velocity(1.0f, 0.0f, 0.0f, 0.0f)
24 , rotation(1.0f) {
25
26 }
27
28
29 void Entity::SetShape(const Shape *s, const glm::vec3 &color) {
30         shape = s;
31         model_buffer.Clear();
32         shape->Vertices(model_buffer.vertices, model_buffer.normals, model_buffer.indices);
33         model_buffer.colors.resize(shape->VertexCount(), color);
34         model.Update(model_buffer);
35 }
36
37 void Entity::SetShapeless() noexcept {
38         shape = nullptr;
39 }
40
41
42 void Entity::Velocity(const glm::vec3 &vel) noexcept {
43         velocity = vel;
44 }
45
46 void Entity::Position(const Block::Pos &pos) noexcept {
47         position = pos;
48         while (position.x >= Chunk::width) {
49                 position.x -= Chunk::width;
50                 ++chunk.x;
51         }
52         while (position.x < 0) {
53                 position.x += Chunk::width;
54                 --chunk.x;
55         }
56         while (position.y >= Chunk::height) {
57                 position.y -= Chunk::height;
58                 ++chunk.y;
59         }
60         while (position.y < 0) {
61                 position.y += Chunk::height;
62                 --chunk.y;
63         }
64         while (position.z >= Chunk::depth) {
65                 position.z -= Chunk::depth;
66                 ++chunk.z;
67         }
68         while (position.z < 0) {
69                 position.z += Chunk::depth;
70                 --chunk.z;
71         }
72 }
73
74 void Entity::Move(const glm::vec3 &delta) noexcept {
75         Position(position + delta);
76 }
77
78 void Entity::AngularVelocity(const glm::quat &v) noexcept {
79         angular_velocity = v;
80 }
81
82 void Entity::Rotation(const glm::mat4 &rot) noexcept {
83         rotation = rot;
84 }
85
86 void Entity::Rotate(const glm::quat &delta) noexcept {
87         Rotation(rotation * glm::mat4_cast(delta));
88 }
89
90 glm::mat4 Entity::Transform(const Chunk::Pos &chunk_offset) const noexcept {
91         const glm::vec3 chunk_pos = (chunk - chunk_offset) * Chunk::Extent();
92         return glm::translate(position + chunk_pos) * rotation;
93 }
94
95 Ray Entity::Aim(const Chunk::Pos &chunk_offset) const noexcept {
96         glm::mat4 transform = Transform(chunk_offset);
97         glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
98         from /= from.w;
99         glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
100         to /= to.w;
101         return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
102 }
103
104 void Entity::Update(int dt) noexcept {
105         Move(velocity * float(dt));
106         Rotate(angular_velocity * float(dt));
107 }
108
109
110 void Entity::Draw() noexcept {
111         model.Draw();
112 }
113
114 }