This commit is contained in:
Chewico 2026-10-14 22:31:56 +02:00
parent 7fe056a659
commit f98d577b89
23 changed files with 147 additions and 88 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20)
project(MyEngine
project(DeerithEngine
VERSION 0.1
LANGUAGES CXX
)

View File

@ -5,5 +5,5 @@
void deerith::setup_base_protocols()
{
router_add_packet_processor<sync_world_tick_t>();
router_add_packet_processor<sync_world_tick_packet_t>();
}

View File

@ -0,0 +1,25 @@
#include <deerith/engine.h>
#include <deerith/network/router.h>
#include <deerith/network/packets.h>
#include <deerith/world/entity.h>
#include <deerith/world.h>
#include "world/world_internals.h"
namespace deerith
{
}
template <>
void deerith::process_packet(deerith::sync_world_tick_packet_t &packet, client_t *client)
{
deerith_trace("tick set to {}", packet.tick_id);
set_tick(packet.tick_id);
}
template <>
void deerith::process_packet(deerith::create_player_entity_packet_t &packet, client_t *client)
{
self_entity = create_entity();
add_component<entity_sync_client_position_t>(self_entity);
}

View File

@ -0,0 +1,5 @@
#include <deerith/world.h>
void deerith::render_world()
{
}

View File

@ -1,8 +1,6 @@
#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"
@ -14,7 +12,7 @@ namespace deerith
std::unique_ptr<std::thread> world_thread;
std::atomic<bool> world_running = false;
character_sync_interface_t character_sync_interface;
entity_t self_entity = entt::null;
void world_simulation();
}
@ -55,7 +53,7 @@ void deerith::world_simulation()
send_player_input();
}
wait_till_next_click();
wait_till_next_tick();
}
world_running = false;

View File

@ -1,7 +1,10 @@
#pragma once
#include <deerith/world.h>
namespace deerith
{
extern entity_t self_entity;
void process_player_input();
void send_player_input();
}

View File

@ -1,14 +0,0 @@
#include <deerith/packets/sync_world_tick.h>
#include <deerith/network/router.h>
#include <deerith/world.h>
namespace deerith
{
}
template <>
void deerith::process_packet(sync_world_tick_t &world_tick, client_t *client)
{
deerith_trace("sync_world_tick_t tick set to {}", world_tick.tick_id);
set_tick(world_tick.tick_id);
}

View File

@ -38,5 +38,6 @@ target_link_libraries(DeerithCore
spdlog::spdlog
fmt::fmt
glm::glm
EnTT::EnTT
${ENET_LIBRARY}
)

View File

@ -1,11 +1,17 @@
#pragma once
#include <enet/enet.h>
#include <entt/entt.hpp>
namespace deerith
{
using entity_registry_t = entt::registry;
using entity_t = entt::entity;
struct client_t
{
client_t(ENetPeer *_peer) : peer(_peer) {}
ENetPeer *peer;
entity_t entity_id = entt::null;
client_t(ENetPeer *_peer) : peer(_peer) {}
};
}

View File

@ -3,7 +3,7 @@
namespace deerith
{
struct sync_world_tick_t
struct sync_world_tick_packet_t
{
static constexpr uint16_t packet_id = 1;
static constexpr bool reliable = true;
@ -16,4 +16,10 @@ namespace deerith
archive(tick_id);
}
};
struct create_player_entity_packet_t
{
static constexpr uint16_t packet_id = 2;
static constexpr bool reliable = true;
};
}

View File

@ -1,30 +0,0 @@
#pragma once
#include <deerith/engine.h>
#include <deerith/world/character_sync_interface.h>
namespace deerith
{
struct sync_character_interface_t
{
static constexpr uint16_t packet_id = 2;
static constexpr bool reliable = false;
sync_variable_transport_t<glm::vec2> move_input_transport;
void load(const character_sync_interface_t &ci)
{
move_input_transport.load(ci.move_input);
}
void dipose(character_sync_interface_t &ci)
{
move_input_transport.load(ci.move_input);
}
template <typename Archive>
void serialize(Archive &archive)
{
archive(move_input_transport);
}
};
}

View File

@ -1,11 +0,0 @@
#pragma once
#include <deerith/network/sync_variable.h>
namespace deerith
{
struct character_sync_interface_t
{
sync_variable_t<glm::vec2> move_input;
sync_variable_t<glm::vec2> position;
};
}

View File

@ -0,0 +1,19 @@
#pragma once
#include <deerith/network/client.h>
#include <deerith/network/sync_variable.h>
namespace deerith
{
// struct for linking a entity to a client, mostly in the server
struct entity_link_to_client_t
{
client_t *client = nullptr;
};
// struct for linking a entity to a client
struct entity_sync_client_position_t
{
sync_variable_t<glm::vec2> move_input;
sync_variable_t<glm::vec2> position;
};
}

View File

@ -1,11 +1,29 @@
#pragma once
#include <deerith/engine.h>
#include <entt/entt.hpp>
namespace deerith
{
using entity_registry_t = entt::registry;
using entity_t = entt::entity;
uint32_t get_world_tick();
void wait_till_next_click();
void wait_till_next_tick();
void set_tick(uint32_t);
entity_registry_t &get_entity_registry_t();
inline entity_t create_entity() { return get_entity_registry_t().create(); }
template <typename T>
T &add_component(entity_t ent)
{
return get_entity_registry_t().emplace<T>(ent);
}
template <typename T>
T &get_component(entity_t ent)
{
return get_entity_registry_t().get<T>(ent);
}
}

View File

@ -6,4 +6,5 @@ namespace deerith
void init_network();
void shutdown_network();
}

View File

@ -18,7 +18,7 @@ uint32_t deerith::get_world_tick()
return tick_id;
}
void deerith::wait_till_next_click()
void deerith::wait_till_next_tick()
{
using clock = std::chrono::steady_clock;

View File

@ -0,0 +1,11 @@
#include <deerith/world/world_core.h>
namespace deerith
{
entity_registry_t entity_registry;
}
entt::registry &deerith::get_entity_registry_t()
{
return entity_registry;
}

View File

@ -12,6 +12,7 @@
#include <memory>
#include "network_internals.h"
#include "world/world_internals.h"
namespace deerith
{
@ -88,13 +89,9 @@ void deerith::create_client(ENetPeer *peer)
enet_address_get_host_ip(&peer->address, enet_address, 31);
deerith_trace("a new client has connected : {}", enet_address);
sync_world_tick_t sync_world_tick;
sync_world_tick.tick_id = get_world_tick();
std::unique_ptr<client_t> client = std::make_unique<client_t>(peer);
peer->data = client.get();
clients.push_back(std::move(client));
router_send_packet<sync_world_tick_t>(sync_world_tick, (client_t *)peer->data);
client_connects((client_t *)peer->data);
}

View File

@ -11,5 +11,4 @@ namespace deerith
void flush_network_events();
void load_base_protocols();
}

View File

@ -1,7 +1,12 @@
#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>
@ -47,5 +52,23 @@ void deerith::world_simulation()
// process movments
wait_till_next_click();
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;
}

View File

@ -1,15 +0,0 @@
#include <deerith/engine.h>
#include <deerith/packets/sync_character_interface.h>
#include <deerith/network/router.h>
namespace deerith
{
}
template <>
void deerith::process_packet(sync_character_interface_t &interface, client_t *client)
{
deerith_trace("sync player input {} {}");
}

View File

@ -0,0 +1,15 @@
#include <deerith/engine.h>
#include <deerith/packets/sync_character_interface.h>
#include <deerith/network/router.h>
namespace deerith
{
}
template <>
void deerith::process_packet(sync_character_interface_t &interface, client_t *client)
{
deerith_trace("sync player input {} {}");
}

View File

@ -1,6 +1,8 @@
#pragma once
#include <deerith/network/client.h>
namespace deerith
{
void update_players_characters();
void client_connects(client_t *client);
}