]> git.localhorst.tv Git - blank.git/blob - src/world/Entity.hpp
more transform caching
[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 "../model/Instance.hpp"
7 #include "../model/geometry.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         glm::mat4 Transform(const glm::ivec3 &reference) const noexcept;
90         /// get a transform for this entity's view space
91         glm::mat4 ViewTransform(const glm::ivec3 &reference) const noexcept;
92         /// get a ray in entity's face direction originating from center of vision
93         Ray Aim(const Chunk::Pos &chunk_offset) const noexcept;
94
95         /// true if this entity's position will change (significantly) the next update
96         bool Moving() const noexcept { return speed > 0.0f; }
97         /// magnitude of velocity
98         float Speed() const noexcept { return speed; }
99         /// normalized velocity or heading if standing still
100         const glm::vec3 &Heading() const noexcept { return heading; }
101
102         void SetState(const EntityState &s) noexcept { state = s; UpdateModel(); }
103         const EntityState &GetState() const noexcept { return state; }
104
105         void Ref() noexcept { ++ref_count; }
106         void UnRef() noexcept { --ref_count; }
107         void Kill() noexcept { dead = true; }
108         bool Referenced() const noexcept { return ref_count > 0; }
109         bool Dead() const noexcept { return dead; }
110         bool CanRemove() const noexcept { return dead && ref_count <= 0; }
111
112         void Update(float dt);
113
114         void Render(const glm::mat4 &M, DirectionalLighting &prog) noexcept {
115                 if (model) model.Render(M, prog);
116         }
117
118 private:
119         void UpdateModel() noexcept;
120         void UpdateTransforms() noexcept;
121         void UpdateHeading() noexcept;
122
123 private:
124         EntityController *ctrl;
125         Instance model;
126
127         std::uint32_t id;
128         std::string name;
129
130         AABB bounds;
131         EntityState state;
132
133         /// chunk to model space
134         glm::mat4 model_transform;
135         /// model to view space
136         /// if this entity has no model, the eyes are assumed to
137         /// be at origin and oriented towards pitch of model space
138         glm::mat4 view_transform;
139         float speed;
140         glm::vec3 heading;
141
142         // TODO: I'd prefer a drag solution
143         float max_vel;
144         float max_force;
145
146         int ref_count;
147
148         bool world_collision;
149         bool dead;
150
151         bool owns_controller;
152
153 };
154
155 }
156
157 #endif