68 lines
1.4 KiB
C++
68 lines
1.4 KiB
C++
#include <deerith/engine.h>
|
|
#include <deerith/client/world.h>
|
|
|
|
#include <deerith/client/internals/network.h>
|
|
#include <deerith/client/internals/world.h>
|
|
|
|
#include <thread>
|
|
#include <memory>
|
|
|
|
namespace deerith
|
|
{
|
|
std::unique_ptr<std::thread> world_thread;
|
|
std::atomic<bool> world_running = false;
|
|
|
|
entity_t self_entity = entt::null;
|
|
|
|
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();
|
|
|
|
if (get_world_tick() % 2 == 0)
|
|
{
|
|
send_player_input();
|
|
}
|
|
|
|
wait_till_next_tick();
|
|
}
|
|
|
|
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();
|
|
}
|