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: check for turn advantage, see #26
302 // TODO: other transition
303 BattleState *battleState(new BattleState(game, map->BattleBackgroundAt(ToInt((*e)->Position())), (*e)->PartyLayout()));
304 for (int i(0); i < 4; ++i) {
305 if (game->state->party[i]) {
306 battleState->AddHero(*game->state->party[i]);
309 if (game->state->capsule) {
310 battleState->SetCapsule(&game->state->GetCapsule());
312 for (battle::Monster **monster((*e)->MonstersBegin()); monster != (*e)->MonstersEnd(); ++monster) {
313 battleState->AddMonster(**monster);
316 ColorFade *fadeIn(new ColorFade(this, 0, 500, true));
317 fadeIn->SetLeadInTime(500);
318 ColorFade *fadeOut(new ColorFade(this, 0, 500));
319 fadeOut->SetLeadOutTime(500);
321 Ctrl().PushState(fadeIn);
322 Ctrl().PushState(battleState);
323 Ctrl().PushState(fadeOut);
324 // TODO: move entity erase to happen after the transition or battle
333 bool MapState::CheckLockTrigger() {
334 Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
335 if (!trigger || trigger->GetType() != Trigger::TYPE_CONTACT) return false;
336 RunTrigger(*trigger);
340 void MapState::OnMove(bool realMove) {
341 if (CheckMoveTrigger()) {
344 // TODO: evaluate monster movements
346 UpdateFollower(*controlled);
348 StopFollowers(*controlled);
352 bool MapState::CheckMoveTrigger() {
353 Trigger *trigger(map->TriggerAt(ToInt(controlled->Position())));
354 if (!trigger || int(trigger->GetType()) != nextDirection) return false;
355 RunTrigger(*trigger);
359 void MapState::RunTrigger(Trigger &trigger) {
360 if (!trigger.HasScript()) return;
361 runner.Run(*this, trigger.GetScript());
364 void MapState::UpdateFollower(Entity &e) {
365 if (!e.Follower()) return;
367 Entity &f(*e.Follower());
370 Vector<int> coords(map->TileCoordinates(ToInt(e.Position())));
371 Vector<int> fCoords(map->TileCoordinates(ToInt(f.Position())));
372 Vector<int> direction(coords - fCoords);
374 if (direction.Y() < 0) {
375 f.SetDirection(Entity::ORIENTATION_NORTH);
376 f.SetSpeed(walkingSpeed);
377 f.StartAnimation(*this);
378 } else if (direction.X() > 0) {
379 f.SetDirection(Entity::ORIENTATION_EAST);
380 f.SetSpeed(walkingSpeed);
381 f.StartAnimation(*this);
382 } else if (direction.Y() > 0) {
383 f.SetDirection(Entity::ORIENTATION_SOUTH);
384 f.SetSpeed(walkingSpeed);
385 f.StartAnimation(*this);
386 } else if (direction.X() < 0) {
387 f.SetDirection(Entity::ORIENTATION_WEST);
388 f.SetSpeed(walkingSpeed);
389 f.StartAnimation(*this);
396 void MapState::StopFollowers(Entity &e) {
397 for (Entity *f(e.Follower()); f; f = f->Follower()) {
404 void MapState::Transition(Map *newMap, const Vector<int> &coordinates) {
406 Vector<int> position(coordinates * map->Tileset()->Size());
407 for (Entity *e(controlled); e; e = e->Follower()) {
408 e->Position() = position;
409 e->SetDirection(controlled->GetDirection());
415 void MapState::UnloadMap() {
419 void MapState::LoadMap(Map *m) {
421 for (Entity *e(m->EntitiesBegin()), *end(m->EntitiesEnd()); e != end; ++e) {
422 entities.push_back(e);
423 e->ResetPosition(map->Tileset()->Size());
425 for (Entity *e(controlled); e; e = e->Follower()) {
426 entities.push_back(e);
431 void MapState::Render(SDL_Surface *screen) {
432 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
434 Vector<int> offset(camera.CalculateOffset());
435 map->Render(screen, offset);
438 map->RenderDebug(screen, offset);
441 std::sort(entities.begin(), entities.end(), ZCompare);
442 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
443 (*i)->Render(screen, offset);
448 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
449 return lhs->Position().Y() < rhs->Position().Y();
453 void MapState::HandleSyscall(common::ScriptRunner &r) {
454 switch (r.IntegerRegister(0)) {
456 Ctrl().PushState(new ColorFade(this, 0, 500, true));
457 Ctrl().PushState(new TransitionState(this, reinterpret_cast<Map *>(r.AddressRegister(0)), r.VectorRegister(0)));
458 ColorFade *fadeOut(new ColorFade(this, 0, 500, false));
459 fadeOut->SetLeadOutTime(500);
460 Ctrl().PushState(fadeOut);