]> git.localhorst.tv Git - l2e.git/blob - src/map/MapState.cpp
added entity orientation awareness
[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 "../app/Application.h"
12 #include "../app/Input.h"
13
14 #include <algorithm>
15
16 using app::Application;
17 using app::Input;
18 using geometry::Vector;
19
20 namespace map {
21
22 MapState::MapState(const Map *map)
23 : map(map)
24 , controlled(0)
25 , tempTarget(20, 20)
26 , camera(100, 100, &tempTarget)
27 , walkingSpeed(64) {
28
29 }
30
31
32 void MapState::EnterState(Application &ctrl, SDL_Surface *screen) {
33         camera.Resize(screen->w, screen->h);
34 }
35
36 void MapState::ExitState(Application &ctrl, SDL_Surface *screen) {
37
38 }
39
40 void MapState::ResumeState(Application &ctrl, SDL_Surface *screen) {
41         camera.Resize(screen->w, screen->h);
42 }
43
44 void MapState::PauseState(Application &ctrl, SDL_Surface *screen) {
45
46 }
47
48 void MapState::Resize(int width, int height) {
49         camera.Resize(width, height);
50 }
51
52
53 void MapState::HandleEvents(const Input &input) {
54         if (!controlled) return;
55         if (!controlled->TileLock(map->Tileset()->Width(), map->Tileset()->Height())) return;
56
57         if (input.IsDown(Input::PAD_UP)) {
58                 controlled->SetOrientation(Entity::ORIENTATION_NORTH);
59                 controlled->SetSpeed(walkingSpeed);
60         } else if (input.IsDown(Input::PAD_RIGHT)) {
61                 controlled->SetOrientation(Entity::ORIENTATION_EAST);
62                 controlled->SetSpeed(walkingSpeed);
63         } else if (input.IsDown(Input::PAD_DOWN)) {
64                 controlled->SetOrientation(Entity::ORIENTATION_SOUTH);
65                 controlled->SetSpeed(walkingSpeed);
66         } else if (input.IsDown(Input::PAD_LEFT)) {
67                 controlled->SetOrientation(Entity::ORIENTATION_WEST);
68                 controlled->SetSpeed(walkingSpeed);
69         } else {
70                 controlled->SetSpeed(0.0f);
71         }
72 }
73
74 void MapState::UpdateWorld(float deltaT) {
75         for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
76                 (*i)->Update(deltaT);
77         }
78 }
79
80 void MapState::Render(SDL_Surface *screen) {
81         SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
82
83         Vector<int> offset(camera.CalculateOffset());
84         map->Render(screen, offset);
85
86         std::sort(entities.begin(), entities.end(), ZCompare);
87         for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
88                 (*i)->Render(screen, offset);
89         }
90 }
91
92
93 bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
94         return lhs->Position().Y() < rhs->Position().Y();
95 }
96
97 }