Skip to content

Commit

Permalink
Added sound when equiping weapon (rotating between them)
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed May 9, 2024
1 parent 949fa85 commit 24ac960
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 4 deletions.
Binary file added assets/sounds/7mm/equip.wav
Binary file not shown.
Binary file added assets/sounds/akm/equip.wav
Binary file not shown.
1 change: 1 addition & 0 deletions include/sk_weapon.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef struct {
ModelAnimation *model_anims;
u8 model_anims_count;
u8 model_anim_frame_count;
Sound sound_equip;
Sound sound_shoot;
Sound sound_reload;
sk_weapon_ammo_spec ammo;
Expand Down
2 changes: 2 additions & 0 deletions src/sk_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ void sk_player_move(sk_player *p, sk_config *config, f32 roll) {
}

void sk_player_rotate_weapon(sk_player *p) {
if (IsSoundPlaying(p->weapon->sound_equip) || IsSoundPlaying(p->weapon->sound_reload)) return;
static usz i = SIDE_WEAPON_IDX;
i = (i + 1) % SK_PLAYER_WEAPON_SLOTS;
p->weapon = &p->weapon_slots[i];
PlaySound(p->weapon->sound_equip);
}

void sk_player_draw(sk_player *p) {
Expand Down
8 changes: 4 additions & 4 deletions src/sk_scene_gameplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
#include <sk_scene_gameplay.h>

void sk_scene_gameplay_update(sk_state *s) {
sk_player_move(&s->player, &s->config, sk_player_peek(&s->player, &s->config));
sk_player_jump(&s->player, &s->config);
if (GetMouseWheelMove()) sk_player_rotate_weapon(&s->player);
if (IsKeyPressed(s->config.controls.reload)) sk_weapon_reload(s->player.weapon);
if (IsMouseButtonPressed(s->config.controls.shoot)) {
sk_shot shot = {0};
if (!sk_weapon_shoot(s->player.weapon, s->player.id, &s->player.camera, &shot)) return;
sk_rngbuf_push(&s->shots_rb, &shot);
}
if (IsKeyPressed(s->config.controls.reload)) sk_weapon_reload(s->player.weapon);
if (GetMouseWheelMove() && !IsSoundPlaying(s->player.weapon->sound_reload)) sk_player_rotate_weapon(&s->player);
sk_player_jump(&s->player, &s->config);
sk_player_move(&s->player, &s->config, sk_player_peek(&s->player, &s->config));
}

void sk_scene_gameplay_draw(sk_state *s) {
Expand Down
7 changes: 7 additions & 0 deletions src/sk_weapon.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <raymath.h>

#define MODEL_PATH_PLACEHOLDER "assets/models/%s.glb"
#define SOUND_EQUIP_PATH_PLACEHOLDER "assets/sounds/%s/equip.wav"
#define SOUND_SHOOT_PATH_PLACEHOLDER "assets/sounds/%s/shoot.wav"
#define SOUND_RELOAD_PATH_PLACEHOLDER "assets/sounds/%s/reload.wav"

Expand All @@ -37,6 +38,7 @@ sk_weapon sk_weapon_create(sk_weapon_kind kind) {
.kind = kind,
.model = LoadModel(TextFormat(MODEL_PATH_PLACEHOLDER, name)),
.model_anims = LoadModelAnimations(TextFormat(MODEL_PATH_PLACEHOLDER, name), (int *) &w.model_anims_count),
.sound_equip = LoadSound(TextFormat(SOUND_EQUIP_PATH_PLACEHOLDER, name)),
.sound_shoot = LoadSound(TextFormat(SOUND_SHOOT_PATH_PLACEHOLDER, name)),
.sound_reload = LoadSound(TextFormat(SOUND_RELOAD_PATH_PLACEHOLDER, name)),
.ammo = (sk_weapon_ammo_spec) {
Expand All @@ -45,11 +47,16 @@ sk_weapon sk_weapon_create(sk_weapon_kind kind) {
}
};
for (usz i = 0; i < w.model_anims_count; ++i) assert(IsModelAnimationValid(w.model, w.model_anims[i]));
assert(IsSoundReady(w.sound_equip));
assert(IsSoundReady(w.sound_shoot));
assert(IsSoundReady(w.sound_reload));
return w;
}

void sk_weapon_destroy(sk_weapon *w) {
UnloadSound(w->sound_reload);
UnloadSound(w->sound_shoot);
UnloadSound(w->sound_equip);
UnloadModelAnimations(w->model_anims, w->model_anims_count);
UnloadModel(w->model);
*w = (sk_weapon) {0};
Expand Down

0 comments on commit 24ac960

Please sign in to comment.