Skip to content

Commit bfdea5e

Browse files
committed
Added chest to tilemap items
1 parent 6a3a403 commit bfdea5e

8 files changed

Lines changed: 52 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This may change in the future.
1717
# About the app
1818
This a 2D top down game using C++ and SFML 3.0, includes 8 directional movement, combat, animation and enemies, it is still a work in progress, but is planned to be similar to top down zelda games. I am very new to SFML so I'm learning as I go.
1919

20-
This app requires the SMFL 3.0 library to work, specifically the graphics, window and system components.
20+
This app requires the SMFL 3.0 library to work, specifically the graphics, audio, window and system components.
2121

2222
# Features to consider
2323
Add more tile options

TileMap.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ class TileMapRenderer {
6060

6161
loadTile('H', "assets/images/HealthPickup.png", TileAsset::Type::Item);
6262
loadTile('G', "assets/images/GoldPickup.png", TileAsset::Type::Item);
63-
//loadTile('C', "assets/images/Chest.png", TileAsset::Type::Item);
63+
loadTile('C', "assets/images/ChestClosed.png", TileAsset::Type::Item);
64+
loadTile('Q', "assets/images/ChestOpenFull.png", TileAsset::Type::Item);
65+
loadTile('O', "assets/images/ChestOpenEmpty.png", TileAsset::Type::Item);
6466
}
6567

6668
// Add tiles to tile list

assets/images/ChestClosed.png

8.36 KB
Loading

assets/images/ChestOpenEmpty.png

10.7 KB
Loading

assets/images/ChestOpenFull.png

11.6 KB
Loading

main.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ int main()
119119
Object* collider() { return &goldPickup; }
120120
};
121121
std::vector<goldPickup> goldPickups;
122+
struct chestPickup {
123+
Object chestPickup;
124+
float stateTimer = 0.f;
125+
int state = 0; // 0 = closed, 1 = open-full, 2 = open-empty
126+
Object* collider() { return &chestPickup; }
127+
};
128+
std::vector<chestPickup> chestPickups;
122129

123130
// ---- Set up objects ----
124131
std::vector<Object> wallObjects;
@@ -156,6 +163,15 @@ int main()
156163
// Add to list
157164
goldPickups.push_back(goldPickup{pickup});
158165
}
166+
// Chest pickups
167+
if (map.getTile(x, y) == 'C') {
168+
Object pickup;
169+
pickup.pos = Vec2(x * TILE_SIZE + TILE_SIZE / 2.0f, y * TILE_SIZE + TILE_SIZE / 2.0f);
170+
pickup.aabb.min = Vec2(x * TILE_SIZE, y * TILE_SIZE);
171+
pickup.aabb.max = Vec2((x + 1) * TILE_SIZE, (y + 1) * TILE_SIZE);
172+
// Add to list
173+
chestPickups.push_back(chestPickup{pickup});
174+
}
159175
// Enemies
160176
if (map.getTile(x, y) == 'E') {
161177
Vec2 pos(x * TILE_SIZE + TILE_SIZE / 2.0f, y * TILE_SIZE + TILE_SIZE / 2.0f);
@@ -324,6 +340,38 @@ int main()
324340
player->addGold(25);
325341
goldPickupSound.play();
326342
});
343+
// Give gold when colliding with chest pickups
344+
for (auto& chest : chestPickups) {
345+
Manifold m = { &(player->playerCollider), chest.collider() };
346+
if (AABBvsAABB(&m)) {
347+
// Open chest
348+
Vec2 pos = chest.collider()->pos;
349+
int tileX = pos.x / TILE_SIZE;
350+
int tileY = pos.y / TILE_SIZE;
351+
if (chest.state == 0) {
352+
map.setTile(tileX, tileY, 'Q');
353+
player->addGold(100);
354+
goldPickupSound.play();
355+
356+
chest.state = 1;
357+
chest.stateTimer = 0.f; // Start animation timer
358+
}
359+
}
360+
}
361+
// Check chest states
362+
for (auto& chest : chestPickups) {
363+
if (chest.state == 1) {
364+
chest.stateTimer += deltaTime;
365+
// After 0.4 seconds, switch to empty chest
366+
if (chest.stateTimer >= 0.4f) {
367+
Vec2 pos = chest.collider()->pos;
368+
int tileX = pos.x / TILE_SIZE;
369+
int tileY = pos.y / TILE_SIZE;
370+
map.setTile(tileX, tileY, 'O');
371+
chest.state = 2;
372+
}
373+
}
374+
}
327375
}
328376

329377
//---- Draw items ----

main.exe

4.5 KB
Binary file not shown.

main.obj

17.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)