]> git.localhorst.tv Git - l2e.git/commitdiff
check blocking flags of tiles in map state
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 30 Sep 2012 15:49:57 +0000 (17:49 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 30 Sep 2012 15:49:57 +0000 (17:49 +0200)
src/main.cpp
src/map/Area.cpp
src/map/Area.h
src/map/Entity.cpp
src/map/Entity.h
src/map/Map.cpp
src/map/Map.h
src/map/MapState.cpp

index 8fa1318f6e6d55dfd7554e066c893365da9430a3..fe975deb7dac4706f8310fed4b44d8841291316d 100644 (file)
@@ -326,7 +326,7 @@ int main(int argc, char **argv) {
                Sprite mapMaximSprite(mapMaximImg, 32, 64);
                Entity mapMaxim;
                mapMaxim.SetSprite(&mapMaximSprite);
-               mapMaxim.Position() = Vector<float>(80, 160);
+               mapMaxim.Position() = Vector<float>(80, 128);
 
                InitScreen screen(width, height);
 
index a1ae6804c774b9627fd7b98aeae9b6963fd3b717..292b8952f21ecca579aa4af9a18112e968e5810a 100644 (file)
@@ -10,6 +10,8 @@
 #include "Tile.h"
 #include "../graphics/Sprite.h"
 
+#include <stdexcept>
+
 using geometry::Vector;
 
 namespace map {
@@ -21,6 +23,17 @@ Area::Area()
 
 }
 
+
+const Tile &Area::TileAt(const geometry::Vector<int> &offset) const {
+       int tileIndex(offset.Y() * width + offset.X());
+       if (tileIndex < numTiles) {
+               return tiles[tileIndex];
+       } else {
+               throw std::out_of_range("tile index out of range");
+       }
+}
+
+
 void Area::Render(SDL_Surface *dest, const graphics::Sprite *tileset, const Vector<int> &inOffset) const {
        for (int i(0); i < numTiles; ++i) {
                Vector<int> offset(
index 9834f5c270bc916dfbe5da947301afc7cda7bce0..9832809e93349519461e13c037fa0924e741fd22 100644 (file)
@@ -10,7 +10,7 @@
 
 #include "fwd.h"
 #include "../geometry/Vector.h"
-#include "../graphics/fwd.h"
+#include "../graphics/Sprite.h"
 
 #include <SDL.h>
 
@@ -25,6 +25,7 @@ public:
 public:
        int Width() const { return width; }
        int Height() const { return numTiles / width + (numTiles % width ? 1 : 0); }
+       const Tile &TileAt(const geometry::Vector<int> &) const;
 
        void Render(SDL_Surface *dest, const graphics::Sprite *tileset, const geometry::Vector<int> &offset) const;
 
index 0e1b822ba32256e920e4767051a5e8f68fbecaec..391c711665dc63e7495dedcc6f13cc6470ee2396 100644 (file)
@@ -55,7 +55,7 @@ void Entity::UpdateVelocity() {
 bool Entity::TileLock(int width, int height) const {
        Vector<int> tilePosition(
                        position.X() - (width / 2),
-                       position.Y() - height);
+                       position.Y());
        return (tilePosition.X() % width == 0) && (tilePosition.Y() % height == 0);
 }
 
@@ -66,10 +66,11 @@ void Entity::Update(float deltaT) {
 
 
 void Entity::Render(SDL_Surface *dest, const Vector<int> &offset) const {
+       // TODO: configurable sprite offsets
        if (animation.Running()) {
-               animation.DrawCenterBottom(dest, offset + position);
+               animation.DrawCenter(dest, offset + position);
        } else {
-               sprite->DrawCenterBottom(dest, offset + position, orientation);
+               sprite->DrawCenter(dest, offset + position, orientation);
        }
 }
 
index 2bc6aac6a863913eaece225598bf8ed85e5cd969..e1ede977e65888eea33360f20a37b3f91aa63a93 100644 (file)
@@ -43,6 +43,7 @@ public:
        const graphics::AnimationRunner &Animation() const { return animation; }
 
        void SetOrientation(Orientation);
+       Orientation GetOrientation() const { return orientation; }
        void SetSpeed(float);
 
        bool TileLock(int width, int height) const;
index 450c00463ab7d3aa28ae90fcafd067f3620d833f..2e05e8bf9025832bd810e20d8a0d1861574e8e95 100644 (file)
@@ -10,6 +10,8 @@
 #include "Area.h"
 #include "../graphics/Sprite.h"
 
+#include <stdexcept>
+
 using geometry::Vector;
 
 namespace map {
@@ -23,6 +25,25 @@ Map::Map()
 }
 
 
+const Area &Map::AreaAt(const Vector<int> &offset) const {
+       if (numAreas > 0) {
+               Vector<int> tileOffset(offset.X() / tileset->Width(), offset.Y() / tileset->Height());
+               Vector<int> areaOffset(tileOffset.X() / areas[0].Width(), tileOffset.Y() / areas[0].Height());
+               int areaIndex(areaOffset.Y() * width + areaOffset.X());
+               if (areaIndex < numAreas) {
+                       return areas[areaIndex];
+               }
+       }
+       throw std::out_of_range("area offset out of bounds");
+}
+
+const Tile &Map::TileAt(const Vector<int> &offset) const {
+       const Area &area(AreaAt(offset));
+       Vector<int> tileOffset((offset.X() / tileset->Width()) % area.Width(), (offset.Y() / tileset->Height()) % area.Height());
+       return area.TileAt(tileOffset);
+}
+
+
 void Map::Render(SDL_Surface *dest, const Vector<int> &inOffset) const {
        // TODO: skip invisible areas
        for (int i(0); i < numAreas; ++i) {
index 92c866b5ea6c66683d16bff72fc7710b8f959db6..340c76c707d691951a68afbc8de283fd36aecac4 100644 (file)
@@ -24,6 +24,8 @@ public:
 
 public:
        const graphics::Sprite *Tileset() const { return tileset; }
+       const Area &AreaAt(const geometry::Vector<int> &) const;
+       const Tile &TileAt(const geometry::Vector<int> &) const;
 
        void Render(SDL_Surface *dest, const geometry::Vector<int> &offset) const;
 
index 6ef2be748deb8e1dc874e3588f0b4326f561aa8a..9a94731cd201c41eeaecd41731032b3245051e6b 100644 (file)
@@ -8,6 +8,7 @@
 #include "MapState.h"
 
 #include "Map.h"
+#include "Tile.h"
 #include "../app/Application.h"
 #include "../app/Input.h"
 
@@ -54,18 +55,43 @@ void MapState::HandleEvents(const Input &input) {
        if (!controlled) return;
        if (!controlled->TileLock(map->Tileset()->Width(), map->Tileset()->Height())) return;
 
+       bool down(false);
        if (input.IsDown(Input::PAD_UP)) {
                controlled->SetOrientation(Entity::ORIENTATION_NORTH);
-               controlled->SetSpeed(walkingSpeed);
+               down = true;
        } else if (input.IsDown(Input::PAD_RIGHT)) {
                controlled->SetOrientation(Entity::ORIENTATION_EAST);
-               controlled->SetSpeed(walkingSpeed);
+               down = true;
        } else if (input.IsDown(Input::PAD_DOWN)) {
                controlled->SetOrientation(Entity::ORIENTATION_SOUTH);
-               controlled->SetSpeed(walkingSpeed);
+               down = true;
        } else if (input.IsDown(Input::PAD_LEFT)) {
                controlled->SetOrientation(Entity::ORIENTATION_WEST);
-               controlled->SetSpeed(walkingSpeed);
+               down = true;
+       }
+
+       if (down) {
+               const Tile &tile(map->TileAt(controlled->Position()));
+               bool blocked(false);
+               switch (controlled->GetOrientation()) {
+                       case Entity::ORIENTATION_NORTH:
+                               blocked = tile.BlocksNorth();
+                               break;
+                       case Entity::ORIENTATION_EAST:
+                               blocked = tile.BlocksEast();
+                               break;
+                       case Entity::ORIENTATION_SOUTH:
+                               blocked = tile.BlocksSouth();
+                               break;
+                       case Entity::ORIENTATION_WEST:
+                               blocked = tile.BlocksWest();
+                               break;
+               }
+               if (!blocked) {
+                       controlled->SetSpeed(walkingSpeed);
+               } else {
+                       controlled->SetSpeed(0.0f);
+               }
        } else {
                controlled->SetSpeed(0.0f);
        }