Skeleton of the multiplayer architecture
parent
e5cd725fc8
commit
c27090fc53
@ -0,0 +1,29 @@
|
||||
#include "types.h"
|
||||
|
||||
// do not use any global variables to process gamestate
|
||||
|
||||
|
||||
static void process_body(struct Body *body, float dt)
|
||||
{
|
||||
V2 current = body->position;
|
||||
body->position = V2add(body->position, V2sub(current, body->old_position));
|
||||
body->position = V2add(body->position, V2scale(body->acceleration, dt*dt));
|
||||
body->old_position = current;
|
||||
}
|
||||
|
||||
|
||||
void process(struct GameState * gs, float dt) {
|
||||
for(int i = 0; i < MAX_PLAYERS; i++)
|
||||
{
|
||||
struct Player * p = &gs->players[i];
|
||||
if(!p->connected)
|
||||
continue;
|
||||
p->body.acceleration = V2scale(p->input, 5.0f);
|
||||
process_body(&p->body, dt);
|
||||
}
|
||||
|
||||
for(int i = 0; i < gs->num_boxes; i++)
|
||||
{
|
||||
process_body(&gs->boxes[i].body, dt);
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
#include <winsock.h>
|
||||
|
||||
// started in a thread from host
|
||||
void server() {
|
||||
// SOCKET s = socket();
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
|
||||
// including headers from headers bad
|
||||
#ifndef SOKOL_GP_INCLUDED
|
||||
|
||||
typedef struct sgp_vec2
|
||||
{
|
||||
float x, y;
|
||||
} sgp_vec2;
|
||||
|
||||
typedef sgp_vec2 sgp_point;
|
||||
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifndef _STDBOOL
|
||||
|
||||
#define bool _Bool
|
||||
#define false 0
|
||||
#define true 1
|
||||
|
||||
#endif
|
||||
|
||||
typedef sgp_vec2 V2;
|
||||
typedef sgp_point P2;
|
||||
|
||||
#define MAX_BOXES 32
|
||||
#define MAX_PLAYERS 2
|
||||
#define BOX_SIZE 0.5f
|
||||
#define TIMESTEP 1.0f / 60.0f
|
||||
|
||||
struct Body
|
||||
{
|
||||
P2 position;
|
||||
P2 old_position;
|
||||
V2 acceleration;
|
||||
};
|
||||
|
||||
struct Player
|
||||
{
|
||||
struct Body body;
|
||||
bool connected;
|
||||
V2 input;
|
||||
};
|
||||
|
||||
struct GameState
|
||||
{
|
||||
struct Player players[MAX_PLAYERS];
|
||||
|
||||
int num_boxes;
|
||||
struct Box
|
||||
{
|
||||
struct Body body;
|
||||
} boxes[MAX_BOXES];
|
||||
};
|
||||
|
||||
struct ServerToClient
|
||||
{
|
||||
struct GameState cur_gs;
|
||||
int your_player;
|
||||
};
|
||||
|
||||
struct ClientToServer
|
||||
{
|
||||
V2 input;
|
||||
};
|
||||
|
||||
// server
|
||||
void server();
|
||||
|
||||
// gamestate
|
||||
void process(struct GameState * gs, float dt); // does in place
|
||||
|
||||
|
||||
// all the math is static so that it can be defined in each compilation unit its included in
|
||||
|
||||
static V2 V2add(V2 a, V2 b)
|
||||
{
|
||||
return (V2){
|
||||
.x = a.x + b.x,
|
||||
.y = a.y + b.y,
|
||||
};
|
||||
}
|
||||
|
||||
static V2 V2scale(V2 a, float f)
|
||||
{
|
||||
return (V2){
|
||||
.x = a.x * f,
|
||||
.y = a.y * f,
|
||||
};
|
||||
}
|
||||
|
||||
static V2 V2sub(V2 a, V2 b)
|
||||
{
|
||||
return (V2){
|
||||
.x = a.x - b.x,
|
||||
.y = a.y - b.y,
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue