]> git.localhorst.tv Git - blank.git/blob - src/world/Entity.hpp
use "forces" for entity control and RK4 integrator
[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 "EntityState.hpp"
6 #include "../model/Instance.hpp"
7 #include "../model/geometry.hpp"
8
9 #include <cstdint>
10 #include <string>
11 #include <glm/glm.hpp>
12 #include <glm/gtc/quaternion.hpp>
13
14
15 namespace blank {
16
17 class DirectionalLighting;
18 struct EntityDerivative;
19 class Shape;
20
21 class Entity {
22
23 public:
24         Entity() noexcept;
25
26         Instance &GetModel() noexcept { return model; }
27         const Instance &GetModel() const noexcept { return model; }
28
29         std::uint32_t ID() const noexcept { return id; }
30         void ID(std::uint32_t i) noexcept { id = i; }
31
32         const std::string &Name() const noexcept { return name; }
33         void Name(const std::string &n) { name = n; }
34
35         const AABB &Bounds() const noexcept { return bounds; }
36         void Bounds(const AABB &b) noexcept { bounds = b; }
37
38         bool WorldCollidable() const noexcept { return world_collision; }
39         void WorldCollidable(bool b) noexcept { world_collision = b; }
40
41         const glm::vec3 &TargetVelocity() const noexcept { return tgt_vel; }
42         void TargetVelocity(const glm::vec3 &v) noexcept { tgt_vel = v; }
43
44         const glm::vec3 &Velocity() const noexcept { return state.velocity; }
45         void Velocity(const glm::vec3 &v) noexcept { state.velocity = v; }
46
47         const glm::vec3 &Position() const noexcept { return state.block_pos; }
48         void Position(const glm::ivec3 &, const glm::vec3 &) noexcept;
49         void Position(const glm::vec3 &) noexcept;
50
51         const glm::ivec3 ChunkCoords() const noexcept { return state.chunk_pos; }
52
53         glm::vec3 AbsolutePosition() const noexcept {
54                 return state.AbsolutePosition();
55         }
56         glm::vec3 AbsoluteDifference(const Entity &other) const noexcept {
57                 return state.Diff(other.state);
58         }
59
60         /// direction is rotation axis, magnitude is speed in rad/ms
61         const glm::vec3 &AngularVelocity() const noexcept { return state.ang_vel; }
62         void AngularVelocity(const glm::vec3 &v) noexcept { state.ang_vel = v; }
63
64         const glm::quat &Orientation() const noexcept { return state.orient; }
65         void Orientation(const glm::quat &o) noexcept { state.orient = o; }
66
67         glm::mat4 Transform(const glm::ivec3 &reference) const noexcept {
68                 return state.Transform(reference);
69         }
70         Ray Aim(const Chunk::Pos &chunk_offset) const noexcept;
71
72         void SetState(const EntityState &s) noexcept { state = s; }
73         EntityState &GetState() noexcept { return state; }
74         const EntityState &GetState() const noexcept { return state; }
75
76         void Ref() noexcept { ++ref_count; }
77         void UnRef() noexcept { --ref_count; }
78         void Kill() noexcept { dead = true; }
79         bool Referenced() const noexcept { return ref_count > 0; }
80         bool Dead() const noexcept { return dead; }
81         bool CanRemove() const noexcept { return dead && ref_count <= 0; }
82
83         void Update(int dt) noexcept;
84
85         void Render(const glm::mat4 &M, DirectionalLighting &prog) noexcept {
86                 if (model) model.Render(M, prog);
87         }
88
89 private:
90         EntityDerivative CalculateStep(
91                 const EntityState &cur,
92                 float dt,
93                 const EntityDerivative &prev
94         ) const noexcept;
95         glm::vec3 ControlForce(const EntityState &) const noexcept;
96
97 private:
98         Instance model;
99
100         std::uint32_t id;
101         std::string name;
102
103         AABB bounds;
104         EntityState state;
105         glm::vec3 tgt_vel;
106
107         int ref_count;
108
109         bool world_collision;
110         bool dead;
111
112 };
113
114 }
115
116 #endif