Add sounds

This commit is contained in:
2024-02-10 15:03:39 +01:00
parent 6eddf11cfa
commit 20af3967a8
22 changed files with 88 additions and 14 deletions

BIN
assets/sounds/hurt_1.wav Normal file

Binary file not shown.

BIN
assets/sounds/hurt_2.wav Normal file

Binary file not shown.

BIN
assets/sounds/hurt_3.wav Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -226,7 +226,28 @@ bool init(void *userData) {
sounds->masterVolume = 0.5f;
sounds->musicVolume = 0.5f;
sounds->soundVolume = 0.5f;
soundsLoad(sounds, SOUND_WOOD_PUNCH, 0.1f, "assets/sounds/wood hit 17.wav");
soundsLoad(sounds, SOUND_HURT_1, 0.2f, "assets/sounds/hurt_1.wav");
soundsLoad(sounds, SOUND_HURT_2, 0.2f, "assets/sounds/hurt_2.wav");
soundsLoad(sounds, SOUND_HURT_3, 0.2f, "assets/sounds/hurt_3.wav");
soundsLoad(sounds, SOUNDS_ORE_PUNCH_1, 0.5f, "assets/sounds/ore_punch_1.wav");
soundsLoad(sounds, SOUNDS_ORE_PUNCH_2, 0.5f, "assets/sounds/ore_punch_2.wav");
soundsLoad(sounds, SOUNDS_ORE_PUNCH_3, 0.5f, "assets/sounds/ore_punch_3.wav");
soundsLoad(sounds, SOUNDS_ORE_PUNCH_4, 0.5f, "assets/sounds/ore_punch_4.wav");
soundsLoad(sounds, SOUNDS_ORE_PUNCH_5, 0.5f, "assets/sounds/ore_punch_5.wav");
soundsLoad(sounds, SOUNDS_WHEAT_PUNCH_1, 0.5f, "assets/sounds/wheat_punch_1.wav");
soundsLoad(sounds, SOUNDS_WHEAT_PUNCH_2, 0.5f, "assets/sounds/wheat_punch_2.wav");
soundsLoad(sounds, SOUNDS_WHEAT_PUNCH_3, 0.5f, "assets/sounds/wheat_punch_3.wav");
soundsLoad(sounds, SOUNDS_WHEAT_PUNCH_4, 0.5f, "assets/sounds/wheat_punch_4.wav");
soundsLoad(sounds, SOUNDS_WHEAT_PUNCH_5, 0.5f, "assets/sounds/wheat_punch_5.wav");
soundsLoad(sounds, SOUNDS_WOOD_PUNCH_1, 0.5f, "assets/sounds/wood_punch_1.wav");
soundsLoad(sounds, SOUNDS_WOOD_PUNCH_2, 0.5f, "assets/sounds/wood_punch_2.wav");
soundsLoad(sounds, SOUNDS_WOOD_PUNCH_3, 0.5f, "assets/sounds/wood_punch_3.wav");
soundsLoad(sounds, SOUNDS_WOOD_PUNCH_4, 0.5f, "assets/sounds/wood_punch_4.wav");
soundsLoad(sounds, SOUNDS_WOOD_PUNCH_5, 0.5f, "assets/sounds/wood_punch_5.wav");
}
setScreen(game, SCREEN_MAIN_MENU);
@@ -455,7 +476,7 @@ void update(float dt, void *userData) {
}
SoundState *soundState = ecs_singleton_get_mut(ECS, SoundState);
soundsUpdate(soundState);
soundsUpdate(soundState, getCameraBounds(game->camera));
}
static void drawOverScreen(Color c) {

View File

@@ -4,8 +4,10 @@ void soundsApplyVolume(SoundState *sounds, f32 master, f32 music, f32 sound) {
SetMasterVolume(master);
if (sounds->musicLoaded)
SetMusicVolume(sounds->music, music);
if (IsSoundPlaying(sounds->playing))
SetSoundVolume(sounds->playing, sound);
for (i32 i = 0; i < SOUND_MAX_PLAYING; i++) {
if (IsSoundPlaying(sounds->playing[i]))
SetSoundVolume(sounds->playing[i], sound);
}
sounds->masterVolume = master;
sounds->musicVolume = music;
@@ -16,11 +18,33 @@ void soundsLoad(SoundState *sounds, SoundType type, f32 interval, const char *pa
Sound newSound = LoadSound(path);
sounds->sounds[type] = newSound;
sounds->soundInterval[type] = interval;
sounds->playing = newSound;
}
void soundsPlay(SoundState *sounds, SoundType type) {
PlaySound(sounds->playing);
SetSoundVolume(sounds->playing, sounds->soundVolume);
bool soundsPlay(SoundState *sounds, SoundType type) {
f32 time = GetTime();
f32 lastPlayedTime = sounds->soundLastPlayed[type];
if (time - lastPlayedTime < sounds->soundInterval[type])
return false;
i32 freeSlot = -1;
for (i32 i = 0; i < SOUND_MAX_PLAYING; i++) {
if (IsSoundPlaying(sounds->playing[i])) {
freeSlot = i;
break;
}
}
if (freeSlot == -1) {
return false;
}
PlaySound(sounds->playing[freeSlot]);
SetSoundVolume(sounds->playing[freeSlot], sounds->soundVolume);
sounds->soundLastPlayed[type] = time;
return true;
}
bool soundsPosPlay(SoundState *sounds, Vector2 position, SoundType type) {
if (!CheckCollisionPointRec(position, sounds->cameraBounds))
return false;
return soundsPlay(sounds, type);
}
void soundsUnloadAll(SoundState *sounds) {
@@ -29,10 +53,11 @@ void soundsUnloadAll(SoundState *sounds) {
}
}
void soundsUpdate(SoundState *sounds) {
void soundsUpdate(SoundState *sounds, Rectangle cameraBounds) {
if (sounds->musicLoaded == true) {
UpdateMusicStream(sounds->music);
}
sounds->cameraBounds = cameraBounds;
}
void soundsLoadMusicStream(SoundState *sounds, const char *path) {
soundsUnloadMusicStream(sounds);

View File

@@ -6,15 +6,41 @@
#include <raylib.h>
typedef enum SoundType {
SOUND_WOOD_PUNCH,
SOUND_HURT_1,
SOUND_HURT_2,
SOUND_HURT_3,
SOUNDS_ORE_PUNCH_1,
SOUNDS_ORE_PUNCH_2,
SOUNDS_ORE_PUNCH_3,
SOUNDS_ORE_PUNCH_4,
SOUNDS_ORE_PUNCH_5,
SOUNDS_WHEAT_PUNCH_1,
SOUNDS_WHEAT_PUNCH_2,
SOUNDS_WHEAT_PUNCH_3,
SOUNDS_WHEAT_PUNCH_4,
SOUNDS_WHEAT_PUNCH_5,
SOUNDS_WOOD_PUNCH_1,
SOUNDS_WOOD_PUNCH_2,
SOUNDS_WOOD_PUNCH_3,
SOUNDS_WOOD_PUNCH_4,
SOUNDS_WOOD_PUNCH_5,
SOUND_COUNT
} SoundType;
static SoundType getRandomSoundType(SoundType min, SoundType max) {
BZ_ASSERT(min <= max && min >= 0 && max < SOUND_COUNT);
return GetRandomValue(min, max);
}
#define SOUND_MAX_PLAYING 16
typedef struct SoundState {
Rectangle cameraBounds;
Sound sounds[SOUND_COUNT];
Sound playing;
Sound playing[SOUND_MAX_PLAYING];
// How long it needs to wait,
// before playing the same sound
f32 soundLastPlayed[SOUND_COUNT];
f32 soundInterval[SOUND_COUNT];
bool musicLoaded;
@@ -28,11 +54,12 @@ typedef struct SoundState {
void soundsApplyVolume(SoundState *sounds, f32 master, f32 music, f32 sound);
void soundsLoad(SoundState *sounds, SoundType type, f32 interval, const char *path);
void soundsPlay(SoundState *sounds, SoundType type);
bool soundsPlay(SoundState *sounds, SoundType type);
bool soundsPosPlay(SoundState *sounds, Vector2 position, SoundType type);
void soundsUnloadAll(SoundState *sounds);
void soundsUpdate(SoundState *sounds);
void soundsUpdate(SoundState *sounds, Rectangle cameraBounds);
void soundsLoadMusicStream(SoundState *sounds, const char *path);
void soundsPlayMusicStream(SoundState *sounds);

View File

@@ -87,7 +87,8 @@ i32 harvestEvent(ecs_entity_t entity, HarvestEvent event) {
.easeStart = 1.0f
});
SoundState *sounds = ecs_singleton_get_mut(ECS, SoundState);
soundsPlay(sounds, SOUND_WOOD_PUNCH);
Vector2 center = entityGetCenter(*ecs_get(ECS, entity, Position), *ecs_get(ECS, entity, HitBox));
soundsPosPlay(sounds, center, getRandomSoundType(SOUNDS_WOOD_PUNCH_1, SOUNDS_WOOD_PUNCH_5));
Resource *res = ecs_get_mut(ECS, entity, Resource);
BZ_ASSERT(res->type == event.type);