4 * Created on: Sep 29, 2012
13 #include "../app/Application.h"
14 #include "../app/Input.h"
18 using app::Application;
20 using geometry::Vector;
24 MapState::MapState(Map *map)
28 , camera(100, 100, &tempTarget)
38 void MapState::EnterState(Application &ctrl, SDL_Surface *screen) {
39 camera.Resize(screen->w, screen->h);
43 void MapState::ExitState(Application &ctrl, SDL_Surface *screen) {
47 void MapState::ResumeState(Application &ctrl, SDL_Surface *screen) {
48 camera.Resize(screen->w, screen->h);
51 void MapState::PauseState(Application &ctrl, SDL_Surface *screen) {
55 void MapState::Resize(int width, int height) {
56 camera.Resize(width, height);
60 void MapState::HandleEvents(const Input &input) {
61 if (!controlled) return;
63 if (input.IsDown(Input::PAD_UP)) {
64 nextDirection = Entity::ORIENTATION_NORTH;
65 } else if (input.IsDown(Input::PAD_RIGHT)) {
66 nextDirection = Entity::ORIENTATION_EAST;
67 } else if (input.IsDown(Input::PAD_DOWN)) {
68 nextDirection = Entity::ORIENTATION_SOUTH;
69 } else if (input.IsDown(Input::PAD_LEFT)) {
70 nextDirection = Entity::ORIENTATION_WEST;
75 if (input.JustPressed(Input::DEBUG_1)) {
80 void MapState::UpdateWorld(float deltaT) {
81 if (controlled && controlled->TileLock(map->Tileset()->Size())) {
84 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
89 void MapState::OnTileLock() {
90 if (moveTimer.Running() && !moveTimer.JustHit()) return;
92 Vector<int> nowLock(controlled->Position());
93 if (nowLock != lastLock) {
97 } else if (moveTimer.JustHit()) {
102 // TODO: halt all activity if lock caused a state/map transition
104 if (nextDirection >= 0) {
105 bool blocked(CheckBlocking());
110 controlled->SetOrientation(Entity::Orientation(nextDirection));
112 controlled->SetSpeed(walkingSpeed);
115 controlled->SetSpeed(0.0f);
116 StopFollowers(*controlled);
117 if (!moveTimer.Running()) {
118 int tileSize((controlled->GetOrientation() % 2) ? map->Tileset()->Width() : map->Tileset()->Height());
119 moveTimer = PhysicsTimers().StartInterval(tileSize/walkingSpeed);
122 if (!controlled->AnimationRunning()) {
123 controlled->StartAnimation(*this);
126 controlled->SetSpeed(0.0f);
127 StopFollowers(*controlled);
128 controlled->StopAnimation();
135 bool MapState::CheckBlocking() const {
136 const Tile *tile(map->TileAt(controlled->Position()));
140 Vector<int> nextPosition;
141 switch (nextDirection) {
142 case Entity::ORIENTATION_NORTH:
143 if (tile->BlocksNorth()) {
146 nextPosition = Vector<int>(
147 controlled->Position().X(),
148 controlled->Position().Y() - map->Tileset()->Height());
151 case Entity::ORIENTATION_EAST:
152 if (tile->BlocksEast()) {
155 nextPosition = Vector<int>(
156 controlled->Position().X() + map->Tileset()->Width(),
157 controlled->Position().Y());
160 case Entity::ORIENTATION_SOUTH:
161 if (tile->BlocksSouth()) {
164 nextPosition = Vector<int>(
165 controlled->Position().X(),
166 controlled->Position().Y() + map->Tileset()->Height());
169 case Entity::ORIENTATION_WEST:
170 if (tile->BlocksWest()) {
173 nextPosition = Vector<int>(
174 controlled->Position().X() - map->Tileset()->Width(),
175 controlled->Position().Y());
181 Vector<int> nextTileCoords(map->TileCoordinates(nextPosition));
182 for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
183 const Entity &e(**i);
184 if (map->TileCoordinates(e.Position()) == nextTileCoords && e.Blocking()) {
191 void MapState::OnGridLock() {
201 void MapState::LockEntities() {
202 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
203 if (*i == controlled) {
207 (*i)->Position().Lock(map->Tileset()->Size());
211 void MapState::CheckMonster() {
212 Vector<int> coords(map->TileCoordinates(controlled->Position()));
213 Vector<int> neighbor[4];
214 neighbor[0] = Vector<int>(coords.X(), coords.Y() - 1); // N
215 neighbor[1] = Vector<int>(coords.X() + 1, coords.Y()); // E
216 neighbor[2] = Vector<int>(coords.X(), coords.Y() + 1); // S
217 neighbor[3] = Vector<int>(coords.X() - 1, coords.Y()); // W
219 for (int i(0); i < 4; ++i) {
220 for (std::vector<Entity *>::iterator e(entities.begin()), end(entities.end()); e != end; ++e) {
221 if ((*e)->Hostile() && map->TileCoordinates((*e)->Position()) == neighbor[i]) {
222 // TODO: check for turn advantage, see #26
223 // TODO: remove entity, push battle state and transition and halt all other activity
224 // needed information here:
225 // - battle background (from tile?)
226 // - monsters + layout (from entity)
227 // - battle resources (from global resources)
233 void MapState::CheckTrigger() {
234 Trigger *trigger(map->TriggerAt(Vector<int>(controlled->Position())));
236 // TODO: run trigger script
238 Transition(trigger->map, trigger->target);
244 void MapState::OnMove(bool realMove) {
245 // TODO: evaluate monster movements
247 UpdateFollower(*controlled);
249 StopFollowers(*controlled);
253 void MapState::UpdateFollower(Entity &e) {
254 if (!e.Follower()) return;
256 Entity &f(*e.Follower());
259 Vector<int> coords(map->TileCoordinates(e.Position()));
260 Vector<int> fCoords(map->TileCoordinates(f.Position()));
261 Vector<int> direction(coords - fCoords);
263 if (direction.Y() < 0) {
264 f.SetOrientation(Entity::ORIENTATION_NORTH);
265 f.SetSpeed(walkingSpeed);
266 f.StartAnimation(*this);
267 } else if (direction.X() > 0) {
268 f.SetOrientation(Entity::ORIENTATION_EAST);
269 f.SetSpeed(walkingSpeed);
270 f.StartAnimation(*this);
271 } else if (direction.Y() > 0) {
272 f.SetOrientation(Entity::ORIENTATION_SOUTH);
273 f.SetSpeed(walkingSpeed);
274 f.StartAnimation(*this);
275 } else if (direction.X() < 0) {
276 f.SetOrientation(Entity::ORIENTATION_WEST);
277 f.SetSpeed(walkingSpeed);
278 f.StartAnimation(*this);
285 void MapState::StopFollowers(Entity &e) {
286 for (Entity *f(e.Follower()); f; f = f->Follower()) {
293 void MapState::Transition(Map *newMap, const Vector<int> &coordinates) {
295 Vector<int> position(coordinates * map->Tileset()->Size());
296 for (Entity *e(controlled); e; e = e->Follower()) {
297 e->Position() = position;
298 e->SetOrientation(controlled->GetOrientation());
304 void MapState::UnloadMap() {
308 void MapState::LoadMap(Map *m) {
310 entities.insert(entities.end(), m->EntitiesBegin(), m->EntitiesEnd());
311 for (Entity *e(controlled); e; e = e->Follower()) {
312 entities.push_back(e);
317 void MapState::Render(SDL_Surface *screen) {
318 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
320 Vector<int> offset(camera.CalculateOffset());
321 map->Render(screen, offset);
324 map->RenderDebug(screen, offset);
327 std::sort(entities.begin(), entities.end(), ZCompare);
328 for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
329 (*i)->Render(screen, offset);
334 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
335 return lhs->Position().Y() < rhs->Position().Y();