]> git.localhorst.tv Git - space.git/blobdiff - src/world/Universe.cpp
added universe environmental resources
[space.git] / src / world / Universe.cpp
index 9f9efe627839eb4be29d33ed2751dc225fe0250a..bc1aad8bde2f0dff8c94251c5cb7c7df56f9d65d 100644 (file)
@@ -9,7 +9,9 @@ namespace space {
 Universe::Universe(Vector<int> size, Vector<int> secSize, Vector<int> areaSize)
 : size(size)
 , secSize(secSize)
-, areaSize(areaSize) {
+, areaSize(areaSize)
+, bounds(size * secSize * areaSize)
+, env(bounds.x * bounds.y) {
 
 }
 
@@ -18,31 +20,40 @@ Universe::~Universe() {
 }
 
 
-Entity *Universe::AddEntity(const Entity &e) {
-       entities.emplace_back(e);
-       return &entities.back();
+Ship *Universe::AddShip(const Ship &s) {
+       ships.emplace_back(s);
+       return &ships.back();
+}
+
+int Universe::AddResource(const Resource &r) {
+       resources.emplace_back(r);
+       resources.back().id = resources.size();
+       return resources.size();
+}
+
+
+void Universe::DumpEnv(int res, Vector<int> coords, float amount) {
+       if (coords.x < 0) coords.x = 0;
+       if (coords.x > bounds.x) coords.x = bounds.x - 1;
+       if (coords.y < 0) coords.y = 0;
+       if (coords.y > bounds.x) coords.y = bounds.y - 1;
+
+       int index = coords.x * bounds.y + coords.y;
+
+       Env &point = env[index];
+       Env::iterator entry(point.find(res));
+
+       if (entry == point.end()) {
+               point.emplace(res, amount);
+       } else {
+               entry->second += amount;
+       }
 }
 
 
 void Universe::Update(float delta) {
-       for (Entity &e : entities) {
-               e.Update(delta);
-               while (e.pos.x > areaSize.x) {
-                       e.pos.x -= areaSize.x;
-                       ++e.area.x;
-               }
-               while (e.pos.x < 0) {
-                       e.pos.x += areaSize.x;
-                       --e.area.x;
-               }
-               while (e.pos.y > areaSize.y) {
-                       e.pos.y -= areaSize.y;
-                       ++e.area.y;
-               }
-               while (e.pos.y < 0) {
-                       e.pos.y += areaSize.y;
-                       --e.area.y;
-               }
+       for (Ship &s : ships) {
+               s.Update(delta);
        }
 }