From ca4935cb65325edbd45d4a3aacc921ea9ed9483b Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 20 Feb 2021 13:19:15 -0500 Subject: Added enclosure zones A sprite with an enclosure zone will collide with it if it attempts to leave the area defined by the zone. This is used to make sure that wandering sprites don't end up in weird places. --- res/maps/map2.tmx | 4 +++- src/game.cpp | 3 +++ src/map.cpp | 10 ++++++++++ src/map.h | 9 +++++++++ src/sprite.h | 1 + src/transform_system.cpp | 37 +++++++++++++++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 1 deletion(-) diff --git a/res/maps/map2.tmx b/res/maps/map2.tmx index 594fd93..87dbb1e 100644 --- a/res/maps/map2.tmx +++ b/res/maps/map2.tmx @@ -1,5 +1,5 @@ - + @@ -695,6 +695,7 @@ + @@ -703,6 +704,7 @@ + diff --git a/src/game.cpp b/src/game.cpp index 729e665..5c0017d 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -66,6 +66,9 @@ void Game::loadMap(std::string filename) { getSystem().initSprite(spriteId); getSprite(spriteId).wander = true; } + if (!p.enclosureZone.empty()) { + getSprite(spriteId).enclosureZone = p.enclosureZone; + } } for (const Trigger& t : map_->getTriggers()) { diff --git a/src/map.cpp b/src/map.cpp index 7bff071..5845009 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -78,6 +78,8 @@ Map::Map(std::string_view name) : name_(name) { p.shadow = property.getBoolValue(); } else if (property.getName() == "wander") { p.wander = property.getBoolValue(); + } else if (property.getName() == "enclosureZone") { + p.enclosureZone = property.getStringValue(); } } @@ -118,6 +120,14 @@ Map::Map(std::string_view name) : name_(name) { } prototypes_.push_back(std::move(p)); + } else if (object.getType() == "zone") { + Zone z; + z.ul.x() = object.getPosition().x; + z.ul.y() = object.getPosition().y; + z.dr.x() = z.ul.x() + object.getAABB().width; + z.dr.y() = z.ul.y() + object.getAABB().height; + + zones_[object.getName()] = std::move(z); } } } diff --git a/src/map.h b/src/map.h index c5ecc64..e4096f4 100644 --- a/src/map.h +++ b/src/map.h @@ -25,6 +25,7 @@ struct Prototype { std::string interactionScript; bool shadow = false; bool wander = false; + std::string enclosureZone; }; struct Trigger { @@ -34,6 +35,11 @@ struct Trigger { std::string script; }; +struct Zone { + vec2i ul; + vec2i dr; +}; + class Map { public: @@ -61,6 +67,8 @@ public: const std::vector& getTriggers() const { return triggers_; } + const Zone& getZone(const std::string& name) const { return zones_.at(name); } + private: std::string name_; @@ -72,6 +80,7 @@ private: std::vector prototypes_; std::map warpPoints_; std::vector triggers_; + std::map zones_; }; #endif /* end of include guard: MAP_H_D95D6D47 */ diff --git a/src/sprite.h b/src/sprite.h index 82849fa..e0cff0c 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -53,6 +53,7 @@ public: vec2i collisionSize; std::string interactionScript; std::string walkthroughScript; + std::string enclosureZone; // Animation bool isAnimated = false; diff --git a/src/transform_system.cpp b/src/transform_system.cpp index 1d4a9f3..b5e9b7c 100644 --- a/src/transform_system.cpp +++ b/src/transform_system.cpp @@ -67,6 +67,11 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Dire vec2i oldTileDR = oldColDR / map.getTileSize(); vec2i newTileDR = newColDR / map.getTileSize(); + const Zone* enclosureZone = nullptr; + if (!sprite.enclosureZone.empty()) { + enclosureZone = &map.getZone(sprite.enclosureZone); + } + if (dirHasDir(sprite.dir, Direction::right)) { if (newTileDR.x() > oldTileDR.x() && newColDR.x() < mapBounds.w()) { @@ -80,6 +85,14 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Dire } } + if (!result.horiz.blocked && enclosureZone) { + if (oldColDR.x() <= enclosureZone->dr.x() && + newColDR.x() > enclosureZone->dr.x()) { + result.horiz.blocked = true; + result.horiz.dir = Direction::right; + } + } + if (!result.horiz.blocked) { auto it = rightCollidables_.lower_bound({oldColDR.x(), INT_MAX}); for (; @@ -113,6 +126,14 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Dire } } + if (!result.horiz.blocked && enclosureZone) { + if (oldColUL.x() >= enclosureZone->ul.x() && + newColUL.x() < enclosureZone->ul.x()) { + result.horiz.blocked = true; + result.horiz.dir = Direction::left; + } + } + if (!result.horiz.blocked) { auto it = leftCollidables_.lower_bound({oldColUL.x(), 0}); for (; @@ -146,6 +167,14 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Dire } } + if (!result.vert.blocked && enclosureZone) { + if (oldColDR.y() <= enclosureZone->dr.y() && + newColDR.y() > enclosureZone->dr.y()) { + result.vert.blocked = true; + result.vert.dir = Direction::down; + } + } + if (!result.vert.blocked) { auto it = downCollidables_.lower_bound({oldColDR.y(), INT_MAX}); for (; @@ -179,6 +208,14 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Dire } } + if (!result.vert.blocked && enclosureZone) { + if (oldColUL.y() >= enclosureZone->ul.y() && + newColUL.y() < enclosureZone->ul.y()) { + result.vert.blocked = true; + result.vert.dir = Direction::up; + } + } + if (!result.vert.blocked) { auto it = upCollidables_.lower_bound({oldColUL.y(), 0}); for (; -- cgit 1.4.1