From a092f6fbc1731e3423127d2d05a3bd62102c1535 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Mon, 19 Aug 2024 16:19:45 +0200 Subject: [PATCH] make world abstract --- godot/Scenes/Levels/client_world.tscn | 7 +++++ godot/Scenes/Levels/server_world.tscn | 7 +++++ godot/Scenes/Levels/world.tscn | 20 ++++++------- godot/Scenes/main.tscn | 2 -- include/{client => blitz/godot}/World.h | 5 ++-- include/client/ClientWorld.h | 19 ++++++++++++ include/client/Main.h | 4 ++- include/server/ServerWorld.h | 19 ++++++++++++ src/{client => blitz/godot}/World.cpp | 33 +-------------------- src/client/ClientWorld.cpp | 39 +++++++++++++++++++++++++ src/client/Main.cpp | 26 ++++++++++++----- src/client/MainMenu.cpp | 19 +++++++----- src/client/register_types.cpp | 7 +++-- src/server/ServerWorld.cpp | 38 ++++++++++++++++++++++++ 14 files changed, 180 insertions(+), 65 deletions(-) create mode 100644 godot/Scenes/Levels/client_world.tscn create mode 100644 godot/Scenes/Levels/server_world.tscn rename include/{client => blitz/godot}/World.h (96%) create mode 100644 include/client/ClientWorld.h create mode 100644 include/server/ServerWorld.h rename src/{client => blitz/godot}/World.cpp (78%) create mode 100644 src/client/ClientWorld.cpp create mode 100644 src/server/ServerWorld.cpp diff --git a/godot/Scenes/Levels/client_world.tscn b/godot/Scenes/Levels/client_world.tscn new file mode 100644 index 0000000..3035263 --- /dev/null +++ b/godot/Scenes/Levels/client_world.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3 uid="uid://bqv0m8kbr300b"] + +[ext_resource type="PackedScene" path="res://Scenes/Levels/world.tscn" id="1_ajsqk"] + +[node name="World" type="ClientWorld"] + +[node name="WorldContent" parent="." instance=ExtResource("1_ajsqk")] diff --git a/godot/Scenes/Levels/server_world.tscn b/godot/Scenes/Levels/server_world.tscn new file mode 100644 index 0000000..fa8c865 --- /dev/null +++ b/godot/Scenes/Levels/server_world.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3 uid="uid://c2p67anlxe3mk"] + +[ext_resource type="PackedScene" path="res://Scenes/Levels/world.tscn" id="1_tecss"] + +[node name="World" type="ServerWorld"] + +[node name="WorldContent" parent="." instance=ExtResource("1_tecss")] diff --git a/godot/Scenes/Levels/world.tscn b/godot/Scenes/Levels/world.tscn index 80d3526..98553bc 100644 --- a/godot/Scenes/Levels/world.tscn +++ b/godot/Scenes/Levels/world.tscn @@ -1,12 +1,12 @@ -[gd_scene load_steps=17 format=3 uid="uid://coue2qehpn4fr"] +[gd_scene load_steps=17 format=3 uid="uid://cl8gww414apoq"] -[ext_resource type="Texture2D" path="res://Assets/Textures/Sky.png" id="1_mnexj"] -[ext_resource type="Texture2D" path="res://Assets/Textures/Black.png" id="2_fkwcn"] -[ext_resource type="Texture2D" path="res://Assets/Textures/Orange.png" id="3_ux02w"] -[ext_resource type="Texture2D" path="res://Assets/Textures/Green.png" id="4_wp15n"] +[ext_resource type="Texture2D" path="res://Assets/Textures/Sky.png" id="1_tcyn8"] +[ext_resource type="Texture2D" path="res://Assets/Textures/Black.png" id="2_j33w8"] +[ext_resource type="Texture2D" path="res://Assets/Textures/Orange.png" id="3_n1lus"] +[ext_resource type="Texture2D" path="res://Assets/Textures/Green.png" id="4_klpsf"] [sub_resource type="PanoramaSkyMaterial" id="PanoramaSkyMaterial_6c4vd"] -panorama = ExtResource("1_mnexj") +panorama = ExtResource("1_tcyn8") [sub_resource type="Sky" id="Sky_5ngqa"] sky_material = SubResource("PanoramaSkyMaterial_6c4vd") @@ -18,7 +18,7 @@ tonemap_mode = 2 glow_enabled = true [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ajchh"] -albedo_texture = ExtResource("2_fkwcn") +albedo_texture = ExtResource("2_j33w8") uv1_triplanar = true [sub_resource type="PlaneMesh" id="PlaneMesh_mmup0"] @@ -29,7 +29,7 @@ size = Vector2(50, 50) data = PackedVector3Array(25, 0, 25, -25, 0, 25, 25, 0, -25, -25, 0, 25, -25, 0, -25, 25, 0, -25) [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_jkvud"] -albedo_texture = ExtResource("3_ux02w") +albedo_texture = ExtResource("3_n1lus") uv1_triplanar = true [sub_resource type="BoxMesh" id="BoxMesh_plpqy"] @@ -44,13 +44,13 @@ left_to_right = -2.0 size = Vector3(5, 5, 5) [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_pfpgv"] -albedo_texture = ExtResource("4_wp15n") +albedo_texture = ExtResource("4_klpsf") uv1_triplanar = true [sub_resource type="ConcavePolygonShape3D" id="ConcavePolygonShape3D_rit6o"] data = PackedVector3Array(-12.5, 2.5, 2.5, 2.5, -2.5, 2.5, -2.5, -2.5, 2.5, -12.5, 2.5, -2.5, -2.5, -2.5, -2.5, 2.5, -2.5, -2.5, -12.5, 2.5, 2.5, -12.5, 2.5, -2.5, 2.5, -2.5, 2.5, -12.5, 2.5, -2.5, 2.5, -2.5, -2.5, 2.5, -2.5, 2.5, -12.5, 2.5, -2.5, -12.5, 2.5, 2.5, -2.5, -2.5, -2.5, -12.5, 2.5, 2.5, -2.5, -2.5, 2.5, -2.5, -2.5, -2.5, -2.5, -2.5, 2.5, 2.5, -2.5, 2.5, -2.5, -2.5, -2.5, 2.5, -2.5, 2.5, 2.5, -2.5, -2.5, -2.5, -2.5, -2.5) -[node name="World" type="World"] +[node name="World" type="Node3D"] [node name="WorldEnvironment" type="WorldEnvironment" parent="."] environment = SubResource("Environment_ctwiv") diff --git a/godot/Scenes/main.tscn b/godot/Scenes/main.tscn index 1312a1c..ee40fa4 100644 --- a/godot/Scenes/main.tscn +++ b/godot/Scenes/main.tscn @@ -8,5 +8,3 @@ [node name="Network" parent="." instance=ExtResource("1_06ibn")] [node name="MainMenu" parent="." instance=ExtResource("2_lavg1")] - -[connection signal="change_scene" from="MainMenu" to="." method="change_scene"] diff --git a/include/client/World.h b/include/blitz/godot/World.h similarity index 96% rename from include/client/World.h rename to include/blitz/godot/World.h index 183221b..e699a9e 100644 --- a/include/client/World.h +++ b/include/blitz/godot/World.h @@ -14,13 +14,12 @@ class World : public godot::Node3D, public protocol::PacketHandler { protected: static void _bind_methods(); - public: World(); ~World(); + public: // Godot overrides void _ready() override; - void _process(float delta); Player* GetPlayerById(PlayerID a_PlayerId); @@ -28,7 +27,7 @@ class World : public godot::Node3D, public protocol::PacketHandler { void HandlePacket(const protocol::packets::PlayerLeave&) override; void HandlePacket(const protocol::packets::PlayerPositionAndRotation&) override; - private: + protected: NetworkInterface* m_NetworkInterface; godot::Node* m_Players; float m_PassedTime; diff --git a/include/client/ClientWorld.h b/include/client/ClientWorld.h new file mode 100644 index 0000000..4b8087b --- /dev/null +++ b/include/client/ClientWorld.h @@ -0,0 +1,19 @@ +#include + +namespace blitz { + +class ClientWorld : public World { + GDCLASS(ClientWorld, World) + protected: + static void _bind_methods(); + + public: + ClientWorld(); + ~ClientWorld(); + void _process(float delta); + + private: + void UpdatePlayerPos(); +}; + +} // namespace blitz \ No newline at end of file diff --git a/include/client/Main.h b/include/client/Main.h index 6e6bfca..71bf9b4 100644 --- a/include/client/Main.h +++ b/include/client/Main.h @@ -13,7 +13,9 @@ class Main : public godot::Node { Main(); ~Main(); - void ChangeScene(); + void _ready() override; + + void ChangeScene(bool a_Server); }; } // namespace blitz \ No newline at end of file diff --git a/include/server/ServerWorld.h b/include/server/ServerWorld.h new file mode 100644 index 0000000..2f609f8 --- /dev/null +++ b/include/server/ServerWorld.h @@ -0,0 +1,19 @@ +#include + +namespace blitz { + +class ServerWorld : public World { + GDCLASS(ServerWorld, World) + protected: + static void _bind_methods(); + + public: + ServerWorld(); + ~ServerWorld(); + void _process(float delta); + + private: + void SyncPlayersPos(); +}; + +} // namespace blitz \ No newline at end of file diff --git a/src/client/World.cpp b/src/blitz/godot/World.cpp similarity index 78% rename from src/client/World.cpp rename to src/blitz/godot/World.cpp index 2d4d598..a0b0bb3 100644 --- a/src/client/World.cpp +++ b/src/blitz/godot/World.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -44,37 +44,6 @@ World::~World() { m_NetworkInterface->UnregisterHandler(*this); } -void World::_process(float delta) { -#if DEBUG_ENABLED - if (Engine::get_singleton()->is_editor_hint()) - return; -#endif - m_PassedTime += delta; - if (m_PassedTime < 0.05f) - return; - - // UtilityFunctions::print(m_PassedTime); - - // m_PassedTime -= 0.05f; - // if (m_PassedTime > 0.5f) - // m_PassedTime = 0.0f; - - if (get_multiplayer()->is_server()) { - for (int i = 0; i < m_Players->get_child_count(); i++) { - Player* player = Object::cast_to(m_Players->get_child(i)); - DEV_ASSERT(player); - m_NetworkInterface->BroadcastPacket( - protocol::packets::PlayerPositionAndRotation({player->GetId(), player->get_position(), player->GetCameraRotation()})); - } - } else { - Player* player = GetPlayerById(get_multiplayer()->get_unique_id()); - if (player) { - m_NetworkInterface->BroadcastPacket(protocol::packets::PlayerPositionAndRotation( - {get_multiplayer()->get_unique_id(), player->get_position(), player->GetCameraRotation()})); - } - } -} - Player* World::GetPlayerById(PlayerID a_PlayerId) { String stringId = UtilityFunctions::var_to_str(a_PlayerId); for (int i = 0; i < m_Players->get_child_count(); i++) { diff --git a/src/client/ClientWorld.cpp b/src/client/ClientWorld.cpp new file mode 100644 index 0000000..f140374 --- /dev/null +++ b/src/client/ClientWorld.cpp @@ -0,0 +1,39 @@ +#include + +#include +#include +#include +#include + +namespace blitz { + +using namespace godot; + +void ClientWorld::_bind_methods() {} + +ClientWorld::ClientWorld() {} + +ClientWorld::~ClientWorld() {} + +void ClientWorld::_process(float delta) { +#if DEBUG_ENABLED + if (Engine::get_singleton()->is_editor_hint()) + return; +#endif + m_PassedTime += delta; + if (m_PassedTime < 0.05f) + return; + + + UpdatePlayerPos(); +} + +void ClientWorld::UpdatePlayerPos() { + Player* player = GetPlayerById(get_multiplayer()->get_unique_id()); + if (player) { + m_NetworkInterface->BroadcastPacket(protocol::packets::PlayerPositionAndRotation( + {get_multiplayer()->get_unique_id(), player->get_position(), player->GetCameraRotation()})); + } +} + +} // namespace blitz \ No newline at end of file diff --git a/src/client/Main.cpp b/src/client/Main.cpp index 920e8b2..b38f556 100644 --- a/src/client/Main.cpp +++ b/src/client/Main.cpp @@ -7,26 +7,36 @@ #include #include -#include +#include +#include using namespace godot; namespace blitz { -static constexpr char MainScenePath[] = "res://Scenes/Levels/world.tscn"; +static constexpr char ClientWorldScenePath[] = "res://Scenes/Levels/client_world.tscn"; +static constexpr char ServerWorldScenePath[] = "res://Scenes/Levels/server_world.tscn"; -void Main::_bind_methods() { - godot::ClassDB::bind_method(godot::D_METHOD("change_scene"), &Main::ChangeScene); +void Main::_bind_methods() {} + +void Main::_ready() { + auto* mainMenu = find_child("MainMenu"); + DEV_ASSERT(mainMenu); + mainMenu->connect("change_scene_to_game", callable_mp(this, &Main::ChangeScene)); } Main::Main() {} Main::~Main() {} -void Main::ChangeScene() { - Ref sceneData = ResourceLoader::get_singleton()->load(MainScenePath); - World* world = Object::cast_to(sceneData->instantiate()); - add_child(world); +void Main::ChangeScene(bool a_Server) { + Ref sceneData; + if (a_Server) + sceneData = ResourceLoader::get_singleton()->load(ServerWorldScenePath); + else + sceneData = ResourceLoader::get_singleton()->load(ClientWorldScenePath); + + add_child(sceneData->instantiate()); } } // namespace blitz \ No newline at end of file diff --git a/src/client/MainMenu.cpp b/src/client/MainMenu.cpp index 7308ddc..2a02168 100644 --- a/src/client/MainMenu.cpp +++ b/src/client/MainMenu.cpp @@ -1,5 +1,7 @@ #include +#include +#include #include #include @@ -9,7 +11,7 @@ namespace blitz { void MainMenu::_bind_methods() { godot::ClassDB::bind_method(godot::D_METHOD("on_connected"), &MainMenu::OnConnected); - ADD_SIGNAL(MethodInfo("change_scene")); + ADD_SIGNAL(MethodInfo("change_scene_to_game", PropertyInfo(Variant::BOOL, "server"))); } MainMenu::MainMenu() {} @@ -28,19 +30,22 @@ void MainMenu::_ready() { DEV_ASSERT(m_CreateButton); DEV_ASSERT(m_QuitButton); - m_NetworkInterface = Object::cast_to(get_parent()->find_child("Network")); - DEV_ASSERT(m_NetworkInterface); - m_JoinButton->connect("pressed", callable_mp(this, &MainMenu::OnJoinPressed)); m_CreateButton->connect("pressed", callable_mp(this, &MainMenu::OnCreatePressed)); m_QuitButton->connect("pressed", callable_mp(this, &MainMenu::OnQuitPressed)); - m_NetworkInterface->connect("local_player_connected", callable_mp(this, &MainMenu::OnConnected)); - m_NetworkInterface->connect("server_disconnected", callable_mp(this, &MainMenu::OnDisconnected)); + + if (!Engine::get_singleton()->is_editor_hint()) { + m_NetworkInterface = Object::cast_to(get_parent()->find_child("Network")); + DEV_ASSERT(m_NetworkInterface); + + m_NetworkInterface->connect("local_player_connected", callable_mp(this, &MainMenu::OnConnected)); + m_NetworkInterface->connect("server_disconnected", callable_mp(this, &MainMenu::OnDisconnected)); + } } void MainMenu::OnConnected() { - emit_signal("change_scene"); + emit_signal("change_scene_to_game", get_multiplayer()->is_server()); set_visible(false); } diff --git a/src/client/register_types.cpp b/src/client/register_types.cpp index 1a3e84b..99b775e 100644 --- a/src/client/register_types.cpp +++ b/src/client/register_types.cpp @@ -1,10 +1,11 @@ #include +#include #include #include #include #include #include -#include +#include #include #include @@ -16,10 +17,12 @@ static void RegisterClasses() { GDREGISTER_CLASS(blitz::Player); GDREGISTER_CLASS(blitz::FirstPersonPlayer); GDREGISTER_CLASS(blitz::MainMenu); - GDREGISTER_CLASS(blitz::World); GDREGISTER_CLASS(blitz::Main); GDREGISTER_CLASS(blitz::NetworkInterface); GDREGISTER_CLASS(blitz::Server); + GDREGISTER_ABSTRACT_CLASS(blitz::World); + GDREGISTER_CLASS(blitz::ClientWorld); + GDREGISTER_CLASS(blitz::ServerWorld); } static void initialize_blitz_module(ModuleInitializationLevel p_level) { diff --git a/src/server/ServerWorld.cpp b/src/server/ServerWorld.cpp new file mode 100644 index 0000000..568e4cc --- /dev/null +++ b/src/server/ServerWorld.cpp @@ -0,0 +1,38 @@ +#include + +#include +#include +#include + +namespace blitz { + +using namespace godot; + +void ServerWorld::_bind_methods() {} + +ServerWorld::ServerWorld() {} + +ServerWorld::~ServerWorld() {} + +void ServerWorld::_process(float delta) { +#if DEBUG_ENABLED + if (Engine::get_singleton()->is_editor_hint()) + return; +#endif + m_PassedTime += delta; + if (m_PassedTime < 0.05f) + return; + + SyncPlayersPos(); +} + +void ServerWorld::SyncPlayersPos() { + for (int i = 0; i < m_Players->get_child_count(); i++) { + Player* player = Object::cast_to(m_Players->get_child(i)); + DEV_ASSERT(player); + m_NetworkInterface->BroadcastPacket( + protocol::packets::PlayerPositionAndRotation({player->GetId(), player->get_position(), player->GetCameraRotation()})); + } +} + +} // namespace blitz \ No newline at end of file