]> git.localhorst.tv Git - blank.git/blob - src/world/Entity.hpp
33eeb7db20540e5e354f1398d7105894d7b3f6ea
[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 EntityController;
19 class Shape;
20
21 class Entity {
22
23 public:
24         Entity() noexcept;
25         ~Entity() noexcept;
26
27         // note that when copying an entity which owns its controller, the
28         // original must outlive the copy, otherwise the copy ends up with
29         // an invalid controller pointer
30         Entity(const Entity &) noexcept;
31         Entity &operator =(const Entity &) = delete;
32
33         bool HasController() const noexcept { return ctrl; }
34         // entity takes over ownership of controller
35         void SetController(EntityController *c) noexcept;
36         // entity uses shared controller
37         void SetController(EntityController &c) noexcept;
38         void UnsetController() noexcept;
39         EntityController &GetController() noexcept { return *ctrl; }
40         const EntityController &GetController() const noexcept { return *ctrl; }
41
42         Instance &GetModel() noexcept { return model; }
43         const Instance &GetModel() const noexcept { return model; }
44
45         std::uint32_t ID() const noexcept { return id; }
46         void ID(std::uint32_t i) noexcept { id = i; }
47
48         const std::string &Name() const noexcept { return name; }
49         void Name(const std::string &n) { name = n; }
50
51         const AABB &Bounds() const noexcept { return bounds; }
52         void Bounds(const AABB &b) noexcept { bounds = b; }
53
54         bool WorldCollidable() const noexcept { return world_collision; }
55         void WorldCollidable(bool b) noexcept { world_collision = b; }
56
57         float MaxVelocity() const noexcept { return max_vel; }
58         void MaxVelocity(float v) noexcept { max_vel = v; }
59         float MaxControlForce() const noexcept { return max_force; }
60         void MaxControlForce(float f) noexcept { max_force = f; }
61
62         glm::vec3 ControlForce(const EntityState &) const noexcept;
63
64         const glm::vec3 &Velocity() const noexcept { return state.velocity; }
65         void Velocity(const glm::vec3 &v) noexcept { state.velocity = v; }
66
67         bool Moving() const noexcept {
68                 return dot(Velocity(), Velocity()) > std::numeric_limits<float>::epsilon();
69         }
70
71         const glm::vec3 &Position() const noexcept { return state.block_pos; }
72         void Position(const glm::ivec3 &, const glm::vec3 &) noexcept;
73         void Position(const glm::vec3 &) noexcept;
74
75         const glm::ivec3 ChunkCoords() const noexcept { return state.chunk_pos; }
76
77         glm::vec3 AbsolutePosition() const noexcept {
78                 return state.AbsolutePosition();
79         }
80         glm::vec3 AbsoluteDifference(const Entity &other) const noexcept {
81                 return state.Diff(other.state);
82         }
83
84         /// orientation of local coordinate system
85         const glm::quat &Orientation() const noexcept { return state.orient; }
86         void Orientation(const glm::quat &o) noexcept { state.orient = o; }
87
88         /// orientation of head within local coordinate system, in radians
89         float Pitch() const noexcept { return state.pitch; }
90         float Yaw() const noexcept { return state.yaw; }
91         void TurnHead(float delta_pitch, float delta_yaw) noexcept;
92         void SetHead(float pitch, float yaw) noexcept;
93
94         /// get a transform for this entity's coordinate space
95         glm::mat4 Transform(const glm::ivec3 &reference) const noexcept;
96         /// get a transform for this entity's view space
97         glm::mat4 ViewTransform(const glm::ivec3 &reference) const noexcept;
98         /// get a ray in entity's face direction originating from center of vision
99         Ray Aim(const Chunk::Pos &chunk_offset) const noexcept;
100
101         void SetState(const EntityState &s) noexcept { state = s; UpdateModel(); }
102         const EntityState &GetState() const noexcept { return state; }
103
104         void Ref() noexcept { ++ref_count; }
105         void UnRef() noexcept { --ref_count; }
106         void Kill() noexcept { dead = true; }
107         bool Referenced() const noexcept { return ref_count > 0; }
108         bool Dead() const noexcept { return dead; }
109         bool CanRemove() const noexcept { return dead && ref_count <= 0; }
110
111         void Update(float dt);
112
113         void Render(const glm::mat4 &M, DirectionalLighting &prog) noexcept {
114                 if (model) model.Render(M, prog);
115         }
116
117 private:
118         void UpdateModel() noexcept;
119
120 private:
121         EntityController *ctrl;
122         Instance model;
123
124         std::uint32_t id;
125         std::string name;
126
127         AABB bounds;
128         EntityState state;
129
130         // TODO: I'd prefer a drag solution
131         float max_vel;
132         float max_force;
133
134         int ref_count;
135
136         bool world_collision;
137         bool dead;
138
139         bool owns_controller;
140
141 };
142
143 }
144
145 #endif