eco2d/code/game/src/packets/pkt_00_init.c

46 lines
1.5 KiB
C
Raw Normal View History

2021-05-06 15:30:38 +00:00
#include "packets/pkt_00_init.h"
#include "packets/pkt_01_welcome.h"
#include "packet.h"
#include "world/world.h"
#include "game.h"
2021-09-08 14:12:38 +00:00
#include "network.h"
2021-05-06 15:30:38 +00:00
#include "entity_view.h"
#include "camera.h"
2021-05-10 06:28:56 +00:00
#include "player.h"
2021-05-06 15:30:38 +00:00
2021-07-27 12:43:26 +00:00
#include "modules/components.h"
#include "modules/systems.h"
2021-05-06 15:30:38 +00:00
pkt_desc pkt_00_init_desc[] = {
{ PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_00_init, view_id) },
{ PKT_END },
};
size_t pkt_00_init_send(uint16_t view_id) {
pkt_00_init table = {.view_id = view_id };
2022-07-31 09:21:27 +00:00
return pkt_world_write(MSG_ID_00_INIT, pkt_table_encode(pkt_00_init_desc, PKT_STRUCT_PTR(&table)), 1, view_id, NULL, 1);
2021-05-06 15:30:38 +00:00
}
int32_t pkt_00_init_handler(pkt_header *header) {
pkt_00_init table;
PKT_IF(pkt_msg_decode(header, pkt_00_init_desc, pkt_pack_desc_args(pkt_00_init_desc), PKT_STRUCT_PTR(&table)));
2021-05-06 15:30:38 +00:00
uint64_t peer_id = (uint64_t)header->udata;
2021-05-06 18:24:01 +00:00
uint64_t ent_id = player_spawn(NULL);
2022-07-31 14:34:47 +00:00
Position *pos = ecs_get_mut(world_ecs(), ent_id, Position);
#if 0
2021-09-08 14:12:38 +00:00
pos->x = world_dim()/2.0f + rand()%15*15.0f;
pos->y = world_dim()/2.0f + rand()%15*15.0f;
#else
pos->x = rand()%world_dim();
pos->y = rand()%world_dim();
#endif
2022-07-31 10:58:17 +00:00
zpl_printf("[INFO] initializing player entity id: %d with view id: %d for peer id: %d...\n", ent_id, table.view_id, peer_id);
ecs_set(world_ecs(), ent_id, ClientInfo, {.peer = peer_id, .view_id = header->view_id, .active = false });
2021-08-30 17:39:25 +00:00
pkt_01_welcome_send(world_seed(), peer_id, header->view_id, ent_id, world_chunk_size(), world_chunk_amount());
2021-05-06 15:30:38 +00:00
return 0;
}