]> git.localhorst.tv Git - blank.git/blob - src/world/Entity.hpp
cache some basic entity axes
[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         bool Moving() const noexcept {
67                 return dot(Velocity(), Velocity()) > std::numeric_limits<float>::epsilon();
68         }
69
70         const glm::vec3 &Position() const noexcept { return state.block_pos; }
71         void Position(const glm::ivec3 &, const glm::vec3 &) noexcept;
72         void Position(const glm::vec3 &) noexcept;
73
74         const glm::ivec3 ChunkCoords() const noexcept { return state.chunk_pos; }
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         const glm::quat &Orientation() const noexcept { return state.orient; }
85
86         /// orientation of head within local coordinate system, in radians
87         float Pitch() const noexcept { return state.pitch; }
88         float Yaw() const noexcept { return state.yaw; }
89         void TurnHead(float delta_pitch, float delta_yaw) noexcept;
90         void SetHead(float pitch, float yaw) noexcept;
91
92         /// get a transform for this entity's coordinate space
93         glm::mat4 Transform(const glm::ivec3 &reference) const noexcept;
94         /// get a transform for this entity's view space
95         glm::mat4 ViewTransform(const glm::ivec3 &reference) const noexcept;
96         /// get a ray in entity's face direction originating from center of vision
97         Ray Aim(const Chunk::Pos &chunk_offset) const noexcept;
98
99         const glm::vec3 &Heading() const noexcept { return heading; }
100
101         void SetState(const EntityState &s) noexcept { state = s; UpdateModel(); }
102         const EntityState &GetState() const noexcept { return state; }
103
104         void Ref() noexcept { ++ref_count; }
105         void UnRef() noexcept { --ref_count; }
106         void Kill() noexcept { dead = true; }
107         bool Referenced() const noexcept { return ref_count > 0; }
108         bool Dead() const noexcept { return dead; }
109         bool CanRemove() const noexcept { return dead && ref_count <= 0; }
110
111         void Update(float dt);
112
113         void Render(const glm::mat4 &M, DirectionalLighting &prog) noexcept {
114                 if (model) model.Render(M, prog);
115         }
116
117 private:
118         void UpdateModel() noexcept;
119         void UpdateView() noexcept;
120         void UpdateHeading() noexcept;
121
122 private:
123         EntityController *ctrl;
124         Instance model;
125
126         std::uint32_t id;
127         std::string name;
128
129         AABB bounds;
130         EntityState state;
131
132         /// local transform of eyes
133         /// if this entity has no model, the eyes are assumed to
134         /// be at local origin and oriented towards -Z
135         glm::mat4 view_local;
136         /// normalized velocity or heading if standing still
137         glm::vec3 heading;
138
139         // TODO: I'd prefer a drag solution
140         float max_vel;
141         float max_force;
142
143         int ref_count;
144
145         bool world_collision;
146         bool dead;
147
148         bool owns_controller;
149
150 };
151
152 }
153
154 #endif