diff --git a/include/sk_player.h b/include/sk_player.h index 35e4557..34413e0 100644 --- a/include/sk_player.h +++ b/include/sk_player.h @@ -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; diff --git a/include/sk_uuid.h b/include/sk_uuid.h new file mode 100644 index 0000000..ed1f352 --- /dev/null +++ b/include/sk_uuid.h @@ -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 . + */ + + +#pragma once + +#define SK_UUID_LEN 36 + +typedef struct { + char value[SK_UUID_LEN + 1]; +} sk_uuid; + +sk_uuid sk_uuid_gen(void); diff --git a/include/sk_weapon.h b/include/sk_weapon.h index 2c1cc5c..fe2ee30 100644 --- a/include/sk_weapon.h +++ b/include/sk_weapon.h @@ -22,6 +22,7 @@ #pragma once #include +#include #include typedef enum { @@ -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" }; @@ -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); diff --git a/src/sk_player.c b/src/sk_player.c index 8412fc9..84f0d2d 100644 --- a/src/sk_player.c +++ b/src/sk_player.c @@ -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, diff --git a/src/sk_scene_gameplay.c b/src/sk_scene_gameplay.c index d2cb807..6a4aa4d 100644 --- a/src/sk_scene_gameplay.c +++ b/src/sk_scene_gameplay.c @@ -19,10 +19,22 @@ */ +#include #include 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)); diff --git a/src/sk_uuid.c b/src/sk_uuid.c new file mode 100644 index 0000000..bfeba94 --- /dev/null +++ b/src/sk_uuid.c @@ -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 . + */ + + +#include +#include +#include + +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; +} diff --git a/src/sk_weapon.c b/src/sk_weapon.c index d894e6f..7e260d1 100644 --- a/src/sk_weapon.c +++ b/src/sk_weapon.c @@ -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) {