diff --git a/CMakeLists.txt b/CMakeLists.txt index a9bbd15..2df3dee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,8 @@ add_executable(PixelDefense game/map_layers.h game/pathfinding.c game/pathfinding.h + game/sounds.c + game/sounds.h game/ui_widgets.c game/ui_widgets.h game/unit_actions.c diff --git a/assets/sounds/wood hit 17.wav b/assets/sounds/wood hit 17.wav new file mode 100644 index 0000000..5561959 Binary files /dev/null and b/assets/sounds/wood hit 17.wav differ diff --git a/game/main.c b/game/main.c index 14dda48..4533a40 100644 --- a/game/main.c +++ b/game/main.c @@ -16,10 +16,12 @@ #include "unit_actions.h" #include "pathfinding.h" +#include "sounds.h" ECS_COMPONENT_DECLARE(Game); ECS_COMPONENT_DECLARE(InputState); +ECS_COMPONENT_DECLARE(SoundState); BzUI *UI = NULL; ecs_world_t *ECS = NULL; @@ -180,18 +182,29 @@ bool init(void *userData) { }, }); - ECS_COMPONENT_DEFINE(ECS, InputState); - ecs_singleton_set(ECS, InputState, {}); - InputState *input = ecs_singleton_get_mut(ECS, InputState); + { + ECS_COMPONENT_DEFINE(ECS, InputState); + ecs_singleton_set(ECS, InputState, {}); + InputState *input = ecs_singleton_get_mut(ECS, InputState); - input->mapping = inputDefaultMapping(); + input->mapping = inputDefaultMapping(); - // Create queries - input->queries.selected = ecs_query(ECS, { - .filter.terms = { - { ecs_id(Position) }, { ecs_id(Size) }, { ecs_id(Selected) } - } - }); + // Create queries + input->queries.selected = ecs_query(ECS, { + .filter.terms = { + {ecs_id(Position)}, + {ecs_id(Size)}, + {ecs_id(Selected)} + } + }); + } + { + InitAudioDevice(); + ECS_COMPONENT_DEFINE(ECS, SoundState); + ecs_singleton_set(ECS, SoundState, {}); + SoundState *sounds = ecs_singleton_get_mut(ECS, SoundState); + soundsLoad(sounds, SOUND_WOOD_PUNCH, 0.1f, "assets/sounds/wood hit 17.wav"); + } game->stackAlloc = bzStackAllocCreate(10 * 1000 * 1000); // 10 MB // init pools @@ -230,6 +243,7 @@ void deinit(void *userData) { BZ_UNUSED(userData); Game *game = ecs_singleton_get_mut(ECS, Game); InputState *input = ecs_singleton_get_mut(ECS, InputState); + SoundState *sounds = ecs_singleton_get_mut(ECS, SoundState); unloadMap(game); @@ -239,6 +253,7 @@ void deinit(void *userData) { Game gameCopy = *game; InputState inputCopy = *input; + SoundState soundsCopy = *sounds; // Destroy ECS ecs_fini(ECS); @@ -246,6 +261,7 @@ void deinit(void *userData) { game = &gameCopy; input = &inputCopy; + sounds = &soundsCopy; bzTilesetDestroy(&game->tileset); @@ -255,6 +271,8 @@ void deinit(void *userData) { bzArrayDestroy(game->drawData); + soundsUnloadAll(sounds); + bzUIDestroy(UI); UI = NULL; diff --git a/game/sounds.c b/game/sounds.c new file mode 100644 index 0000000..01c1848 --- /dev/null +++ b/game/sounds.c @@ -0,0 +1,17 @@ +#include "sounds.h" + +void soundsLoad(SoundState *sounds, SoundType type, f32 interval, const char *path) { + Sound newSound = LoadSound(path); + sounds->sounds[type] = newSound; + sounds->soundInterval[type] = interval; + sounds->playing = newSound; +} +void soundsPlay(SoundState *sounds, SoundType type) { + PlaySound(sounds->playing); +} + +void soundsUnloadAll(SoundState *sounds) { + for (i32 i = 0; i < SOUND_COUNT; i++) { + UnloadSound(sounds->sounds[i]); + } +} diff --git a/game/sounds.h b/game/sounds.h new file mode 100644 index 0000000..10e33ba --- /dev/null +++ b/game/sounds.h @@ -0,0 +1,30 @@ +#ifndef PIXELDEFENSE_SOUNDS_H +#define PIXELDEFENSE_SOUNDS_H + +#include +#include +#include + +typedef enum SoundType { + SOUND_WOOD_PUNCH, + SOUND_COUNT +} SoundType; + +typedef struct SoundState { + Sound sounds[SOUND_COUNT]; + Sound playing; + // How long it needs to wait, + // before playing the same sound + f32 soundInterval[SOUND_COUNT]; +} SoundState; + +void soundsLoad(SoundState *sounds, SoundType type, f32 interval, const char *path); +void soundsPlay(SoundState *sounds, SoundType type); + +void soundsUnloadAll(SoundState *sounds); + +extern ECS_COMPONENT_DECLARE(SoundState); // defined in main.c + + + +#endif //PIXELDEFENSE_SOUNDS_H diff --git a/game/systems/s_event.c b/game/systems/s_event.c index 6d5f0b5..2819171 100644 --- a/game/systems/s_event.c +++ b/game/systems/s_event.c @@ -1,6 +1,7 @@ #include "systems.h" #include "../game_state.h" +#include "../sounds.h" i32 harvestEvent(ecs_entity_t entity, HarvestEvent event) { BZ_ASSERT(ecs_has_id(ECS, entity, Harvestable)); @@ -10,13 +11,15 @@ i32 harvestEvent(ecs_entity_t entity, HarvestEvent event) { .compID = ecs_id(Rotation), .memberOffset = 0, .easingFunc = BZ_EASE_OUT_ELASTIC, - .duration = 0.4f, + .duration = 0.6f, // 45 * (1.0f + (-1.0f) * x) .target = 45, .easeTarget = -1.0f, .easeStart = 1.0f }); + SoundState *sounds = ecs_singleton_get_mut(ECS, SoundState); + soundsPlay(sounds, SOUND_WOOD_PUNCH); Resource *res = ecs_get_mut(ECS, entity, Resource); event.amount = BZ_MIN(event.amount, res->amount);