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