1 #ifndef BLANK_WORLD_ENTITY_HPP_
2 #define BLANK_WORLD_ENTITY_HPP_
5 #include "EntityState.hpp"
6 #include "../model/Instance.hpp"
7 #include "../model/geometry.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 void Bounds(const AABB &b) noexcept { bounds = b; }
54 bool WorldCollidable() const noexcept { return world_collision; }
55 void WorldCollidable(bool b) noexcept { world_collision = b; }
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; }
62 glm::vec3 ControlForce(const EntityState &) const noexcept;
64 const glm::vec3 &Velocity() const noexcept { return state.velocity; }
65 void Velocity(const glm::vec3 &v) noexcept { state.velocity = v; }
67 bool Moving() const noexcept {
68 return dot(Velocity(), Velocity()) > std::numeric_limits<float>::epsilon();
71 const glm::vec3 &Position() const noexcept { return state.block_pos; }
72 void Position(const glm::ivec3 &, const glm::vec3 &) noexcept;
73 void Position(const glm::vec3 &) noexcept;
75 const glm::ivec3 ChunkCoords() const noexcept { return state.chunk_pos; }
77 glm::vec3 AbsolutePosition() const noexcept {
78 return state.AbsolutePosition();
80 glm::vec3 AbsoluteDifference(const Entity &other) const noexcept {
81 return state.Diff(other.state);
84 /// orientation of local coordinate system
85 const glm::quat &Orientation() const noexcept { return state.orient; }
86 void Orientation(const glm::quat &o) noexcept { state.orient = o; }
88 /// orientation of head within local coordinate system, in radians
89 float Pitch() const noexcept { return state.pitch; }
90 float Yaw() const noexcept { return state.yaw; }
91 void TurnHead(float delta_pitch, float delta_yaw) noexcept;
92 void SetHead(float pitch, float yaw) noexcept;
94 /// get a transform for this entity's coordinate space
95 glm::mat4 Transform(const glm::ivec3 &reference) const noexcept;
96 /// get a transform for this entity's view space
97 glm::mat4 ViewTransform(const glm::ivec3 &reference) const noexcept;
98 /// get a ray in entity's face direction originating from center of vision
99 Ray Aim(const Chunk::Pos &chunk_offset) const noexcept;
101 void SetState(const EntityState &s) noexcept { state = s; UpdateModel(); }
102 const EntityState &GetState() const noexcept { return state; }
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; }
111 void Update(float dt);
113 void Render(const glm::mat4 &M, DirectionalLighting &prog) noexcept {
114 if (model) model.Render(M, prog);
118 void UpdateModel() noexcept;
121 EntityController *ctrl;
130 // TODO: I'd prefer a drag solution
136 bool world_collision;
139 bool owns_controller;