Skip to content

Commit c3ef880

Browse files
committed
Added sound effects when picking up objects
1 parent a4258da commit c3ef880

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

.vscode/tasks.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"/LIBPATH:C:\\C++ Libraries\\SFML-3.0.0-windows-vc17-32-bit\\SFML-3.0.0\\lib",
1515
"sfml-graphics.lib",
1616
"sfml-window.lib",
17-
"sfml-system.lib"
17+
"sfml-system.lib",
18+
"sfml-audio.lib"
1819
],
1920
"group": {
2021
"kind": "build",

main.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ int main()
6565
sf::Clock clock;
6666
sf::Clock sprintClock;
6767

68+
// Set up audio
69+
sf::SoundBuffer goldPickupBuffer;
70+
sf::SoundBuffer healthPickupBuffer;
71+
72+
// Load audio from files
73+
if (!goldPickupBuffer.loadFromFile("assets/audio/goldPickup.ogg")) {
74+
std::cerr << "Error: Could not load gold pickup sound file!" << std::endl;
75+
return -1;
76+
}
77+
if (!healthPickupBuffer.loadFromFile("assets/audio/healthPickup.ogg")) {
78+
std::cerr << "Error: Could not load health pickup sound file!" << std::endl;
79+
return -1;
80+
}
81+
82+
// Creat sounds
83+
sf::Sound goldPickupSound(goldPickupBuffer);
84+
sf::Sound healthPickupSound(healthPickupBuffer);
85+
6886
// Create list of entities
6987
std::vector<std::shared_ptr<Entity>> entities;
7088

@@ -274,13 +292,14 @@ int main()
274292
// Heal player when colliding with health pickups if not at max health
275293
if (player->getHealth() < player->getMaxHealth()) {
276294
handlePickup(healthPickups, (player->playerCollider), map, [&](auto& pickup) {
277-
player->heal(1);
278-
295+
player->heal(1);
296+
healthPickupSound.play();
279297
});
280298
}
281299
// Give gold when colliding with gold pickups
282300
handlePickup(goldPickups, (player->playerCollider), map, [&](auto& pickup) {
283301
player->addGold(25);
302+
goldPickupSound.play();
284303
});
285304

286305
//---- Draw items ----

main.exe

2.5 KB
Binary file not shown.

main.obj

5.29 KB
Binary file not shown.

0 commit comments

Comments
 (0)