DeerithEngine/deerith_engine_client/src/world/server_communication.cpp
2026-10-20 23:01:33 +02:00

81 lines
2.5 KiB
C++

#include <deerith/engine.h>
#include <deerith/network/router.h>
#include <deerith/network/packets.h>
#include <deerith/world/components.h>
#include <deerith/world.h>
#include "world/world_internals.h"
#include "network/network_internals.h"
void deerith::setup_base_protocols()
{
router_add_packet_processor<packet_sync_world_tick_t>();
router_add_packet_processor<packet_create_player_entity_t>();
router_add_packet_processor<packet_create_entity_t>();
router_add_packet_processor<packet_update_entities_coordinates_t>();
router_add_packet_processor<packet_destroy_entity_t>();
}
template <>
void deerith::process_packet(deerith::packet_sync_world_tick_t &packet, client_t *client)
{
deerith_trace("tick set to {}", packet.tick_id);
set_tick(packet.tick_id);
}
template <>
void deerith::process_packet(deerith::packet_create_player_entity_t &packet, client_t *client)
{
self_entity = create_entity();
add_component<component_player_sync_input_t>(self_entity);
}
template <>
void deerith::process_packet(deerith::packet_create_entity_t &packet, client_t *client)
{
deerith_trace("creating entity with network id {}", packet.network_sync_id);
entity_t entity;
if (packet.is_self_client)
{
deerith_trace("creating self entity");
entity = self_entity;
}
else
{
entity = create_entity();
}
component_network_id_t &entity_network_id = add_entity_network_with_id(entity, packet.network_sync_id);
if (packet.add_entity_sync_coordinates)
{
deerith_trace("-> adding entity sync coordinates to entity");
component_sync_coordinates_t &sync_coords = add_component<component_sync_coordinates_t>(entity);
}
}
template <>
void deerith::process_packet(deerith::packet_update_entities_coordinates_t &packet, client_t *client)
{
for (int i = 0; i < packet.entities_ids.size(); i++)
{
if (!exists_network_entity_with_id(packet.entities_ids[i]))
{
continue;
}
entity_t entity = get_network_entity_with_id(packet.entities_ids[i]);
component_sync_coordinates_t &sync_coords = get_component<component_sync_coordinates_t>(entity);
packet.positions_to_update[i].recive(sync_coords.position);
}
}
template <>
void deerith::process_packet(deerith::packet_destroy_entity_t &packet, client_t *client)
{
deerith_trace("destroying entity : {}", packet.network_sync_id);
entity_t specific_entity = get_network_entity_with_id(packet.network_sync_id);
get_entity_registry().destroy(specific_entity);
}