]> git.localhorst.tv Git - blank.git/blob - src/world/Entity.hpp
give unique IDs to entities
[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 "../model/CompositeInstance.hpp"
6 #include "../model/geometry.hpp"
7
8 #include <cstdint>
9 #include <string>
10 #include <glm/glm.hpp>
11 #include <glm/gtc/quaternion.hpp>
12
13
14 namespace blank {
15
16 class DirectionalLighting;
17 class Shape;
18
19 class Entity {
20
21 public:
22         Entity() noexcept;
23
24         CompositeInstance &GetModel() noexcept { return model; }
25         const CompositeInstance &GetModel() const noexcept { return model; }
26
27         std::uint32_t ID() const noexcept { return id; }
28         void ID(std::uint32_t i) noexcept { id = i; }
29
30         const std::string &Name() const noexcept { return name; }
31         void Name(const std::string &n) { name = n; }
32
33         const AABB &Bounds() const noexcept { return bounds; }
34         void Bounds(const AABB &b) noexcept { bounds = b; }
35
36         bool WorldCollidable() const noexcept { return world_collision; }
37         void WorldCollidable(bool b) noexcept { world_collision = b; }
38
39         const glm::vec3 &Velocity() const noexcept { return velocity; }
40         void Velocity(const glm::vec3 &v) noexcept { velocity = v; }
41
42         const glm::vec3 &Position() const noexcept { return model.Position(); }
43         void Position(const Chunk::Pos &, const glm::vec3 &) noexcept;
44         void Position(const glm::vec3 &) noexcept;
45         void Move(const glm::vec3 &delta) noexcept;
46
47         const Chunk::Pos ChunkCoords() const noexcept { return chunk; }
48
49         glm::vec3 AbsolutePosition() const noexcept {
50                 return glm::vec3(chunk * Chunk::Extent()) + Position();
51         }
52         glm::vec3 AbsoluteDifference(const Entity &other) const noexcept {
53                 return glm::vec3((chunk - other.chunk) * Chunk::Extent()) + Position() - other.Position();
54         }
55
56         /// direction is rotation axis, magnitude is speed in rad/ms
57         const glm::vec3 &AngularVelocity() const noexcept { return angular_velocity; }
58         void AngularVelocity(const glm::vec3 &v) noexcept { angular_velocity = v; }
59
60         const glm::quat &Orientation() const noexcept { return model.Orientation(); }
61         void Orientation(const glm::quat &o) noexcept { model.Orientation(o); }
62         void Rotate(const glm::quat &delta) noexcept;
63
64         glm::mat4 ChunkTransform(const Chunk::Pos &chunk_offset) const noexcept;
65         glm::mat4 Transform(const Chunk::Pos &chunk_offset) const noexcept;
66         Ray Aim(const Chunk::Pos &chunk_offset) const noexcept;
67
68         void Ref() noexcept { ++ref_count; }
69         void UnRef() noexcept { --ref_count; }
70         void Kill() noexcept { dead = true; }
71         bool Referenced() const noexcept { return ref_count > 0; }
72         bool Dead() const noexcept { return dead; }
73         bool CanRemove() const noexcept { return dead && ref_count <= 0; }
74
75         void Update(int dt) noexcept;
76
77         void Render(const glm::mat4 &M, DirectionalLighting &prog) noexcept {
78                 if (model) model.Render(M, prog);
79         }
80
81 private:
82         CompositeInstance model;
83
84         std::uint32_t id;
85         std::string name;
86
87         AABB bounds;
88
89         glm::vec3 velocity;
90         Chunk::Pos chunk;
91
92         glm::vec3 angular_velocity;
93
94         int ref_count;
95
96         bool world_collision;
97         bool dead;
98
99 };
100
101 }
102
103 #endif