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
310 void MapState::LoadBattle(Entity &hero, Entity &monster) {
311 SDL_Surface *bg = map->BattleBackgroundAt(ToInt(monster.Position()));
312 BattleState *battleState(new BattleState(game, bg, monster.PartyLayout()));
313 for (int i(0); i < 4; ++i) {
314 if (game->state->party[i]) {
315 battleState->AddHero(*game->state->party[i]);
318 if (game->state->capsule) {
319 battleState->SetCapsule(&game->state->GetCapsule());
321 for (battle::Monster **m(monster.MonstersBegin()); m != monster.MonstersEnd(); ++m) {
322 battleState->AddMonster(**m);
325 // TODO: pass turn advantage to battle, see #26
326 Entity::Orientation faceDirection;
327 if (monster.Position().Y() < hero.Position().Y()) {
328 faceDirection = Entity::ORIENTATION_NORTH;
329 } else if (monster.Position().X() > hero.Position().X()) {
330 faceDirection = Entity::ORIENTATION_EAST;
331 } else if (monster.Position().Y() > hero.Position().Y()) {
332 faceDirection = Entity::ORIENTATION_SOUTH;
334 faceDirection = Entity::ORIENTATION_WEST;
336 if (hero.GetOrientation() == monster.GetOrientation()
337 && hero.GetOrientation() == faceDirection) {
339 } else if (hero.GetOrientation() == monster.GetOrientation()
340 && hero.GetOrientation() == ((faceDirection + 2) % 4)) {
342 } else if (((monster.GetOrientation() == faceDirection && (hero.GetOrientation() % 2) != (faceDirection % 2))
343 || (hero.GetOrientation() == faceDirection && (monster.GetOrientation() % 2) != (faceDirection % 2)))
345 // 50% advantage chance hero
346 } else if ((monster.GetOrientation() == (faceDirection + 2) % 4)
347 && ((hero.GetOrientation() % 2) != (faceDirection % 2))
349 // 50% advantage chance monster
352 // TODO: other transition
353 ColorFade *fadeIn(new ColorFade(this, 0, 500, true));
354 fadeIn->SetLeadInTime(500);
355 ColorFade *fadeOut(new ColorFade(this, 0, 500));
356 fadeOut->SetLeadOutTime(500);
358 Ctrl().PushState(fadeIn);
359 Ctrl().PushState(battleState);
360 Ctrl().PushState(fadeOut);
363 bool MapState::CheckLockTrigger() {
364 Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
365 if (!trigger || trigger->GetType() != Trigger::TYPE_CONTACT) return false;
366 RunTrigger(*trigger);
370 void MapState::OnMove(bool realMove) {
371 if (CheckMoveTrigger()) {
374 // TODO: evaluate monster movements
376 UpdateFollower(*controlled);
378 StopFollowers(*controlled);
382 bool MapState::CheckMoveTrigger() {
383 Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
384 if (!trigger || int(trigger->GetType()) != nextDirection) return false;
385 RunTrigger(*trigger);
389 void MapState::RunTrigger(Trigger &trigger) {
390 if (!trigger.HasScript()) return;
391 runner.Run(*this, trigger.GetScript());
394 void MapState::UpdateFollower(Entity &e) {
395 if (!e.Follower()) return;
397 Entity &f(*e.Follower());
400 Vector<int> coords(map->TileCoordinates(ToInt(e.Position())));
401 Vector<int> fCoords(map->TileCoordinates(ToInt(f.Position())));
402 Vector<int> direction(coords - fCoords);
404 if (direction.Y() < 0) {
405 f.SetDirection(Entity::ORIENTATION_NORTH);
406 f.SetSpeed(walkingSpeed);
407 f.StartAnimation(*this);
408 } else if (direction.X() > 0) {
409 f.SetDirection(Entity::ORIENTATION_EAST);
410 f.SetSpeed(walkingSpeed);
411 f.StartAnimation(*this);
412 } else if (direction.Y() > 0) {
413 f.SetDirection(Entity::ORIENTATION_SOUTH);
414 f.SetSpeed(walkingSpeed);
415 f.StartAnimation(*this);
416 } else if (direction.X() < 0) {
417 f.SetDirection(Entity::ORIENTATION_WEST);
418 f.SetSpeed(walkingSpeed);
419 f.StartAnimation(*this);
426 void MapState::StopFollowers(Entity &e) {
427 for (Entity *f(e.Follower()); f; f = f->Follower()) {
434 void MapState::Transition(Map *newMap, const Vector<int> &coordinates) {
436 Vector<int> position(coordinates * map->Tileset()->Size());
437 for (Entity *e(controlled); e; e = e->Follower()) {
438 e->Position() = position;
439 e->SetDirection(controlled->GetDirection());
445 void MapState::UnloadMap() {
449 void MapState::LoadMap(Map *m) {
451 for (Entity *e(m->EntitiesBegin()), *end(m->EntitiesEnd()); e != end; ++e) {
452 entities.push_back(e);
453 e->ResetPosition(map->Tileset()->Size());
455 for (Entity *e(controlled); e; e = e->Follower()) {
456 entities.push_back(e);
461 void MapState::Render(SDL_Surface *screen) {
462 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
464 Vector<int> offset(camera.CalculateOffset());
465 map->Render(screen, offset);
468 map->RenderDebug(screen, offset);
471 std::sort(entities.begin(), entities.end(), ZCompare);
472 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
473 (*i)->Render(screen, offset);
478 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
479 return lhs->Position().Y() < rhs->Position().Y();
483 void MapState::HandleSyscall(common::ScriptRunner &r) {
484 switch (r.IntegerRegister(0)) {
486 Ctrl().PushState(new ColorFade(this, 0, 500, true));
487 Ctrl().PushState(new TransitionState(this, reinterpret_cast<Map *>(r.AddressRegister(0)), r.VectorRegister(0)));
488 ColorFade *fadeOut(new ColorFade(this, 0, 500, false));
489 fadeOut->SetLeadOutTime(500);
490 Ctrl().PushState(fadeOut);