From: Daniel Karbach Date: Fri, 5 Oct 2012 16:52:22 +0000 (+0200) Subject: check for blocking entities when trying to move X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=34e6fb59f355d1a4afc0a9a647aeb098b998dbbd;p=l2e.git check for blocking entities when trying to move --- diff --git a/src/main.cpp b/src/main.cpp index c1b70af..a75a087 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -344,6 +344,7 @@ int main(int argc, char **argv) { mapSelan.SetAnimation(&mapSelanAnimation); mapSelan.Position() = Vector(64, 128); mapSelan.SpriteOffset() = Vector(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(64, 128); mapGuy.SpriteOffset() = Vector(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(64, 128); mapDekar.SpriteOffset() = Vector(0, -32); + mapDekar.SetFlags(Entity::FLAG_NONBLOCKING); mapGuy.AddFollower(&mapDekar); SDL_Surface *mapMonsterImg(IMG_Load("test-data/monster-map.png")); diff --git a/src/map/Entity.cpp b/src/map/Entity.cpp index 666db7a..610e585 100644 --- a/src/map/Entity.cpp +++ b/src/map/Entity.cpp @@ -15,7 +15,8 @@ Entity::Entity() : follower(0) , animation(0) , orientation(ORIENTATION_NORTH) -, speed(0) { +, speed(0) +, flags(0) { runner.SetFrameShift(1); } diff --git a/src/map/Entity.h b/src/map/Entity.h index 12f22c9..92b1b7c 100644 --- a/src/map/Entity.h +++ b/src/map/Entity.h @@ -30,6 +30,9 @@ public: ORIENTATION_SOUTH = 2, ORIENTATION_WEST = 3, }; + enum Flags { + FLAG_NONBLOCKING = 0x01, + }; public: geometry::Vector &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 velocity; Orientation orientation; float speed; + int flags; }; diff --git a/src/map/MapState.cpp b/src/map/MapState.cpp index 26e20da..98e9b28 100644 --- a/src/map/MapState.cpp +++ b/src/map/MapState.cpp @@ -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 nextPosition; + switch (nextDirection) { + case Entity::ORIENTATION_NORTH: + if (tile.BlocksNorth()) { + return true; + } else { + nextPosition = Vector( + controlled->Position().X(), + controlled->Position().Y() - map->Tileset()->Height()); + } + break; + case Entity::ORIENTATION_EAST: + if (tile.BlocksEast()) { + return true; + } else { + nextPosition = Vector( + controlled->Position().X() + map->Tileset()->Width(), + controlled->Position().Y()); + } + break; + case Entity::ORIENTATION_SOUTH: + if (tile.BlocksSouth()) { + return true; + } else { + nextPosition = Vector( + controlled->Position().X(), + controlled->Position().Y() + map->Tileset()->Height()); + } + break; + case Entity::ORIENTATION_WEST: + if (tile.BlocksWest()) { + return true; + } else { + nextPosition = Vector( + controlled->Position().X() - map->Tileset()->Width(), + controlled->Position().Y()); + } + break; + default: + return false; + } + Vector nextTileCoords(map->TileCoordinates(nextPosition)); + for (std::vector::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(controlled->Position()))); diff --git a/src/map/MapState.h b/src/map/MapState.h index f4da5ce..371a881 100644 --- a/src/map/MapState.h +++ b/src/map/MapState.h @@ -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);