]> git.localhorst.tv Git - l2e.git/blob - src/map/MapState.cpp
added comparison operators to vector template
[l2e.git] / src / map / MapState.cpp
1 /*
2  * MapState.cpp
3  *
4  *  Created on: Sep 29, 2012
5  *      Author: holy
6  */
7
8 #include "MapState.h"
9
10 #include "Map.h"
11 #include "Tile.h"
12 #include "../app/Application.h"
13 #include "../app/Input.h"
14
15 #include <algorithm>
16
17 using app::Application;
18 using app::Input;
19 using geometry::Vector;
20
21 namespace map {
22
23 MapState::MapState(const Map *map)
24 : map(map)
25 , controlled(0)
26 , tempTarget(20, 20)
27 , camera(100, 100, &tempTarget)
28 , walkingSpeed(64)
29 , nextDirection(-1) {
30
31 }
32
33
34 void MapState::EnterState(Application &ctrl, SDL_Surface *screen) {
35         camera.Resize(screen->w, screen->h);
36 }
37
38 void MapState::ExitState(Application &ctrl, SDL_Surface *screen) {
39
40 }
41
42 void MapState::ResumeState(Application &ctrl, SDL_Surface *screen) {
43         camera.Resize(screen->w, screen->h);
44 }
45
46 void MapState::PauseState(Application &ctrl, SDL_Surface *screen) {
47
48 }
49
50 void MapState::Resize(int width, int height) {
51         camera.Resize(width, height);
52 }
53
54
55 void MapState::HandleEvents(const Input &input) {
56         if (!controlled) return;
57
58         if (input.IsDown(Input::PAD_UP)) {
59                 nextDirection = Entity::ORIENTATION_NORTH;
60         } else if (input.IsDown(Input::PAD_RIGHT)) {
61                 nextDirection = Entity::ORIENTATION_EAST;
62         } else if (input.IsDown(Input::PAD_DOWN)) {
63                 nextDirection = Entity::ORIENTATION_SOUTH;
64         } else if (input.IsDown(Input::PAD_LEFT)) {
65                 nextDirection = Entity::ORIENTATION_WEST;
66         } else {
67                 nextDirection = -1;
68         }
69 }
70
71 void MapState::UpdateWorld(float deltaT) {
72         if (controlled && controlled->TileLock(map->Tileset()->Width(), map->Tileset()->Height())) {
73                 // TODO: call step hooks here
74                 // TODO: break/merge time deltas into distinct pixel movements for better step accuracy
75                 if (nextDirection >= 0) {
76                         controlled->SetOrientation(Entity::Orientation(nextDirection));
77                         const Tile &tile(map->TileAt(controlled->Position()));
78                         bool blocked(false);
79                         switch (controlled->GetOrientation()) {
80                                 case Entity::ORIENTATION_NORTH:
81                                         blocked = tile.BlocksNorth();
82                                         break;
83                                 case Entity::ORIENTATION_EAST:
84                                         blocked = tile.BlocksEast();
85                                         break;
86                                 case Entity::ORIENTATION_SOUTH:
87                                         blocked = tile.BlocksSouth();
88                                         break;
89                                 case Entity::ORIENTATION_WEST:
90                                         blocked = tile.BlocksWest();
91                                         break;
92                         }
93                         if (!blocked) {
94                                 controlled->SetSpeed(walkingSpeed);
95                         } else {
96                                 controlled->SetSpeed(0.0f);
97                         }
98                 } else {
99                         controlled->SetSpeed(0.0f);
100                 }
101         }
102         for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
103                 (*i)->Update(deltaT);
104         }
105 }
106
107 void MapState::Render(SDL_Surface *screen) {
108         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
109
110         Vector<int> offset(camera.CalculateOffset());
111         map->Render(screen, offset);
112
113         std::sort(entities.begin(), entities.end(), ZCompare);
114         for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
115                 (*i)->Render(screen, offset);
116         }
117 }
118
119
120 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
121         return lhs->Position().Y() < rhs->Position().Y();
122 }
123
124 }