Slight reconstruction/abstraction. Getting close to connecting to other sessions etc.

This commit is contained in:
2025-12-14 20:22:29 +01:00
parent 11e91aa01c
commit e9080d7332
17 changed files with 490 additions and 217 deletions

View File

@@ -20,24 +20,6 @@ static void state_enter(Presentation_State *state) {
(void)ctx;
printf("Entered ingame.\n");
printf("Setting up session ...\n");
assert(g_current_session != NULL);
Game_Session_Settings *settings = &g_current_session->settings;
printf(
"Singleplayer? %i.\n"
"Settings:\n"
"- Seed: %u\n"
"- Level width: %u, height: %u\n"
"- Max players: %u\n"
,
g_current_session->is_singleplayer,
settings->seed,
settings->level_width, settings->level_height,
settings->max_players
);
printf("Setting up context ...\n");
ctx->main_camera = (Camera2D) {
@@ -65,23 +47,46 @@ static void state_tick(Presentation_State *state) {
Presentation_State_Ingame_Context *ctx = (Presentation_State_Ingame_Context *)state->context;
(void)ctx;
Game_Session *session = g_current_session;
Game_Instance *instance = ctx->game_instance;
assert(instance != NULL);
Game_Session *session = instance->game_session;
assert(session != NULL);
{ // Push local input to queue.
game_session_enqueue_player_input(session, session->local_player_index, (Simulation_Game_Input) {
Simulation_Game_Input input = (Simulation_Game_Input) { // NOTE: SS - This needs to be slightly modified to support N local players.
.up = IsKeyDown(KEY_UP) || IsKeyDown(KEY_W),
.down = IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S),
.right = IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D),
.left = IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)
});
};
if(!simulation_input_equal(input, ctx->prev_local_input)) {
game_instance_push_local_input(instance, input);
ctx->prev_local_input = input;
}
}
const double delta_time = GetFrameTime();
ctx->simulation_accumulator += delta_time;
const double sim_dt = 1.0f / session->settings.tickrate;
while (ctx->simulation_accumulator >= sim_dt) {
// Pop input from instance's local_input_queue and set the local player's input to it, if we have one.
Simulation_Game_Input input = {0};
if(game_instance_pop_local_input(instance, &input)) {
// TODO: SS - We should probably check if this input is invalid for the snake. (pressing "go right" when going left, for example.)
// If it is invalid, the next input in the queue should be tried.
game_session_set_player_input(session, instance->local_player_index, input);
}
game_session_tick(session);
ctx->simulation_accumulator -= sim_dt;
}
double delta_time = GetFrameTime();
game_session_update(session, delta_time);
{ // TEMP: SS
if(IsKeyPressed(KEY_TAB)) {
ctx->debug_render_match_state = !ctx->debug_render_match_state;
ctx->debug_draw_session_details = !ctx->debug_draw_session_details;
}
if(IsKeyPressed(KEY_ESCAPE)) {
@@ -102,7 +107,9 @@ static void state_render(Presentation_State *state) {
ClearBackground((Color) { 240, 236, 226, 255 });
Simulation_World *sim_world = &g_current_session->simulation_world;
Game_Instance *instance = ctx->game_instance;
Game_Session *session = instance->game_session;
Simulation_World *sim_world = &session->simulation_world;
assert(sim_world != NULL);
Game_World *world = sim_world->game_world;
@@ -222,7 +229,7 @@ static void state_render(Presentation_State *state) {
// TODO: SS - Don't draw player-name if playing by yourself.
// TODO: SS - Don't draw your own player-name, only others.
{ // Draw player-name.
const char *player_name = "mirakel"; // NOTE: SS - Hardcoded.
const char *player_name = "player"; // NOTE: SS - Hardcoded.
const uint32_t font_size = 8;
int text_width = MeasureText(player_name, font_size);
DrawText(player_name, pres_x - (float)text_width/2.0f + 4, pres_y - 16, font_size, (Color) { 255, 255, 255, 128 });
@@ -255,7 +262,7 @@ static void state_render(Presentation_State *state) {
},
(Rectangle) { // Destination.
pres_x + origin.x,
pres_y + origin.y + ENTITY_PRESENTATION_Y_OFFSET - ((sin(GetTime() * 12) + 1)/2) * 1,
pres_y + origin.y + ENTITY_PRESENTATION_Y_OFFSET - ((sin((GetTime() + (x + y)) * 12) + 1)/2) * 1,
GRID_CELL_SIZE,
GRID_CELL_SIZE
},
@@ -271,11 +278,33 @@ static void state_render(Presentation_State *state) {
// TODO: SS - Switch on 'sim_world->match_state'.
if(ctx->debug_render_match_state) {
char buf[512];
snprintf(&buf[0], sizeof(buf), "Match-state: %i. Tick: %lu.", sim_world->match_state, sim_world->tick);
if(ctx->debug_draw_session_details) {
char buf[1024];
snprintf(&buf[0], sizeof(buf),
"Match-state: %i\n"
"Tick: %lu\n"
"\n"
"Seed: %u\n"
"Level: %ux%u\n"
"Tickrate: %.2f\n"
"\n"
DrawText(buf, 16, 16, 8, RED);
// TODO: SS - This should only be shown when playing online. Other things should be shown in the cases below;
// When playing offline/singleplayer you can only be one player.
// When playing local-multiplayer, you control multiple players from the same computer/client/instance.
"Local player: %u (host? %s)\n"
"Player count: %u / %u\n"
,
sim_world->match_state, sim_world->tick,
sim_world->game_world->seed,
sim_world->game_world->grid.width, sim_world->game_world->grid.height,
session->settings.tickrate,
instance->local_player_index, instance->local_player_index == 0 ? "true" : "false", // NOTE: SS - You're the host if you're player 0.
game_session_get_amount_of_active_players(session), sim_world->max_players
);
DrawText(buf, 17, 17, 8, BLACK);
DrawText(buf, 16, 16, 8, WHITE);
}
}
EndMode2D();
@@ -292,7 +321,7 @@ static void state_exit(Presentation_State *state) {
UnloadTexture(ctx->texture_snake_head);
UnloadTexture(ctx->texture_snake_body);
game_session_destroy();
ctx->game_instance = NULL;
}
Presentation_State presentation_state_ingame;