]> git.localhorst.tv Git - blank.git/blob - src/world/Entity.hpp
fix return type for Entity::Transform()
[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 "EntityDerivative.hpp"
6 #include "EntityState.hpp"
7 #include "Steering.hpp"
8 #include "../geometry/primitive.hpp"
9 #include "../model/Instance.hpp"
10
11 #include <cstdint>
12 #include <string>
13 #include <glm/glm.hpp>
14 #include <glm/gtc/quaternion.hpp>
15
16
17 namespace blank {
18
19 class DirectionalLighting;
20 class EntityController;
21 class Shape;
22 class World;
23
24 class Entity {
25
26 public:
27         Entity() noexcept;
28         ~Entity() noexcept;
29
30         // note that when copying an entity which owns its controller, the
31         // original must outlive the copy, otherwise the copy ends up with
32         // an invalid controller pointer
33         Entity(const Entity &) noexcept;
34         Entity &operator =(const Entity &) = delete;
35
36         Steering &GetSteering() noexcept { return steering; }
37         const Steering &GetSteering() const noexcept { return steering; }
38
39         bool HasController() const noexcept { return ctrl; }
40         // entity takes over ownership of controller
41         void SetController(EntityController *c) noexcept;
42         // entity uses shared controller
43         void SetController(EntityController &c) noexcept;
44         void UnsetController() noexcept;
45         EntityController &GetController() noexcept { return *ctrl; }
46         const EntityController &GetController() const noexcept { return *ctrl; }
47
48         Instance &GetModel() noexcept { return model; }
49         const Instance &GetModel() const noexcept { return model; }
50
51         std::uint32_t ID() const noexcept { return id; }
52         void ID(std::uint32_t i) noexcept { id = i; }
53
54         const std::string &Name() const noexcept { return name; }
55         void Name(const std::string &n) { name = n; }
56
57         const AABB &Bounds() const noexcept { return bounds; }
58         // get distance between local origin and farthest vertex
59         float Radius() const noexcept { return radius; }
60         void Bounds(const AABB &b) noexcept { bounds = b; radius = b.OriginRadius(); }
61
62         bool WorldCollidable() const noexcept { return world_collision; }
63         void WorldCollidable(bool b) noexcept { world_collision = b; }
64
65         float MaxVelocity() const noexcept { return max_vel; }
66         void MaxVelocity(float v) noexcept { max_vel = v; }
67         float MaxControlForce() const noexcept { return max_force; }
68         void MaxControlForce(float f) noexcept { max_force = f; }
69
70         glm::vec3 ControlForce(const EntityState &) const noexcept;
71
72         const glm::vec3 &Velocity() const noexcept { return state.velocity; }
73
74         const ExactLocation::Fine &Position() const noexcept { return state.pos.block; }
75         void Position(const ExactLocation::Coarse &, const ExactLocation::Fine &) noexcept;
76         void Position(const ExactLocation::Fine &) noexcept;
77
78         const glm::ivec3 ChunkCoords() const noexcept { return state.pos.chunk; }
79
80         glm::vec3 AbsolutePosition() const noexcept {
81                 return state.AbsolutePosition();
82         }
83         glm::vec3 AbsoluteDifference(const Entity &other) const noexcept {
84                 return state.Diff(other.state);
85         }
86
87         /// orientation of local coordinate system
88         void Orientation(const glm::quat &o) noexcept { state.orient = o; }
89         const glm::quat &Orientation() const noexcept { return state.orient; }
90
91         /// orientation of head within local coordinate system, in radians
92         float Pitch() const noexcept { return state.pitch; }
93         float Yaw() const noexcept { return state.yaw; }
94         void TurnHead(float delta_pitch, float delta_yaw) noexcept;
95         void SetHead(float pitch, float yaw) noexcept;
96
97         /// get a transform for this entity's coordinate space
98         const glm::mat4 &Transform() const noexcept { return model_transform; }
99         /// get a transform for this entity's coordinate space relative to reference chunk
100         glm::mat4 Transform(const glm::ivec3 &reference) const noexcept;
101         /// get a transform for this entity's view space relative to reference chunk
102         glm::mat4 ViewTransform(const glm::ivec3 &reference) const noexcept;
103         /// get a ray in entity's face direction originating from center of vision
104         Ray Aim(const ExactLocation::Coarse &chunk_offset) const noexcept;
105
106         /// true if this entity's position will change (significantly) the next update
107         bool Moving() const noexcept { return speed > 0.0f; }
108         /// magnitude of velocity
109         float Speed() const noexcept { return speed; }
110         /// normalized velocity or heading if standing still
111         const glm::vec3 &Heading() const noexcept { return heading; }
112
113         void SetState(const EntityState &s) noexcept { state = s; }
114         const EntityState &GetState() const noexcept { return state; }
115
116         void Ref() noexcept { ++ref_count; }
117         void UnRef() noexcept { --ref_count; }
118         void Kill() noexcept { dead = true; }
119         bool Referenced() const noexcept { return ref_count > 0; }
120         bool Dead() const noexcept { return dead; }
121         bool CanRemove() const noexcept { return dead && ref_count <= 0; }
122
123         void Update(World &, float dt);
124
125         void Render(const glm::mat4 &M, DirectionalLighting &prog) noexcept {
126                 if (model) model.Render(M, prog);
127         }
128
129 private:
130         void UpdatePhysics(World &, float dt);
131         void UpdateTransforms() noexcept;
132         void UpdateHeading() noexcept;
133         void UpdateModel(float dt) noexcept;
134 public:
135         // temporarily made public so AI can use it until it's smoothed out to be suitable for players, too
136         void OrientBody(float dt) noexcept;
137 private:
138         void OrientHead(float dt) noexcept;
139
140         EntityDerivative CalculateStep(
141                 World &,
142                 const EntityState &cur,
143                 float dt,
144                 const EntityDerivative &prev
145         ) const;
146
147
148 private:
149         Steering steering;
150         EntityController *ctrl;
151         Instance model;
152
153         std::uint32_t id;
154         std::string name;
155
156         AABB bounds;
157         float radius;
158         EntityState state;
159
160         /// chunk to model space
161         glm::mat4 model_transform;
162         /// model to view space
163         /// if this entity has no model, the eyes are assumed to
164         /// be at origin and oriented towards pitch of model space
165         glm::mat4 view_transform;
166         float speed;
167         glm::vec3 heading;
168
169         // TODO: I'd prefer a drag solution
170         float max_vel;
171         // TODO: split max_force into (local) axes?
172         //       e.g. players may want to disable vertical thrust under certain
173         //       conditions (e.g. "walking" ^^)
174         float max_force;
175
176         int ref_count;
177
178         bool world_collision;
179         bool dead;
180
181         bool owns_controller;
182
183 };
184
185 }
186
187 #endif