]> git.localhorst.tv Git - l2e.git/blob - src/map/MapState.cpp
check blocking flags of tiles in map state
[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
30 }
31
32
33 void MapState::EnterState(Application &ctrl, SDL_Surface *screen) {
34         camera.Resize(screen->w, screen->h);
35 }
36
37 void MapState::ExitState(Application &ctrl, SDL_Surface *screen) {
38
39 }
40
41 void MapState::ResumeState(Application &ctrl, SDL_Surface *screen) {
42         camera.Resize(screen->w, screen->h);
43 }
44
45 void MapState::PauseState(Application &ctrl, SDL_Surface *screen) {
46
47 }
48
49 void MapState::Resize(int width, int height) {
50         camera.Resize(width, height);
51 }
52
53
54 void MapState::HandleEvents(const Input &input) {
55         if (!controlled) return;
56         if (!controlled->TileLock(map->Tileset()->Width(), map->Tileset()->Height())) return;
57
58         bool down(false);
59         if (input.IsDown(Input::PAD_UP)) {
60                 controlled->SetOrientation(Entity::ORIENTATION_NORTH);
61                 down = true;
62         } else if (input.IsDown(Input::PAD_RIGHT)) {
63                 controlled->SetOrientation(Entity::ORIENTATION_EAST);
64                 down = true;
65         } else if (input.IsDown(Input::PAD_DOWN)) {
66                 controlled->SetOrientation(Entity::ORIENTATION_SOUTH);
67                 down = true;
68         } else if (input.IsDown(Input::PAD_LEFT)) {
69                 controlled->SetOrientation(Entity::ORIENTATION_WEST);
70                 down = true;
71         }
72
73         if (down) {
74                 const Tile &tile(map->TileAt(controlled->Position()));
75                 bool blocked(false);
76                 switch (controlled->GetOrientation()) {
77                         case Entity::ORIENTATION_NORTH:
78                                 blocked = tile.BlocksNorth();
79                                 break;
80                         case Entity::ORIENTATION_EAST:
81                                 blocked = tile.BlocksEast();
82                                 break;
83                         case Entity::ORIENTATION_SOUTH:
84                                 blocked = tile.BlocksSouth();
85                                 break;
86                         case Entity::ORIENTATION_WEST:
87                                 blocked = tile.BlocksWest();
88                                 break;
89                 }
90                 if (!blocked) {
91                         controlled->SetSpeed(walkingSpeed);
92                 } else {
93                         controlled->SetSpeed(0.0f);
94                 }
95         } else {
96                 controlled->SetSpeed(0.0f);
97         }
98 }
99
100 void MapState::UpdateWorld(float deltaT) {
101         for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
102                 (*i)->Update(deltaT);
103         }
104 }
105
106 void MapState::Render(SDL_Surface *screen) {
107         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
108
109         Vector<int> offset(camera.CalculateOffset());
110         map->Render(screen, offset);
111
112         std::sort(entities.begin(), entities.end(), ZCompare);
113         for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
114                 (*i)->Render(screen, offset);
115         }
116 }
117
118
119 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
120         return lhs->Position().Y() < rhs->Position().Y();
121 }
122
123 }