Skip to content

Commit

Permalink
Factored out weapon logic into sk_weapon module
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Mar 26, 2024
1 parent 967930b commit d2a35ec
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 35 deletions.
13 changes: 3 additions & 10 deletions include/sk_gametypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#pragma once

#include <raylib.h>
#include <sk_weapon.h>

typedef enum {
SCENE_MAIN_MENU,
Expand All @@ -32,18 +33,10 @@ typedef struct {
Model model;
} Enemy;

typedef struct {
Model model;
ModelAnimation *model_anims;
u8 model_anims_count;
u8 model_anim_frame_count;
Sound sound_shoot;
} Weapon;

typedef struct {
typedef struct Player {
Camera3D camera;
Model model;
Weapon weapon;
sk_weapon weapon;
float v_y;
} Player;

Expand Down
50 changes: 50 additions & 0 deletions include/sk_weapon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#pragma once

#include <raylib.h>
#include <sk_defines.h>

// fwd-decl
struct Player;

typedef struct {
Model model;
ModelAnimation *model_anims;
u8 model_anims_count;
u8 model_anim_frame_count;
Sound sound_shoot;
} sk_weapon;

typedef enum {
SK_WEAPON_KIND_7MM
} sk_weapon_kind;

static const char * const sk_weapon_kinds[] = {
[SK_WEAPON_KIND_7MM] = "7mm"
};

void sk_weapon_create(struct Player *p, sk_weapon_kind kind);

void sk_weapon_destroy(sk_weapon *w);

void sk_weapon_shoot(sk_weapon *w);
4 changes: 1 addition & 3 deletions src/sk_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ static void __init(void) {
}

static u8 __shutdown(void) {
UnloadSound(state.player.weapon.sound_shoot);
UnloadModelAnimations(state.player.weapon.model_anims, state.player.weapon.model_anims_count);
UnloadModel(state.player.weapon.model);
sk_weapon_destroy(&state.player.weapon);
UnloadModel(state.player.model);
CloseAudioDevice();
CloseWindow();
Expand Down
28 changes: 6 additions & 22 deletions src/sk_client_renderer_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,13 @@
#include <sk_defines.h>
#include <sk_client_renderer.h>

#define MODEL_PLAYER_JETT "assets/models/jett/scene.gltf"
#define MODEL_WEAPON_7MM "assets/models/7mm.glb"
#define MODEL_WEAPON_7MM_SOUND_SHOOT "assets/sounds/7mm/shoot.wav"

static inline void __load_weapon(State *s) {
s->player.weapon.model = LoadModel(MODEL_WEAPON_7MM);
s->player.weapon.model_anims = LoadModelAnimations(MODEL_WEAPON_7MM, (int *) &s->player.weapon.model_anims_count);
assert(s->player.weapon.model_anims_count == 1);
assert(IsModelAnimationValid(s->player.weapon.model, s->player.weapon.model_anims[0]));
s->player.weapon.model_anim_frame_count = 0;
s->player.weapon.sound_shoot = LoadSound(MODEL_WEAPON_7MM_SOUND_SHOOT);
}
#define MODEL_PLAYER_JETT "assets/models/jett/scene.gltf"

static inline void __load_player(State *s) {
s->player.model = LoadModel(MODEL_PLAYER_JETT);
__load_weapon(s);
sk_weapon_create(&s->player, SK_WEAPON_KIND_7MM);
assert(s->player.weapon.model_anims_count == 1);
assert(IsModelAnimationValid(s->player.weapon.model, s->player.weapon.model_anims[0]));
}

static void __update_main_menu(State *s) {
Expand All @@ -63,15 +54,8 @@ static void __update_gameplay_jump(State *s) {
else s->player.camera.target.y += s->player.v_y * dt;
}

static void __update_gameplay_shoot(State *s) {
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
++s->player.weapon.model_anim_frame_count;
UpdateModelAnimation(s->player.weapon.model, s->player.weapon.model_anims[0], s->player.weapon.model_anim_frame_count);
if (s->player.weapon.model_anim_frame_count >= s->player.weapon.model_anims[0].frameCount) {
s->player.weapon.model_anim_frame_count = 0;
}
PlaySound(s->player.weapon.sound_shoot);
}
static inline void __update_gameplay_shoot(State *s) {
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) sk_weapon_shoot(&s->player.weapon);
}

static float __update_gameplay_cam_roll(State *s) {
Expand Down
53 changes: 53 additions & 0 deletions src/sk_weapon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#include <stdio.h>
#include <string.h>
#include <sk_weapon.h>
#include <sk_gametypes.h>

#define FILEPATH_BUFFER_MAX_SIZE 64

void sk_weapon_create(Player *p, sk_weapon_kind kind) {
char filepath[FILEPATH_BUFFER_MAX_SIZE] = {0};
sprintf(filepath, "assets/models/%s.glb", sk_weapon_kinds[kind]);
p->weapon.model = LoadModel(filepath);
p->weapon.model_anims = LoadModelAnimations(filepath,
(int *) &p->weapon.model_anims_count);
p->weapon.model_anim_frame_count = 0;
memset(filepath, 0, sizeof(filepath));
sprintf(filepath, "assets/sounds/%s/shoot.wav", sk_weapon_kinds[kind]);
p->weapon.sound_shoot = LoadSound(filepath);
}

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

void sk_weapon_shoot(sk_weapon *w) {
++w->model_anim_frame_count;
UpdateModelAnimation(w->model, w->model_anims[0], w->model_anim_frame_count);
if (w->model_anim_frame_count >= w->model_anims[0].frameCount) w->model_anim_frame_count = 0;
PlaySound(w->sound_shoot);
}

0 comments on commit d2a35ec

Please sign in to comment.