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