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