IDK
This commit is contained in:
parent
7fe056a659
commit
f98d577b89
@ -1,6 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
project(MyEngine
|
||||
project(DeerithEngine
|
||||
VERSION 0.1
|
||||
LANGUAGES CXX
|
||||
)
|
||||
|
||||
@ -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>();
|
||||
}
|
||||
25
deerith_engine_client/src/world/server_communication.cpp
Normal file
25
deerith_engine_client/src/world/server_communication.cpp
Normal 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);
|
||||
}
|
||||
5
deerith_engine_client/src/world/universe_render.cpp
Normal file
5
deerith_engine_client/src/world/universe_render.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
#include <deerith/world.h>
|
||||
|
||||
void deerith::render_world()
|
||||
{
|
||||
}
|
||||
@ -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;
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
@ -38,5 +38,6 @@ target_link_libraries(DeerithCore
|
||||
spdlog::spdlog
|
||||
fmt::fmt
|
||||
glm::glm
|
||||
EnTT::EnTT
|
||||
${ENET_LIBRARY}
|
||||
)
|
||||
@ -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) {}
|
||||
};
|
||||
}
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
19
deerith_engine_core/include/deerith/world/entity.h
Normal file
19
deerith_engine_core/include/deerith/world/entity.h
Normal 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;
|
||||
};
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,4 +6,5 @@ namespace deerith
|
||||
|
||||
void init_network();
|
||||
void shutdown_network();
|
||||
|
||||
}
|
||||
@ -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;
|
||||
|
||||
|
||||
11
deerith_engine_core/src/world.cpp
Normal file
11
deerith_engine_core/src/world.cpp
Normal 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;
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
@ -11,5 +11,4 @@ namespace deerith
|
||||
void flush_network_events();
|
||||
|
||||
void load_base_protocols();
|
||||
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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 {} {}");
|
||||
}
|
||||
15
deerith_engine_server/src/world/player_communication.cpp
Normal file
15
deerith_engine_server/src/world/player_communication.cpp
Normal 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 {} {}");
|
||||
}
|
||||
@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
#include <deerith/network/client.h>
|
||||
|
||||
namespace deerith
|
||||
{
|
||||
void update_players_characters();
|
||||
void client_connects(client_t *client);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user