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