]> git.localhorst.tv Git - blobs.git/blob - src/world/sim.cpp
track a few things
[blobs.git] / src / world / sim.cpp
1 #include "Simulation.hpp"
2
3 #include "Body.hpp"
4 #include "Planet.hpp"
5 #include "Sun.hpp"
6 #include "../creature/Creature.hpp"
7
8 #include <algorithm>
9
10
11 namespace blobs {
12 namespace world {
13
14 Simulation::Simulation(Body &r, app::Assets &assets)
15 : root(r)
16 , assets(assets)
17 , bodies()
18 , planets()
19 , suns()
20 , alive()
21 , dead()
22 , time(0.0)
23 , records(7) {
24         AddBody(r);
25         records[0].name = "Age";
26         records[0].type = Record::TIME;
27         records[1].name = "Mass";
28         records[1].type = Record::MASS;
29         records[2].name = "Size";
30         records[2].type = Record::LENGTH;
31         records[3].name = "Strength";
32         records[4].name = "Stamina";
33         records[5].name = "Dexerty";
34         records[6].name = "Intelligence";
35 }
36
37 Simulation::~Simulation() {
38         for (auto c : alive) {
39                 delete c;
40         }
41         for (auto c : dead) {
42                 delete c;
43         }
44 }
45
46 void Simulation::Tick(double dt) {
47         time += dt;
48         for (auto body : bodies) {
49                 body->Tick(dt);
50         }
51         for (auto c : alive) {
52                 CheckRecords(*c);
53         }
54 }
55
56 void Simulation::AddBody(Body &b) {
57         b.SetSimulation(*this);
58         bodies.insert(&b);
59 }
60
61 void Simulation::AddPlanet(Planet &p) {
62         AddBody(p);
63         planets.insert(&p);
64 }
65
66 void Simulation::AddSun(Sun &s) {
67         AddBody(s);
68         suns.insert(&s);
69 }
70
71 void Simulation::SetAlive(creature::Creature *c) {
72         alive.push_back(c);
73 }
74
75 void Simulation::SetDead(creature::Creature *c) {
76         auto entry = std::find(alive.begin(), alive.end(), c);
77         if (entry != alive.end()) {
78                 alive.erase(entry);
79         }
80         dead.push_back(c);
81         CheckRecords(*c);
82 }
83
84 void Simulation::CheckRecords(creature::Creature &c) noexcept {
85         if (c.Age() > records[0].value) {
86                 records[0].value = c.Age();
87                 records[0].time = Time();
88                 records[0].holder = &c;
89         }
90         if (c.Mass() > records[1].value) {
91                 records[1].value = c.Mass();
92                 records[1].time = Time();
93                 records[1].holder = &c;
94         }
95         if (c.Size() > records[2].value) {
96                 records[2].value = c.Size();
97                 records[2].time = Time();
98                 records[2].holder = &c;
99         }
100         if (c.Strength() > records[3].value) {
101                 records[3].value = c.Strength();
102                 records[3].time = Time();
103                 records[3].holder = &c;
104         }
105         if (c.Stamina() > records[4].value) {
106                 records[4].value = c.Stamina();
107                 records[4].time = Time();
108                 records[4].holder = &c;
109         }
110         if (c.Dexerty() > records[5].value) {
111                 records[5].value = c.Dexerty();
112                 records[5].time = Time();
113                 records[5].holder = &c;
114         }
115         if (c.Intelligence() > records[6].value) {
116                 records[6].value = c.Intelligence();
117                 records[6].time = Time();
118                 records[6].holder = &c;
119         }
120 }
121
122 }
123 }