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)
35 void MapState::EnterState(Application &ctrl, SDL_Surface *screen) {
36 camera.Resize(screen->w, screen->h);
39 void MapState::ExitState(Application &ctrl, SDL_Surface *screen) {
43 void MapState::ResumeState(Application &ctrl, SDL_Surface *screen) {
44 camera.Resize(screen->w, screen->h);
47 void MapState::PauseState(Application &ctrl, SDL_Surface *screen) {
51 void MapState::Resize(int width, int height) {
52 camera.Resize(width, height);
56 void MapState::HandleEvents(const Input &input) {
57 if (!controlled) return;
59 if (input.IsDown(Input::PAD_UP)) {
60 nextDirection = Entity::ORIENTATION_NORTH;
61 } else if (input.IsDown(Input::PAD_RIGHT)) {
62 nextDirection = Entity::ORIENTATION_EAST;
63 } else if (input.IsDown(Input::PAD_DOWN)) {
64 nextDirection = Entity::ORIENTATION_SOUTH;
65 } else if (input.IsDown(Input::PAD_LEFT)) {
66 nextDirection = Entity::ORIENTATION_WEST;
72 void MapState::UpdateWorld(float deltaT) {
73 if (controlled && controlled->TileLock(map->Tileset()->Width(), map->Tileset()->Height())) {
74 Vector<int> nowLock(controlled->Position());
75 if (nowLock != lastLock) {
78 if (nextDirection >= 0) {
83 controlled->SetOrientation(Entity::Orientation(nextDirection));
84 const Tile &tile(map->TileAt(controlled->Position()));
86 switch (controlled->GetOrientation()) {
87 case Entity::ORIENTATION_NORTH:
88 blocked = tile.BlocksNorth();
90 case Entity::ORIENTATION_EAST:
91 blocked = tile.BlocksEast();
93 case Entity::ORIENTATION_SOUTH:
94 blocked = tile.BlocksSouth();
96 case Entity::ORIENTATION_WEST:
97 blocked = tile.BlocksWest();
101 controlled->SetSpeed(walkingSpeed);
103 controlled->SetSpeed(0.0f);
105 if (!controlled->AnimationRunning()) {
106 controlled->StartAnimation(*this);
109 controlled->SetSpeed(0.0f);
110 controlled->StopAnimation();
112 if (nowLock != lastLock) {
117 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
118 (*i)->Update(deltaT);
122 void MapState::OnGridLock() {
123 // TODO: check for adjacent monsters and triggers on the current tile
124 // TODO: force all entities into their grid positions?
127 void MapState::OnMove() {
128 // TODO: evaluate monster movements
132 void MapState::Render(SDL_Surface *screen) {
133 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
135 Vector<int> offset(camera.CalculateOffset());
136 map->Render(screen, offset);
138 std::sort(entities.begin(), entities.end(), ZCompare);
139 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
140 (*i)->Render(screen, offset);
145 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
146 return lhs->Position().Y() < rhs->Position().Y();