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(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) {
80 // FIXME: this check is unreliable, see #21
84 controlled->SetOrientation(Entity::Orientation(nextDirection));
85 const Tile &tile(map->TileAt(controlled->Position()));
87 switch (controlled->GetOrientation()) {
88 case Entity::ORIENTATION_NORTH:
89 blocked = tile.BlocksNorth();
91 case Entity::ORIENTATION_EAST:
92 blocked = tile.BlocksEast();
94 case Entity::ORIENTATION_SOUTH:
95 blocked = tile.BlocksSouth();
97 case Entity::ORIENTATION_WEST:
98 blocked = tile.BlocksWest();
102 controlled->SetSpeed(walkingSpeed);
104 controlled->SetSpeed(0.0f);
106 if (!controlled->AnimationRunning()) {
107 controlled->StartAnimation(*this);
110 controlled->SetSpeed(0.0f);
111 controlled->StopAnimation();
113 if (nowLock != lastLock) {
118 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
119 (*i)->Update(deltaT);
123 void MapState::OnGridLock() {
124 Trigger *trigger(map->TriggerAt(Vector<int>(controlled->Position())));
128 // TODO: check for adjacent monsters
129 // TODO: force all entities into their grid positions?
132 void MapState::OnMove() {
133 // TODO: evaluate monster movements
134 UpdateFollower(controlled);
137 void MapState::UpdateFollower(Entity *e) {
138 if (!e->Follower()) return;
139 UpdateFollower(e->Follower());
141 e->Follower()->SetOrientation(e->GetOrientation());
142 // TODO: set follower speed
146 void MapState::Render(SDL_Surface *screen) {
147 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
149 Vector<int> offset(camera.CalculateOffset());
150 map->Render(screen, offset);
152 std::sort(entities.begin(), entities.end(), ZCompare);
153 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
154 (*i)->Render(screen, offset);
159 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
160 return lhs->Position().Y() < rhs->Position().Y();