]> git.localhorst.tv Git - blank.git/blob - src/world/Entity.cpp
49ad34bf07ef51c8eaaa5f99c64d89e6ab430827
[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 Chunk::Pos &c, const Block::Pos &pos) noexcept {
50         chunk = c;
51         position = pos;
52 }
53
54 void Entity::Position(const Block::Pos &pos) noexcept {
55         position = pos;
56         while (position.x >= Chunk::width) {
57                 position.x -= Chunk::width;
58                 ++chunk.x;
59         }
60         while (position.x < 0) {
61                 position.x += Chunk::width;
62                 --chunk.x;
63         }
64         while (position.y >= Chunk::height) {
65                 position.y -= Chunk::height;
66                 ++chunk.y;
67         }
68         while (position.y < 0) {
69                 position.y += Chunk::height;
70                 --chunk.y;
71         }
72         while (position.z >= Chunk::depth) {
73                 position.z -= Chunk::depth;
74                 ++chunk.z;
75         }
76         while (position.z < 0) {
77                 position.z += Chunk::depth;
78                 --chunk.z;
79         }
80 }
81
82 void Entity::Move(const glm::vec3 &delta) noexcept {
83         Position(position + delta);
84 }
85
86 void Entity::AngularVelocity(const glm::quat &v) noexcept {
87         angular_velocity = v;
88 }
89
90 void Entity::Rotation(const glm::mat4 &rot) noexcept {
91         rotation = rot;
92 }
93
94 void Entity::Rotate(const glm::quat &delta) noexcept {
95         Rotation(rotation * glm::mat4_cast(delta));
96 }
97
98 glm::mat4 Entity::Transform(const Chunk::Pos &chunk_offset) const noexcept {
99         const glm::vec3 chunk_pos = (chunk - chunk_offset) * Chunk::Extent();
100         return glm::translate(position + chunk_pos) * rotation;
101 }
102
103 Ray Entity::Aim(const Chunk::Pos &chunk_offset) const noexcept {
104         glm::mat4 transform = Transform(chunk_offset);
105         glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
106         from /= from.w;
107         glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
108         to /= to.w;
109         return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
110 }
111
112 void Entity::Update(int dt) noexcept {
113         Move(velocity * float(dt));
114         Rotate(angular_velocity * float(dt));
115 }
116
117
118 void Entity::Draw() noexcept {
119         model.Draw();
120 }
121
122 }