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)
34 void MapState::EnterState(Application &ctrl, SDL_Surface *screen) {
35 camera.Resize(screen->w, screen->h);
38 void MapState::ExitState(Application &ctrl, SDL_Surface *screen) {
42 void MapState::ResumeState(Application &ctrl, SDL_Surface *screen) {
43 camera.Resize(screen->w, screen->h);
46 void MapState::PauseState(Application &ctrl, SDL_Surface *screen) {
50 void MapState::Resize(int width, int height) {
51 camera.Resize(width, height);
55 void MapState::HandleEvents(const Input &input) {
56 if (!controlled) return;
58 if (input.IsDown(Input::PAD_UP)) {
59 nextDirection = Entity::ORIENTATION_NORTH;
60 } else if (input.IsDown(Input::PAD_RIGHT)) {
61 nextDirection = Entity::ORIENTATION_EAST;
62 } else if (input.IsDown(Input::PAD_DOWN)) {
63 nextDirection = Entity::ORIENTATION_SOUTH;
64 } else if (input.IsDown(Input::PAD_LEFT)) {
65 nextDirection = Entity::ORIENTATION_WEST;
71 void MapState::UpdateWorld(float deltaT) {
72 if (controlled && controlled->TileLock(map->Tileset()->Width(), map->Tileset()->Height())) {
73 // TODO: call step hooks here
74 // TODO: break/merge time deltas into distinct pixel movements for better step accuracy
75 if (nextDirection >= 0) {
76 controlled->SetOrientation(Entity::Orientation(nextDirection));
77 const Tile &tile(map->TileAt(controlled->Position()));
79 switch (controlled->GetOrientation()) {
80 case Entity::ORIENTATION_NORTH:
81 blocked = tile.BlocksNorth();
83 case Entity::ORIENTATION_EAST:
84 blocked = tile.BlocksEast();
86 case Entity::ORIENTATION_SOUTH:
87 blocked = tile.BlocksSouth();
89 case Entity::ORIENTATION_WEST:
90 blocked = tile.BlocksWest();
94 controlled->SetSpeed(walkingSpeed);
96 controlled->SetSpeed(0.0f);
99 controlled->SetSpeed(0.0f);
102 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
103 (*i)->Update(deltaT);
107 void MapState::Render(SDL_Surface *screen) {
108 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
110 Vector<int> offset(camera.CalculateOffset());
111 map->Render(screen, offset);
113 std::sort(entities.begin(), entities.end(), ZCompare);
114 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
115 (*i)->Render(screen, offset);
120 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
121 return lhs->Position().Y() < rhs->Position().Y();