]> git.localhorst.tv Git - l2e.git/blobdiff - src/map/MapState.cpp
added entity orientation awareness
[l2e.git] / src / map / MapState.cpp
index ed776482e3763084308af422cfe31766787cca1b..6ef2be748deb8e1dc874e3588f0b4326f561aa8a 100644 (file)
@@ -11,6 +11,8 @@
 #include "../app/Application.h"
 #include "../app/Input.h"
 
+#include <algorithm>
+
 using app::Application;
 using app::Input;
 using geometry::Vector;
@@ -19,8 +21,10 @@ namespace map {
 
 MapState::MapState(const Map *map)
 : map(map)
+, controlled(0)
 , tempTarget(20, 20)
-, camera(100, 100, &tempTarget) {
+, camera(100, 100, &tempTarget)
+, walkingSpeed(64) {
 
 }
 
@@ -47,16 +51,47 @@ void MapState::Resize(int width, int height) {
 
 
 void MapState::HandleEvents(const Input &input) {
-
+       if (!controlled) return;
+       if (!controlled->TileLock(map->Tileset()->Width(), map->Tileset()->Height())) return;
+
+       if (input.IsDown(Input::PAD_UP)) {
+               controlled->SetOrientation(Entity::ORIENTATION_NORTH);
+               controlled->SetSpeed(walkingSpeed);
+       } else if (input.IsDown(Input::PAD_RIGHT)) {
+               controlled->SetOrientation(Entity::ORIENTATION_EAST);
+               controlled->SetSpeed(walkingSpeed);
+       } else if (input.IsDown(Input::PAD_DOWN)) {
+               controlled->SetOrientation(Entity::ORIENTATION_SOUTH);
+               controlled->SetSpeed(walkingSpeed);
+       } else if (input.IsDown(Input::PAD_LEFT)) {
+               controlled->SetOrientation(Entity::ORIENTATION_WEST);
+               controlled->SetSpeed(walkingSpeed);
+       } else {
+               controlled->SetSpeed(0.0f);
+       }
 }
 
 void MapState::UpdateWorld(float deltaT) {
-
+       for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
+               (*i)->Update(deltaT);
+       }
 }
 
 void MapState::Render(SDL_Surface *screen) {
+       SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
+
        Vector<int> offset(camera.CalculateOffset());
        map->Render(screen, offset);
+
+       std::sort(entities.begin(), entities.end(), ZCompare);
+       for (std::vector<Entity *>::iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
+               (*i)->Render(screen, offset);
+       }
+}
+
+
+bool MapState::ZCompare(const Entity *lhs, const Entity *rhs) {
+       return lhs->Position().Y() < rhs->Position().Y();
 }
 
 }