Simulation now (sort of) respects the match-state
This commit is contained in:
@@ -210,6 +210,8 @@ static void state_render(Presentation_State *state) {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: SS - Switch on 'sim_world->match_state'.
|
||||
|
||||
{ // TEMP: SS - Render match state.
|
||||
char buf[512];
|
||||
snprintf(&buf[0], sizeof(buf), "Match-state: %i. Tick: %lu.", sim_world->match_state, sim_world->tick);
|
||||
|
||||
@@ -196,9 +196,14 @@ bool game_session_enqueue_player_input(Game_Session *session, uint16_t player_in
|
||||
assert(player != NULL);
|
||||
|
||||
if(simulation_input_equal(player->most_recent_input, input)) {
|
||||
// Ignore 'input' if it's equal to the most recent input. This way we avoid duplicates.
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: SS - Check if 'input' is a valid "snake-move".
|
||||
// Inputting right when going left will sadly be "okay" here which will make the game
|
||||
// feel less responsive.
|
||||
|
||||
if(!squeue_push(&player->input_queue, (void *)&input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -49,14 +49,14 @@ void simulation_world_tick(Simulation_World *simulation_world) {
|
||||
|
||||
for(uint16_t i = 0; i < simulation_world->command_count; i++) {
|
||||
Simulation_Command *cmd = &simulation_world->commands[i];
|
||||
printf("Command type: %i, player id: %i.\n", cmd->type, cmd->player_id);
|
||||
// printf("Command type: %i, player id: %i.\n", cmd->type, cmd->player_id);
|
||||
|
||||
Simulation_Player *player = &simulation_world->players[cmd->player_id];
|
||||
assert(player != NULL);
|
||||
|
||||
switch(cmd->type) {
|
||||
case Simulation_Command_Type_Add_Player: {
|
||||
printf("Simulation_Command_Type_Add_Player\n");
|
||||
// printf("Simulation_Command_Type_Add_Player\n");
|
||||
assert(!player->active);
|
||||
player->active = true;
|
||||
|
||||
@@ -76,7 +76,7 @@ void simulation_world_tick(Simulation_World *simulation_world) {
|
||||
break;
|
||||
}
|
||||
case Simulation_Command_Type_Remove_Player: {
|
||||
printf("Simulation_Command_Type_Remove_Player\n");
|
||||
// printf("Simulation_Command_Type_Remove_Player\n");
|
||||
assert(player->active);
|
||||
player->active = false;
|
||||
|
||||
@@ -85,7 +85,7 @@ void simulation_world_tick(Simulation_World *simulation_world) {
|
||||
break;
|
||||
}
|
||||
case Simulation_Command_Type_Set_Player_Input: {
|
||||
printf("Simulation_Command_Type_Set_Player_Input\n");
|
||||
// printf("Simulation_Command_Type_Set_Player_Input\n");
|
||||
assert(player->active);
|
||||
player->input = cmd->player_input;
|
||||
|
||||
@@ -95,6 +95,33 @@ void simulation_world_tick(Simulation_World *simulation_world) {
|
||||
}
|
||||
simulation_world->command_count = 0;
|
||||
|
||||
switch(simulation_world->match_state) {
|
||||
case Simulation_Match_State_Waiting_To_Start: {
|
||||
bool is_singleplayer = simulation_world->max_players == 1;
|
||||
|
||||
if(is_singleplayer) {
|
||||
simulation_world->match_state = Simulation_Match_State_Counting_Down;
|
||||
}
|
||||
else {
|
||||
if(simulation_world_amount_of_active_players(simulation_world) > 1) {
|
||||
simulation_world->match_state = Simulation_Match_State_Counting_Down;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case Simulation_Match_State_Counting_Down: {
|
||||
if(simulation_world->tick > 50) { // TEMP: SS - Hardcoded number and condition to break out of this state.
|
||||
simulation_world->match_state = Simulation_Match_State_Active;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case Simulation_Match_State_Active: {
|
||||
if(simulation_world->tick > 500) { // TEMP: SS - Hardcoded number and condition to break out of this state.
|
||||
simulation_world->match_state = Simulation_Match_State_Ended;
|
||||
}
|
||||
else {
|
||||
|
||||
Game_World *gw = simulation_world->game_world;
|
||||
|
||||
@@ -105,10 +132,10 @@ void simulation_world_tick(Simulation_World *simulation_world) {
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("* Input for player %i - up: %i, down: %i, right: %i, left: %i.\n",
|
||||
i,
|
||||
player->input.up, player->input.down, player->input.right, player->input.left
|
||||
);
|
||||
// printf("* Input for player %i - up: %i, down: %i, right: %i, left: %i.\n",
|
||||
// i,
|
||||
// player->input.up, player->input.down, player->input.right, player->input.left
|
||||
// );
|
||||
|
||||
Entity *player_entity = game_world_try_get_entity_by_id(gw, player->entity_id);
|
||||
assert(player_entity != NULL);
|
||||
@@ -116,6 +143,9 @@ void simulation_world_tick(Simulation_World *simulation_world) {
|
||||
// Snakes can't go backwards. They have to turn.
|
||||
|
||||
switch(player_entity->move_direction) {
|
||||
case Entity_Movement_Direction_None: {
|
||||
break;
|
||||
}
|
||||
case Entity_Movement_Direction_Up:
|
||||
case Entity_Movement_Direction_Down: // Snakes can only go right/left if it's currently moving up/down.
|
||||
{
|
||||
@@ -141,11 +171,9 @@ void simulation_world_tick(Simulation_World *simulation_world) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// printf("Entity %i ~ x: %u, y: %u. Move-dir: %i.\n", player->entity_id, player_entity->x, player_entity->y, player_entity->move_direction);
|
||||
}
|
||||
|
||||
// TODO: SS - Game-logic! :)
|
||||
// Game-logic! :)
|
||||
|
||||
for(uint16_t i = 0; i < gw->max_entities; i++) {
|
||||
Entity *entity = &gw->entities[i];
|
||||
@@ -153,8 +181,6 @@ void simulation_world_tick(Simulation_World *simulation_world) {
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("Ticking entity %i (x: %u, y: %u).\n", i, entity->x, entity->y);
|
||||
|
||||
{ // Move entities.
|
||||
int16_t dx = 0;
|
||||
int16_t dy = 0;
|
||||
@@ -183,22 +209,21 @@ void simulation_world_tick(Simulation_World *simulation_world) {
|
||||
|
||||
if(dx != 0 || dy != 0) {
|
||||
// Try moving.
|
||||
printf("Trying to move %i.\n", i);
|
||||
|
||||
// Figure out what cell we're on and what cell we want to go to.
|
||||
Grid_Cell *current_cell = grid_get_cell(&gw->grid, entity->x, entity->y);
|
||||
assert(current_cell != NULL);
|
||||
Grid_Cell *target_cell = grid_get_cell(&gw->grid, entity->x + dx, entity->y + dy);
|
||||
|
||||
{ // Debug-logging.
|
||||
printf("Current cell = (x: %u, y: %u, entity: %p).\n", current_cell->x, current_cell->y, current_cell->entity);
|
||||
if(target_cell != NULL) {
|
||||
printf("Target cell = (x: %u, y: %u, entity: %p).\n", target_cell->x, target_cell->y, target_cell->entity);
|
||||
}
|
||||
else {
|
||||
printf("Target cell = NULL!\n");
|
||||
}
|
||||
}
|
||||
// { // Debug-logging.
|
||||
// printf("Current cell = (x: %u, y: %u, entity: %p).\n", current_cell->x, current_cell->y, current_cell->entity);
|
||||
// if(target_cell != NULL) {
|
||||
// printf("Target cell = (x: %u, y: %u, entity: %p).\n", target_cell->x, target_cell->y, target_cell->entity);
|
||||
// }
|
||||
// else {
|
||||
// printf("Target cell = NULL!\n");
|
||||
// }
|
||||
// }
|
||||
|
||||
if(target_cell == NULL) {
|
||||
// Target cell does not exist.
|
||||
@@ -232,8 +257,18 @@ void simulation_world_tick(Simulation_World *simulation_world) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
simulation_world->tick += 1;
|
||||
break;
|
||||
}
|
||||
case Simulation_Match_State_Ended: {
|
||||
printf("Match over!\n");
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
simulation_world->tick += 1; // TODO: SS - Stop ticking if match has ended.
|
||||
}
|
||||
|
||||
bool simulation_world_enqueue_command(Simulation_World *simulation_world, Simulation_Command command) {
|
||||
@@ -248,3 +283,18 @@ bool simulation_world_enqueue_command(Simulation_World *simulation_world, Simula
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t simulation_world_amount_of_active_players(Simulation_World *simulation_world) {
|
||||
assert(simulation_world != NULL);
|
||||
|
||||
uint16_t count = 0;
|
||||
|
||||
for(uint16_t i = 0; i < simulation_world->max_players; i++) {
|
||||
Simulation_Player *player = &simulation_world->players[i];
|
||||
if(player->active) {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
@@ -36,4 +36,6 @@ void simulation_world_tick(Simulation_World *simulation_world);
|
||||
|
||||
bool simulation_world_enqueue_command(Simulation_World *simulation_world, Simulation_Command command);
|
||||
|
||||
uint16_t simulation_world_amount_of_active_players(Simulation_World *simulation_world);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user