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;
21 using geometry::Vector;
22 using graphics::ColorFade;
23 using menu::PartyMenu;
27 MapState::MapState(GameConfig *g, Map *map)
44 void MapState::OnEnterState(SDL_Surface *screen) {
45 camera.Resize(screen->w, screen->h);
49 void MapState::OnExitState(SDL_Surface *screen) {
53 void MapState::OnResumeState(SDL_Surface *screen) {
54 camera.Resize(screen->w, screen->h);
57 void MapState::OnPauseState(SDL_Surface *screen) {
61 void MapState::OnResize(int width, int height) {
62 camera.Resize(width, height);
66 void MapState::HandleEvents(const Input &input) {
67 if (input.JustPressed(Input::ACTION_X)) {
68 Ctrl().PushState(new PartyMenu(game));
72 if (!controlled) return;
74 if (input.IsDown(Input::PAD_UP)) {
75 nextDirection = Entity::ORIENTATION_NORTH;
76 } else if (input.IsDown(Input::PAD_RIGHT)) {
77 nextDirection = Entity::ORIENTATION_EAST;
78 } else if (input.IsDown(Input::PAD_DOWN)) {
79 nextDirection = Entity::ORIENTATION_SOUTH;
80 } else if (input.IsDown(Input::PAD_LEFT)) {
81 nextDirection = Entity::ORIENTATION_WEST;
86 pushing = input.IsDown(Input::ACTION_A);
88 if (input.JustPressed(Input::DEBUG_1)) {
93 void MapState::UpdateWorld(float deltaT) {
94 if (controlled && controlled->TileLock(map->Tileset()->Size())) {
97 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
102 void MapState::OnTileLock() {
103 if (moveTimer.Running() && !moveTimer.JustHit()) return;
105 Vector<int> nowLock(controlled->Position());
107 if (nowLock != lastLock) {
108 event = OnGridLock();
111 } else if (moveTimer.JustHit()) {
112 event = OnGridLock();
120 if (nextDirection >= 0) {
122 bool blocked(CheckBlocking());
124 controlled->SetOrientation(Entity::Orientation(nextDirection));
127 controlled->SetSpeed(walkingSpeed);
130 pushed->SetOrientation(Entity::Orientation(nextDirection));
131 pushed->SetSpeed(walkingSpeed);
132 controlled->SetPushing();
134 controlled->SetHandsFree();
137 controlled->SetSpeed(0.0f);
138 StopFollowers(*controlled);
139 if (!moveTimer.Running()) {
140 int tileSize((controlled->GetOrientation() % 2) ? map->Tileset()->Width() : map->Tileset()->Height());
141 moveTimer = PhysicsTimers().StartInterval(tileSize/walkingSpeed);
145 if (!controlled->AnimationRunning()) {
146 controlled->StartAnimation(*this);
150 controlled->SetSpeed(0.0f);
151 StopFollowers(*controlled);
152 controlled->StopAnimation();
155 pushed->SetSpeed(0.0f);
163 bool MapState::CheckBlocking() {
165 pushed->SetSpeed(0.0f);
168 const Tile *tile(map->TileAt(controlled->Position()));
169 Vector<int> direction;
170 switch (nextDirection) {
171 case Entity::ORIENTATION_NORTH:
172 if (tile && tile->BlocksNorth()) {
175 direction = Vector<int>(0, -map->Tileset()->Height());
178 case Entity::ORIENTATION_EAST:
179 if (tile && tile->BlocksEast()) {
182 direction = Vector<int>(map->Tileset()->Width(), 0);
185 case Entity::ORIENTATION_SOUTH:
186 if (tile && tile->BlocksSouth()) {
189 direction = Vector<int>(0, map->Tileset()->Height());
192 case Entity::ORIENTATION_WEST:
193 if (tile && tile->BlocksWest()) {
196 direction = Vector<int>(-map->Tileset()->Width(), 0);
202 Vector<int> nextTilePosition(direction + controlled->Position());
203 Vector<int> nextTileCoords(map->TileCoordinates(nextTilePosition));
204 for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
205 const Entity &e(**i);
206 if (map->TileCoordinates(e.Position()) != nextTileCoords) continue;
207 if (!e.Blocking()) continue;
208 if (!pushing || !e.Pushable()) return true;
209 if (CheckBlocking(nextTilePosition, Entity::Orientation(nextDirection))) return true;
215 bool MapState::CheckBlocking(const Vector<int> &position, Entity::Orientation direction) const {
216 const Tile *tile(map->TileAt(position));
217 Vector<int> directionVector;
219 case Entity::ORIENTATION_NORTH:
220 if (tile && tile->BlocksNorth()) {
223 directionVector = Vector<int>(0, -map->Tileset()->Height());
226 case Entity::ORIENTATION_EAST:
227 if (tile && tile->BlocksEast()) {
230 directionVector = Vector<int>(map->Tileset()->Width(), 0);
233 case Entity::ORIENTATION_SOUTH:
234 if (tile && tile->BlocksSouth()) {
237 directionVector = Vector<int>(0, map->Tileset()->Height());
240 case Entity::ORIENTATION_WEST:
241 if (tile && tile->BlocksWest()) {
244 directionVector = Vector<int>(-map->Tileset()->Width(), 0);
250 Vector<int> nextTileCoords(map->TileCoordinates(directionVector + position));
251 for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
252 const Entity &e(**i);
253 if (map->TileCoordinates(e.Position()) == nextTileCoords && e.Blocking()) {
260 bool MapState::OnGridLock() {
266 return CheckMonster() || CheckLockTrigger();
270 void MapState::LockEntities() {
271 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
272 if (*i == controlled) {
276 (*i)->Position().Lock(map->Tileset()->Size());
280 bool MapState::CheckMonster() {
281 Vector<int> coords(map->TileCoordinates(controlled->Position()));
282 Vector<int> neighbor[4];
283 neighbor[0] = Vector<int>(coords.X(), coords.Y() - 1); // N
284 neighbor[1] = Vector<int>(coords.X() + 1, coords.Y()); // E
285 neighbor[2] = Vector<int>(coords.X(), coords.Y() + 1); // S
286 neighbor[3] = Vector<int>(coords.X() - 1, coords.Y()); // W
288 for (int i(0); i < 4; ++i) {
289 for (std::vector<Entity *>::iterator e(entities.begin()), end(entities.end()); e != end; ++e) {
290 if ((*e)->Hostile() && map->TileCoordinates((*e)->Position()) == neighbor[i]) {
291 // TODO: check for turn advantage, see #26
292 // TODO: other transition
293 BattleState *battleState(new BattleState(game, map->BattleBackgroundAt((*e)->Position()), (*e)->PartyLayout()));
294 for (int i(0); i < 4; ++i) {
295 if (game->state->party[i]) {
296 battleState->AddHero(*game->state->party[i]);
299 if (game->state->capsule) {
300 battleState->SetCapsule(game->state->capsule);
302 for (battle::Monster **monster((*e)->MonstersBegin()); monster != (*e)->MonstersEnd(); ++monster) {
303 battleState->AddMonster(**monster);
306 ColorFade *fadeIn(new ColorFade(this, 0, 500, true));
307 fadeIn->SetLeadInTime(500);
308 ColorFade *fadeOut(new ColorFade(this, 0, 500));
309 fadeOut->SetLeadOutTime(500);
311 Ctrl().PushState(fadeIn);
312 Ctrl().PushState(battleState);
313 Ctrl().PushState(fadeOut);
314 // TODO: move entity erase to happen after the transition or battle
323 bool MapState::CheckLockTrigger() {
324 Trigger *trigger(map->TriggerAt(Vector<int>(controlled->Position())));
325 if (!trigger || trigger->GetType() != Trigger::TYPE_CONTACT) return false;
326 RunTrigger(*trigger);
330 void MapState::OnMove(bool realMove) {
331 if (CheckMoveTrigger()) {
334 // TODO: evaluate monster movements
336 UpdateFollower(*controlled);
338 StopFollowers(*controlled);
342 bool MapState::CheckMoveTrigger() {
343 Trigger *trigger(map->TriggerAt(Vector<int>(controlled->Position())));
344 if (!trigger || int(trigger->GetType()) != nextDirection) return false;
345 RunTrigger(*trigger);
349 void MapState::RunTrigger(Trigger &trigger) {
350 if (!trigger.HasScript()) return;
351 runner.Run(*this, trigger.GetScript());
354 void MapState::UpdateFollower(Entity &e) {
355 if (!e.Follower()) return;
357 Entity &f(*e.Follower());
360 Vector<int> coords(map->TileCoordinates(e.Position()));
361 Vector<int> fCoords(map->TileCoordinates(f.Position()));
362 Vector<int> direction(coords - fCoords);
364 if (direction.Y() < 0) {
365 f.SetOrientation(Entity::ORIENTATION_NORTH);
366 f.SetSpeed(walkingSpeed);
367 f.StartAnimation(*this);
368 } else if (direction.X() > 0) {
369 f.SetOrientation(Entity::ORIENTATION_EAST);
370 f.SetSpeed(walkingSpeed);
371 f.StartAnimation(*this);
372 } else if (direction.Y() > 0) {
373 f.SetOrientation(Entity::ORIENTATION_SOUTH);
374 f.SetSpeed(walkingSpeed);
375 f.StartAnimation(*this);
376 } else if (direction.X() < 0) {
377 f.SetOrientation(Entity::ORIENTATION_WEST);
378 f.SetSpeed(walkingSpeed);
379 f.StartAnimation(*this);
386 void MapState::StopFollowers(Entity &e) {
387 for (Entity *f(e.Follower()); f; f = f->Follower()) {
394 void MapState::Transition(Map *newMap, const Vector<int> &coordinates) {
396 Vector<int> position(coordinates * map->Tileset()->Size());
397 for (Entity *e(controlled); e; e = e->Follower()) {
398 e->Position() = position;
399 e->SetOrientation(controlled->GetOrientation());
405 void MapState::UnloadMap() {
409 void MapState::LoadMap(Map *m) {
411 for (Entity *e(m->EntitiesBegin()), *end(m->EntitiesEnd()); e != end; ++e) {
412 entities.push_back(e);
413 e->ResetPosition(map->Tileset()->Size());
415 for (Entity *e(controlled); e; e = e->Follower()) {
416 entities.push_back(e);
421 void MapState::Render(SDL_Surface *screen) {
422 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
424 Vector<int> offset(camera.CalculateOffset());
425 map->Render(screen, offset);
428 map->RenderDebug(screen, offset);
431 std::sort(entities.begin(), entities.end(), ZCompare);
432 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
433 (*i)->Render(screen, offset);
438 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
439 return lhs->Position().Y() < rhs->Position().Y();
443 void MapState::HandleSyscall(common::ScriptRunner &r) {
444 switch (r.IntegerRegister(0)) {
446 Ctrl().PushState(new ColorFade(this, 0, 500, true));
447 Ctrl().PushState(new TransitionState(this, reinterpret_cast<Map *>(r.AddressRegister(0)), r.VectorRegister(0)));
448 ColorFade *fadeOut(new ColorFade(this, 0, 500, false));
449 fadeOut->SetLeadOutTime(500);
450 Ctrl().PushState(fadeOut);