Skip to content

Commit b58c712

Browse files
committed
Changed how pickups were stored, resolved pickup collider issue
1 parent dedc65c commit b58c712

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

main.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,23 @@ int main()
3636
map.loadFromFile("level.txt");
3737

3838
// Set up wall and pickup colliders
39+
struct Pickup {
40+
Object pickup;
41+
Object* collider() { return &pickup; }
42+
};
43+
3944
std::vector<Object> wallObjects;
40-
std::vector<Object*> pickupColliders;
41-
std::vector<Object> pickupObjects;
45+
std::vector<Pickup> pickups;
4246
std::vector<Enemy> enemies;
4347
sf::Texture enemyTex;
4448
if (!enemyTex.loadFromFile("playerSpritesheet.png"))
4549
throw std::runtime_error("Failed to load Wall.png");
4650
sf::Vector2i size = map.getSize();
51+
wallObjects.reserve(size.x * size.y);
4752
for (int y = 0; y < size.y; ++y) { // height
4853
for (int x = 0; x < size.x; ++x) { // width
4954
// Walls
5055
if (map.getTile(x, y) == '#') {
51-
wallObjects.reserve(size.x * size.y);
5256
Object wall;
5357
wall.pos = Vec2(x * TILE_SIZE + TILE_SIZE / 2.0f, y * TILE_SIZE + TILE_SIZE / 2.0f);
5458
wall.aabb.min = Vec2(x * TILE_SIZE, y * TILE_SIZE);
@@ -59,14 +63,13 @@ int main()
5963
}
6064
// Pickups
6165
if (map.getTile(x, y) == 'P') {
62-
pickupObjects.reserve(size.x * size.y);
66+
std::cout << "P" << std::endl;
6367
Object pickup;
6468
pickup.pos = Vec2(x * TILE_SIZE + TILE_SIZE / 2.0f, y * TILE_SIZE + TILE_SIZE / 2.0f);
6569
pickup.aabb.min = Vec2(x * TILE_SIZE, y * TILE_SIZE);
6670
pickup.aabb.max = Vec2((x + 1) * TILE_SIZE, (y + 1) * TILE_SIZE);
67-
// Add to colliders
68-
pickupObjects.push_back(pickup);
69-
pickupColliders.push_back(&pickupObjects.back());
71+
// Add to list
72+
pickups.push_back(Pickup{pickup});
7073
}
7174
// Enemies
7275
if (map.getTile(x, y) == 'E') {
@@ -187,21 +190,17 @@ int main()
187190
}
188191
}
189192
// Delete pickup if collided with
190-
for (auto it = pickupColliders.begin(); it != pickupColliders.end(); ) {
191-
Manifold m = {&playerCollider, *it};
193+
for (auto it = pickups.begin(); it != pickups.end(); ) {
194+
Manifold m = {&playerCollider, it->collider()};
192195
if (AABBvsAABB(&m)) {
196+
std::cout << "Collision detected!" << std::endl;
193197
// Remove pickup from tilemap
194-
Vec2 pos = (*it)->pos;
198+
Vec2 pos = (it)->pickup.pos;
195199
int tileX = static_cast<int>(pos.x) / TILE_SIZE;
196200
int tileY = static_cast<int>(pos.y) / TILE_SIZE;
197201
map.setTile(tileX, tileY, '.');
198202
// Remove pickup from objects and colliders
199-
Object* collidedPickup = *it;
200-
pickupObjects.erase(std::remove_if(pickupObjects.begin(), pickupObjects.end(),
201-
[&](const Object& shape) {
202-
return &shape == collidedPickup;
203-
}), pickupObjects.end());
204-
it = pickupColliders.erase(it);
203+
it = pickups.erase(it);
205204
break;
206205
} else {
207206
++it;

main.exe

5.5 KB
Binary file not shown.

main.obj

15 KB
Binary file not shown.

0 commit comments

Comments
 (0)