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()->Size())) {
76 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
81 void MapState::OnTileLock() {
82 if (moveTimer.Running() && !moveTimer.JustHit()) return;
84 Vector<int> nowLock(controlled->Position());
85 if (nowLock != lastLock) {
89 } else if (moveTimer.JustHit()) {
94 // TODO: halt all activity if lock caused a state/map transition
96 if (nextDirection >= 0) {
97 const Tile &tile(map->TileAt(controlled->Position()));
99 switch (nextDirection) {
100 case Entity::ORIENTATION_NORTH:
101 blocked = tile.BlocksNorth();
103 case Entity::ORIENTATION_EAST:
104 blocked = tile.BlocksEast();
106 case Entity::ORIENTATION_SOUTH:
107 blocked = tile.BlocksSouth();
109 case Entity::ORIENTATION_WEST:
110 blocked = tile.BlocksWest();
117 controlled->SetOrientation(Entity::Orientation(nextDirection));
119 controlled->SetSpeed(walkingSpeed);
122 controlled->SetSpeed(0.0f);
123 StopFollowers(*controlled);
124 if (!moveTimer.Running()) {
125 int tileSize((controlled->GetOrientation() % 2) ? map->Tileset()->Width() : map->Tileset()->Height());
126 moveTimer = PhysicsTimers().StartInterval(tileSize/walkingSpeed);
129 if (!controlled->AnimationRunning()) {
130 controlled->StartAnimation(*this);
133 controlled->SetSpeed(0.0f);
134 StopFollowers(*controlled);
135 controlled->StopAnimation();
142 void MapState::OnGridLock() {
144 Trigger *trigger(map->TriggerAt(Vector<int>(controlled->Position())));
148 // TODO: check for adjacent monsters
149 // TODO: force all entities into their grid positions?
152 void MapState::LockEntities() {
153 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
154 (*i)->Position().Lock(map->Tileset()->Size());
158 void MapState::OnMove(bool realMove) {
159 // TODO: evaluate monster movements
161 UpdateFollower(*controlled);
163 StopFollowers(*controlled);
167 void MapState::UpdateFollower(Entity &e) {
168 if (!e.Follower()) return;
170 Entity &f(*e.Follower());
173 Vector<int> coords(map->TileCoordinates(e.Position()));
174 Vector<int> fCoords(map->TileCoordinates(f.Position()));
175 Vector<int> direction(coords - fCoords);
177 if (direction.Y() < 0) {
178 f.SetOrientation(Entity::ORIENTATION_NORTH);
179 f.SetSpeed(walkingSpeed);
180 f.StartAnimation(*this);
181 } else if (direction.X() > 0) {
182 f.SetOrientation(Entity::ORIENTATION_EAST);
183 f.SetSpeed(walkingSpeed);
184 f.StartAnimation(*this);
185 } else if (direction.Y() > 0) {
186 f.SetOrientation(Entity::ORIENTATION_SOUTH);
187 f.SetSpeed(walkingSpeed);
188 f.StartAnimation(*this);
189 } else if (direction.X() < 0) {
190 f.SetOrientation(Entity::ORIENTATION_WEST);
191 f.SetSpeed(walkingSpeed);
192 f.StartAnimation(*this);
199 void MapState::StopFollowers(Entity &e) {
200 for (Entity *f(e.Follower()); f; f = f->Follower()) {
207 void MapState::Render(SDL_Surface *screen) {
208 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
210 Vector<int> offset(camera.CalculateOffset());
211 map->Render(screen, offset);
213 std::sort(entities.begin(), entities.end(), ZCompare);
214 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
215 (*i)->Render(screen, offset);
220 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
221 return lhs->Position().Y() < rhs->Position().Y();