From: Daniel Karbach Date: Thu, 9 Jan 2014 07:14:25 +0000 (+0100) Subject: added universe environmental resources X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;ds=inline;h=dd2641dd1b87a0ee3ae27f000ea9527ee05fddbd;p=space.git added universe environmental resources --- diff --git a/src/world/Universe.cpp b/src/world/Universe.cpp index daac4db..bc1aad8 100644 --- a/src/world/Universe.cpp +++ b/src/world/Universe.cpp @@ -9,7 +9,9 @@ namespace space { Universe::Universe(Vector size, Vector secSize, Vector areaSize) : size(size) , secSize(secSize) -, areaSize(areaSize) { +, areaSize(areaSize) +, bounds(size * secSize * areaSize) +, env(bounds.x * bounds.y) { } @@ -30,6 +32,25 @@ int Universe::AddResource(const Resource &r) { } +void Universe::DumpEnv(int res, Vector 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 (Ship &s : ships) { s.Update(delta); diff --git a/src/world/Universe.h b/src/world/Universe.h index 807c1cd..c4754bd 100644 --- a/src/world/Universe.h +++ b/src/world/Universe.h @@ -6,6 +6,7 @@ #include "../graphics/Vector.h" #include +#include #include @@ -24,8 +25,11 @@ public: const Vector size; const Vector secSize; const Vector areaSize; + const Vector bounds; public: + using Env = std::map; + class Area; class Sector { @@ -57,6 +61,7 @@ public: Vector AreaCoords() const { return coords; } Sector ParentSector() const { return univ.SectorAt(SectorCoords()); } + const Env &GetEnv() const { return univ.EnvAt(coords); } private: const Universe &univ; @@ -67,6 +72,7 @@ public: public: Sector SectorAt(Vector coords) const { return Sector(*this, coords); } Area AreaAt(Vector coords) const { return Area(*this, coords); } + const Env &EnvAt(Vector coords) const { return env[coords.x * bounds.y + coords.y]; } public: Ship *AddShip(const Ship &); @@ -75,12 +81,18 @@ public: int AddResource(const Resource &); const Resource &GetResource(int id) const { return resources[id]; } + void DumpEnv(int res, Vector coords, float amount); + void DumpEnv(int res, Vector coords, float amount) { + DumpEnv(res, Vector(coords), amount); + } + public: void Update(float deltaT); private: std::list ships; std::vector resources; + std::vector env; };