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;
57 if (input.IsDown(Input::PAD_UP)) {
58 nextDirection = Entity::ORIENTATION_NORTH;
59 } else if (input.IsDown(Input::PAD_RIGHT)) {
60 nextDirection = Entity::ORIENTATION_EAST;
61 } else if (input.IsDown(Input::PAD_DOWN)) {
62 nextDirection = Entity::ORIENTATION_SOUTH;
63 } else if (input.IsDown(Input::PAD_LEFT)) {
64 nextDirection = Entity::ORIENTATION_WEST;
70 void MapState::UpdateWorld(float deltaT) {
71 if (controlled && controlled->TileLock(map->Tileset()->Width(), map->Tileset()->Height())) {
72 // TODO: call step hooks here
73 // TODO: break/merge time deltas into distinct pixel movements for better step accuracy
74 if (nextDirection >= 0) {
75 controlled->SetOrientation(Entity::Orientation(nextDirection));
76 const Tile &tile(map->TileAt(controlled->Position()));
78 switch (controlled->GetOrientation()) {
79 case Entity::ORIENTATION_NORTH:
80 blocked = tile.BlocksNorth();
82 case Entity::ORIENTATION_EAST:
83 blocked = tile.BlocksEast();
85 case Entity::ORIENTATION_SOUTH:
86 blocked = tile.BlocksSouth();
88 case Entity::ORIENTATION_WEST:
89 blocked = tile.BlocksWest();
93 controlled->SetSpeed(walkingSpeed);
95 controlled->SetSpeed(0.0f);
98 controlled->SetSpeed(0.0f);
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();