]> git.localhorst.tv Git - blank.git/blob - src/entity.cpp
split entity from controller
[blank.git] / src / entity.cpp
1 #include "entity.hpp"
2
3 #include <glm/gtx/transform.hpp>
4
5
6 namespace blank {
7
8 Entity::Entity()
9 : velocity()
10 , position()
11 , rotation(1.0f)
12 , transform(1.0f)
13 , dirty(false) {
14
15 }
16
17
18 void Entity::Velocity(const glm::vec3 &vel) {
19         velocity = vel;
20 }
21
22 void Entity::Position(const glm::vec3 &pos) {
23         position = pos;
24         dirty = true;
25 }
26
27 void Entity::Move(const glm::vec3 &delta) {
28         position += delta;
29         dirty = true;
30 }
31
32 void Entity::Rotation(const glm::mat4 &rot) {
33         rotation = rot;
34 }
35
36 const glm::mat4 &Entity::Transform() const {
37         if (dirty) {
38                 transform = glm::translate(position) * rotation;
39                 dirty = false;
40         }
41         return transform;
42 }
43
44 Ray Entity::Aim() const {
45         Transform();
46         glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
47         from /= from.w;
48         glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
49         to /= to.w;
50         return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
51 }
52
53 void Entity::Update(int dt) {
54         Move(velocity * float(dt));
55 }
56
57 }