More progress.
This commit is contained in:
@@ -4,6 +4,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
Game_Session *g_current_session = NULL;
|
||||
|
||||
@@ -22,25 +25,127 @@ void game_session_create(bool is_singleplayer, bool is_host, Game_Session_Settin
|
||||
session->settings.level_height
|
||||
);
|
||||
|
||||
session->players = (Game_Session_Player *)calloc(session->settings.max_players, sizeof(Game_Session_Player));
|
||||
session->players_prev = (Game_Session_Player *)calloc(session->settings.max_players, sizeof(Game_Session_Player));
|
||||
|
||||
{ // TEMP
|
||||
session->local_player_index = 0;
|
||||
assert(game_session_create_player(session, &session->local_player_index));
|
||||
}
|
||||
|
||||
g_current_session = session;
|
||||
|
||||
printf("New Game_Session created.\n");
|
||||
}
|
||||
|
||||
void game_session_destroy() {
|
||||
if(g_current_session == NULL) {
|
||||
Game_Session *session = g_current_session;
|
||||
|
||||
if(session == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
simulation_destroy_world(&g_current_session->simulation_world);
|
||||
simulation_destroy_world(&session->simulation_world);
|
||||
|
||||
free(g_current_session);
|
||||
free(session->players);
|
||||
session->players = NULL;
|
||||
free(session->players_prev);
|
||||
session->players_prev = NULL;
|
||||
|
||||
free(session);
|
||||
g_current_session = NULL;
|
||||
}
|
||||
|
||||
void game_session_init_default_settings(bool is_singleplayer, Game_Session_Settings *out_settings) {
|
||||
out_settings->seed = 1337; // TODO: SS - Randomize.
|
||||
out_settings->level_width = 48;
|
||||
out_settings->level_height = 48;
|
||||
out_settings->level_width = 32;
|
||||
out_settings->level_height = 32;
|
||||
out_settings->max_players = is_singleplayer ? 1 : 8;
|
||||
}
|
||||
|
||||
void game_session_tick(Game_Session *session) {
|
||||
Game_Session_Player *local_session_player = game_session_get_local_player(session);
|
||||
assert(local_session_player != NULL);
|
||||
local_session_player->input = (Simulation_Game_Input) { // TODO: SS - Move this somewhere else, maybe.
|
||||
.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)
|
||||
};
|
||||
|
||||
{ // TODO: SS - Use delta-time to accumulate time. Tick the simulation in a constant tick-rate.
|
||||
// Update input.
|
||||
for(uint16_t i = 0; i < session->settings.max_players; i++) {
|
||||
Game_Session_Player *session_player_prev = &session->players_prev[i];
|
||||
Game_Session_Player *session_player = &session->players[i];
|
||||
|
||||
bool was_active = session_player_prev->active;
|
||||
bool is_active = session_player->active;
|
||||
|
||||
if (!was_active && is_active) {
|
||||
simulation_world_enqueue_command(&session->simulation_world, (Simulation_Command) {
|
||||
.type = Simulation_Command_Type_Add_Player,
|
||||
.player_id = i
|
||||
});
|
||||
} else if (was_active && !is_active) {
|
||||
simulation_world_enqueue_command(&session->simulation_world, (Simulation_Command) {
|
||||
.type = Simulation_Command_Type_Remove_Player,
|
||||
.player_id = i
|
||||
});
|
||||
}
|
||||
|
||||
if(!session_player->active) {
|
||||
continue;
|
||||
}
|
||||
|
||||
simulation_world_enqueue_command(&session->simulation_world, (Simulation_Command) {
|
||||
.type = Simulation_Command_Type_Set_Player_Input,
|
||||
.player_id = i,
|
||||
.player_input = session_player->input,
|
||||
});
|
||||
}
|
||||
|
||||
// Tick.
|
||||
simulation_world_tick(&session->simulation_world);
|
||||
|
||||
// Copy 'players' from session to 'players_prev'.
|
||||
memcpy(session->players_prev, session->players, sizeof(Game_Session_Player) * session->settings.max_players);
|
||||
}
|
||||
}
|
||||
|
||||
Game_Session_Player *game_session_get_local_player(Game_Session *session) {
|
||||
assert(session != NULL);
|
||||
return &session->players[session->local_player_index];
|
||||
}
|
||||
|
||||
bool game_session_create_player(Game_Session *session, uint16_t *out_player_index) {
|
||||
assert(session != NULL);
|
||||
|
||||
int32_t found_index = -1;
|
||||
for(uint16_t i = 0; i < session->settings.max_players; i++) {
|
||||
Game_Session_Player *session_player = &session->players[i];
|
||||
assert(session_player != NULL);
|
||||
if(!session_player->active) {
|
||||
session_player->active = true;
|
||||
found_index = i;
|
||||
}
|
||||
}
|
||||
|
||||
if(found_index < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*out_player_index = (uint16_t)found_index;
|
||||
return true;
|
||||
}
|
||||
|
||||
void game_session_destroy_player(Game_Session *session, uint16_t player_index) {
|
||||
assert(session != NULL);
|
||||
assert(player_index < session->settings.max_players);
|
||||
|
||||
Game_Session_Player *player = &session->players[player_index];
|
||||
assert(player != NULL);
|
||||
assert(player->active);
|
||||
|
||||
memset(player, 0, sizeof(Game_Session_Player));
|
||||
}
|
||||
Reference in New Issue
Block a user