4 * Created on: Sep 29, 2012
11 #include "../app/Application.h"
12 #include "../app/Input.h"
16 using app::Application;
18 using geometry::Vector;
22 MapState::MapState(const Map *map)
26 , camera(100, 100, &tempTarget)
32 void MapState::EnterState(Application &ctrl, SDL_Surface *screen) {
33 camera.Resize(screen->w, screen->h);
36 void MapState::ExitState(Application &ctrl, SDL_Surface *screen) {
40 void MapState::ResumeState(Application &ctrl, SDL_Surface *screen) {
41 camera.Resize(screen->w, screen->h);
44 void MapState::PauseState(Application &ctrl, SDL_Surface *screen) {
48 void MapState::Resize(int width, int height) {
49 camera.Resize(width, height);
53 void MapState::HandleEvents(const Input &input) {
54 if (!controlled) return;
55 if (!controlled->TileLock(map->Tileset()->Width(), map->Tileset()->Height())) return;
57 if (input.IsDown(Input::PAD_UP)) {
58 controlled->SetOrientation(Entity::ORIENTATION_NORTH);
59 controlled->SetSpeed(walkingSpeed);
60 } else if (input.IsDown(Input::PAD_RIGHT)) {
61 controlled->SetOrientation(Entity::ORIENTATION_EAST);
62 controlled->SetSpeed(walkingSpeed);
63 } else if (input.IsDown(Input::PAD_DOWN)) {
64 controlled->SetOrientation(Entity::ORIENTATION_SOUTH);
65 controlled->SetSpeed(walkingSpeed);
66 } else if (input.IsDown(Input::PAD_LEFT)) {
67 controlled->SetOrientation(Entity::ORIENTATION_WEST);
68 controlled->SetSpeed(walkingSpeed);
70 controlled->SetSpeed(0.0f);
74 void MapState::UpdateWorld(float deltaT) {
75 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
80 void MapState::Render(SDL_Surface *screen) {
81 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
83 Vector<int> offset(camera.CalculateOffset());
84 map->Render(screen, offset);
86 std::sort(entities.begin(), entities.end(), ZCompare);
87 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
88 (*i)->Render(screen, offset);
93 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
94 return lhs->Position().Y() < rhs->Position().Y();