]> git.localhorst.tv Git - blank.git/blob - src/world/Entity.hpp
use entity's eyes to aim
[blank.git] / src / world / Entity.hpp
1 #ifndef BLANK_WORLD_ENTITY_HPP_
2 #define BLANK_WORLD_ENTITY_HPP_
3
4 #include "Chunk.hpp"
5 #include "EntityState.hpp"
6 #include "../model/Instance.hpp"
7 #include "../model/geometry.hpp"
8
9 #include <cstdint>
10 #include <string>
11 #include <glm/glm.hpp>
12 #include <glm/gtc/quaternion.hpp>
13
14
15 namespace blank {
16
17 class DirectionalLighting;
18 class Shape;
19
20 class Entity {
21
22 public:
23         Entity() noexcept;
24
25         Instance &GetModel() noexcept { return model; }
26         const Instance &GetModel() const noexcept { return model; }
27
28         std::uint32_t ID() const noexcept { return id; }
29         void ID(std::uint32_t i) noexcept { id = i; }
30
31         const std::string &Name() const noexcept { return name; }
32         void Name(const std::string &n) { name = n; }
33
34         const AABB &Bounds() const noexcept { return bounds; }
35         void Bounds(const AABB &b) noexcept { bounds = b; }
36
37         bool WorldCollidable() const noexcept { return world_collision; }
38         void WorldCollidable(bool b) noexcept { world_collision = b; }
39
40         const glm::vec3 &TargetVelocity() const noexcept { return tgt_vel; }
41         void TargetVelocity(const glm::vec3 &v) noexcept { tgt_vel = v; }
42
43         const glm::vec3 &Velocity() const noexcept { return state.velocity; }
44         void Velocity(const glm::vec3 &v) noexcept { state.velocity = v; }
45
46         const glm::vec3 &Position() const noexcept { return state.block_pos; }
47         void Position(const glm::ivec3 &, const glm::vec3 &) noexcept;
48         void Position(const glm::vec3 &) noexcept;
49
50         const glm::ivec3 ChunkCoords() const noexcept { return state.chunk_pos; }
51
52         glm::vec3 AbsolutePosition() const noexcept {
53                 return state.AbsolutePosition();
54         }
55         glm::vec3 AbsoluteDifference(const Entity &other) const noexcept {
56                 return state.Diff(other.state);
57         }
58
59         /// direction is rotation axis, magnitude is speed in rad/s
60         const glm::vec3 &AngularVelocity() const noexcept { return state.ang_vel; }
61         void AngularVelocity(const glm::vec3 &v) noexcept { state.ang_vel = v; }
62
63         const glm::quat &Orientation() const noexcept { return state.orient; }
64         void Orientation(const glm::quat &o) noexcept { state.orient = o; }
65
66         /// get a transform for this entity's coordinate space
67         glm::mat4 Transform(const glm::ivec3 &reference) const noexcept;
68         /// get a transform for this entity's view space
69         glm::mat4 ViewTransform(const glm::ivec3 &reference) const noexcept;
70         /// get a ray in entity's face direction originating from center of vision
71         Ray Aim(const Chunk::Pos &chunk_offset) const noexcept;
72
73         void SetState(const EntityState &s) noexcept { state = s; }
74         EntityState &GetState() noexcept { return state; }
75         const EntityState &GetState() const noexcept { return state; }
76
77         void Ref() noexcept { ++ref_count; }
78         void UnRef() noexcept { --ref_count; }
79         void Kill() noexcept { dead = true; }
80         bool Referenced() const noexcept { return ref_count > 0; }
81         bool Dead() const noexcept { return dead; }
82         bool CanRemove() const noexcept { return dead && ref_count <= 0; }
83
84         void Render(const glm::mat4 &M, DirectionalLighting &prog) noexcept {
85                 if (model) model.Render(M, prog);
86         }
87
88 private:
89         Instance model;
90
91         std::uint32_t id;
92         std::string name;
93
94         AABB bounds;
95         EntityState state;
96         glm::vec3 tgt_vel;
97
98         int ref_count;
99
100         bool world_collision;
101         bool dead;
102
103 };
104
105 }
106
107 #endif