4 * Created on: Sep 29, 2012
12 #include "../app/Application.h"
13 #include "../app/Input.h"
17 using app::Application;
19 using geometry::Vector;
23 MapState::MapState(const Map *map)
27 , camera(100, 100, &tempTarget)
33 void MapState::EnterState(Application &ctrl, SDL_Surface *screen) {
34 camera.Resize(screen->w, screen->h);
37 void MapState::ExitState(Application &ctrl, SDL_Surface *screen) {
41 void MapState::ResumeState(Application &ctrl, SDL_Surface *screen) {
42 camera.Resize(screen->w, screen->h);
45 void MapState::PauseState(Application &ctrl, SDL_Surface *screen) {
49 void MapState::Resize(int width, int height) {
50 camera.Resize(width, height);
54 void MapState::HandleEvents(const Input &input) {
55 if (!controlled) return;
56 if (!controlled->TileLock(map->Tileset()->Width(), map->Tileset()->Height())) return;
59 if (input.IsDown(Input::PAD_UP)) {
60 controlled->SetOrientation(Entity::ORIENTATION_NORTH);
62 } else if (input.IsDown(Input::PAD_RIGHT)) {
63 controlled->SetOrientation(Entity::ORIENTATION_EAST);
65 } else if (input.IsDown(Input::PAD_DOWN)) {
66 controlled->SetOrientation(Entity::ORIENTATION_SOUTH);
68 } else if (input.IsDown(Input::PAD_LEFT)) {
69 controlled->SetOrientation(Entity::ORIENTATION_WEST);
74 const Tile &tile(map->TileAt(controlled->Position()));
76 switch (controlled->GetOrientation()) {
77 case Entity::ORIENTATION_NORTH:
78 blocked = tile.BlocksNorth();
80 case Entity::ORIENTATION_EAST:
81 blocked = tile.BlocksEast();
83 case Entity::ORIENTATION_SOUTH:
84 blocked = tile.BlocksSouth();
86 case Entity::ORIENTATION_WEST:
87 blocked = tile.BlocksWest();
91 controlled->SetSpeed(walkingSpeed);
93 controlled->SetSpeed(0.0f);
96 controlled->SetSpeed(0.0f);
100 void MapState::UpdateWorld(float deltaT) {
101 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
102 (*i)->Update(deltaT);
106 void MapState::Render(SDL_Surface *screen) {
107 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
109 Vector<int> offset(camera.CalculateOffset());
110 map->Render(screen, offset);
112 std::sort(entities.begin(), entities.end(), ZCompare);
113 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
114 (*i)->Render(screen, offset);
119 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
120 return lhs->Position().Y() < rhs->Position().Y();