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);
47 tileAnimation = GraphicsTimers().StartInterval(512);
51 void MapState::OnExitState(SDL_Surface *screen) {
55 void MapState::OnResumeState(SDL_Surface *screen) {
56 camera.Resize(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::HandleEvents(const Input &input) {
69 if (input.JustPressed(Input::ACTION_X)) {
70 Ctrl().PushState(new PartyMenu(game));
74 if (!controlled) return;
76 if (input.IsDown(Input::PAD_UP)) {
77 nextDirection = Entity::ORIENTATION_NORTH;
78 } else if (input.IsDown(Input::PAD_RIGHT)) {
79 nextDirection = Entity::ORIENTATION_EAST;
80 } else if (input.IsDown(Input::PAD_DOWN)) {
81 nextDirection = Entity::ORIENTATION_SOUTH;
82 } else if (input.IsDown(Input::PAD_LEFT)) {
83 nextDirection = Entity::ORIENTATION_WEST;
88 pushing = input.IsDown(Input::ACTION_A);
90 if (input.JustPressed(Input::DEBUG_1)) {
95 void MapState::UpdateWorld(Uint32 deltaT) {
96 if (controlled && controlled->TileLock(map->Tileset()->Size())) {
99 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
100 (*i)->Update(deltaT);
104 void MapState::OnTileLock() {
105 if (moveTimer.Running() && !moveTimer.JustHit()) return;
107 Vector<int> nowLock(ToInt(controlled->Position()));
109 if (nowLock != lastLock) {
110 event = OnGridLock();
113 } else if (moveTimer.JustHit()) {
114 event = OnGridLock();
122 const Tile *tile(map->TileAt(nowLock));
124 if (nextDirection >= 0) {
126 bool blocked(CheckBlocking());
128 controlled->SetDirection(Entity::Orientation(nextDirection));
131 controlled->SetSpeed(walkingSpeed);
134 pushed->SetDirection(Entity::Orientation(nextDirection));
135 pushed->SetSpeed(walkingSpeed);
136 controlled->SetPushing();
138 controlled->SetHandsFree();
141 controlled->SetSpeed(0);
142 StopFollowers(*controlled);
143 if (!moveTimer.Running()) {
144 int tileSize((controlled->GetDirection() % 2) ? map->Tileset()->Width() : map->Tileset()->Height());
145 Fixed<8> walkingInterval(tileSize);
146 walkingInterval /= walkingSpeed;
147 moveTimer = PhysicsTimers().StartInterval(walkingInterval.Int());
151 if (!controlled->AnimationRunning()) {
152 controlled->StartAnimation(*this);
156 controlled->SetSpeed(0);
157 StopFollowers(*controlled);
158 controlled->StopAnimation();
166 if (controlled->GetDirection() == Entity::ORIENTATION_SOUTH
167 && tile && tile->IsLadder()) {
168 controlled->SetOrientation(Entity::ORIENTATION_NORTH);
174 bool MapState::CheckBlocking() {
179 const Tile *tile(map->TileAt(ToInt(controlled->Position())));
180 Vector<int> direction;
181 switch (nextDirection) {
182 case Entity::ORIENTATION_NORTH:
183 if (tile && tile->BlocksNorth()) {
186 direction = Vector<int>(0, -map->Tileset()->Height());
189 case Entity::ORIENTATION_EAST:
190 if (tile && tile->BlocksEast()) {
193 direction = Vector<int>(map->Tileset()->Width(), 0);
196 case Entity::ORIENTATION_SOUTH:
197 if (tile && tile->BlocksSouth()) {
200 direction = Vector<int>(0, map->Tileset()->Height());
203 case Entity::ORIENTATION_WEST:
204 if (tile && tile->BlocksWest()) {
207 direction = Vector<int>(-map->Tileset()->Width(), 0);
213 Vector<int> nextTilePosition(direction + ToInt(controlled->Position()));
214 Vector<int> nextTileCoords(map->TileCoordinates(nextTilePosition));
215 for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
216 const Entity &e(**i);
217 if (map->TileCoordinates(ToInt(e.Position())) != nextTileCoords) continue;
218 if (!e.Blocking()) continue;
219 if (!pushing || !e.Pushable()) return true;
220 if (CheckBlocking(nextTilePosition, Entity::Orientation(nextDirection))) return true;
226 bool MapState::CheckBlocking(const Vector<int> &position, Entity::Orientation direction) const {
227 const Tile *tile(map->TileAt(position));
228 Vector<int> directionVector;
230 case Entity::ORIENTATION_NORTH:
231 if (tile && tile->BlocksNorth()) {
234 directionVector = Vector<int>(0, -map->Tileset()->Height());
237 case Entity::ORIENTATION_EAST:
238 if (tile && tile->BlocksEast()) {
241 directionVector = Vector<int>(map->Tileset()->Width(), 0);
244 case Entity::ORIENTATION_SOUTH:
245 if (tile && tile->BlocksSouth()) {
248 directionVector = Vector<int>(0, map->Tileset()->Height());
251 case Entity::ORIENTATION_WEST:
252 if (tile && tile->BlocksWest()) {
255 directionVector = Vector<int>(-map->Tileset()->Width(), 0);
261 Vector<int> nextTileCoords(map->TileCoordinates(directionVector + position));
262 for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
263 const Entity &e(**i);
264 if (map->TileCoordinates(ToInt(e.Position())) == nextTileCoords && e.Blocking()) {
271 bool MapState::OnGridLock() {
277 return CheckMonster() || CheckLockTrigger();
281 void MapState::LockEntities() {
282 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
283 if (*i == controlled) {
287 (*i)->Position().Lock(map->Tileset()->Size());
291 bool MapState::CheckMonster() {
292 Vector<int> coords(map->TileCoordinates(ToInt(controlled->Position())));
293 Vector<int> neighbor[4];
294 neighbor[0] = Vector<int>(coords.X(), coords.Y() - 1); // N
295 neighbor[1] = Vector<int>(coords.X() + 1, coords.Y()); // E
296 neighbor[2] = Vector<int>(coords.X(), coords.Y() + 1); // S
297 neighbor[3] = Vector<int>(coords.X() - 1, coords.Y()); // W
299 for (int i(0); i < 4; ++i) {
300 for (std::vector<Entity *>::iterator e(entities.begin()), end(entities.end()); e != end; ++e) {
301 if ((*e)->Hostile() && map->TileCoordinates(ToInt((*e)->Position())) == neighbor[i]) {
302 // TODO: move entity erase to happen after the transition or battle
303 LoadBattle(*controlled, **e);
312 void MapState::LoadBattle(Entity &hero, Entity &monster) {
313 SDL_Surface *bg = map->BattleBackgroundAt(ToInt(monster.Position()));
314 BattleState *battleState(new BattleState(game, bg, monster.PartyLayout()));
315 for (int i(0); i < 4; ++i) {
316 if (game->state->party[i]) {
317 battleState->AddHero(*game->state->party[i]);
320 if (game->state->capsule) {
321 battleState->SetCapsule(&game->state->GetCapsule());
323 for (battle::Monster **m(monster.MonstersBegin()); m != monster.MonstersEnd(); ++m) {
324 battleState->AddMonster(**m);
327 // TODO: pass turn advantage to battle, see #26
328 Entity::Orientation faceDirection;
329 if (monster.Position().Y() < hero.Position().Y()) {
330 faceDirection = Entity::ORIENTATION_NORTH;
331 } else if (monster.Position().X() > hero.Position().X()) {
332 faceDirection = Entity::ORIENTATION_EAST;
333 } else if (monster.Position().Y() > hero.Position().Y()) {
334 faceDirection = Entity::ORIENTATION_SOUTH;
336 faceDirection = Entity::ORIENTATION_WEST;
338 if (hero.GetOrientation() == monster.GetOrientation()
339 && hero.GetOrientation() == faceDirection) {
341 } else if (hero.GetOrientation() == monster.GetOrientation()
342 && hero.GetOrientation() == ((faceDirection + 2) % 4)) {
344 } else if (((monster.GetOrientation() == faceDirection && (hero.GetOrientation() % 2) != (faceDirection % 2))
345 || (hero.GetOrientation() == faceDirection && (monster.GetOrientation() % 2) != (faceDirection % 2)))
347 // 50% advantage chance hero
348 } else if ((monster.GetOrientation() == (faceDirection + 2) % 4)
349 && ((hero.GetOrientation() % 2) != (faceDirection % 2))
351 // 50% advantage chance monster
354 // TODO: other transition
355 ColorFade *fadeIn(new ColorFade(this, 0, 500, true));
356 fadeIn->SetLeadInTime(500);
357 ColorFade *fadeOut(new ColorFade(this, 0, 500));
358 fadeOut->SetLeadOutTime(500);
360 Ctrl().PushState(fadeIn);
361 Ctrl().PushState(battleState);
362 Ctrl().PushState(fadeOut);
365 bool MapState::CheckLockTrigger() {
366 Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
367 if (!trigger || trigger->GetType() != Trigger::TYPE_CONTACT) return false;
368 RunTrigger(*trigger);
372 void MapState::OnMove(bool realMove) {
373 if (CheckMoveTrigger()) {
376 // TODO: evaluate monster movements
378 UpdateFollower(*controlled);
380 StopFollowers(*controlled);
384 bool MapState::CheckMoveTrigger() {
385 Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
386 if (!trigger || int(trigger->GetType()) != nextDirection) return false;
387 RunTrigger(*trigger);
391 void MapState::RunTrigger(Trigger &trigger) {
392 if (!trigger.HasScript()) return;
393 runner.Run(*this, trigger.GetScript());
396 void MapState::UpdateFollower(Entity &e) {
397 if (!e.Follower()) return;
399 Entity &f(*e.Follower());
402 Vector<int> coords(map->TileCoordinates(ToInt(e.Position())));
403 Vector<int> fCoords(map->TileCoordinates(ToInt(f.Position())));
404 Vector<int> direction(coords - fCoords);
406 if (direction.Y() < 0) {
407 f.SetDirection(Entity::ORIENTATION_NORTH);
408 f.SetSpeed(walkingSpeed);
409 f.StartAnimation(*this);
410 } else if (direction.X() > 0) {
411 f.SetDirection(Entity::ORIENTATION_EAST);
412 f.SetSpeed(walkingSpeed);
413 f.StartAnimation(*this);
414 } else if (direction.Y() > 0) {
415 f.SetDirection(Entity::ORIENTATION_SOUTH);
416 f.SetSpeed(walkingSpeed);
417 f.StartAnimation(*this);
418 } else if (direction.X() < 0) {
419 f.SetDirection(Entity::ORIENTATION_WEST);
420 f.SetSpeed(walkingSpeed);
421 f.StartAnimation(*this);
428 void MapState::StopFollowers(Entity &e) {
429 for (Entity *f(e.Follower()); f; f = f->Follower()) {
436 void MapState::Transition(Map *newMap, const Vector<int> &coordinates) {
438 Vector<int> position(coordinates * map->Tileset()->Size());
439 for (Entity *e(controlled); e; e = e->Follower()) {
440 e->Position() = position;
441 e->SetDirection(controlled->GetDirection());
447 void MapState::UnloadMap() {
451 void MapState::LoadMap(Map *m) {
453 for (Entity *e(m->EntitiesBegin()), *end(m->EntitiesEnd()); e != end; ++e) {
454 entities.push_back(e);
455 e->ResetPosition(map->Tileset()->Size());
457 for (Entity *e(controlled); e; e = e->Follower()) {
458 entities.push_back(e);
463 void MapState::Render(SDL_Surface *screen) {
464 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
466 Vector<int> offset(camera.CalculateOffset());
467 map->Render(screen, offset, tileAnimation.Iteration());
470 map->RenderDebug(screen, offset);
473 std::sort(entities.begin(), entities.end(), ZCompare);
474 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
475 (*i)->Render(screen, offset);
480 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
481 return lhs->Position().Y() < rhs->Position().Y();
485 void MapState::HandleSyscall(common::ScriptRunner &r) {
486 switch (r.IntegerRegister(0)) {
488 Ctrl().PushState(new ColorFade(this, 0, 500, true));
489 Ctrl().PushState(new TransitionState(this, reinterpret_cast<Map *>(r.AddressRegister(0)), r.VectorRegister(0)));
490 ColorFade *fadeOut(new ColorFade(this, 0, 500, false));
491 fadeOut->SetLeadOutTime(500);
492 Ctrl().PushState(fadeOut);