2026-10-14 14:24:35 +02:00

72 lines
1.4 KiB
C++

#include <deerith/engine.h>
#include <deerith/world.h>
#include <deerith/world/character_sync_interface.h>
#include "network/network_internals.h"
#include "world/world_internals.h"
#include <thread>
#include <memory>
namespace deerith
{
std::unique_ptr<std::thread> world_thread;
std::atomic<bool> world_running = false;
character_sync_interface_t character_sync_interface;
void world_simulation();
}
bool deerith::connect_to_world(const char *address, int port)
{
deerith_assert(!world_running, "connect_to_world failed because there is a current running world");
shutdown_world();
connect_to_server(address, port);
if (!wait_till_connected_to_server())
{
deerith_error("engine was not able to connect to server");
return false;
}
world_running = true;
world_thread = std::make_unique<std::thread>(world_simulation);
return true;
}
bool deerith::is_world_running()
{
return world_running;
}
void deerith::world_simulation()
{
while (world_running)
{
flush_network_events();
process_player_input();
if (get_world_tick() % 2 == 0)
{
send_player_input();
}
wait_till_next_click();
}
world_running = false;
deerith_trace("world shutdown");
}
void deerith::shutdown_world()
{
world_running = false;
if (world_thread && world_thread->joinable())
world_thread->join();
world_thread.reset();
}