]> git.localhorst.tv Git - blobs.git/blob - src/creature/Creature.hpp
track where a creature has been
[blobs.git] / src / creature / Creature.hpp
1 #ifndef BLOBS_CREATURE_CREATURE_HPP_
2 #define BLOBS_CREATURE_CREATURE_HPP_
3
4 #include "Genome.hpp"
5 #include "Goal.hpp"
6 #include "Memory.hpp"
7 #include "Need.hpp"
8 #include "Situation.hpp"
9 #include "Steering.hpp"
10 #include "../graphics/SimpleVAO.hpp"
11 #include "../math/glm.hpp"
12
13 #include <memory>
14 #include <string>
15 #include <vector>
16
17
18 namespace blobs {
19 namespace app {
20         struct Assets;
21 }
22 namespace graphics {
23         class Viewport;
24 }
25 namespace world {
26         class Body;
27         class Planet;
28         class Simulation;
29 }
30 namespace creature {
31
32 class Creature {
33
34 public:
35         using Callback = std::function<void(Creature &)>;
36
37 public:
38         explicit Creature(world::Simulation &);
39         ~Creature();
40
41         Creature(const Creature &) = delete;
42         Creature &operator =(const Creature &) = delete;
43
44         Creature(Creature &&) = delete;
45         Creature &operator =(Creature &&) = delete;
46
47 public:
48         world::Simulation &GetSimulation() noexcept { return sim; }
49         const world::Simulation &GetSimulation() const noexcept { return sim; }
50
51         void Name(const std::string &n) noexcept { name = n; }
52         const std::string &Name() const noexcept { return name; }
53
54         Genome &GetGenome() noexcept { return genome; }
55         const Genome &GetGenome() const noexcept { return genome; }
56
57         Genome::Properties<double> &GetProperties() noexcept { return properties; }
58         const Genome::Properties<double> &GetProperties() const noexcept { return properties; }
59
60         void Mass(double m) noexcept { mass = m; size = std::cbrt(mass / density); }
61         double Mass() const noexcept { return mass; }
62         void Grow(double amount) noexcept;
63
64         void Density(double d) noexcept { density = d; size = std::cbrt(mass / density); }
65         double Density() const noexcept { return density; }
66
67         double Size() const noexcept;
68         double Age() const noexcept;
69         // change of giving birth per tick
70         double Fertility() const noexcept;
71
72         void Health(double h) noexcept { health = h; }
73         double Health() const noexcept { return health; }
74         void Hurt(double d) noexcept;
75         void Die() noexcept;
76         void OnDeath(Callback cb) noexcept { on_death = cb; }
77         void Remove() noexcept { removable = true; }
78         bool Removable() const noexcept { return removable; }
79
80         Memory &GetMemory() noexcept { return memory; }
81         const Memory &GetMemory() const noexcept { return memory; }
82
83         void AddNeed(std::unique_ptr<Need> &&n) { needs.emplace_back(std::move(n)); }
84         const std::vector<std::unique_ptr<Need>> &Needs() const noexcept { return needs; }
85
86         void AddGoal(std::unique_ptr<Goal> &&);
87         const std::vector<std::unique_ptr<Goal>> &Goals() const noexcept { return goals; }
88
89         void Tick(double dt);
90
91         Situation &GetSituation() noexcept { return situation; }
92         const Situation &GetSituation() const noexcept { return situation; }
93
94         Steering &GetSteering() noexcept { return steering; }
95         const Steering &GetSteering() const noexcept { return steering; }
96
97         void Velocity(const glm::dvec3 &v) noexcept { vel = v; }
98         const glm::dvec3 &Velocity() const noexcept { return vel; }
99         bool Moving() const noexcept { return glm::length2(vel) < 0.00000001; }
100
101         glm::dmat4 LocalTransform() noexcept;
102
103         void BuildVAO();
104         void Draw(graphics::Viewport &);
105
106 private:
107         world::Simulation &sim;
108         std::string name;
109
110         Genome genome;
111         Genome::Properties<double> properties;
112
113         double mass;
114         double density;
115         double size;
116
117         double birth;
118         double health;
119         Callback on_death;
120         bool removable;
121
122         Memory memory;
123
124         std::vector<std::unique_ptr<Need>> needs;
125         std::vector<std::unique_ptr<Goal>> goals;
126
127         Situation situation;
128         Steering steering;
129
130         glm::dvec3 vel;
131
132         struct Attributes {
133                 glm::vec3 position;
134                 glm::vec3 normal;
135                 glm::vec3 texture;
136         };
137         graphics::SimpleVAO<Attributes, unsigned short> vao;
138
139 };
140
141 /// put creature on planet and configure it to (hopefully) survive
142 void Spawn(Creature &, world::Planet &);
143
144 /// split the creature into two
145 void Split(Creature &);
146
147 }
148 }
149
150 #endif