]> git.localhorst.tv Git - space.git/blob - src/world/Universe.cpp
added universe environmental resources
[space.git] / src / world / Universe.cpp
1 #include "Universe.h"
2
3 #include <cstring>
4 #include <memory>
5
6
7 namespace space {
8
9 Universe::Universe(Vector<int> size, Vector<int> secSize, Vector<int> areaSize)
10 : size(size)
11 , secSize(secSize)
12 , areaSize(areaSize)
13 , bounds(size * secSize * areaSize)
14 , env(bounds.x * bounds.y) {
15
16 }
17
18 Universe::~Universe() {
19
20 }
21
22
23 Ship *Universe::AddShip(const Ship &s) {
24         ships.emplace_back(s);
25         return &ships.back();
26 }
27
28 int Universe::AddResource(const Resource &r) {
29         resources.emplace_back(r);
30         resources.back().id = resources.size();
31         return resources.size();
32 }
33
34
35 void Universe::DumpEnv(int res, Vector<int> coords, float amount) {
36         if (coords.x < 0) coords.x = 0;
37         if (coords.x > bounds.x) coords.x = bounds.x - 1;
38         if (coords.y < 0) coords.y = 0;
39         if (coords.y > bounds.x) coords.y = bounds.y - 1;
40
41         int index = coords.x * bounds.y + coords.y;
42
43         Env &point = env[index];
44         Env::iterator entry(point.find(res));
45
46         if (entry == point.end()) {
47                 point.emplace(res, amount);
48         } else {
49                 entry->second += amount;
50         }
51 }
52
53
54 void Universe::Update(float delta) {
55         for (Ship &s : ships) {
56                 s.Update(delta);
57         }
58 }
59
60 }