5 #include "TransitionState.h"
7 #include "../app/Application.h"
8 #include "../app/Input.h"
9 #include "../battle/BattleState.h"
10 #include "../common/GameConfig.h"
11 #include "../common/GameState.h"
12 #include "../graphics/ColorFade.h"
13 #include "../menu/PartyMenu.h"
17 using app::Application;
19 using battle::BattleState;
20 using common::GameConfig;
23 using graphics::ColorFade;
24 using menu::PartyMenu;
28 MapState::MapState(GameConfig *g, Map *map)
45 void MapState::OnEnterState(SDL_Surface *screen) {
46 camera.Resize(screen->w, screen->h);
50 void MapState::OnExitState(SDL_Surface *screen) {
54 void MapState::OnResumeState(SDL_Surface *screen) {
55 camera.Resize(screen->w, screen->h);
58 void MapState::OnPauseState(SDL_Surface *screen) {
62 void MapState::OnResize(int width, int height) {
63 camera.Resize(width, height);
67 void MapState::HandleEvents(const Input &input) {
68 if (input.JustPressed(Input::ACTION_X)) {
69 Ctrl().PushState(new PartyMenu(game));
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(Uint32 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(ToInt(controlled->Position()));
108 if (nowLock != lastLock) {
109 event = OnGridLock();
112 } else if (moveTimer.JustHit()) {
113 event = OnGridLock();
121 const Tile *tile(map->TileAt(nowLock));
123 if (nextDirection >= 0) {
125 bool blocked(CheckBlocking());
127 controlled->SetDirection(Entity::Orientation(nextDirection));
130 controlled->SetSpeed(walkingSpeed);
133 pushed->SetDirection(Entity::Orientation(nextDirection));
134 pushed->SetSpeed(walkingSpeed);
135 controlled->SetPushing();
137 controlled->SetHandsFree();
140 controlled->SetSpeed(0);
141 StopFollowers(*controlled);
142 if (!moveTimer.Running()) {
143 int tileSize((controlled->GetDirection() % 2) ? map->Tileset()->Width() : map->Tileset()->Height());
144 Fixed<8> walkingInterval(tileSize);
145 walkingInterval /= walkingSpeed;
146 moveTimer = PhysicsTimers().StartInterval(walkingInterval.Int());
150 if (!controlled->AnimationRunning()) {
151 controlled->StartAnimation(*this);
155 controlled->SetSpeed(0);
156 StopFollowers(*controlled);
157 controlled->StopAnimation();
165 if (controlled->GetDirection() == Entity::ORIENTATION_SOUTH
166 && tile && tile->IsLadder()) {
167 controlled->SetOrientation(Entity::ORIENTATION_NORTH);
173 bool MapState::CheckBlocking() {
178 const Tile *tile(map->TileAt(ToInt(controlled->Position())));
179 Vector<int> direction;
180 switch (nextDirection) {
181 case Entity::ORIENTATION_NORTH:
182 if (tile && tile->BlocksNorth()) {
185 direction = Vector<int>(0, -map->Tileset()->Height());
188 case Entity::ORIENTATION_EAST:
189 if (tile && tile->BlocksEast()) {
192 direction = Vector<int>(map->Tileset()->Width(), 0);
195 case Entity::ORIENTATION_SOUTH:
196 if (tile && tile->BlocksSouth()) {
199 direction = Vector<int>(0, map->Tileset()->Height());
202 case Entity::ORIENTATION_WEST:
203 if (tile && tile->BlocksWest()) {
206 direction = Vector<int>(-map->Tileset()->Width(), 0);
212 Vector<int> nextTilePosition(direction + ToInt(controlled->Position()));
213 Vector<int> nextTileCoords(map->TileCoordinates(nextTilePosition));
214 for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
215 const Entity &e(**i);
216 if (map->TileCoordinates(ToInt(e.Position())) != nextTileCoords) continue;
217 if (!e.Blocking()) continue;
218 if (!pushing || !e.Pushable()) return true;
219 if (CheckBlocking(nextTilePosition, Entity::Orientation(nextDirection))) return true;
225 bool MapState::CheckBlocking(const Vector<int> &position, Entity::Orientation direction) const {
226 const Tile *tile(map->TileAt(position));
227 Vector<int> directionVector;
229 case Entity::ORIENTATION_NORTH:
230 if (tile && tile->BlocksNorth()) {
233 directionVector = Vector<int>(0, -map->Tileset()->Height());
236 case Entity::ORIENTATION_EAST:
237 if (tile && tile->BlocksEast()) {
240 directionVector = Vector<int>(map->Tileset()->Width(), 0);
243 case Entity::ORIENTATION_SOUTH:
244 if (tile && tile->BlocksSouth()) {
247 directionVector = Vector<int>(0, map->Tileset()->Height());
250 case Entity::ORIENTATION_WEST:
251 if (tile && tile->BlocksWest()) {
254 directionVector = Vector<int>(-map->Tileset()->Width(), 0);
260 Vector<int> nextTileCoords(map->TileCoordinates(directionVector + position));
261 for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
262 const Entity &e(**i);
263 if (map->TileCoordinates(ToInt(e.Position())) == nextTileCoords && e.Blocking()) {
270 bool MapState::OnGridLock() {
276 return CheckMonster() || CheckLockTrigger();
280 void MapState::LockEntities() {
281 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
282 if (*i == controlled) {
286 (*i)->Position().Lock(map->Tileset()->Size());
290 bool MapState::CheckMonster() {
291 Vector<int> coords(map->TileCoordinates(ToInt(controlled->Position())));
292 Vector<int> neighbor[4];
293 neighbor[0] = Vector<int>(coords.X(), coords.Y() - 1); // N
294 neighbor[1] = Vector<int>(coords.X() + 1, coords.Y()); // E
295 neighbor[2] = Vector<int>(coords.X(), coords.Y() + 1); // S
296 neighbor[3] = Vector<int>(coords.X() - 1, coords.Y()); // W
298 for (int i(0); i < 4; ++i) {
299 for (std::vector<Entity *>::iterator e(entities.begin()), end(entities.end()); e != end; ++e) {
300 if ((*e)->Hostile() && map->TileCoordinates(ToInt((*e)->Position())) == neighbor[i]) {
301 // TODO: move entity erase to happen after the transition or battle
302 LoadBattle(*controlled, **e);
311 void MapState::LoadBattle(Entity &hero, Entity &monster) {
312 SDL_Surface *bg = map->BattleBackgroundAt(ToInt(monster.Position()));
313 BattleState *battleState(new BattleState(game, bg, monster.PartyLayout()));
314 for (int i(0); i < 4; ++i) {
315 if (game->state->party[i]) {
316 battleState->AddHero(*game->state->party[i]);
319 if (game->state->capsule) {
320 battleState->SetCapsule(&game->state->GetCapsule());
322 for (battle::Monster **m(monster.MonstersBegin()); m != monster.MonstersEnd(); ++m) {
323 battleState->AddMonster(**m);
326 // TODO: pass turn advantage to battle, see #26
327 Entity::Orientation faceDirection;
328 if (monster.Position().Y() < hero.Position().Y()) {
329 faceDirection = Entity::ORIENTATION_NORTH;
330 } else if (monster.Position().X() > hero.Position().X()) {
331 faceDirection = Entity::ORIENTATION_EAST;
332 } else if (monster.Position().Y() > hero.Position().Y()) {
333 faceDirection = Entity::ORIENTATION_SOUTH;
335 faceDirection = Entity::ORIENTATION_WEST;
337 if (hero.GetOrientation() == monster.GetOrientation()
338 && hero.GetOrientation() == faceDirection) {
340 } else if (hero.GetOrientation() == monster.GetOrientation()
341 && hero.GetOrientation() == ((faceDirection + 2) % 4)) {
343 } else if (((monster.GetOrientation() == faceDirection && (hero.GetOrientation() % 2) != (faceDirection % 2))
344 || (hero.GetOrientation() == faceDirection && (monster.GetOrientation() % 2) != (faceDirection % 2)))
346 // 50% advantage chance hero
347 } else if ((monster.GetOrientation() == (faceDirection + 2) % 4)
348 && ((hero.GetOrientation() % 2) != (faceDirection % 2))
350 // 50% advantage chance monster
353 // TODO: other transition
354 ColorFade *fadeIn(new ColorFade(this, 0, 500, true));
355 fadeIn->SetLeadInTime(500);
356 ColorFade *fadeOut(new ColorFade(this, 0, 500));
357 fadeOut->SetLeadOutTime(500);
359 Ctrl().PushState(fadeIn);
360 Ctrl().PushState(battleState);
361 Ctrl().PushState(fadeOut);
364 bool MapState::CheckLockTrigger() {
365 Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
366 if (!trigger || trigger->GetType() != Trigger::TYPE_CONTACT) return false;
367 RunTrigger(*trigger);
371 void MapState::OnMove(bool realMove) {
372 if (CheckMoveTrigger()) {
375 // TODO: evaluate monster movements
377 UpdateFollower(*controlled);
379 StopFollowers(*controlled);
383 bool MapState::CheckMoveTrigger() {
384 Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
385 if (!trigger || int(trigger->GetType()) != nextDirection) return false;
386 RunTrigger(*trigger);
390 void MapState::RunTrigger(Trigger &trigger) {
391 if (!trigger.HasScript()) return;
392 runner.Run(*this, trigger.GetScript());
395 void MapState::UpdateFollower(Entity &e) {
396 if (!e.Follower()) return;
398 Entity &f(*e.Follower());
401 Vector<int> coords(map->TileCoordinates(ToInt(e.Position())));
402 Vector<int> fCoords(map->TileCoordinates(ToInt(f.Position())));
403 Vector<int> direction(coords - fCoords);
405 if (direction.Y() < 0) {
406 f.SetDirection(Entity::ORIENTATION_NORTH);
407 f.SetSpeed(walkingSpeed);
408 f.StartAnimation(*this);
409 } else if (direction.X() > 0) {
410 f.SetDirection(Entity::ORIENTATION_EAST);
411 f.SetSpeed(walkingSpeed);
412 f.StartAnimation(*this);
413 } else if (direction.Y() > 0) {
414 f.SetDirection(Entity::ORIENTATION_SOUTH);
415 f.SetSpeed(walkingSpeed);
416 f.StartAnimation(*this);
417 } else if (direction.X() < 0) {
418 f.SetDirection(Entity::ORIENTATION_WEST);
419 f.SetSpeed(walkingSpeed);
420 f.StartAnimation(*this);
427 void MapState::StopFollowers(Entity &e) {
428 for (Entity *f(e.Follower()); f; f = f->Follower()) {
435 void MapState::Transition(Map *newMap, const Vector<int> &coordinates) {
437 Vector<int> position(coordinates * map->Tileset()->Size());
438 for (Entity *e(controlled); e; e = e->Follower()) {
439 e->Position() = position;
440 e->SetDirection(controlled->GetDirection());
446 void MapState::UnloadMap() {
450 void MapState::LoadMap(Map *m) {
452 for (Entity *e(m->EntitiesBegin()), *end(m->EntitiesEnd()); e != end; ++e) {
453 entities.push_back(e);
454 e->ResetPosition(map->Tileset()->Size());
456 for (Entity *e(controlled); e; e = e->Follower()) {
457 entities.push_back(e);
462 void MapState::Render(SDL_Surface *screen) {
463 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
465 Vector<int> offset(camera.CalculateOffset());
466 map->Render(screen, offset);
469 map->RenderDebug(screen, offset);
472 std::sort(entities.begin(), entities.end(), ZCompare);
473 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
474 (*i)->Render(screen, offset);
479 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
480 return lhs->Position().Y() < rhs->Position().Y();
484 void MapState::HandleSyscall(common::ScriptRunner &r) {
485 switch (r.IntegerRegister(0)) {
487 Ctrl().PushState(new ColorFade(this, 0, 500, true));
488 Ctrl().PushState(new TransitionState(this, reinterpret_cast<Map *>(r.AddressRegister(0)), r.VectorRegister(0)));
489 ColorFade *fadeOut(new ColorFade(this, 0, 500, false));
490 fadeOut->SetLeadOutTime(500);
491 Ctrl().PushState(fadeOut);