4 * Created on: Sep 29, 2012
12 #include "TransitionState.h"
14 #include "../app/Application.h"
15 #include "../app/Input.h"
16 #include "../battle/BattleState.h"
17 #include "../common/GameConfig.h"
18 #include "../common/GameState.h"
19 #include "../graphics/ColorFade.h"
23 using app::Application;
25 using battle::BattleState;
26 using common::GameConfig;
27 using geometry::Vector;
28 using graphics::ColorFade;
32 MapState::MapState(GameConfig *g, Map *map)
49 void MapState::EnterState(Application &c, SDL_Surface *screen) {
51 camera.Resize(screen->w, screen->h);
55 void MapState::ExitState(Application &ctrl, SDL_Surface *screen) {
59 void MapState::ResumeState(Application &ctrl, SDL_Surface *screen) {
60 camera.Resize(screen->w, screen->h);
63 void MapState::PauseState(Application &ctrl, SDL_Surface *screen) {
67 void MapState::Resize(int width, int height) {
68 camera.Resize(width, height);
72 void MapState::HandleEvents(const Input &input) {
73 if (!controlled) return;
75 if (input.IsDown(Input::PAD_UP)) {
76 nextDirection = Entity::ORIENTATION_NORTH;
77 } else if (input.IsDown(Input::PAD_RIGHT)) {
78 nextDirection = Entity::ORIENTATION_EAST;
79 } else if (input.IsDown(Input::PAD_DOWN)) {
80 nextDirection = Entity::ORIENTATION_SOUTH;
81 } else if (input.IsDown(Input::PAD_LEFT)) {
82 nextDirection = Entity::ORIENTATION_WEST;
87 pushing = input.IsDown(Input::ACTION_A);
89 if (input.JustPressed(Input::DEBUG_1)) {
94 void MapState::UpdateWorld(float deltaT) {
95 if (controlled && controlled->TileLock(map->Tileset()->Size())) {
98 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
103 void MapState::OnTileLock() {
104 if (moveTimer.Running() && !moveTimer.JustHit()) return;
106 Vector<int> nowLock(controlled->Position());
108 if (nowLock != lastLock) {
109 event = OnGridLock();
112 } else if (moveTimer.JustHit()) {
113 event = OnGridLock();
121 if (nextDirection >= 0) {
123 bool blocked(CheckBlocking());
126 controlled->SetOrientation(Entity::Orientation(nextDirection));
128 controlled->SetSpeed(walkingSpeed);
131 pushed->SetOrientation(Entity::Orientation(nextDirection));
132 pushed->SetSpeed(walkingSpeed);
133 controlled->SetPushing();
135 controlled->SetHandsFree();
138 controlled->SetSpeed(0.0f);
139 StopFollowers(*controlled);
140 if (!moveTimer.Running()) {
141 int tileSize((controlled->GetOrientation() % 2) ? map->Tileset()->Width() : map->Tileset()->Height());
142 moveTimer = PhysicsTimers().StartInterval(tileSize/walkingSpeed);
146 if (!controlled->AnimationRunning()) {
147 controlled->StartAnimation(*this);
151 controlled->SetSpeed(0.0f);
152 StopFollowers(*controlled);
153 controlled->StopAnimation();
156 pushed->SetSpeed(0.0f);
164 bool MapState::CheckBlocking() {
166 pushed->SetSpeed(0.0f);
169 const Tile *tile(map->TileAt(controlled->Position()));
170 Vector<int> direction;
171 switch (nextDirection) {
172 case Entity::ORIENTATION_NORTH:
173 if (tile && tile->BlocksNorth()) {
176 direction = Vector<int>(0, -map->Tileset()->Height());
179 case Entity::ORIENTATION_EAST:
180 if (tile && tile->BlocksEast()) {
183 direction = Vector<int>(map->Tileset()->Width(), 0);
186 case Entity::ORIENTATION_SOUTH:
187 if (tile && tile->BlocksSouth()) {
190 direction = Vector<int>(0, map->Tileset()->Height());
193 case Entity::ORIENTATION_WEST:
194 if (tile && tile->BlocksWest()) {
197 direction = Vector<int>(-map->Tileset()->Width(), 0);
203 Vector<int> nextTilePosition(direction + controlled->Position());
204 Vector<int> nextTileCoords(map->TileCoordinates(nextTilePosition));
205 for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
206 const Entity &e(**i);
207 if (map->TileCoordinates(e.Position()) != nextTileCoords) continue;
208 if (!e.Blocking()) continue;
209 if (!pushing || !e.Pushable()) return true;
210 if (CheckBlocking(nextTilePosition, Entity::Orientation(nextDirection))) return true;
216 bool MapState::CheckBlocking(const Vector<int> &position, Entity::Orientation direction) const {
217 const Tile *tile(map->TileAt(position));
218 Vector<int> directionVector;
220 case Entity::ORIENTATION_NORTH:
221 if (tile && tile->BlocksNorth()) {
224 directionVector = Vector<int>(0, -map->Tileset()->Height());
227 case Entity::ORIENTATION_EAST:
228 if (tile && tile->BlocksEast()) {
231 directionVector = Vector<int>(map->Tileset()->Width(), 0);
234 case Entity::ORIENTATION_SOUTH:
235 if (tile && tile->BlocksSouth()) {
238 directionVector = Vector<int>(0, map->Tileset()->Height());
241 case Entity::ORIENTATION_WEST:
242 if (tile && tile->BlocksWest()) {
245 directionVector = Vector<int>(-map->Tileset()->Width(), 0);
251 Vector<int> nextTileCoords(map->TileCoordinates(directionVector + position));
252 for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
253 const Entity &e(**i);
254 if (map->TileCoordinates(e.Position()) == nextTileCoords && e.Blocking()) {
261 bool MapState::OnGridLock() {
267 return CheckMonster() || CheckTrigger();
271 void MapState::LockEntities() {
272 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
273 if (*i == controlled) {
277 (*i)->Position().Lock(map->Tileset()->Size());
281 bool MapState::CheckMonster() {
282 Vector<int> coords(map->TileCoordinates(controlled->Position()));
283 Vector<int> neighbor[4];
284 neighbor[0] = Vector<int>(coords.X(), coords.Y() - 1); // N
285 neighbor[1] = Vector<int>(coords.X() + 1, coords.Y()); // E
286 neighbor[2] = Vector<int>(coords.X(), coords.Y() + 1); // S
287 neighbor[3] = Vector<int>(coords.X() - 1, coords.Y()); // W
289 for (int i(0); i < 4; ++i) {
290 for (std::vector<Entity *>::iterator e(entities.begin()), end(entities.end()); e != end; ++e) {
291 if ((*e)->Hostile() && map->TileCoordinates((*e)->Position()) == neighbor[i]) {
292 // TODO: check for turn advantage, see #26
293 // TODO: other transition
294 BattleState *battleState(new BattleState(game, map->BattleBackgroundAt((*e)->Position()), (*e)->PartyLayout()));
295 for (int i(0); i < 4; ++i) {
296 if (game->state->party[i]) {
297 battleState->AddHero(*game->state->party[i]);
300 for (battle::Monster *monster((*e)->MonstersBegin()); monster != (*e)->MonstersEnd(); ++monster) {
301 battleState->AddMonster(*monster);
304 ColorFade *fadeIn(new ColorFade(this, 0, 500, true));
305 fadeIn->SetLeadInTime(500);
306 ColorFade *fadeOut(new ColorFade(this, 0, 500));
307 fadeOut->SetLeadOutTime(500);
309 ctrl->PushState(fadeIn);
310 ctrl->PushState(battleState);
311 ctrl->PushState(fadeOut);
312 // TODO: move entity erase to happen after the transition or battle
315 // needed information here:
316 // - battle background (from tile/area/map)
317 // - monsters + layout (from entity)
324 bool MapState::CheckTrigger() {
325 Trigger *trigger(map->TriggerAt(Vector<int>(controlled->Position())));
327 // TODO: run trigger script
329 ctrl->PushState(new ColorFade(this, 0, 500, true));
330 ctrl->PushState(new TransitionState(this, trigger->map, trigger->target));
331 ColorFade *fadeOut(new ColorFade(this, 0, 500, false));
332 fadeOut->SetLeadOutTime(500);
333 ctrl->PushState(fadeOut);
340 void MapState::OnMove(bool realMove) {
341 // TODO: evaluate monster movements
343 UpdateFollower(*controlled);
345 StopFollowers(*controlled);
349 void MapState::UpdateFollower(Entity &e) {
350 if (!e.Follower()) return;
352 Entity &f(*e.Follower());
355 Vector<int> coords(map->TileCoordinates(e.Position()));
356 Vector<int> fCoords(map->TileCoordinates(f.Position()));
357 Vector<int> direction(coords - fCoords);
359 if (direction.Y() < 0) {
360 f.SetOrientation(Entity::ORIENTATION_NORTH);
361 f.SetSpeed(walkingSpeed);
362 f.StartAnimation(*this);
363 } else if (direction.X() > 0) {
364 f.SetOrientation(Entity::ORIENTATION_EAST);
365 f.SetSpeed(walkingSpeed);
366 f.StartAnimation(*this);
367 } else if (direction.Y() > 0) {
368 f.SetOrientation(Entity::ORIENTATION_SOUTH);
369 f.SetSpeed(walkingSpeed);
370 f.StartAnimation(*this);
371 } else if (direction.X() < 0) {
372 f.SetOrientation(Entity::ORIENTATION_WEST);
373 f.SetSpeed(walkingSpeed);
374 f.StartAnimation(*this);
381 void MapState::StopFollowers(Entity &e) {
382 for (Entity *f(e.Follower()); f; f = f->Follower()) {
389 void MapState::Transition(Map *newMap, const Vector<int> &coordinates) {
391 Vector<int> position(coordinates * map->Tileset()->Size());
392 for (Entity *e(controlled); e; e = e->Follower()) {
393 e->Position() = position;
394 e->SetOrientation(controlled->GetOrientation());
400 void MapState::UnloadMap() {
404 void MapState::LoadMap(Map *m) {
406 for (Entity *e(m->EntitiesBegin()), *end(m->EntitiesEnd()); e != end; ++e) {
407 entities.push_back(e);
409 for (Entity *e(controlled); e; e = e->Follower()) {
410 entities.push_back(e);
415 void MapState::Render(SDL_Surface *screen) {
416 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
418 Vector<int> offset(camera.CalculateOffset());
419 map->Render(screen, offset);
422 map->RenderDebug(screen, offset);
425 std::sort(entities.begin(), entities.end(), ZCompare);
426 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
427 (*i)->Render(screen, offset);
432 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
433 return lhs->Position().Y() < rhs->Position().Y();