1 #ifndef BLANK_WORLD_ENTITY_HPP_
2 #define BLANK_WORLD_ENTITY_HPP_
5 #include "EntityDerivative.hpp"
6 #include "EntityState.hpp"
7 #include "Steering.hpp"
8 #include "../geometry/primitive.hpp"
9 #include "../graphics/glm.hpp"
10 #include "../model/Instance.hpp"
14 #include <glm/gtc/quaternion.hpp>
19 class DirectionalLighting;
20 class EntityController;
30 // note that when copying an entity which owns its controller, the
31 // original must outlive the copy, otherwise the copy ends up with
32 // an invalid controller pointer
33 Entity(const Entity &) noexcept;
34 Entity &operator =(const Entity &) = delete;
36 Steering &GetSteering() noexcept { return steering; }
37 const Steering &GetSteering() const noexcept { return steering; }
39 bool HasController() const noexcept { return ctrl; }
40 // entity takes over ownership of controller
41 void SetController(EntityController *c) noexcept;
42 // entity uses shared controller
43 void SetController(EntityController &c) noexcept;
44 void UnsetController() noexcept;
45 EntityController &GetController() noexcept { return *ctrl; }
46 const EntityController &GetController() const noexcept { return *ctrl; }
48 Instance &GetModel() noexcept { return model; }
49 const Instance &GetModel() const noexcept { return model; }
51 std::uint32_t ID() const noexcept { return id; }
52 void ID(std::uint32_t i) noexcept { id = i; }
54 const std::string &Name() const noexcept { return name; }
55 void Name(const std::string &n) { name = n; }
57 const AABB &Bounds() const noexcept { return bounds; }
58 // get distance between local origin and farthest vertex
59 float Radius() const noexcept { return radius; }
60 void Bounds(const AABB &b) noexcept { bounds = b; radius = b.OriginRadius(); }
62 bool WorldCollidable() const noexcept { return world_collision; }
63 void WorldCollidable(bool b) noexcept { world_collision = b; }
65 float MaxVelocity() const noexcept { return max_vel; }
66 void MaxVelocity(float v) noexcept { max_vel = v; }
67 float MaxControlForce() const noexcept { return max_force; }
68 void MaxControlForce(float f) noexcept { max_force = f; }
70 glm::vec3 ControlForce(const EntityState &) const noexcept;
72 const glm::vec3 &Velocity() const noexcept { return state.velocity; }
74 const ExactLocation::Fine &Position() const noexcept { return state.pos.block; }
75 void Position(const ExactLocation::Coarse &, const ExactLocation::Fine &) noexcept;
76 void Position(const ExactLocation::Fine &) noexcept;
78 const glm::ivec3 ChunkCoords() const noexcept { return state.pos.chunk; }
80 glm::vec3 AbsolutePosition() const noexcept {
81 return state.AbsolutePosition();
83 glm::vec3 AbsoluteDifference(const Entity &other) const noexcept {
84 return state.Diff(other.state);
87 /// orientation of local coordinate system
88 void Orientation(const glm::quat &o) noexcept { state.orient = o; }
89 const glm::quat &Orientation() const noexcept { return state.orient; }
91 /// orientation of head within local coordinate system, in radians
92 float Pitch() const noexcept { return state.pitch; }
93 float Yaw() const noexcept { return state.yaw; }
94 void TurnHead(float delta_pitch, float delta_yaw) noexcept;
95 void SetHead(float pitch, float yaw) noexcept;
97 /// get a transform for this entity's coordinate space
98 const glm::mat4 &Transform() const noexcept { return model_transform; }
99 /// get the entity's local up vector
100 const glm::vec4 &Up() const noexcept { return model_transform[1]; }
101 /// get a transform for this entity's coordinate space relative to reference chunk
102 glm::mat4 Transform(const glm::ivec3 &reference) const noexcept;
103 /// get a transform for this entity's view space relative to reference chunk
104 glm::mat4 ViewTransform(const glm::ivec3 &reference) const noexcept;
105 /// get a ray in entity's face direction originating from center of vision
106 Ray Aim(const ExactLocation::Coarse &chunk_offset) const noexcept;
108 /// true if this entity's position will change (significantly) the next update
109 bool Moving() const noexcept { return speed > 0.0f; }
110 /// magnitude of velocity
111 float Speed() const noexcept { return speed; }
112 /// normalized velocity or heading if standing still
113 const glm::vec3 &Heading() const noexcept { return heading; }
115 void SetState(const EntityState &s) noexcept { state = s; }
116 const EntityState &GetState() const noexcept { return state; }
118 void Ref() noexcept { ++ref_count; }
119 void UnRef() noexcept { --ref_count; }
120 void Kill() noexcept { dead = true; }
121 bool Referenced() const noexcept { return ref_count > 0; }
122 bool Dead() const noexcept { return dead; }
123 bool CanRemove() const noexcept { return dead && ref_count <= 0; }
125 void Update(World &, float dt);
127 void Render(const glm::mat4 &M, DirectionalLighting &prog) noexcept {
128 if (model) model.Render(M, prog);
132 void UpdatePhysics(World &, float dt);
133 void UpdateTransforms() noexcept;
134 void UpdateHeading() noexcept;
135 void UpdateModel(float dt) noexcept;
137 // temporarily made public so AI can use it until it's smoothed out to be suitable for players, too
138 void OrientBody(float dt) noexcept;
140 void OrientHead(float dt) noexcept;
142 EntityDerivative CalculateStep(
144 const EntityState &cur,
146 const EntityDerivative &prev
152 EntityController *ctrl;
162 /// chunk to model space
163 glm::mat4 model_transform;
164 /// model to view space
165 /// if this entity has no model, the eyes are assumed to
166 /// be at origin and oriented towards pitch of model space
167 glm::mat4 view_transform;
171 // TODO: I'd prefer a drag solution
173 // TODO: split max_force into (local) axes?
174 // e.g. players may want to disable vertical thrust under certain
175 // conditions (e.g. "walking" ^^)
180 bool world_collision;
183 bool owns_controller;