]> git.localhorst.tv Git - blank.git/blob - src/world/Entity.hpp
4b24944b42b7648e2634daf00087b65284aea4bc
[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 "../geometry/primitive.hpp"
7 #include "../model/Instance.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
66         const glm::vec3 &Position() const noexcept { return state.block_pos; }
67         void Position(const glm::ivec3 &, const glm::vec3 &) noexcept;
68         void Position(const glm::vec3 &) noexcept;
69
70         const glm::ivec3 ChunkCoords() const noexcept { return state.chunk_pos; }
71
72         glm::vec3 AbsolutePosition() const noexcept {
73                 return state.AbsolutePosition();
74         }
75         glm::vec3 AbsoluteDifference(const Entity &other) const noexcept {
76                 return state.Diff(other.state);
77         }
78
79         /// orientation of local coordinate system
80         const glm::quat &Orientation() const noexcept { return state.orient; }
81
82         /// orientation of head within local coordinate system, in radians
83         float Pitch() const noexcept { return state.pitch; }
84         float Yaw() const noexcept { return state.yaw; }
85         void TurnHead(float delta_pitch, float delta_yaw) noexcept;
86         void SetHead(float pitch, float yaw) noexcept;
87
88         /// get a transform for this entity's coordinate space
89         const glm::mat4 Transform() const noexcept { return model_transform; }
90         /// get a transform for this entity's coordinate space relative to reference chunk
91         glm::mat4 Transform(const glm::ivec3 &reference) const noexcept;
92         /// get a transform for this entity's view space relative to reference chunk
93         glm::mat4 ViewTransform(const glm::ivec3 &reference) const noexcept;
94         /// get a ray in entity's face direction originating from center of vision
95         Ray Aim(const Chunk::Pos &chunk_offset) const noexcept;
96
97         /// true if this entity's position will change (significantly) the next update
98         bool Moving() const noexcept { return speed > 0.0f; }
99         /// magnitude of velocity
100         float Speed() const noexcept { return speed; }
101         /// normalized velocity or heading if standing still
102         const glm::vec3 &Heading() const noexcept { return heading; }
103
104         void SetState(const EntityState &s) noexcept { state = s; }
105         const EntityState &GetState() const noexcept { return state; }
106
107         void Ref() noexcept { ++ref_count; }
108         void UnRef() noexcept { --ref_count; }
109         void Kill() noexcept { dead = true; }
110         bool Referenced() const noexcept { return ref_count > 0; }
111         bool Dead() const noexcept { return dead; }
112         bool CanRemove() const noexcept { return dead && ref_count <= 0; }
113
114         void Update(float dt);
115
116         void Render(const glm::mat4 &M, DirectionalLighting &prog) noexcept {
117                 if (model) model.Render(M, prog);
118         }
119
120 private:
121         void UpdateTransforms() noexcept;
122         void UpdateHeading() noexcept;
123         void UpdateModel(float dt) noexcept;
124 public:
125         // temporarily made public so AI can use it until it's smoothed out to be suitable for players, too
126         void OrientBody(float dt) noexcept;
127 private:
128         void OrientHead(float dt) noexcept;
129
130 private:
131         EntityController *ctrl;
132         Instance model;
133
134         std::uint32_t id;
135         std::string name;
136
137         AABB bounds;
138         EntityState state;
139
140         /// chunk to model space
141         glm::mat4 model_transform;
142         /// model to view space
143         /// if this entity has no model, the eyes are assumed to
144         /// be at origin and oriented towards pitch of model space
145         glm::mat4 view_transform;
146         float speed;
147         glm::vec3 heading;
148
149         // TODO: I'd prefer a drag solution
150         float max_vel;
151         float max_force;
152
153         int ref_count;
154
155         bool world_collision;
156         bool dead;
157
158         bool owns_controller;
159
160 };
161
162 }
163
164 #endif