#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) {
+, camera(100, 100, &tempTarget)
+, walkingSpeed(64) {
}
void MapState::HandleEvents(const Input &input) {
-
+ if (!controlled) return;
+ if (!controlled->TileLock(map->Tileset()->Width(), map->Tileset()->Height())) return;
+
+ if (input.IsDown(Input::PAD_UP)) {
+ controlled->SetOrientation(Entity::ORIENTATION_NORTH);
+ controlled->SetSpeed(walkingSpeed);
+ } else if (input.IsDown(Input::PAD_RIGHT)) {
+ controlled->SetOrientation(Entity::ORIENTATION_EAST);
+ controlled->SetSpeed(walkingSpeed);
+ } else if (input.IsDown(Input::PAD_DOWN)) {
+ controlled->SetOrientation(Entity::ORIENTATION_SOUTH);
+ controlled->SetSpeed(walkingSpeed);
+ } else if (input.IsDown(Input::PAD_LEFT)) {
+ controlled->SetOrientation(Entity::ORIENTATION_WEST);
+ controlled->SetSpeed(walkingSpeed);
+ } else {
+ controlled->SetSpeed(0.0f);
+ }
}
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) {
+ SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
+
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();
}
}