]> git.localhorst.tv Git - blank.git/blob - src/world/Entity.hpp
first ideas for placing oriented blocks
[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 the entity's local up vector
100         const glm::vec4 &Up() const noexcept { return model_transform[1]; }
101         /// get a transform for this entity's coordinate space relative to reference chunk
102         glm::mat4 Transform(const glm::ivec3 &reference) const noexcept;
103         /// get a transform for this entity's view space relative to reference chunk
104         glm::mat4 ViewTransform(const glm::ivec3 &reference) const noexcept;
105         /// get a ray in entity's face direction originating from center of vision
106         Ray Aim(const ExactLocation::Coarse &chunk_offset) const noexcept;
107
108         /// true if this entity's position will change (significantly) the next update
109         bool Moving() const noexcept { return speed > 0.0f; }
110         /// magnitude of velocity
111         float Speed() const noexcept { return speed; }
112         /// normalized velocity or heading if standing still
113         const glm::vec3 &Heading() const noexcept { return heading; }
114
115         void SetState(const EntityState &s) noexcept { state = s; }
116         const EntityState &GetState() const noexcept { return state; }
117
118         void Ref() noexcept { ++ref_count; }
119         void UnRef() noexcept { --ref_count; }
120         void Kill() noexcept { dead = true; }
121         bool Referenced() const noexcept { return ref_count > 0; }
122         bool Dead() const noexcept { return dead; }
123         bool CanRemove() const noexcept { return dead && ref_count <= 0; }
124
125         void Update(World &, float dt);
126
127         void Render(const glm::mat4 &M, DirectionalLighting &prog) noexcept {
128                 if (model) model.Render(M, prog);
129         }
130
131 private:
132         void UpdatePhysics(World &, float dt);
133         void UpdateTransforms() noexcept;
134         void UpdateHeading() noexcept;
135         void UpdateModel(float dt) noexcept;
136 public:
137         // temporarily made public so AI can use it until it's smoothed out to be suitable for players, too
138         void OrientBody(float dt) noexcept;
139 private:
140         void OrientHead(float dt) noexcept;
141
142         EntityDerivative CalculateStep(
143                 World &,
144                 const EntityState &cur,
145                 float dt,
146                 const EntityDerivative &prev
147         ) const;
148
149
150 private:
151         Steering steering;
152         EntityController *ctrl;
153         Instance model;
154
155         std::uint32_t id;
156         std::string name;
157
158         AABB bounds;
159         float radius;
160         EntityState state;
161
162         /// chunk to model space
163         glm::mat4 model_transform;
164         /// model to view space
165         /// if this entity has no model, the eyes are assumed to
166         /// be at origin and oriented towards pitch of model space
167         glm::mat4 view_transform;
168         float speed;
169         glm::vec3 heading;
170
171         // TODO: I'd prefer a drag solution
172         float max_vel;
173         // TODO: split max_force into (local) axes?
174         //       e.g. players may want to disable vertical thrust under certain
175         //       conditions (e.g. "walking" ^^)
176         float max_force;
177
178         int ref_count;
179
180         bool world_collision;
181         bool dead;
182
183         bool owns_controller;
184
185 };
186
187 }
188
189 #endif