world data
This commit is contained in:
parent
be89e2a980
commit
cc890b56d1
@ -47,13 +47,13 @@ void deerith::create_grid_mesh()
|
|||||||
void deerith::init_primitives()
|
void deerith::init_primitives()
|
||||||
{
|
{
|
||||||
primitive_shader = std::make_unique<shader_t>(
|
primitive_shader = std::make_unique<shader_t>(
|
||||||
"../deerith_engine_client/shaders/primitive.vert.glsl",
|
"shaders/primitive.vert.glsl",
|
||||||
"../deerith_engine_client/shaders/primitive.frag.glsl");
|
"shaders/primitive.frag.glsl");
|
||||||
|
|
||||||
primitive_rectangle_mesh = std::make_unique<mesh_2d_t>(rectangle_vertices, 4, rectangle_indices, 6);
|
primitive_rectangle_mesh = std::make_unique<mesh_2d_t>(rectangle_vertices, 4, rectangle_indices, 6);
|
||||||
lit_shader = std::make_unique<shader_t>(
|
lit_shader = std::make_unique<shader_t>(
|
||||||
"../deerith_engine_client/shaders/lit.vert.glsl",
|
"shaders/lit.vert.glsl",
|
||||||
"../deerith_engine_client/shaders/lit.frag.glsl");
|
"shaders/lit.frag.glsl");
|
||||||
|
|
||||||
cube_mesh = std::make_unique<mesh_3d_t>(cube_vertices, 8, cube_indices, 36); // 6 faces * 2 * 3
|
cube_mesh = std::make_unique<mesh_3d_t>(cube_vertices, 8, cube_indices, 36); // 6 faces * 2 * 3
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#include <deerith/engine.h>
|
#include <deerith/engine.h>
|
||||||
#include <deerith/network/router.h>
|
#include <deerith/network/router.h>
|
||||||
#include <deerith/network/packets.h>
|
#include <deerith/network/packets.h>
|
||||||
|
#include <deerith/network/packets_voxel.h>
|
||||||
#include <deerith/world/components.h>
|
#include <deerith/world/components.h>
|
||||||
#include <deerith/client/world.h>
|
#include <deerith/client/world.h>
|
||||||
|
|
||||||
@ -14,6 +15,7 @@ void deerith::setup_base_protocols()
|
|||||||
router_add_packet_processor<packet_create_entity_t>();
|
router_add_packet_processor<packet_create_entity_t>();
|
||||||
router_add_packet_processor<packet_update_entities_coordinates_t>();
|
router_add_packet_processor<packet_update_entities_coordinates_t>();
|
||||||
router_add_packet_processor<packet_destroy_entity_t>();
|
router_add_packet_processor<packet_destroy_entity_t>();
|
||||||
|
router_add_packet_processor<packet_voxel_data_definition_t>();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
@ -79,3 +81,11 @@ void deerith::process_packet(deerith::packet_destroy_entity_t &packet, client_t
|
|||||||
entity_t specific_entity = get_network_entity_with_id(packet.network_sync_id);
|
entity_t specific_entity = get_network_entity_with_id(packet.network_sync_id);
|
||||||
get_entity_registry().destroy(specific_entity);
|
get_entity_registry().destroy(specific_entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void deerith::process_packet(deerith::packet_voxel_data_definition_t &packet, client_t *client)
|
||||||
|
{
|
||||||
|
deerith_trace("recive voxel data definition with {} materials and {} types", packet.voxel_materials.size(), packet.voxel_types.size());
|
||||||
|
set_voxel_materials(packet.voxel_materials);
|
||||||
|
set_voxel_types(packet.voxel_types);
|
||||||
|
}
|
||||||
@ -89,5 +89,4 @@ namespace deerith
|
|||||||
archive(positions_to_update);
|
archive(positions_to_update);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
27
deerith_engine_core/include/deerith/network/packets_voxel.h
Normal file
27
deerith_engine_core/include/deerith/network/packets_voxel.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <deerith/engine.h>
|
||||||
|
#include <deerith/world/voxels.h>
|
||||||
|
|
||||||
|
#include <cereal/types/vector.hpp>
|
||||||
|
#include <cereal/types/string.hpp>
|
||||||
|
|
||||||
|
// VOXEL SPECIFIC PACKETS 1xxx
|
||||||
|
|
||||||
|
namespace deerith
|
||||||
|
{
|
||||||
|
struct packet_voxel_data_definition_t
|
||||||
|
{
|
||||||
|
static constexpr uint16_t packet_id = 1001;
|
||||||
|
static constexpr bool reliable = true;
|
||||||
|
|
||||||
|
std::vector<voxel_material_t> voxel_materials;
|
||||||
|
std::vector<voxel_type_t> voxel_types;
|
||||||
|
|
||||||
|
template <typename Archive>
|
||||||
|
void serialize(Archive &archive)
|
||||||
|
{
|
||||||
|
archive(voxel_materials);
|
||||||
|
archive(voxel_types);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
6
deerith_engine_core/include/deerith/world/voxel_world.h
Normal file
6
deerith_engine_core/include/deerith/world/voxel_world.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include <deerith/world/voxels.h>
|
||||||
|
|
||||||
|
namespace deerith
|
||||||
|
{
|
||||||
|
void init_world();
|
||||||
|
}
|
||||||
@ -2,18 +2,60 @@
|
|||||||
#include <deerith/engine.h>
|
#include <deerith/engine.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace deerith
|
namespace deerith
|
||||||
{
|
{
|
||||||
using voxel_type_id_t = uint16_t;
|
using voxel_type_id_t = uint16_t;
|
||||||
|
using voxel_material_id_t = uint16_t;
|
||||||
|
|
||||||
|
enum class voxel_material_type_t : int
|
||||||
|
{
|
||||||
|
AIR = 0,
|
||||||
|
OPAQUE_BOX = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
struct voxel_material_t
|
||||||
|
{
|
||||||
|
// universal identifies
|
||||||
|
std::string id;
|
||||||
|
voxel_material_type_t material_type = voxel_material_type_t::AIR;
|
||||||
|
|
||||||
|
template <typename Archive>
|
||||||
|
void serialize(Archive &archive)
|
||||||
|
{
|
||||||
|
archive(id);
|
||||||
|
archive(material_type);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct voxel_type_t
|
struct voxel_type_t
|
||||||
{
|
{
|
||||||
|
// universal identifies
|
||||||
std::string id;
|
std::string id;
|
||||||
bool is_air = false;
|
// is walkable
|
||||||
|
bool is_solid = false;
|
||||||
|
// how to be rendered
|
||||||
|
voxel_material_id_t material_id;
|
||||||
|
|
||||||
|
template <typename Archive>
|
||||||
|
void serialize(Archive &archive)
|
||||||
|
{
|
||||||
|
archive(id);
|
||||||
|
archive(is_solid);
|
||||||
|
archive(material_id);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void set_voxel_materials(const std::vector<voxel_material_t> &mats);
|
||||||
|
voxel_material_id_t register_voxel_material(const voxel_material_t &type);
|
||||||
|
voxel_material_id_t get_voxel_material_id(const std::string &name);
|
||||||
|
const voxel_material_t &get_voxel_material(voxel_material_id_t id);
|
||||||
|
const std::vector<voxel_material_t> &get_voxel_material_vector();
|
||||||
|
|
||||||
|
void set_voxel_types(const std::vector<voxel_type_t> &types);
|
||||||
voxel_type_id_t register_voxel_type(const voxel_type_t &type);
|
voxel_type_id_t register_voxel_type(const voxel_type_t &type);
|
||||||
voxel_type_id_t get_voxel_type_id(const std::string &name);
|
voxel_type_id_t get_voxel_type_id(const std::string &name);
|
||||||
const voxel_type_t &get_voxel_type(voxel_type_id_t id);
|
const voxel_type_t &get_voxel_type(voxel_type_id_t id);
|
||||||
|
const std::vector<voxel_type_t> &get_voxel_type_vector();
|
||||||
}
|
}
|
||||||
@ -7,12 +7,18 @@ namespace deerith
|
|||||||
{
|
{
|
||||||
std::vector<voxel_type_t> voxel_type_array;
|
std::vector<voxel_type_t> voxel_type_array;
|
||||||
std::unordered_map<std::string, voxel_type_id_t> voxel_type_name_id;
|
std::unordered_map<std::string, voxel_type_id_t> voxel_type_name_id;
|
||||||
|
|
||||||
|
std::vector<voxel_material_t> voxel_material_array;
|
||||||
|
std::unordered_map<std::string, voxel_material_id_t> voxel_material_name_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
deerith::voxel_type_id_t deerith::register_voxel_type(const voxel_type_t &type)
|
deerith::voxel_type_id_t deerith::register_voxel_type(const voxel_type_t &type)
|
||||||
{
|
{
|
||||||
|
voxel_type_id_t id = voxel_type_array.size();
|
||||||
|
|
||||||
voxel_type_array.push_back(type);
|
voxel_type_array.push_back(type);
|
||||||
return voxel_type_array.size() - 1;
|
voxel_type_name_id[type.id] = id;
|
||||||
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
const deerith::voxel_type_t &deerith::get_voxel_type(deerith::voxel_type_id_t id)
|
const deerith::voxel_type_t &deerith::get_voxel_type(deerith::voxel_type_id_t id)
|
||||||
@ -26,3 +32,57 @@ deerith::voxel_type_id_t deerith::get_voxel_type_id(const std::string &name)
|
|||||||
deerith_assert(voxel_type_name_id.contains(name), "invalid get_voxel_type_id name");
|
deerith_assert(voxel_type_name_id.contains(name), "invalid get_voxel_type_id name");
|
||||||
return voxel_type_name_id[name];
|
return voxel_type_name_id[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deerith::voxel_material_id_t deerith::register_voxel_material(const deerith::voxel_material_t &type)
|
||||||
|
{
|
||||||
|
voxel_material_id_t id = voxel_material_array.size();
|
||||||
|
|
||||||
|
voxel_material_name_id[type.id] = id;
|
||||||
|
voxel_material_array.push_back(type);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
deerith::voxel_material_id_t deerith::get_voxel_material_id(const std::string &name)
|
||||||
|
{
|
||||||
|
deerith_assert(voxel_material_name_id.contains(name), "invalid get_voxel_material_id name");
|
||||||
|
return voxel_material_name_id[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
const deerith::voxel_material_t &deerith::get_voxel_material(deerith::voxel_material_id_t id)
|
||||||
|
{
|
||||||
|
deerith_assert(id >= 0 && id < voxel_material_array.size(), "invalid get_voxel_material id");
|
||||||
|
return voxel_material_array[id];
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<deerith::voxel_material_t> &deerith::get_voxel_material_vector()
|
||||||
|
{
|
||||||
|
return voxel_material_array;
|
||||||
|
}
|
||||||
|
const std::vector<deerith::voxel_type_t> &deerith::get_voxel_type_vector()
|
||||||
|
{
|
||||||
|
return voxel_type_array;
|
||||||
|
}
|
||||||
|
|
||||||
|
void deerith::set_voxel_materials(const std::vector<deerith::voxel_material_t> &mats)
|
||||||
|
{
|
||||||
|
voxel_material_array = mats;
|
||||||
|
voxel_material_name_id.clear();
|
||||||
|
|
||||||
|
for (voxel_material_id_t id = 0; id < voxel_material_array.size(); ++id)
|
||||||
|
{
|
||||||
|
const voxel_material_t &material = voxel_material_array[id];
|
||||||
|
voxel_material_name_id[material.id] = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void deerith::set_voxel_types(const std::vector<deerith::voxel_type_t> &types)
|
||||||
|
{
|
||||||
|
voxel_type_array = types;
|
||||||
|
voxel_type_name_id.clear();
|
||||||
|
|
||||||
|
for (voxel_type_id_t id = 0; id < voxel_type_array.size(); ++id)
|
||||||
|
{
|
||||||
|
const voxel_type_t &type = voxel_type_array[id];
|
||||||
|
voxel_type_name_id[type.id] = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -14,5 +14,7 @@ namespace deerith
|
|||||||
void send_create_entity_to_client(entity_t ent, client_t *client);
|
void send_create_entity_to_client(entity_t ent, client_t *client);
|
||||||
void send_create_actual_entities_to_client(client_t *client);
|
void send_create_actual_entities_to_client(client_t *client);
|
||||||
|
|
||||||
|
void send_voxel_data_to_client(client_t *client);
|
||||||
|
|
||||||
void send_delete_entity_to_all_clients(entity_t ent);
|
void send_delete_entity_to_all_clients(entity_t ent);
|
||||||
}
|
}
|
||||||
49
deerith_engine_server/src/world/client_managment.cpp
Normal file
49
deerith_engine_server/src/world/client_managment.cpp
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#include <deerith/engine.h>
|
||||||
|
#include <deerith/world/components.h>
|
||||||
|
#include <deerith/network/packets.h>
|
||||||
|
#include <deerith/network/router.h>
|
||||||
|
#include <deerith/network/client.h>
|
||||||
|
|
||||||
|
#include <deerith/server/world.h>
|
||||||
|
|
||||||
|
#include <deerith/server/network/network_internals.h>
|
||||||
|
#include <deerith/server/world/world_internals.h>
|
||||||
|
|
||||||
|
void deerith::client_connects(client_t *client)
|
||||||
|
{
|
||||||
|
// sync world tick
|
||||||
|
packet_sync_world_tick_t sync_world_tick;
|
||||||
|
sync_world_tick.tick_id = get_world_tick();
|
||||||
|
router_send_packet(sync_world_tick, client);
|
||||||
|
|
||||||
|
// send voxel data
|
||||||
|
send_voxel_data_to_client(client);
|
||||||
|
|
||||||
|
// create entity player entity
|
||||||
|
entity_t entity = create_entity();
|
||||||
|
component_player_sync_input_t &position_sync = add_component<component_player_sync_input_t>(entity);
|
||||||
|
|
||||||
|
packet_create_player_entity_t create_player_entity;
|
||||||
|
router_send_packet(create_player_entity, client);
|
||||||
|
|
||||||
|
// create the server representation of the entity
|
||||||
|
add_entity_network_id(entity);
|
||||||
|
add_component<component_sync_coordinates_t>(entity);
|
||||||
|
|
||||||
|
send_create_entity_to_all_clients(entity);
|
||||||
|
|
||||||
|
// we do this the last to avoid send_create_entity_to_all_clients send_create_actual_entities_to_client create the same entity for the player
|
||||||
|
component_client_link_t &link = add_component<component_client_link_t>(entity);
|
||||||
|
client->entity_id = entity;
|
||||||
|
link.client = client;
|
||||||
|
|
||||||
|
send_create_actual_entities_to_client(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
void deerith::client_disconnects(client_t *client)
|
||||||
|
{
|
||||||
|
entity_t client_entity = client->entity_id;
|
||||||
|
send_delete_entity_to_all_clients(client_entity);
|
||||||
|
|
||||||
|
get_entity_registry().destroy(client_entity);
|
||||||
|
}
|
||||||
@ -28,34 +28,6 @@ void deerith::process_packet(packet_update_player_input_t &packet, client_t *cli
|
|||||||
packet.position_transport.recive(sync_client.position);
|
packet.position_transport.recive(sync_client.position);
|
||||||
}
|
}
|
||||||
|
|
||||||
void deerith::client_connects(client_t *client)
|
|
||||||
{
|
|
||||||
// sync world tick
|
|
||||||
packet_sync_world_tick_t sync_world_tick;
|
|
||||||
sync_world_tick.tick_id = get_world_tick();
|
|
||||||
router_send_packet(sync_world_tick, client);
|
|
||||||
|
|
||||||
// create entity player entity
|
|
||||||
entity_t entity = create_entity();
|
|
||||||
component_player_sync_input_t &position_sync = add_component<component_player_sync_input_t>(entity);
|
|
||||||
|
|
||||||
packet_create_player_entity_t create_player_entity;
|
|
||||||
router_send_packet(create_player_entity, client);
|
|
||||||
|
|
||||||
// create the server representation of the entity
|
|
||||||
add_entity_network_id(entity);
|
|
||||||
add_component<component_sync_coordinates_t>(entity);
|
|
||||||
|
|
||||||
send_create_entity_to_all_clients(entity);
|
|
||||||
|
|
||||||
// we do this the last to avoid send_create_entity_to_all_clients send_create_actual_entities_to_client create the same entity for the player
|
|
||||||
component_client_link_t &link = add_component<component_client_link_t>(entity);
|
|
||||||
client->entity_id = entity;
|
|
||||||
link.client = client;
|
|
||||||
|
|
||||||
send_create_actual_entities_to_client(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
void deerith::send_create_entity_to_all_clients(deerith::entity_t ent)
|
void deerith::send_create_entity_to_all_clients(deerith::entity_t ent)
|
||||||
{
|
{
|
||||||
auto view = get_entity_registry().view<component_client_link_t>();
|
auto view = get_entity_registry().view<component_client_link_t>();
|
||||||
@ -88,14 +60,6 @@ void deerith::send_create_entity_to_client(entity_t ent, client_t *client)
|
|||||||
router_send_packet(create_entity, client);
|
router_send_packet(create_entity, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
void deerith::client_disconnects(client_t *client)
|
|
||||||
{
|
|
||||||
entity_t client_entity = client->entity_id;
|
|
||||||
send_delete_entity_to_all_clients(client_entity);
|
|
||||||
|
|
||||||
get_entity_registry().destroy(client_entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
void deerith::send_delete_entity_to_all_clients(entity_t ent)
|
void deerith::send_delete_entity_to_all_clients(entity_t ent)
|
||||||
{
|
{
|
||||||
packet_destroy_entity_t destroy_entity;
|
packet_destroy_entity_t destroy_entity;
|
||||||
|
|||||||
17
deerith_engine_server/src/world/voxel_data_managment.cpp
Normal file
17
deerith_engine_server/src/world/voxel_data_managment.cpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include <deerith/server/world/world_internals.h>
|
||||||
|
#include <deerith/network/packets_voxel.h>
|
||||||
|
#include <deerith/network/router.h>
|
||||||
|
|
||||||
|
namespace deerith
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void deerith::send_voxel_data_to_client(client_t *client)
|
||||||
|
{
|
||||||
|
packet_voxel_data_definition_t voxel_data_definition;
|
||||||
|
voxel_data_definition.voxel_materials = get_voxel_material_vector();
|
||||||
|
voxel_data_definition.voxel_types = get_voxel_type_vector();
|
||||||
|
|
||||||
|
router_send_packet(voxel_data_definition, client);
|
||||||
|
}
|
||||||
@ -2,5 +2,7 @@
|
|||||||
|
|
||||||
namespace sandbox
|
namespace sandbox
|
||||||
{
|
{
|
||||||
void register_voxels();
|
|
||||||
|
void define_voxel_materials();
|
||||||
|
void define_voxels_types();
|
||||||
}
|
}
|
||||||
@ -1,18 +1,48 @@
|
|||||||
#include "voxels.h"
|
#include "voxels.h"
|
||||||
|
|
||||||
#include <deerith/world/voxels.h>
|
#include <deerith/world/voxels.h>
|
||||||
|
|
||||||
void sandbox::register_voxels()
|
void sandbox::define_voxel_materials()
|
||||||
{
|
{
|
||||||
deerith::voxel_type_t air_voxel_type;
|
// air
|
||||||
air_voxel_type.is_air = true;
|
{
|
||||||
air_voxel_type.id = "air";
|
deerith::voxel_material_t voxel_material;
|
||||||
|
voxel_material.id = "air";
|
||||||
|
voxel_material.material_type = deerith::voxel_material_type_t::AIR;
|
||||||
|
|
||||||
deerith::register_voxel_type(air_voxel_type);
|
deerith::register_voxel_material(voxel_material);
|
||||||
|
}
|
||||||
deerith::voxel_type_t rock_voxel_type;
|
|
||||||
air_voxel_type.is_air = false;
|
// rock
|
||||||
air_voxel_type.id = "rock";
|
{
|
||||||
|
deerith::voxel_material_t voxel_material;
|
||||||
deerith::register_voxel_type(air_voxel_type);
|
voxel_material.id = "rock";
|
||||||
|
voxel_material.material_type = deerith::voxel_material_type_t::OPAQUE_BOX;
|
||||||
|
|
||||||
|
// textures...
|
||||||
|
|
||||||
|
deerith::register_voxel_material(voxel_material);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sandbox::define_voxels_types()
|
||||||
|
{
|
||||||
|
// air
|
||||||
|
{
|
||||||
|
deerith::voxel_type_t voxel_type;
|
||||||
|
voxel_type.is_solid = false;
|
||||||
|
voxel_type.id = "air";
|
||||||
|
|
||||||
|
voxel_type.material_id = deerith::get_voxel_material_id("air");
|
||||||
|
deerith::register_voxel_type(voxel_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
// rock
|
||||||
|
{
|
||||||
|
deerith::voxel_type_t voxel_type;
|
||||||
|
voxel_type.is_solid = true;
|
||||||
|
voxel_type.id = "rock";
|
||||||
|
|
||||||
|
voxel_type.material_id = deerith::get_voxel_material_id("rock");
|
||||||
|
deerith::register_voxel_type(voxel_type);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -14,8 +14,11 @@ int main(int argc, const char **argv)
|
|||||||
int port = 8080;
|
int port = 8080;
|
||||||
cmdl("port", 8080) >> port;
|
cmdl("port", 8080) >> port;
|
||||||
|
|
||||||
|
sandbox::define_voxel_materials();
|
||||||
|
sandbox::define_voxels_types();
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
if (cmdl[{"-s", "--single"}] && !deerith::execute_client("../deerith_engine_client/DeerithClient", port))
|
if (cmdl[{"-s", "--single"}] && !deerith::execute_client("../deerith_client/DeerithClient", port))
|
||||||
{
|
{
|
||||||
deerith_error("could not start server");
|
deerith_error("could not start server");
|
||||||
deerith::shutdown_engine();
|
deerith::shutdown_engine();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user