]> git.localhorst.tv Git - l2e.git/blob - src/map/MapState.cpp
added movement timer to track blocked movement
[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(Map *map)
24 : map(map)
25 , controlled(0)
26 , tempTarget(20, 20)
27 , camera(100, 100, &tempTarget)
28 , walkingSpeed(64)
29 , nextDirection(-1)
30 , afterLock(false) {
31
32 }
33
34
35 void MapState::EnterState(Application &ctrl, SDL_Surface *screen) {
36         camera.Resize(screen->w, screen->h);
37 }
38
39 void MapState::ExitState(Application &ctrl, SDL_Surface *screen) {
40
41 }
42
43 void MapState::ResumeState(Application &ctrl, SDL_Surface *screen) {
44         camera.Resize(screen->w, screen->h);
45 }
46
47 void MapState::PauseState(Application &ctrl, SDL_Surface *screen) {
48
49 }
50
51 void MapState::Resize(int width, int height) {
52         camera.Resize(width, height);
53 }
54
55
56 void MapState::HandleEvents(const Input &input) {
57         if (!controlled) return;
58
59         if (input.IsDown(Input::PAD_UP)) {
60                 nextDirection = Entity::ORIENTATION_NORTH;
61         } else if (input.IsDown(Input::PAD_RIGHT)) {
62                 nextDirection = Entity::ORIENTATION_EAST;
63         } else if (input.IsDown(Input::PAD_DOWN)) {
64                 nextDirection = Entity::ORIENTATION_SOUTH;
65         } else if (input.IsDown(Input::PAD_LEFT)) {
66                 nextDirection = Entity::ORIENTATION_WEST;
67         } else {
68                 nextDirection = -1;
69         }
70 }
71
72 void MapState::UpdateWorld(float deltaT) {
73         if (controlled && controlled->TileLock(map->Tileset()->Width(), map->Tileset()->Height())) {
74                 OnTileLock();
75         }
76         for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
77                 (*i)->Update(deltaT);
78         }
79 }
80
81 void MapState::OnTileLock() {
82         if (moveTimer.Running() && !moveTimer.JustHit()) return;
83
84         Vector<int> nowLock(controlled->Position());
85         if (nowLock != lastLock) {
86                 OnGridLock();
87                 afterLock = true;
88                 moveTimer.Clear();
89         } else if (moveTimer.JustHit()) {
90                 OnGridLock();
91                 afterLock = true;
92         }
93
94         if (nextDirection >= 0) {
95                 if (afterLock) {
96                         // FIXME: this check is unreliable, see #21
97                         OnMove();
98                         afterLock = false;
99                 }
100                 controlled->SetOrientation(Entity::Orientation(nextDirection));
101                 const Tile &tile(map->TileAt(controlled->Position()));
102                 bool blocked(false);
103                 switch (controlled->GetOrientation()) {
104                         case Entity::ORIENTATION_NORTH:
105                                 blocked = tile.BlocksNorth();
106                                 break;
107                         case Entity::ORIENTATION_EAST:
108                                 blocked = tile.BlocksEast();
109                                 break;
110                         case Entity::ORIENTATION_SOUTH:
111                                 blocked = tile.BlocksSouth();
112                                 break;
113                         case Entity::ORIENTATION_WEST:
114                                 blocked = tile.BlocksWest();
115                                 break;
116                 }
117                 if (!blocked) {
118                         controlled->SetSpeed(walkingSpeed);
119                         moveTimer.Clear();
120                 } else {
121                         controlled->SetSpeed(0.0f);
122                         if (!moveTimer.Running()) {
123                                 int tileSize((controlled->GetOrientation() % 2) ? map->Tileset()->Width() : map->Tileset()->Height());
124                                 moveTimer = PhysicsTimers().StartInterval(tileSize/walkingSpeed);
125                         }
126                 }
127                 if (!controlled->AnimationRunning()) {
128                         controlled->StartAnimation(*this);
129                 }
130         } else {
131                 controlled->SetSpeed(0.0f);
132                 controlled->StopAnimation();
133                 moveTimer.Clear();
134         }
135
136         lastLock = nowLock;
137 }
138
139 void MapState::OnGridLock() {
140         Trigger *trigger(map->TriggerAt(Vector<int>(controlled->Position())));
141         if (trigger) {
142                 // TODO: run trigger
143         }
144         // TODO: check for adjacent monsters
145         // TODO: force all entities into their grid positions?
146 }
147
148 void MapState::OnMove() {
149         // TODO: evaluate monster movements
150         UpdateFollower(controlled);
151 }
152
153 void MapState::UpdateFollower(Entity *e) {
154         if (!e->Follower()) return;
155         UpdateFollower(e->Follower());
156
157         e->Follower()->SetOrientation(e->GetOrientation());
158         // TODO: set follower speed
159 }
160
161
162 void MapState::Render(SDL_Surface *screen) {
163         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
164
165         Vector<int> offset(camera.CalculateOffset());
166         map->Render(screen, offset);
167
168         std::sort(entities.begin(), entities.end(), ZCompare);
169         for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
170                 (*i)->Render(screen, offset);
171         }
172 }
173
174
175 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
176         return lhs->Position().Y() < rhs->Position().Y();
177 }
178
179 }