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) {
}
}
+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 (Ship &s : ships) {
s.Update(delta);
#include "../graphics/Vector.h"
#include <list>
+#include <map>
#include <vector>
const Vector<int> size;
const Vector<int> secSize;
const Vector<int> areaSize;
+ const Vector<int> bounds;
public:
+ using Env = std::map<int, float>;
+
class Area;
class Sector {
Vector<int> AreaCoords() const { return coords; }
Sector ParentSector() const { return univ.SectorAt(SectorCoords()); }
+ const Env &GetEnv() const { return univ.EnvAt(coords); }
private:
const Universe &univ;
public:
Sector SectorAt(Vector<int> coords) const { return Sector(*this, coords); }
Area AreaAt(Vector<int> coords) const { return Area(*this, coords); }
+ const Env &EnvAt(Vector<int> coords) const { return env[coords.x * bounds.y + coords.y]; }
public:
Ship *AddShip(const Ship &);
int AddResource(const Resource &);
const Resource &GetResource(int id) const { return resources[id]; }
+ void DumpEnv(int res, Vector<int> coords, float amount);
+ void DumpEnv(int res, Vector<float> coords, float amount) {
+ DumpEnv(res, Vector<int>(coords), amount);
+ }
+
public:
void Update(float deltaT);
private:
std::list<Ship> ships;
std::vector<Resource> resources;
+ std::vector<Env> env;
};