diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-20 13:19:15 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-20 13:19:15 -0500 |
commit | ca4935cb65325edbd45d4a3aacc921ea9ed9483b (patch) | |
tree | 28376ffac0f817fc3f1f7290745e91d603fd59e8 /src | |
parent | 996076cf151a27a7a8d278aa4d15b28cfb196c46 (diff) | |
download | tanetane-ca4935cb65325edbd45d4a3aacc921ea9ed9483b.tar.gz tanetane-ca4935cb65325edbd45d4a3aacc921ea9ed9483b.tar.bz2 tanetane-ca4935cb65325edbd45d4a3aacc921ea9ed9483b.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/game.cpp | 3 | ||||
-rw-r--r-- | src/map.cpp | 10 | ||||
-rw-r--r-- | src/map.h | 9 | ||||
-rw-r--r-- | src/sprite.h | 1 | ||||
-rw-r--r-- | src/transform_system.cpp | 37 |
5 files changed, 60 insertions, 0 deletions
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) { | |||
66 | getSystem<CharacterSystem>().initSprite(spriteId); | 66 | getSystem<CharacterSystem>().initSprite(spriteId); |
67 | getSprite(spriteId).wander = true; | 67 | getSprite(spriteId).wander = true; |
68 | } | 68 | } |
69 | if (!p.enclosureZone.empty()) { | ||
70 | getSprite(spriteId).enclosureZone = p.enclosureZone; | ||
71 | } | ||
69 | } | 72 | } |
70 | 73 | ||
71 | for (const Trigger& t : map_->getTriggers()) { | 74 | 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) { | |||
78 | p.shadow = property.getBoolValue(); | 78 | p.shadow = property.getBoolValue(); |
79 | } else if (property.getName() == "wander") { | 79 | } else if (property.getName() == "wander") { |
80 | p.wander = property.getBoolValue(); | 80 | p.wander = property.getBoolValue(); |
81 | } else if (property.getName() == "enclosureZone") { | ||
82 | p.enclosureZone = property.getStringValue(); | ||
81 | } | 83 | } |
82 | } | 84 | } |
83 | 85 | ||
@@ -118,6 +120,14 @@ Map::Map(std::string_view name) : name_(name) { | |||
118 | } | 120 | } |
119 | 121 | ||
120 | prototypes_.push_back(std::move(p)); | 122 | prototypes_.push_back(std::move(p)); |
123 | } else if (object.getType() == "zone") { | ||
124 | Zone z; | ||
125 | z.ul.x() = object.getPosition().x; | ||
126 | z.ul.y() = object.getPosition().y; | ||
127 | z.dr.x() = z.ul.x() + object.getAABB().width; | ||
128 | z.dr.y() = z.ul.y() + object.getAABB().height; | ||
129 | |||
130 | zones_[object.getName()] = std::move(z); | ||
121 | } | 131 | } |
122 | } | 132 | } |
123 | } | 133 | } |
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 { | |||
25 | std::string interactionScript; | 25 | std::string interactionScript; |
26 | bool shadow = false; | 26 | bool shadow = false; |
27 | bool wander = false; | 27 | bool wander = false; |
28 | std::string enclosureZone; | ||
28 | }; | 29 | }; |
29 | 30 | ||
30 | struct Trigger { | 31 | struct Trigger { |
@@ -34,6 +35,11 @@ struct Trigger { | |||
34 | std::string script; | 35 | std::string script; |
35 | }; | 36 | }; |
36 | 37 | ||
38 | struct Zone { | ||
39 | vec2i ul; | ||
40 | vec2i dr; | ||
41 | }; | ||
42 | |||
37 | class Map { | 43 | class Map { |
38 | public: | 44 | public: |
39 | 45 | ||
@@ -61,6 +67,8 @@ public: | |||
61 | 67 | ||
62 | const std::vector<Trigger>& getTriggers() const { return triggers_; } | 68 | const std::vector<Trigger>& getTriggers() const { return triggers_; } |
63 | 69 | ||
70 | const Zone& getZone(const std::string& name) const { return zones_.at(name); } | ||
71 | |||
64 | private: | 72 | private: |
65 | 73 | ||
66 | std::string name_; | 74 | std::string name_; |
@@ -72,6 +80,7 @@ private: | |||
72 | std::vector<Prototype> prototypes_; | 80 | std::vector<Prototype> prototypes_; |
73 | std::map<std::string, vec2i> warpPoints_; | 81 | std::map<std::string, vec2i> warpPoints_; |
74 | std::vector<Trigger> triggers_; | 82 | std::vector<Trigger> triggers_; |
83 | std::map<std::string, Zone> zones_; | ||
75 | }; | 84 | }; |
76 | 85 | ||
77 | #endif /* end of include guard: MAP_H_D95D6D47 */ | 86 | #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: | |||
53 | vec2i collisionSize; | 53 | vec2i collisionSize; |
54 | std::string interactionScript; | 54 | std::string interactionScript; |
55 | std::string walkthroughScript; | 55 | std::string walkthroughScript; |
56 | std::string enclosureZone; | ||
56 | 57 | ||
57 | // Animation | 58 | // Animation |
58 | bool isAnimated = false; | 59 | 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 | |||
67 | vec2i oldTileDR = oldColDR / map.getTileSize(); | 67 | vec2i oldTileDR = oldColDR / map.getTileSize(); |
68 | vec2i newTileDR = newColDR / map.getTileSize(); | 68 | vec2i newTileDR = newColDR / map.getTileSize(); |
69 | 69 | ||
70 | const Zone* enclosureZone = nullptr; | ||
71 | if (!sprite.enclosureZone.empty()) { | ||
72 | enclosureZone = &map.getZone(sprite.enclosureZone); | ||
73 | } | ||
74 | |||
70 | if (dirHasDir(sprite.dir, Direction::right)) { | 75 | if (dirHasDir(sprite.dir, Direction::right)) { |
71 | if (newTileDR.x() > oldTileDR.x() && | 76 | if (newTileDR.x() > oldTileDR.x() && |
72 | newColDR.x() < mapBounds.w()) { | 77 | newColDR.x() < mapBounds.w()) { |
@@ -80,6 +85,14 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Dire | |||
80 | } | 85 | } |
81 | } | 86 | } |
82 | 87 | ||
88 | if (!result.horiz.blocked && enclosureZone) { | ||
89 | if (oldColDR.x() <= enclosureZone->dr.x() && | ||
90 | newColDR.x() > enclosureZone->dr.x()) { | ||
91 | result.horiz.blocked = true; | ||
92 | result.horiz.dir = Direction::right; | ||
93 | } | ||
94 | } | ||
95 | |||
83 | if (!result.horiz.blocked) { | 96 | if (!result.horiz.blocked) { |
84 | auto it = rightCollidables_.lower_bound({oldColDR.x(), INT_MAX}); | 97 | auto it = rightCollidables_.lower_bound({oldColDR.x(), INT_MAX}); |
85 | for (; | 98 | for (; |
@@ -113,6 +126,14 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Dire | |||
113 | } | 126 | } |
114 | } | 127 | } |
115 | 128 | ||
129 | if (!result.horiz.blocked && enclosureZone) { | ||
130 | if (oldColUL.x() >= enclosureZone->ul.x() && | ||
131 | newColUL.x() < enclosureZone->ul.x()) { | ||
132 | result.horiz.blocked = true; | ||
133 | result.horiz.dir = Direction::left; | ||
134 | } | ||
135 | } | ||
136 | |||
116 | if (!result.horiz.blocked) { | 137 | if (!result.horiz.blocked) { |
117 | auto it = leftCollidables_.lower_bound({oldColUL.x(), 0}); | 138 | auto it = leftCollidables_.lower_bound({oldColUL.x(), 0}); |
118 | for (; | 139 | for (; |
@@ -146,6 +167,14 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Dire | |||
146 | } | 167 | } |
147 | } | 168 | } |
148 | 169 | ||
170 | if (!result.vert.blocked && enclosureZone) { | ||
171 | if (oldColDR.y() <= enclosureZone->dr.y() && | ||
172 | newColDR.y() > enclosureZone->dr.y()) { | ||
173 | result.vert.blocked = true; | ||
174 | result.vert.dir = Direction::down; | ||
175 | } | ||
176 | } | ||
177 | |||
149 | if (!result.vert.blocked) { | 178 | if (!result.vert.blocked) { |
150 | auto it = downCollidables_.lower_bound({oldColDR.y(), INT_MAX}); | 179 | auto it = downCollidables_.lower_bound({oldColDR.y(), INT_MAX}); |
151 | for (; | 180 | for (; |
@@ -179,6 +208,14 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Dire | |||
179 | } | 208 | } |
180 | } | 209 | } |
181 | 210 | ||
211 | if (!result.vert.blocked && enclosureZone) { | ||
212 | if (oldColUL.y() >= enclosureZone->ul.y() && | ||
213 | newColUL.y() < enclosureZone->ul.y()) { | ||
214 | result.vert.blocked = true; | ||
215 | result.vert.dir = Direction::up; | ||
216 | } | ||
217 | } | ||
218 | |||
182 | if (!result.vert.blocked) { | 219 | if (!result.vert.blocked) { |
183 | auto it = upCollidables_.lower_bound({oldColUL.y(), 0}); | 220 | auto it = upCollidables_.lower_bound({oldColUL.y(), 0}); |
184 | for (; | 221 | for (; |