]> git.localhorst.tv Git - l2e.git/commitdiff
check for blocking entities when trying to move
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 5 Oct 2012 16:52:22 +0000 (18:52 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 5 Oct 2012 16:52:22 +0000 (18:52 +0200)
src/main.cpp
src/map/Entity.cpp
src/map/Entity.h
src/map/MapState.cpp
src/map/MapState.h

index c1b70af05f1dc492d717c17679a11150fe659285..a75a08782190fb29a9d253fe1b686907c21cf408 100644 (file)
@@ -344,6 +344,7 @@ int main(int argc, char **argv) {
                mapSelan.SetAnimation(&mapSelanAnimation);
                mapSelan.Position() = Vector<float>(64, 128);
                mapSelan.SpriteOffset() = Vector<float>(0, -32);
+               mapSelan.SetFlags(Entity::FLAG_NONBLOCKING);
                mapMaxim.AddFollower(&mapSelan);
 
                SDL_Surface *mapGuyImg(IMG_Load("test-data/guy-map.png"));
@@ -353,6 +354,7 @@ int main(int argc, char **argv) {
                mapGuy.SetAnimation(&mapGuyAnimation);
                mapGuy.Position() = Vector<float>(64, 128);
                mapGuy.SpriteOffset() = Vector<float>(0, -32);
+               mapSelan.SetFlags(Entity::FLAG_NONBLOCKING);
                mapSelan.AddFollower(&mapGuy);
 
                SDL_Surface *mapDekarImg(IMG_Load("test-data/dekar-map.png"));
@@ -362,6 +364,7 @@ int main(int argc, char **argv) {
                mapDekar.SetAnimation(&mapDekarAnimation);
                mapDekar.Position() = Vector<float>(64, 128);
                mapDekar.SpriteOffset() = Vector<float>(0, -32);
+               mapDekar.SetFlags(Entity::FLAG_NONBLOCKING);
                mapGuy.AddFollower(&mapDekar);
 
                SDL_Surface *mapMonsterImg(IMG_Load("test-data/monster-map.png"));
index 666db7a2d2f656b0104b57dc4fbb886a6cd815f0..610e58551510a556db8c6045db23ae9de81ddc15 100644 (file)
@@ -15,7 +15,8 @@ Entity::Entity()
 : follower(0)
 , animation(0)
 , orientation(ORIENTATION_NORTH)
-, speed(0) {
+, speed(0)
+, flags(0) {
        runner.SetFrameShift(1);
 }
 
index 12f22c947aae0c903a2468382c0261217ae76456..92b1b7cf1a4ccf6cff5d3b8776ab35cd6594ba84 100644 (file)
@@ -30,6 +30,9 @@ public:
                ORIENTATION_SOUTH = 2,
                ORIENTATION_WEST = 3,
        };
+       enum Flags {
+               FLAG_NONBLOCKING = 0x01,
+       };
 
 public:
        geometry::Vector<float> &Position() { return position; }
@@ -51,6 +54,9 @@ public:
        Orientation GetOrientation() const { return orientation; }
        void SetSpeed(float);
 
+       void SetFlags(int f) { flags = f; }
+       bool Blocking() const { return !(flags & FLAG_NONBLOCKING); }
+
        Entity *Follower() { return follower; }
        const Entity *Follower() const { return follower; }
        void AddFollower(Entity *);
@@ -74,6 +80,7 @@ private:
        geometry::Vector<float> velocity;
        Orientation orientation;
        float speed;
+       int flags;
 
 };
 
index 26e20da4948bec91eb126e18fb3e585ae46e923b..98e9b2848b5a947f2643d1257ade0a17375ee732 100644 (file)
@@ -94,22 +94,7 @@ void MapState::OnTileLock() {
        // TODO: halt all activity if lock caused a state/map transition
 
        if (nextDirection >= 0) {
-               const Tile &tile(map->TileAt(controlled->Position()));
-               bool blocked(false);
-               switch (nextDirection) {
-                       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;
-               }
+               bool blocked(CheckBlocking());
                if (afterLock) {
                        OnMove(!blocked);
                        afterLock = false;
@@ -139,6 +124,59 @@ void MapState::OnTileLock() {
        lastLock = nowLock;
 }
 
+bool MapState::CheckBlocking() const {
+       const Tile &tile(map->TileAt(controlled->Position()));
+       Vector<int> nextPosition;
+       switch (nextDirection) {
+               case Entity::ORIENTATION_NORTH:
+                       if (tile.BlocksNorth()) {
+                               return true;
+                       } else {
+                               nextPosition = Vector<int>(
+                                               controlled->Position().X(),
+                                               controlled->Position().Y() - map->Tileset()->Height());
+                       }
+                       break;
+               case Entity::ORIENTATION_EAST:
+                       if (tile.BlocksEast()) {
+                               return true;
+                       } else {
+                               nextPosition = Vector<int>(
+                                               controlled->Position().X() + map->Tileset()->Width(),
+                                               controlled->Position().Y());
+                       }
+                       break;
+               case Entity::ORIENTATION_SOUTH:
+                       if (tile.BlocksSouth()) {
+                               return true;
+                       } else {
+                               nextPosition = Vector<int>(
+                                               controlled->Position().X(),
+                                               controlled->Position().Y() + map->Tileset()->Height());
+                       }
+                       break;
+               case Entity::ORIENTATION_WEST:
+                       if (tile.BlocksWest()) {
+                               return true;
+                       } else {
+                               nextPosition = Vector<int>(
+                                               controlled->Position().X() - map->Tileset()->Width(),
+                                               controlled->Position().Y());
+                       }
+                       break;
+               default:
+                       return false;
+       }
+       Vector<int> nextTileCoords(map->TileCoordinates(nextPosition));
+       for (std::vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
+               const Entity &e(**i);
+               if (map->TileCoordinates(e.Position()) == nextTileCoords && e.Blocking()) {
+                       return true;
+               }
+       }
+       return false;
+}
+
 void MapState::OnGridLock() {
        LockEntities();
        Trigger *trigger(map->TriggerAt(Vector<int>(controlled->Position())));
index f4da5ce839e0d4c1c98692df38be7f703f65e1b7..371a881e5c54cc1b363ce2269dfa58e71c16b622 100644 (file)
@@ -44,6 +44,9 @@ public:
 
 private:
        static bool ZCompare(const Entity *lhs, const Entity *rhs);
+
+       bool CheckBlocking() const;
+
        void OnTileLock();
        void OnGridLock();
        void OnMove(bool);