simplify/reduce player spawn flow

isolation_bkp/dynres
Dominik Madarász 2021-05-05 09:55:45 +02:00
parent 2094d416c8
commit 3af10cd782
5 changed files with 44 additions and 13 deletions

View File

@ -11,6 +11,7 @@
#include "flecs/flecs_os_api_stdcpp.h"
static int8_t is_networked_play;
static uint64_t sp_player;
static WORLD_PKT_READER(pkt_reader) {
pkt_header header = {0};
@ -58,6 +59,8 @@ void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chu
ecs_set_target_fps(world_ecs(), 60);
}
sp_player = player_spawn("unnamed");
pkt_01_welcome table = {.block_size = block_size, .chunk_size = chunk_size, .world_size = world_size};
pkt_world_write(MSG_ID_01_WELCOME, pkt_01_welcome_encode(&table), 1, NULL);
}
@ -72,6 +75,7 @@ void game_shutdown() {
network_client_disconnect();
network_destroy();
} else {
player_despawn(sp_player);
world_destroy();
}
}

View File

@ -12,6 +12,8 @@
#include "packets/packet.h"
#include "world/world.h"
#include "player.h"
#include "modules/general.h"
#include "modules/controllers.h"
#include "modules/net.h"
@ -150,26 +152,18 @@ void network_server_update(void *data) {
}
uint64_t network_client_create(ENetPeer *peer) {
ECS_IMPORT(world_ecs(), General);
ECS_IMPORT(world_ecs(), Controllers);
ECS_IMPORT(world_ecs(), Net);
ecs_entity_t e = ecs_new(world_ecs(), 0);
ecs_entity_t e = (ecs_entity_t)player_spawn(zpl_bprintf("client_%d", peer->incomingPeerID));
ecs_add(world_ecs(), e, EcsClient);
ecs_set(world_ecs(), e, ClientInfo, {peer});
ecs_set(world_ecs(), e, EcsName, {.alloc_value = zpl_bprintf("client_%d", peer->incomingPeerID) });
ecs_set(world_ecs(), e, ClientInfo, {(uintptr_t)peer});
librg_entity_track(world_tracker(), e);
librg_entity_owner_set(world_tracker(), e, (int64_t)peer);
librg_entity_radius_set(world_tracker(), e, 2); /* 2 chunk radius visibility */
// librg_entity_chunk_set(world_tracker(), e, 1);
return (uint64_t)e;
}
void network_client_destroy(uint64_t ent_id) {
librg_entity_untrack(world_tracker(), ent_id);
ecs_delete(world_ecs(), ent_id);
player_despawn(ent_id);
}
static int32_t network_msg_send_raw(ENetPeer *peer, void *data, size_t datalen, uint32_t flags) {

View File

@ -0,0 +1,28 @@
#include "player.h"
#include "flecs/flecs.h"
#include "flecs/flecs_meta.h"
#include "librg.h"
#include "world.h"
#include "modules/general.h"
#include "modules/controllers.h"
uint64_t player_spawn(char *name) {
ECS_IMPORT(world_ecs(), General);
ECS_IMPORT(world_ecs(), Controllers);
ecs_entity_t e = ecs_new(world_ecs(), 0);
ecs_set(world_ecs(), e, EcsName, {.alloc_value = name });
librg_entity_track(world_tracker(), e);
librg_entity_owner_set(world_tracker(), e, (int64_t)e);
librg_entity_radius_set(world_tracker(), e, 2); /* 2 chunk radius visibility */
// librg_entity_chunk_set(world_tracker(), e, 1);
return (uint64_t)e;
}
void player_despawn(uint64_t ent_id) {
librg_entity_untrack(world_tracker(), ent_id);
ecs_delete(world_ecs(), ent_id);
}

View File

@ -0,0 +1,5 @@
#pragma once
#include "system.h"
uint64_t player_spawn(char *name);
void player_despawn(uint64_t ent_id);

View File

@ -3,7 +3,7 @@
#include "flecs/flecs_meta.h"
ECS_STRUCT(ClientInfo, {
uint16_t peer_id;
uintptr_t peer;
});
typedef struct {
@ -15,4 +15,4 @@ typedef struct {
ECS_IMPORT_ENTITY(handles, EcsClient);\
ECS_IMPORT_COMPONENT(handles, ClientInfo);\
void NetImport(ecs_world_t *ecs);
void NetImport(ecs_world_t *ecs);