109 lines
3.5 KiB
C
109 lines
3.5 KiB
C
|
#include "v4k.h"
|
||
|
|
||
|
enum { MAX_NPCS = 5 };
|
||
|
struct player_t {
|
||
|
uint64_t seen_until;
|
||
|
float x,y,z,angle;
|
||
|
uint32_t color;
|
||
|
};
|
||
|
struct npc_t {
|
||
|
float x,y,z;
|
||
|
uint32_t color;
|
||
|
};
|
||
|
struct world_t {
|
||
|
struct player_t player[MAX_CLIENTS];
|
||
|
struct npc_t npc[MAX_NPCS];
|
||
|
} world = {0};
|
||
|
|
||
|
void bind_netbuffers(int64_t self_id) {
|
||
|
uint32_t colors[] = { ORANGE,GREEN,RED,CYAN,PURPLE,YELLOW,GRAY,PINK,AQUA };
|
||
|
for (int64_t i=0; i<MAX_NPCS; ++i) {
|
||
|
// as an example, let only server to set initial pos
|
||
|
if (self_id==0) {
|
||
|
world.npc[i].x = i*3.f + 4.f;
|
||
|
}
|
||
|
world.npc[i].color = colors[i%(sizeof colors / sizeof colors[0])];
|
||
|
network_buffer(&world.npc[i], sizeof(struct npc_t), NETWORK_RECV, 0);
|
||
|
}
|
||
|
|
||
|
for (int64_t i=0; i<MAX_CLIENTS; ++i) {
|
||
|
world.player[i].color = colors[i%(sizeof colors / sizeof colors[0])];
|
||
|
network_buffer(&world.player[i], sizeof(struct player_t), i!=self_id ? NETWORK_RECV : NETWORK_SEND, i /* each client owns exactly 1 buffer */);
|
||
|
};
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
// ifdef(win32, FreeConsole()); // tty_detach()
|
||
|
|
||
|
// network setup
|
||
|
network_create("127.0.0.1", 0, flag("--client") ? NETWORK_CONNECT : 0);
|
||
|
int64_t self_id = network_get(NETWORK_RANK);
|
||
|
bind_netbuffers(self_id);
|
||
|
|
||
|
// game setup
|
||
|
camera_t cam = camera();
|
||
|
window_create( 0.35f, WINDOW_MSAA8|WINDOW_SQUARE );
|
||
|
struct player_t *self = &world.player[self_id];
|
||
|
|
||
|
// game loop
|
||
|
while( window_swap() && !input(KEY_ESC) ) {
|
||
|
// network sync
|
||
|
char **event = network_sync(0); // timeout_ms:0
|
||
|
while(*event) printf( "network event: %s\n", *event++ );
|
||
|
|
||
|
self_id = network_get(NETWORK_RANK);
|
||
|
if (network_get(NETWORK_LIVE) == 0) {
|
||
|
network_create("127.0.0.1", 0, flag("--client") ? NETWORK_CONNECT|NETWORK_NOFAIL : 0);
|
||
|
self_id = network_get(NETWORK_RANK);
|
||
|
if (self_id != -1) {
|
||
|
bind_netbuffers(self_id);
|
||
|
}
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// camera tracking
|
||
|
cam.position = vec3(self->x,100,self->z);
|
||
|
camera_lookat(&cam, vec3(self->x,0,self->z));
|
||
|
|
||
|
// input - move player
|
||
|
float iy = input(KEY_UP) - input(KEY_DOWN);
|
||
|
float ix = input(KEY_RIGHT) - input(KEY_LEFT);
|
||
|
if( iy || ix ) {
|
||
|
self->x += iy*window_delta()*15;
|
||
|
self->z += ix*window_delta()*15;
|
||
|
}
|
||
|
self->seen_until = date_epoch() + 4;
|
||
|
|
||
|
// npc - update npc movement on server-side
|
||
|
if (self_id == 0) {
|
||
|
for (int i = 0; i < MAX_NPCS; ++i) {
|
||
|
struct npc_t *n = &world.npc[i];
|
||
|
n->z = sinf(window_time())*4.f;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// background - draw grid
|
||
|
ddraw_grid(0);
|
||
|
|
||
|
// foreground - draw all players
|
||
|
for( int id = 0; id < MAX_CLIENTS; ++id ) {
|
||
|
struct player_t *p = &world.player[id];
|
||
|
if (p->seen_until < date_epoch()) continue; /* skip inactive players */
|
||
|
ddraw_color( p->color );
|
||
|
ddraw_capsule(vec3(p->x,0,p->z), vec3(p->x,2,p->z), 1);
|
||
|
ddraw_text(vec3(p->x,4,p->z), 0.01, stringf("player #%d", id));
|
||
|
}
|
||
|
for( int id = 0; id < MAX_NPCS; ++id ) {
|
||
|
struct npc_t *p = &world.npc[id];
|
||
|
ddraw_color( p->color );
|
||
|
ddraw_capsule(vec3(p->x,0,p->z), vec3(p->x,2,p->z), 1);
|
||
|
ddraw_text(vec3(p->x,4,p->z), 0.01, stringf("npc #%d", id));
|
||
|
}
|
||
|
|
||
|
// stats
|
||
|
char title[64];
|
||
|
sprintf(title, "player #%lld", self_id);
|
||
|
window_title(title);
|
||
|
}
|
||
|
}
|