Add wood punch sound

This commit is contained in:
2024-01-06 20:22:33 +01:00
parent 114a464c95
commit adf8545b6e
6 changed files with 81 additions and 11 deletions

View File

@@ -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

Binary file not shown.

View File

@@ -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,6 +182,7 @@ bool init(void *userData) {
},
});
{
ECS_COMPONENT_DEFINE(ECS, InputState);
ecs_singleton_set(ECS, InputState, {});
InputState *input = ecs_singleton_get_mut(ECS, InputState);
@@ -189,9 +192,19 @@ bool init(void *userData) {
// Create queries
input->queries.selected = ecs_query(ECS, {
.filter.terms = {
{ ecs_id(Position) }, { ecs_id(Size) }, { ecs_id(Selected) }
{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;

17
game/sounds.c Normal file
View File

@@ -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]);
}
}

30
game/sounds.h Normal file
View File

@@ -0,0 +1,30 @@
#ifndef PIXELDEFENSE_SOUNDS_H
#define PIXELDEFENSE_SOUNDS_H
#include <breeze.h>
#include <flecs.h>
#include <raylib.h>
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

View File

@@ -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);