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