Skip to content

Commit

Permalink
Abstracting away the game state, scenes, and lobbies
Browse files Browse the repository at this point in the history
WIP, not finished yet.
  • Loading branch information
iWas-Coder committed Mar 31, 2024
1 parent 62dbb68 commit 7dccfba
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 43 deletions.
41 changes: 41 additions & 0 deletions include/sk_lobby.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 <sk_player.h>
#include <sk_defines.h>

#define SK_LOBBY_MAX_PLAYERS 10

typedef struct {
u8 id;
u8 players_count;
sk_player players[SK_LOBBY_MAX_PLAYERS];
} sk_lobby;

sk_lobby sk_lobby_create(u8 id);

void sk_lobby_destroy(sk_lobby *l);

u8 sk_lobby_add(sk_lobby *l, sk_player p);

u8 sk_lobby_kick(sk_lobby *l, i8 p_idx);
6 changes: 3 additions & 3 deletions include/sk_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
#pragma once

#include <raylib.h>
#include <sk_gametypes.h>
#include <sk_state.h>

#define sk_renderer_loop while (!WindowShouldClose())

void sk_renderer_create(void);

void sk_renderer_destroy(void);

void sk_renderer_update(State *s);
void sk_renderer_update(sk_state *s);

void sk_renderer_draw(State *s);
void sk_renderer_draw(sk_state *s);
16 changes: 6 additions & 10 deletions include/sk_gametypes.h → include/sk_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@

#pragma once

#include <sk_player.h>

typedef enum {
SCENE_MAIN_MENU,
SCENE_GAMEPLAY
} Scene;
SK_SCENE_KIND_MAIN_MENU,
SK_SCENE_KIND_GAMEPLAY
} sk_scene_kind;

typedef struct State {
Scene current_scene;
sk_player player;
u8 is_online;
} State;
typedef struct {
sk_scene_kind kind;
} sk_scene;
4 changes: 2 additions & 2 deletions include/sk_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#define SK_SERVER_PORT 27015
#define SK_SERVER_TICK_RATE 128
#define SK_SERVER_MSG_MAX_SIZE 1024
#define SK_SERVER_MSG_CONN_REQ "ping::" SK_SERVER_NAME
#define SK_SERVER_MSG_CONN_RES "pong::" SK_SERVER_NAME
#define SK_SERVER_MSG_CONN_REQ SK_SERVER_NAME "::ping"
#define SK_SERVER_MSG_CONN_RES SK_SERVER_NAME "::pong::%u/%u"

u8 sk_server_run(void);
45 changes: 45 additions & 0 deletions include/sk_state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 <sk_scene.h>
#include <sk_lobby.h>

#define SK_STATE_MAX_LOBBIES 256

typedef struct {
u8 is_online;
sk_scene curr_scene;
union {
struct {
u8 lobbies_count;
sk_lobby lobbies[SK_STATE_MAX_LOBBIES];
};
struct {
sk_player player;
};
};
} sk_state;

sk_state sk_state_create(u8 is_online);

void sk_state_destroy(sk_state *s);
31 changes: 17 additions & 14 deletions src/sk_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@
#include <string.h>
#include <unistd.h>
#include <raylib.h>
#include <sk_state.h>
#include <arpa/inet.h>
#include <sk_config.h>
#include <sk_client.h>
#include <sk_server.h>
#include <sys/socket.h>
#include <sk_renderer.h>
#include <sk_gametypes.h>

static State state = {0};

u8 sk_client_run(const char *ip) {
TraceLog(LOG_INFO, "Initializing %s", SK_CLIENT_NAME);
u8 is_online = 0;
int sock_fd;
if (!ip) TraceLog(LOG_WARNING, "Running in offline mode");
else {
Expand Down Expand Up @@ -68,9 +67,9 @@ u8 sk_client_run(const char *ip) {
}
pong_msg[pong_msg_n] = 0;
if (!strcmp(pong_msg, SK_SERVER_MSG_CONN_RES)) {
state.is_online = 1;
is_online = 1;
}
if (!state.is_online) {
if (!is_online) {
TraceLog(LOG_ERROR, "Unable to communicate with `%s`. Exiting...", ip);
close(sock_fd);
return 1;
Expand All @@ -80,16 +79,20 @@ u8 sk_client_run(const char *ip) {
if (!ChangeDirectory(GetApplicationDirectory())) {
TraceLog(LOG_WARNING, "Could not change CWD to the game's root directory");
}
sk_renderer_create();
state.current_scene = SCENE_MAIN_MENU;
state.player = sk_player_create(SK_PLAYER_KIND_JETT);
sk_renderer_loop {
sk_renderer_update(&state);
sk_renderer_draw(&state);
sk_state state = sk_state_create(is_online);
if (!is_online) {
sk_renderer_create();
sk_renderer_loop {
sk_renderer_update(&state);
sk_renderer_draw(&state);
}
sk_player_destroy(&state.player);
sk_renderer_destroy();
}
else {
TraceLog(LOG_WARNING, "Not implemented yet");
}
sk_player_destroy(&state.player);
sk_renderer_destroy();
if (state.is_online) close(sock_fd);
if (is_online) close(sock_fd);
TraceLog(LOG_INFO, "%s closed successfully", SK_CLIENT_NAME);
return 0;
}
76 changes: 76 additions & 0 deletions src/sk_lobby.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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 <sk_lobby.h>

sk_lobby sk_lobby_create(u8 id) {
sk_lobby l = {
.id = id,
.players_count = 0
};
for (u8 i = 0; i < SK_LOBBY_MAX_PLAYERS; ++i) {
l.players[i] = (sk_player) {
.lobby_id = -1,
.lobby_slot_idx = -1
};
}
return l;
}

void sk_lobby_destroy(sk_lobby *l) {
(void) l;
}

u8 sk_lobby_add(sk_lobby *l, sk_player p) {
if (l->players_count == SK_LOBBY_MAX_PLAYERS) {
TraceLog(LOG_DEBUG, "Lobby %u :: unable to add more players (full)", l->id);
return false;
}
for (u8 i = 0; i < SK_LOBBY_MAX_PLAYERS; ++i) {
if (l->players[i].lobby_id != -1 &&
l->players[i].lobby_slot_idx != -1 ) continue;
p.lobby_id = l->id;
p.lobby_slot_idx = i;
l->players[i] = p;
TraceLog(LOG_INFO, "Lobby %u :: added new player to slot %u/%u",
l->id,
i + 1,
SK_LOBBY_MAX_PLAYERS);
}
++l->players_count;
return true;
}

u8 sk_lobby_kick(sk_lobby *l, i8 p_idx) {
if (p_idx >= l->players_count ||
l->players[p_idx].lobby_id != l->id ||
l->players[p_idx].lobby_slot_idx != p_idx) {
TraceLog(LOG_DEBUG, "Lobby %u :: unable to kick non-existant player", l->id);
return false;
}
l->players[p_idx].lobby_id = -1;
l->players[p_idx].lobby_slot_idx = -1;
l->players[p_idx] = (sk_player) {
.lobby_id = -1,
.lobby_slot_idx = -1
};
return true;
}
12 changes: 6 additions & 6 deletions src/sk_renderer_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static inline void __draw_walls(void) {
DrawCube((Vector3) { 0, 2.5f, 16 }, 32, 5, 1, GOLD);
}

static void __draw_gameplay_world(State *s) {
static void __draw_gameplay_world(sk_state *s) {
ClearBackground(SKYBLUE);
BeginMode3D(s->player.camera);
sk_player_draw(&s->player);
Expand Down Expand Up @@ -101,7 +101,7 @@ static inline void __draw_crosshair(void) {
WHITE);
}

static void __draw_gameplay_hud(State *s) {
static void __draw_gameplay_hud(sk_state *s) {
__draw_fps();
__draw_frametime();
if (s->is_online) {
Expand All @@ -111,13 +111,13 @@ static void __draw_gameplay_hud(State *s) {
__draw_crosshair();
}

void sk_renderer_draw(State *s) {
void sk_renderer_draw(sk_state *s) {
BeginDrawing();
switch (s->current_scene) {
case SCENE_MAIN_MENU:
switch (s->curr_scene.kind) {
case SK_SCENE_KIND_MAIN_MENU:
__draw_main_menu();
break;
case SCENE_GAMEPLAY:
case SK_SCENE_KIND_GAMEPLAY:
__draw_gameplay_world(s);
__draw_gameplay_hud(s);
break;
Expand Down
12 changes: 6 additions & 6 deletions src/sk_renderer_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@
#include <sk_defines.h>
#include <sk_renderer.h>

static void __update_main_menu(State *s) {
static void __update_main_menu(sk_state *s) {
if (IsKeyPressed(KEY_ENTER)) {
s->current_scene = SCENE_GAMEPLAY;
s->curr_scene.kind = SK_SCENE_KIND_GAMEPLAY;
sk_player_load(&s->player, SK_WEAPON_KIND_7MM);
DisableCursor();
}
}

void sk_renderer_update(State *s) {
switch (s->current_scene) {
case SCENE_MAIN_MENU:
void sk_renderer_update(sk_state *s) {
switch (s->curr_scene.kind) {
case SK_SCENE_KIND_MAIN_MENU:
__update_main_menu(s);
break;
case SCENE_GAMEPLAY:
case SK_SCENE_KIND_GAMEPLAY:
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) sk_weapon_shoot(&s->player.weapon);
sk_player_jump(&s->player);
sk_player_move(&s->player, sk_player_peek(&s->player));
Expand Down
22 changes: 22 additions & 0 deletions src/sk_scene.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 <sk_scene.h>
10 changes: 8 additions & 2 deletions src/sk_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/


#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
Expand Down Expand Up @@ -98,8 +99,14 @@ u8 sk_server_run(void) {
msg[msg_n] = 0;
if (!strcmp(msg, SK_SERVER_MSG_CONN_REQ)) {
TraceLog(LOG_INFO, "Connection from client (%s:%d) requested", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// TODO: check if there is a free slot in a lobby
u8 lobby_id = 0;
u8 lobby_slot_id = 0;
// TODO: assign him a lobby and a player slot inside it
memset(msg, 0, sizeof(msg));
sprintf(msg, SK_SERVER_MSG_CONN_RES, lobby_id, lobby_slot_id);
if (sendto(sock_fd,
SK_SERVER_MSG_CONN_RES,
msg,
strlen(SK_SERVER_MSG_CONN_RES),
0,
(struct sockaddr *) &client_addr,
Expand All @@ -108,7 +115,6 @@ u8 sk_server_run(void) {
continue;
}
TraceLog(LOG_INFO, "Connection from client (%s:%d) accepted", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// TODO: assign him a lobby with a free slot to be in
}
wait_next_tick(dt);
}
Expand Down
Loading

0 comments on commit 7dccfba

Please sign in to comment.