1 #ifndef BLANK_WORLD_ENTITY_HPP_
2 #define BLANK_WORLD_ENTITY_HPP_
5 #include "EntityState.hpp"
6 #include "../geometry/primitive.hpp"
7 #include "../model/Instance.hpp"
11 #include <glm/glm.hpp>
12 #include <glm/gtc/quaternion.hpp>
17 class DirectionalLighting;
18 class EntityController;
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;
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; }
42 Instance &GetModel() noexcept { return model; }
43 const Instance &GetModel() const noexcept { return model; }
45 std::uint32_t ID() const noexcept { return id; }
46 void ID(std::uint32_t i) noexcept { id = i; }
48 const std::string &Name() const noexcept { return name; }
49 void Name(const std::string &n) { name = n; }
51 const AABB &Bounds() const noexcept { return bounds; }
52 // get distance between local origin and farthest vertex
53 float Radius() const noexcept { return radius; }
54 void Bounds(const AABB &b) noexcept { bounds = b; radius = b.OriginRadius(); }
56 bool WorldCollidable() const noexcept { return world_collision; }
57 void WorldCollidable(bool b) noexcept { world_collision = b; }
59 float MaxVelocity() const noexcept { return max_vel; }
60 void MaxVelocity(float v) noexcept { max_vel = v; }
61 float MaxControlForce() const noexcept { return max_force; }
62 void MaxControlForce(float f) noexcept { max_force = f; }
64 glm::vec3 ControlForce(const EntityState &) const noexcept;
66 const glm::vec3 &Velocity() const noexcept { return state.velocity; }
68 const ExactLocation::Fine &Position() const noexcept { return state.pos.block; }
69 void Position(const ExactLocation::Coarse &, const ExactLocation::Fine &) noexcept;
70 void Position(const ExactLocation::Fine &) noexcept;
72 const glm::ivec3 ChunkCoords() const noexcept { return state.pos.chunk; }
74 glm::vec3 AbsolutePosition() const noexcept {
75 return state.AbsolutePosition();
77 glm::vec3 AbsoluteDifference(const Entity &other) const noexcept {
78 return state.Diff(other.state);
81 /// orientation of local coordinate system
82 void Orientation(const glm::quat &o) noexcept { state.orient = o; }
83 const glm::quat &Orientation() const noexcept { return state.orient; }
85 /// orientation of head within local coordinate system, in radians
86 float Pitch() const noexcept { return state.pitch; }
87 float Yaw() const noexcept { return state.yaw; }
88 void TurnHead(float delta_pitch, float delta_yaw) noexcept;
89 void SetHead(float pitch, float yaw) noexcept;
91 /// get a transform for this entity's coordinate space
92 const glm::mat4 Transform() const noexcept { return model_transform; }
93 /// get a transform for this entity's coordinate space relative to reference chunk
94 glm::mat4 Transform(const glm::ivec3 &reference) const noexcept;
95 /// get a transform for this entity's view space relative to reference chunk
96 glm::mat4 ViewTransform(const glm::ivec3 &reference) const noexcept;
97 /// get a ray in entity's face direction originating from center of vision
98 Ray Aim(const ExactLocation::Coarse &chunk_offset) const noexcept;
100 /// true if this entity's position will change (significantly) the next update
101 bool Moving() const noexcept { return speed > 0.0f; }
102 /// magnitude of velocity
103 float Speed() const noexcept { return speed; }
104 /// normalized velocity or heading if standing still
105 const glm::vec3 &Heading() const noexcept { return heading; }
107 void SetState(const EntityState &s) noexcept { state = s; }
108 const EntityState &GetState() const noexcept { return state; }
110 void Ref() noexcept { ++ref_count; }
111 void UnRef() noexcept { --ref_count; }
112 void Kill() noexcept { dead = true; }
113 bool Referenced() const noexcept { return ref_count > 0; }
114 bool Dead() const noexcept { return dead; }
115 bool CanRemove() const noexcept { return dead && ref_count <= 0; }
117 void Update(float dt);
119 void Render(const glm::mat4 &M, DirectionalLighting &prog) noexcept {
120 if (model) model.Render(M, prog);
124 void UpdateTransforms() noexcept;
125 void UpdateHeading() noexcept;
126 void UpdateModel(float dt) noexcept;
128 // temporarily made public so AI can use it until it's smoothed out to be suitable for players, too
129 void OrientBody(float dt) noexcept;
131 void OrientHead(float dt) noexcept;
134 EntityController *ctrl;
144 /// chunk to model space
145 glm::mat4 model_transform;
146 /// model to view space
147 /// if this entity has no model, the eyes are assumed to
148 /// be at origin and oriented towards pitch of model space
149 glm::mat4 view_transform;
153 // TODO: I'd prefer a drag solution
159 bool world_collision;
162 bool owns_controller;