Skip to content

Commit

Permalink
Created sk_shot DS and implemented sk_uuid module
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Apr 24, 2024
1 parent f9e0ba8 commit 7b79057
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/sk_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef enum {
} sk_player_kind;

typedef struct sk_player {
sk_uuid id;
i8 lobby_id;
i8 lobby_slot_idx;
sk_player_kind kind;
Expand Down
30 changes: 30 additions & 0 deletions include/sk_uuid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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

#define SK_UUID_LEN 36

typedef struct {
char value[SK_UUID_LEN + 1];
} sk_uuid;

sk_uuid sk_uuid_gen(void);
9 changes: 8 additions & 1 deletion include/sk_weapon.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#pragma once

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

typedef enum {
Expand All @@ -44,6 +45,12 @@ typedef struct {
sk_weapon_ammo_spec ammo;
} sk_weapon;

typedef struct {
Ray bullet;
sk_uuid shooter_id;
sk_weapon_kind weapon_kind;
} sk_shot;

static const char * const sk_weapon_kind2name[] = {
[SK_WEAPON_KIND_7MM] = "7mm"
};
Expand All @@ -58,6 +65,6 @@ void sk_weapon_destroy(sk_weapon *w);

void sk_weapon_draw(sk_weapon *w, Camera3D *cam, Vector3 offset, f32 scale);

void sk_weapon_shoot(sk_weapon *w);
u8 sk_weapon_shoot(sk_weapon *w, sk_uuid id, Camera3D *cam, sk_shot *shot);

void sk_weapon_reload(sk_weapon *w);
1 change: 1 addition & 0 deletions src/sk_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sk_player sk_player_create(i8 lobby_id, i8 lobby_slot_idx, sk_player_kind kind,
assert(lobby_id != -1);
assert(lobby_slot_idx != -1);
return (sk_player) {
.id = sk_uuid_gen(),
.lobby_id = lobby_id,
.lobby_slot_idx = lobby_slot_idx,
.kind = kind,
Expand Down
14 changes: 13 additions & 1 deletion src/sk_scene_gameplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,22 @@
*/


#include <sk_log.h>
#include <sk_scene_gameplay.h>

void sk_scene_gameplay_update(sk_state *s) {
if (IsMouseButtonPressed(s->config.controls.shoot)) sk_weapon_shoot(&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_LOG_DEBUG("sk_shot :: {");
SK_LOG_DEBUG(" .bullet = {");
SK_LOG_DEBUG(" .position = (%f, %f, %f),", shot.bullet.position.x, shot.bullet.position.y, shot.bullet.position.z);
SK_LOG_DEBUG(" .direction = (%f, %f, %f)", shot.bullet.direction.x, shot.bullet.direction.y, shot.bullet.direction.z);
SK_LOG_DEBUG(" },");
SK_LOG_DEBUG(" .shooter_id = %s,", shot.shooter_id.value);
SK_LOG_DEBUG(" .weapon_kind = %d", shot.weapon_kind);
SK_LOG_DEBUG("};");
}
if (IsKeyPressed(s->config.controls.reload)) sk_weapon_reload(&s->player.weapon);
sk_player_jump(&s->player, &s->config);
sk_player_move(&s->player, &s->config, sk_player_peek(&s->player, &s->config));
Expand Down
40 changes: 40 additions & 0 deletions src/sk_uuid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 <raylib.h>
#include <sk_uuid.h>
#include <sk_defines.h>

static const char chars[] = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};

sk_uuid sk_uuid_gen(void) {
sk_uuid id = {0};
for (u8 i = 0; i < SK_UUID_LEN; ++i) {
if (i == 8 || i == 13 || i == 18 || i == 23) id.value[i] = '-';
else id.value[i] = chars[GetRandomValue(0, 15)];
}
return id;
}
8 changes: 6 additions & 2 deletions src/sk_weapon.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ void sk_weapon_draw(sk_weapon *w, Camera3D *cam, Vector3 offset, f32 scale) {
WHITE);
}

void sk_weapon_shoot(sk_weapon *w) {
if (!w->ammo.magazine || IsSoundPlaying(w->sound_reload)) return;
u8 sk_weapon_shoot(sk_weapon *w, sk_uuid player_id, Camera3D *cam, sk_shot *shot) {
if (!w->ammo.magazine || IsSoundPlaying(w->sound_reload)) return 0;
++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);
--w->ammo.magazine;
shot->bullet = GetMouseRay(GetMousePosition(), *cam);
shot->shooter_id = player_id;
shot->weapon_kind = w->kind;
return 1;
}

void sk_weapon_reload(sk_weapon *w) {
Expand Down

0 comments on commit 7b79057

Please sign in to comment.