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