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