75 lines
1.6 KiB
C++
75 lines
1.6 KiB
C++
#include <deerith/world.h>
|
|
#include <deerith/core/log.h>
|
|
#include <deerith/network/router.h>
|
|
|
|
#include "network/network_internals.h"
|
|
#include "world/world_internals.h"
|
|
|
|
#include <deerith/world/entity.h>
|
|
#include <deerith/packets.h>
|
|
|
|
#include <csignal>
|
|
#include <atomic>
|
|
|
|
namespace deerith
|
|
{
|
|
int world_tick = 0;
|
|
void world_simulation();
|
|
|
|
std::atomic<bool> running = true;
|
|
|
|
void create_client(ENetPeer *peer);
|
|
void handle_signal(int signal);
|
|
}
|
|
|
|
void deerith::handle_signal(int signal)
|
|
{
|
|
if (signal == SIGTERM)
|
|
running = false;
|
|
}
|
|
|
|
void deerith::run_world(int port)
|
|
{
|
|
std::signal(SIGTERM, handle_signal);
|
|
|
|
if (init_server(port))
|
|
{
|
|
deerith_trace("server init on port {}", port);
|
|
while (running)
|
|
{
|
|
world_simulation();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
deerith_error("could not init server");
|
|
}
|
|
}
|
|
|
|
void deerith::world_simulation()
|
|
{
|
|
flush_network_events();
|
|
|
|
// process movments
|
|
|
|
wait_till_next_tick();
|
|
}
|
|
|
|
void deerith::client_connects(client_t *client)
|
|
{
|
|
sync_world_tick_packet_t sync_world_tick;
|
|
sync_world_tick.tick_id = get_world_tick();
|
|
|
|
create_player_entity_packet_t create_player_entity;
|
|
|
|
router_send_packet(sync_world_tick, client);
|
|
router_send_packet(create_player_entity, client);
|
|
|
|
entity_t entity = create_entity();
|
|
entity_link_to_client_t &link = add_component<entity_link_to_client_t>(entity);
|
|
entity_sync_client_position_t &position_sync = add_component<entity_sync_client_position_t>(entity);
|
|
|
|
link.client = client;
|
|
client->entity_id = entity;
|
|
}
|