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 OnResize(screen->w, screen->h);
47 tileAnimation = GraphicsTimers().StartInterval(512);
51 void MapState::OnExitState(SDL_Surface *screen) {
55 void MapState::OnResumeState(SDL_Surface *screen) {
56 OnResize(screen->w, screen->h);
59 void MapState::OnPauseState(SDL_Surface *screen) {
63 void MapState::OnResize(int width, int height) {
64 camera.Resize(width, height);
68 void MapState::SetWalkingSpeed(int s) {
69 walkingSpeed = math::Fixed<8>(1, s);
74 void MapState::HandleEvents(const Input &input) {
75 if (input.JustPressed(Input::ACTION_X)) {
76 Ctrl().PushState(new PartyMenu(game));
80 if (!controlled) return;
82 if (input.IsDown(Input::PAD_UP)) {
83 nextDirection = Entity::ORIENTATION_NORTH;
84 } else if (input.IsDown(Input::PAD_RIGHT)) {
85 nextDirection = Entity::ORIENTATION_EAST;
86 } else if (input.IsDown(Input::PAD_DOWN)) {
87 nextDirection = Entity::ORIENTATION_SOUTH;
88 } else if (input.IsDown(Input::PAD_LEFT)) {
89 nextDirection = Entity::ORIENTATION_WEST;
94 pushing = input.IsDown(Input::ACTION_A);
96 if (input.JustPressed(Input::DEBUG_1)) {
101 void MapState::UpdateWorld(Uint32 deltaT) {
102 if (controlled && controlled->TileLock(map->Tileset()->Size())) {
105 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
106 (*i)->Update(deltaT);
110 void MapState::OnTileLock() {
111 // moveTimer is running when the pc is walking against a wall
112 if (moveTimer.Running() && !moveTimer.JustHit()) return;
114 Vector<int> nowLock(ToInt(controlled->Position()));
116 if (nowLock != lastLock) {
117 event = OnGridLock();
120 } else if (moveTimer.JustHit()) {
121 event = OnGridLock();
129 const Tile *tile(map->TileAt(nowLock));
131 if (nextDirection >= 0) {
133 bool blocked(CheckBlocking());
135 controlled->SetDirection(Entity::Orientation(nextDirection));
138 controlled->SetSpeed(walkingSpeed);
141 pushed->SetDirection(Entity::Orientation(nextDirection));
142 pushed->SetSpeed(walkingSpeed);
143 controlled->SetPushing();
145 controlled->SetHandsFree();
148 controlled->SetSpeed(0);
149 StopFollowers(*controlled);
150 if (!moveTimer.Running()) {
151 int tileSize((controlled->GetDirection() % 2) ? map->Tileset()->Width() : map->Tileset()->Height());
152 Fixed<8> walkingInterval(tileSize);
153 walkingInterval /= walkingSpeed;
154 moveTimer = PhysicsTimers().StartInterval(walkingInterval.Int());
158 if (!controlled->AnimationRunning()) {
159 controlled->StartAnimation(*this);
163 controlled->SetSpeed(0);
164 StopFollowers(*controlled);
165 controlled->StopAnimation();
173 if (controlled->GetDirection() == Entity::ORIENTATION_SOUTH
174 && tile && tile->IsLadder()) {
175 controlled->SetOrientation(Entity::ORIENTATION_NORTH);
181 bool MapState::CheckBlocking() {
186 const Tile *tile(map->TileAt(ToInt(controlled->Position())));
187 Vector<int> direction;
188 switch (nextDirection) {
189 case Entity::ORIENTATION_NORTH:
190 if (tile && tile->BlocksNorth()) {
193 direction = Vector<int>(0, -map->Tileset()->Height());
196 case Entity::ORIENTATION_EAST:
197 if (tile && tile->BlocksEast()) {
200 direction = Vector<int>(map->Tileset()->Width(), 0);
203 case Entity::ORIENTATION_SOUTH:
204 if (tile && tile->BlocksSouth()) {
207 direction = Vector<int>(0, map->Tileset()->Height());
210 case Entity::ORIENTATION_WEST:
211 if (tile && tile->BlocksWest()) {
214 direction = Vector<int>(-map->Tileset()->Width(), 0);
220 Vector<int> nextTilePosition(direction + ToInt(controlled->Position()));
221 Vector<int> nextTileCoords(map->TileCoordinates(nextTilePosition));
222 for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
223 const Entity &e(**i);
224 if (map->TileCoordinates(ToInt(e.Position())) != nextTileCoords) continue;
225 if (!e.Blocking()) continue;
226 if (!pushing || !e.Pushable()) return true;
227 if (CheckBlocking(nextTilePosition, Entity::Orientation(nextDirection))) return true;
233 bool MapState::CheckBlocking(const Vector<int> &position, Entity::Orientation direction) const {
234 const Tile *tile(map->TileAt(position));
235 Vector<int> directionVector;
237 case Entity::ORIENTATION_NORTH:
238 if (tile && tile->BlocksNorth()) {
241 directionVector = Vector<int>(0, -map->Tileset()->Height());
244 case Entity::ORIENTATION_EAST:
245 if (tile && tile->BlocksEast()) {
248 directionVector = Vector<int>(map->Tileset()->Width(), 0);
251 case Entity::ORIENTATION_SOUTH:
252 if (tile && tile->BlocksSouth()) {
255 directionVector = Vector<int>(0, map->Tileset()->Height());
258 case Entity::ORIENTATION_WEST:
259 if (tile && tile->BlocksWest()) {
262 directionVector = Vector<int>(-map->Tileset()->Width(), 0);
268 Vector<int> nextTileCoords(map->TileCoordinates(directionVector + position));
269 for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
270 const Entity &e(**i);
271 if (map->TileCoordinates(ToInt(e.Position())) == nextTileCoords && e.Blocking()) {
278 bool MapState::OnGridLock() {
284 return CheckMonster() || CheckLockTrigger();
288 void MapState::LockEntities() {
289 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
290 if (*i == controlled) {
294 (*i)->Position().Lock(map->Tileset()->Size());
298 bool MapState::CheckMonster() {
299 Vector<int> coords(map->TileCoordinates(ToInt(controlled->Position())));
300 Vector<int> neighbor[4];
301 neighbor[0] = Vector<int>(coords.X(), coords.Y() - 1); // N
302 neighbor[1] = Vector<int>(coords.X() + 1, coords.Y()); // E
303 neighbor[2] = Vector<int>(coords.X(), coords.Y() + 1); // S
304 neighbor[3] = Vector<int>(coords.X() - 1, coords.Y()); // W
306 for (int i(0); i < 4; ++i) {
307 for (std::vector<Entity *>::iterator e(entities.begin()), end(entities.end()); e != end; ++e) {
308 if ((*e)->Hostile() && map->TileCoordinates(ToInt((*e)->Position())) == neighbor[i]) {
309 // TODO: move entity erase to happen after the transition or battle
310 LoadBattle(*controlled, **e);
319 void MapState::LoadBattle(Entity &hero, Entity &monster) {
320 SDL_Surface *bg = map->BattleBackgroundAt(ToInt(monster.Position()));
321 BattleState *battleState(new BattleState(game, bg, monster.PartyLayout()));
322 for (int i(0); i < 4; ++i) {
323 if (game->state->party[i]) {
324 battleState->AddHero(*game->state->party[i]);
327 if (game->state->capsule) {
328 battleState->SetCapsule(&game->state->GetCapsule());
330 for (battle::Monster **m(monster.MonstersBegin()); m != monster.MonstersEnd(); ++m) {
331 battleState->AddMonster(**m);
334 // TODO: pass turn advantage to battle, see #26
335 Entity::Orientation faceDirection;
336 if (monster.Position().Y() < hero.Position().Y()) {
337 faceDirection = Entity::ORIENTATION_NORTH;
338 } else if (monster.Position().X() > hero.Position().X()) {
339 faceDirection = Entity::ORIENTATION_EAST;
340 } else if (monster.Position().Y() > hero.Position().Y()) {
341 faceDirection = Entity::ORIENTATION_SOUTH;
343 faceDirection = Entity::ORIENTATION_WEST;
345 if (hero.GetOrientation() == monster.GetOrientation()
346 && hero.GetOrientation() == faceDirection) {
348 } else if (hero.GetOrientation() == monster.GetOrientation()
349 && hero.GetOrientation() == ((faceDirection + 2) % 4)) {
351 } else if (((monster.GetOrientation() == faceDirection && (hero.GetOrientation() % 2) != (faceDirection % 2))
352 || (hero.GetOrientation() == faceDirection && (monster.GetOrientation() % 2) != (faceDirection % 2)))
354 // 50% advantage chance hero
355 } else if ((monster.GetOrientation() == (faceDirection + 2) % 4)
356 && ((hero.GetOrientation() % 2) != (faceDirection % 2))
358 // 50% advantage chance monster
361 // TODO: other transition
362 ColorFade *fadeIn(new ColorFade(this, 0, 500, true));
363 fadeIn->SetLeadInTime(500);
364 ColorFade *fadeOut(new ColorFade(this, 0, 500));
365 fadeOut->SetLeadOutTime(500);
367 Ctrl().PushState(fadeIn);
368 Ctrl().PushState(battleState);
369 Ctrl().PushState(fadeOut);
372 bool MapState::CheckLockTrigger() {
373 Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
374 if (!trigger || trigger->GetType() != Trigger::TYPE_CONTACT) return false;
375 RunTrigger(*trigger);
379 void MapState::OnMove(bool realMove) {
380 if (CheckMoveTrigger()) {
383 // TODO: evaluate monster movements
385 UpdateFollower(*controlled);
387 StopFollowers(*controlled);
391 bool MapState::CheckMoveTrigger() {
392 Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
393 if (!trigger || int(trigger->GetType()) != nextDirection) return false;
394 RunTrigger(*trigger);
398 void MapState::RunTrigger(Trigger &trigger) {
399 if (!trigger.HasScript()) return;
400 runner.Run(*this, trigger.GetScript());
403 void MapState::UpdateFollower(Entity &e) {
404 if (!e.Follower()) return;
406 Entity &f(*e.Follower());
409 Vector<int> coords(map->TileCoordinates(ToInt(e.Position())));
410 Vector<int> fCoords(map->TileCoordinates(ToInt(f.Position())));
411 Vector<int> direction(coords - fCoords);
413 if (direction.Y() < 0) {
414 f.SetDirection(Entity::ORIENTATION_NORTH);
415 f.SetSpeed(walkingSpeed);
416 f.StartAnimation(*this);
417 } else if (direction.X() > 0) {
418 f.SetDirection(Entity::ORIENTATION_EAST);
419 f.SetSpeed(walkingSpeed);
420 f.StartAnimation(*this);
421 } else if (direction.Y() > 0) {
422 f.SetDirection(Entity::ORIENTATION_SOUTH);
423 f.SetSpeed(walkingSpeed);
424 f.StartAnimation(*this);
425 } else if (direction.X() < 0) {
426 f.SetDirection(Entity::ORIENTATION_WEST);
427 f.SetSpeed(walkingSpeed);
428 f.StartAnimation(*this);
435 void MapState::StopFollowers(Entity &e) {
436 for (Entity *f(e.Follower()); f; f = f->Follower()) {
443 void MapState::Transition(Map *newMap, const Vector<int> &coordinates) {
445 Vector<int> position(coordinates * map->Tileset()->Size());
446 for (Entity *e(controlled); e; e = e->Follower()) {
447 e->Position() = position;
448 e->SetDirection(controlled->GetDirection());
454 void MapState::UnloadMap() {
458 void MapState::LoadMap(Map *m) {
460 for (Entity *e(m->EntitiesBegin()), *end(m->EntitiesEnd()); e != end; ++e) {
461 entities.push_back(e);
462 e->ResetPosition(map->Tileset()->Size());
464 for (Entity *e(controlled); e; e = e->Follower()) {
465 entities.push_back(e);
470 void MapState::Render(SDL_Surface *screen) {
471 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
473 Vector<int> offset(camera.CalculateOffset());
474 map->Render(screen, offset, tileAnimation.Iteration());
477 map->RenderDebug(screen, offset);
480 std::sort(entities.begin(), entities.end(), ZCompare);
481 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
482 (*i)->Render(screen, offset);
487 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
488 return lhs->Position().Y() < rhs->Position().Y();
492 void MapState::HandleSyscall(common::ScriptRunner &r) {
493 switch (r.IntegerRegister(0)) {
495 Ctrl().PushState(new ColorFade(this, 0, 500, true));
496 Ctrl().PushState(new TransitionState(this, reinterpret_cast<Map *>(r.AddressRegister(0)), r.VectorRegister(0)));
497 ColorFade *fadeOut(new ColorFade(this, 0, 500, false));
498 fadeOut->SetLeadOutTime(500);
499 Ctrl().PushState(fadeOut);