Skip to content

Commit

Permalink
Plays a sound when weapon is picked up
Browse files Browse the repository at this point in the history
  • Loading branch information
nerudaj committed Feb 4, 2024
1 parent 0f28fcd commit c4b894b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
Binary file added resources/sounds/pickup_weapon.wav
Binary file not shown.
5 changes: 5 additions & 0 deletions src/lib-game/include/core/EntityDefinitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,26 +218,31 @@ const static inline auto ENTITY_PROPERTIES =
{ EntityType::PickupShotgun,
EntityProperties { .radius = 6_px,
.traits = Trait::Pickable | Trait::WeaponPickup,
.specialSound = "pickup_weapon.wav",
.ammoType = AmmoType::Shells,
.initialSpriteIndex = ShotgunA } },
{ EntityType::PickupTrishot,
EntityProperties { .radius = 6_px,
.traits = Trait::Pickable | Trait::WeaponPickup,
.specialSound = "pickup_weapon.wav",
.ammoType = AmmoType::Bullets,
.initialSpriteIndex = TrishotA } },
{ EntityType::PickupCrossbow,
EntityProperties { .radius = 6_px,
.traits = Trait::Pickable | Trait::WeaponPickup,
.specialSound = "pickup_weapon.wav",
.ammoType = AmmoType::Energy,
.initialSpriteIndex = CrossbowA } },
{ EntityType::PickupLauncher,
EntityProperties { .radius = 6_px,
.traits = Trait::Pickable | Trait::WeaponPickup,
.specialSound = "pickup_weapon.wav",
.ammoType = AmmoType::Rockets,
.initialSpriteIndex = LauncherA } },
{ EntityType::PickupBallista,
EntityProperties { .radius = 6_px,
.traits = Trait::Pickable | Trait::WeaponPickup,
.specialSound = "pickup_weapon.wav",
.ammoType = AmmoType::Energy,
.initialSpriteIndex = BallistaA } },
{ EntityType::Pillar,
Expand Down
10 changes: 8 additions & 2 deletions src/lib-game/include/engine/GameRulesEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

import Memory;

struct GiveResult
{
bool given = true;
bool removePickup = true;
bool playSound = true;
};

class GameRulesEngine final
{
public:
Expand Down Expand Up @@ -66,8 +73,7 @@ class GameRulesEngine final

void swapToLastWeapon(PlayerInventory& inventory, EntityIndexType idx);

/// <returns>True if thing was succesfully given</returns>
[[nodiscard]] bool
[[nodiscard]] GiveResult
give(Entity& entity, PlayerInventory& inventory, EntityType pickupId);

[[nodiscard]] int computeProjectileDamage(
Expand Down
39 changes: 27 additions & 12 deletions src/lib-game/src/engine/GameRulesEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,15 @@ void GameRulesEngine::handleGrabbedPickable(
Entity pickup,
std::size_t pickupId)
{
if (give(entity, inventory, pickup.typeId))
if (auto giveResult = give(entity, inventory, pickup.typeId);
giveResult.given)
{
eventQueue->emplace<PickablePickedUpGameEvent>(pickupId);
eventQueue->emplace<SoundTriggeredAudioEvent>(
ENTITY_PROPERTIES.at(pickup.typeId).specialSound, entity.stateIdx);
if (giveResult.removePickup)
eventQueue->emplace<PickablePickedUpGameEvent>(pickupId);
if (giveResult.playSound)
eventQueue->emplace<SoundTriggeredAudioEvent>(
ENTITY_PROPERTIES.at(pickup.typeId).specialSound,
entity.stateIdx);
}
}

Expand Down Expand Up @@ -387,22 +391,32 @@ void GameRulesEngine::swapToLastWeapon(
idx, AnimationStateId::FastLower);
}

bool GameRulesEngine::give(
GiveResult GameRulesEngine::give(
Entity& entity, PlayerInventory& inventory, EntityType pickupType)
{
using enum EntityType;

const auto& def = ENTITY_PROPERTIES.at(pickupType);

const auto NOT_GIVEN = GiveResult { .given = false,
.removePickup = false,
.playSound = false };

const auto PICKUP_GIVEN =
GiveResult { .given = true, .removePickup = true, .playSound = true };

const auto WEAPON_GIVEN =
GiveResult { .given = true, .removePickup = false, .playSound = true };

switch (pickupType)
{
case PickupHealth:
if (entity.health >= MAX_HEALTH) return false;
if (entity.health >= MAX_HEALTH) return NOT_GIVEN;
entity.health =
std::clamp(entity.health + def.healthAmount, 0, MAX_HEALTH);
break;
case PickupArmor:
if (entity.armor >= MAX_ARMOR) return false;
if (entity.armor >= MAX_ARMOR) return NOT_GIVEN;
entity.armor = std::clamp(entity.armor + def.armorAmount, 0, MAX_ARMOR);
break;
case PickupMegaHealth:
Expand All @@ -417,7 +431,8 @@ bool GameRulesEngine::give(
case PickupEnergy:
case PickupRockets: {
const auto ammoIndex = ammoPickupToAmmoIndex(pickupType);
if (inventory.ammo[ammoIndex] == AMMO_LIMIT[ammoIndex]) return false;
if (inventory.ammo[ammoIndex] == AMMO_LIMIT[ammoIndex])
return NOT_GIVEN;
inventory.ammo[ammoIndex] = std::clamp(
inventory.ammo[ammoIndex] + def.ammoAmount,
0,
Expand All @@ -430,17 +445,17 @@ bool GameRulesEngine::give(
case PickupLauncher:
case PickupBallista: {
const auto index = weaponPickupToIndex(pickupType);
if (inventory.acquiredWeapons[index]) return false;
if (inventory.acquiredWeapons[index]) return NOT_GIVEN;
std::ignore =
give(entity, inventory, ammoTypeToPickupType(def.ammoType));
inventory.acquiredWeapons[index] = true;
return false;
return WEAPON_GIVEN;
}
default:
return false;
return NOT_GIVEN;
}

return true;
return PICKUP_GIVEN;
}

int GameRulesEngine::computeProjectileDamage(
Expand Down

0 comments on commit c4b894b

Please sign in to comment.