#include "../app/Application.h"
#include "../app/Input.h"
+#include <algorithm>
+
using app::Application;
using app::Input;
using geometry::Vector;
MapState::MapState(const Map *map)
: map(map)
+, controlled(0)
, tempTarget(20, 20)
, camera(100, 100, &tempTarget) {
void MapState::HandleEvents(const Input &input) {
-
+ if (!controlled) return;
}
void MapState::UpdateWorld(float deltaT) {
-
+ for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
+ (*i)->Update(deltaT);
+ }
}
void MapState::Render(SDL_Surface *screen) {
Vector<int> offset(camera.CalculateOffset());
map->Render(screen, offset);
+
+ std::sort(entities.begin(), entities.end(), ZCompare);
+ for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
+ (*i)->Render(screen, offset);
+ }
+}
+
+
+bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
+ return lhs->Position().Y() < rhs->Position().Y();
}
}
#ifndef MAP_MAPSTATE_H_
#define MAP_MAPSTATE_H_
+#include "Entity.h"
#include "fwd.h"
#include "../app/State.h"
#include "../geometry/Vector.h"
#include "../graphics/Camera.h"
+#include <vector>
+
namespace map {
class MapState
virtual void UpdateWorld(float deltaT);
virtual void Render(SDL_Surface *);
+public:
+ void AddEntity(Entity *e) { entities.push_back(e); }
+ void ControlEntity(Entity *e) { controlled = e; camera.SetTarget(&e->Position()); }
+
+private:
+ static bool ZCompare(const Entity *lhs, const Entity *rhs);
+
private:
const Map *map;
- geometry::Vector<int> tempTarget;
+ Entity *controlled;
+ geometry::Vector<float> tempTarget;
graphics::Camera camera;
+ std::vector<Entity *> entities;
};