generated from Persson-dev/Godot-Xmake
Compare commits
6 Commits
770e0281ef
...
a092f6fbc1
| Author | SHA1 | Date | |
|---|---|---|---|
| a092f6fbc1 | |||
| 3cebb70289 | |||
| 27a3581af1 | |||
| 5c1793c1e7 | |||
| 211533d967 | |||
| 7948e0ce3a |
7
godot/Scenes/Levels/client_world.tscn
Normal file
7
godot/Scenes/Levels/client_world.tscn
Normal file
@@ -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")]
|
||||||
7
godot/Scenes/Levels/server_world.tscn
Normal file
7
godot/Scenes/Levels/server_world.tscn
Normal file
@@ -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")]
|
||||||
@@ -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/Sky.png" id="1_tcyn8"]
|
||||||
[ext_resource type="Texture2D" path="res://Assets/Textures/Black.png" id="2_fkwcn"]
|
[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_ux02w"]
|
[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_wp15n"]
|
[ext_resource type="Texture2D" path="res://Assets/Textures/Green.png" id="4_klpsf"]
|
||||||
|
|
||||||
[sub_resource type="PanoramaSkyMaterial" id="PanoramaSkyMaterial_6c4vd"]
|
[sub_resource type="PanoramaSkyMaterial" id="PanoramaSkyMaterial_6c4vd"]
|
||||||
panorama = ExtResource("1_mnexj")
|
panorama = ExtResource("1_tcyn8")
|
||||||
|
|
||||||
[sub_resource type="Sky" id="Sky_5ngqa"]
|
[sub_resource type="Sky" id="Sky_5ngqa"]
|
||||||
sky_material = SubResource("PanoramaSkyMaterial_6c4vd")
|
sky_material = SubResource("PanoramaSkyMaterial_6c4vd")
|
||||||
@@ -18,7 +18,7 @@ tonemap_mode = 2
|
|||||||
glow_enabled = true
|
glow_enabled = true
|
||||||
|
|
||||||
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ajchh"]
|
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ajchh"]
|
||||||
albedo_texture = ExtResource("2_fkwcn")
|
albedo_texture = ExtResource("2_j33w8")
|
||||||
uv1_triplanar = true
|
uv1_triplanar = true
|
||||||
|
|
||||||
[sub_resource type="PlaneMesh" id="PlaneMesh_mmup0"]
|
[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)
|
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"]
|
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_jkvud"]
|
||||||
albedo_texture = ExtResource("3_ux02w")
|
albedo_texture = ExtResource("3_n1lus")
|
||||||
uv1_triplanar = true
|
uv1_triplanar = true
|
||||||
|
|
||||||
[sub_resource type="BoxMesh" id="BoxMesh_plpqy"]
|
[sub_resource type="BoxMesh" id="BoxMesh_plpqy"]
|
||||||
@@ -44,13 +44,13 @@ left_to_right = -2.0
|
|||||||
size = Vector3(5, 5, 5)
|
size = Vector3(5, 5, 5)
|
||||||
|
|
||||||
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_pfpgv"]
|
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_pfpgv"]
|
||||||
albedo_texture = ExtResource("4_wp15n")
|
albedo_texture = ExtResource("4_klpsf")
|
||||||
uv1_triplanar = true
|
uv1_triplanar = true
|
||||||
|
|
||||||
[sub_resource type="ConcavePolygonShape3D" id="ConcavePolygonShape3D_rit6o"]
|
[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)
|
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="."]
|
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
|
||||||
environment = SubResource("Environment_ctwiv")
|
environment = SubResource("Environment_ctwiv")
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
[gd_scene format=3 uid="uid://clafls1xhludi"]
|
[gd_scene format=3 uid="uid://clafls1xhludi"]
|
||||||
|
|
||||||
[node name="Lobby" type="Lobby"]
|
[node name="NetworkInterface" type="NetworkInterface"]
|
||||||
|
|
||||||
[node name="NetworkInterface" type="NetworkInterface" parent="."]
|
|
||||||
|
|||||||
@@ -5,11 +5,6 @@
|
|||||||
|
|
||||||
[node name="Main" type="Main"]
|
[node name="Main" type="Main"]
|
||||||
|
|
||||||
[node name="Lobby" parent="." instance=ExtResource("1_06ibn")]
|
[node name="Network" parent="." instance=ExtResource("1_06ibn")]
|
||||||
|
|
||||||
[node name="MainMenu" parent="." instance=ExtResource("2_lavg1")]
|
[node name="MainMenu" parent="." instance=ExtResource("2_lavg1")]
|
||||||
|
|
||||||
[connection signal="local_player_connected" from="Lobby" to="MainMenu" method="on_connected"]
|
|
||||||
[connection signal="change_scene" from="MainMenu" to="." method="change_scene"]
|
|
||||||
[connection signal="create_game" from="MainMenu" to="Lobby" method="create_game"]
|
|
||||||
[connection signal="join_game" from="MainMenu" to="Lobby" method="join_game"]
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <blitz/protocol/PacketDispatcher.h>
|
||||||
#include <blitz/protocol/Packets.h>
|
#include <blitz/protocol/Packets.h>
|
||||||
#include <godot_cpp/classes/node.hpp>
|
#include <godot_cpp/classes/node.hpp>
|
||||||
#include <blitz/protocol/PacketDispatcher.h>
|
|
||||||
|
|
||||||
namespace blitz {
|
namespace blitz {
|
||||||
|
|
||||||
class NetworkInterface : public godot::Node, public protocol::PacketDispatcher {
|
class NetworkInterface : public godot::Node, public protocol::PacketDispatcher {
|
||||||
GDCLASS(NetworkInterface, godot::Node)
|
GDCLASS(NetworkInterface, godot::Node)
|
||||||
protected:
|
protected:
|
||||||
@@ -17,10 +18,21 @@ class NetworkInterface : public godot::Node, public protocol::PacketDispatcher {
|
|||||||
void BroadcastPacket(const protocol::Packet& a_Packet);
|
void BroadcastPacket(const protocol::Packet& a_Packet);
|
||||||
void SendPacket(PeerID a_Peer, const protocol::Packet& a_Packet);
|
void SendPacket(PeerID a_Peer, const protocol::Packet& a_Packet);
|
||||||
|
|
||||||
|
godot::Error JoinGame(const godot::String& a_Address, uint16_t a_Port);
|
||||||
|
godot::Error CreateGame(uint16_t a_Port, bool a_Dedicated = false);
|
||||||
|
|
||||||
|
void ShutdownNetwork();
|
||||||
|
|
||||||
void _ready() override;
|
void _ready() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void RecievePacketDataReliable(godot::PackedByteArray a_PacketData);
|
void RecievePacketDataReliable(godot::PackedByteArray a_PacketData);
|
||||||
|
|
||||||
|
void OnPlayerConnected(PeerID a_PeerId);
|
||||||
|
void OnPlayerDisconnected(PeerID a_PeerId);
|
||||||
|
void OnConnectOk();
|
||||||
|
void OnConnectFail();
|
||||||
|
void OnServerDisconnected();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace blitz
|
} // namespace blitz
|
||||||
@@ -14,13 +14,12 @@ class World : public godot::Node3D, public protocol::PacketHandler {
|
|||||||
protected:
|
protected:
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
|
||||||
public:
|
|
||||||
World();
|
World();
|
||||||
~World();
|
~World();
|
||||||
|
|
||||||
|
public:
|
||||||
// Godot overrides
|
// Godot overrides
|
||||||
void _ready() override;
|
void _ready() override;
|
||||||
void _process(float delta);
|
|
||||||
|
|
||||||
Player* GetPlayerById(PlayerID a_PlayerId);
|
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::PlayerLeave&) override;
|
||||||
void HandlePacket(const protocol::packets::PlayerPositionAndRotation&) override;
|
void HandlePacket(const protocol::packets::PlayerPositionAndRotation&) override;
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
NetworkInterface* m_NetworkInterface;
|
NetworkInterface* m_NetworkInterface;
|
||||||
godot::Node* m_Players;
|
godot::Node* m_Players;
|
||||||
float m_PassedTime;
|
float m_PassedTime;
|
||||||
19
include/client/ClientWorld.h
Normal file
19
include/client/ClientWorld.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#include <blitz/godot/World.h>
|
||||||
|
|
||||||
|
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
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Player.h"
|
#include <client/Player.h>
|
||||||
#include <godot_cpp/classes/input_event_mouse_motion.hpp>
|
#include <godot_cpp/classes/input_event_mouse_motion.hpp>
|
||||||
|
|
||||||
namespace blitz {
|
namespace blitz {
|
||||||
@@ -13,7 +13,9 @@ class Main : public godot::Node {
|
|||||||
Main();
|
Main();
|
||||||
~Main();
|
~Main();
|
||||||
|
|
||||||
void ChangeScene();
|
void _ready() override;
|
||||||
|
|
||||||
|
void ChangeScene(bool a_Server);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace blitz
|
} // namespace blitz
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <godot_cpp/classes/button.hpp>
|
#include <godot_cpp/classes/button.hpp>
|
||||||
#include <godot_cpp/classes/control.hpp>
|
#include <godot_cpp/classes/control.hpp>
|
||||||
|
#include <blitz/godot/NetworkInterface.h>
|
||||||
|
|
||||||
namespace blitz {
|
namespace blitz {
|
||||||
|
|
||||||
@@ -22,11 +23,17 @@ class MainMenu : public godot::Control {
|
|||||||
godot::Button* m_CreateButton;
|
godot::Button* m_CreateButton;
|
||||||
godot::Button* m_QuitButton;
|
godot::Button* m_QuitButton;
|
||||||
|
|
||||||
|
NetworkInterface* m_NetworkInterface;
|
||||||
|
|
||||||
void OnConnected();
|
void OnConnected();
|
||||||
|
void OnDisconnected();
|
||||||
|
|
||||||
void OnJoinPressed();
|
void OnJoinPressed();
|
||||||
void OnCreatePressed();
|
void OnCreatePressed();
|
||||||
void OnQuitPressed();
|
void OnQuitPressed();
|
||||||
|
|
||||||
|
void DisableButtons();
|
||||||
|
void EnableButtons();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace blitz
|
} // namespace blitz
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <godot_cpp/classes/node.hpp>
|
|
||||||
#include <blitz/common/Types.h>
|
#include <blitz/common/Types.h>
|
||||||
|
#include <godot_cpp/classes/node.hpp>
|
||||||
|
|
||||||
namespace blitz {
|
namespace blitz {
|
||||||
|
|
||||||
@@ -19,14 +19,14 @@ class Server : public godot::Node {
|
|||||||
|
|
||||||
void _ready() override;
|
void _ready() override;
|
||||||
|
|
||||||
void OnPlayerConnect(PeerID a_PeerId);
|
void OnPlayerConnect(PeerID a_PeerId);
|
||||||
void OnPlayerDisconnect(PeerID a_PeerId);
|
void OnPlayerDisconnect(PeerID a_PeerId);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Lobby* m_Lobby;
|
Lobby* m_Lobby;
|
||||||
NetworkInterface* m_NetworkInterface;
|
NetworkInterface* m_NetworkInterface;
|
||||||
|
|
||||||
godot::TypedArray<PeerID> m_Peers;
|
godot::TypedArray<PeerID> m_Peers;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace blitz
|
} // namespace blitz
|
||||||
19
include/server/ServerWorld.h
Normal file
19
include/server/ServerWorld.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#include <blitz/godot/World.h>
|
||||||
|
|
||||||
|
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
|
||||||
@@ -1,99 +0,0 @@
|
|||||||
#include "Lobby.h"
|
|
||||||
|
|
||||||
#include <godot_cpp/classes/e_net_multiplayer_peer.hpp>
|
|
||||||
#include <godot_cpp/classes/multiplayer_api.hpp>
|
|
||||||
#include <godot_cpp/classes/packed_scene.hpp>
|
|
||||||
#include <godot_cpp/classes/resource_loader.hpp>
|
|
||||||
#include <godot_cpp/variant/dictionary.hpp>
|
|
||||||
#include <godot_cpp/variant/utility_functions.hpp>
|
|
||||||
|
|
||||||
using namespace godot;
|
|
||||||
|
|
||||||
static const char ServerScenePath[] = "res://Scenes/Network/server.tscn";
|
|
||||||
|
|
||||||
namespace blitz {
|
|
||||||
|
|
||||||
void Lobby::_bind_methods() {
|
|
||||||
godot::ClassDB::bind_method(godot::D_METHOD("create_game", "port", "dedicated"), &Lobby::CreateGame);
|
|
||||||
godot::ClassDB::bind_method(godot::D_METHOD("join_game", "address", "port"), &Lobby::JoinGame);
|
|
||||||
ADD_SIGNAL(MethodInfo("player_connected", PropertyInfo(Variant::INT, "peer_id")));
|
|
||||||
ADD_SIGNAL(MethodInfo("player_disconnected", PropertyInfo(Variant::INT, "peer_id")));
|
|
||||||
ADD_SIGNAL(MethodInfo("server_disconnected"));
|
|
||||||
ADD_SIGNAL(MethodInfo("local_player_connected"));
|
|
||||||
}
|
|
||||||
|
|
||||||
Lobby::Lobby() {}
|
|
||||||
|
|
||||||
Lobby::~Lobby() {}
|
|
||||||
|
|
||||||
void Lobby::_ready() {
|
|
||||||
get_multiplayer()->connect("peer_connected", callable_mp(this, &Lobby::OnPlayerConnected));
|
|
||||||
get_multiplayer()->connect("peer_disconnected", callable_mp(this, &Lobby::OnPlayerDisconnected));
|
|
||||||
get_multiplayer()->connect("connected_to_server", callable_mp(this, &Lobby::OnConnectOk));
|
|
||||||
get_multiplayer()->connect("connection_failed", callable_mp(this, &Lobby::OnConnectFail));
|
|
||||||
get_multiplayer()->connect("server_disconnected", callable_mp(this, &Lobby::OnServerDisconnected));
|
|
||||||
}
|
|
||||||
|
|
||||||
Error Lobby::JoinGame(const String& a_Address, uint16_t a_Port) {
|
|
||||||
auto* peer = memnew(ENetMultiplayerPeer);
|
|
||||||
Error error = peer->create_client(a_Address, a_Port);
|
|
||||||
if (error)
|
|
||||||
return error;
|
|
||||||
|
|
||||||
get_multiplayer()->set_multiplayer_peer(peer);
|
|
||||||
return Error::OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
Error Lobby::CreateGame(uint16_t a_Port, bool a_Dedicated) {
|
|
||||||
auto* peer = memnew(ENetMultiplayerPeer);
|
|
||||||
Error error = peer->create_server(a_Port);
|
|
||||||
if (error)
|
|
||||||
return error;
|
|
||||||
|
|
||||||
get_multiplayer()->set_multiplayer_peer(peer);
|
|
||||||
|
|
||||||
Ref<PackedScene> serverScene = ResourceLoader::get_singleton()->load(ServerScenePath);
|
|
||||||
add_child(serverScene->instantiate());
|
|
||||||
|
|
||||||
if (!a_Dedicated) {
|
|
||||||
emit_signal("local_player_connected");
|
|
||||||
emit_signal("player_connected", get_multiplayer()->get_unique_id());
|
|
||||||
}
|
|
||||||
|
|
||||||
return Error::OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Lobby::Shutdown() {
|
|
||||||
get_multiplayer()->set_multiplayer_peer(nullptr);
|
|
||||||
if (auto* server = find_child("Server")) {
|
|
||||||
remove_child(server);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Lobby::OnPlayerConnected(PeerID a_PeerId) {
|
|
||||||
if (get_multiplayer()->is_server()) {
|
|
||||||
emit_signal("player_connected", a_PeerId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Lobby::OnPlayerDisconnected(PeerID a_PeerId) {
|
|
||||||
if (get_multiplayer()->is_server()) {
|
|
||||||
emit_signal("player_disconnected", a_PeerId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Lobby::OnConnectOk() {
|
|
||||||
int32_t peerId = get_multiplayer()->get_unique_id();
|
|
||||||
emit_signal("local_player_connected");
|
|
||||||
}
|
|
||||||
|
|
||||||
void Lobby::OnConnectFail() {
|
|
||||||
Shutdown();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Lobby::OnServerDisconnected() {
|
|
||||||
Shutdown();
|
|
||||||
emit_signal("server_disconnected");
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace blitz
|
|
||||||
32
src/Lobby.h
32
src/Lobby.h
@@ -1,32 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <godot_cpp/classes/node.hpp>
|
|
||||||
#include "NetworkInterface.h"
|
|
||||||
|
|
||||||
namespace blitz {
|
|
||||||
|
|
||||||
class Lobby : public godot::Node {
|
|
||||||
GDCLASS(Lobby, godot::Node)
|
|
||||||
protected:
|
|
||||||
static void _bind_methods();
|
|
||||||
|
|
||||||
public:
|
|
||||||
Lobby();
|
|
||||||
~Lobby();
|
|
||||||
|
|
||||||
void _ready() override;
|
|
||||||
|
|
||||||
godot::Error JoinGame(const godot::String& a_Address, uint16_t a_Port);
|
|
||||||
godot::Error CreateGame(uint16_t a_Port, bool a_Dedicated = false);
|
|
||||||
|
|
||||||
void Shutdown();
|
|
||||||
|
|
||||||
private:
|
|
||||||
void OnPlayerConnected(PeerID a_PeerId);
|
|
||||||
void OnPlayerDisconnected(PeerID a_PeerId);
|
|
||||||
void OnConnectOk();
|
|
||||||
void OnConnectFail();
|
|
||||||
void OnServerDisconnected();
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace blitz
|
|
||||||
33
src/Main.cpp
33
src/Main.cpp
@@ -1,33 +0,0 @@
|
|||||||
#include "Main.h"
|
|
||||||
|
|
||||||
#include <godot_cpp/classes/engine.hpp>
|
|
||||||
#include <godot_cpp/classes/packed_scene.hpp>
|
|
||||||
#include <godot_cpp/classes/resource_loader.hpp>
|
|
||||||
#include <godot_cpp/classes/scene_tree.hpp>
|
|
||||||
#include <godot_cpp/classes/window.hpp>
|
|
||||||
#include <godot_cpp/variant/utility_functions.hpp>
|
|
||||||
|
|
||||||
#include "Lobby.h"
|
|
||||||
#include "World.h"
|
|
||||||
|
|
||||||
using namespace godot;
|
|
||||||
|
|
||||||
namespace blitz {
|
|
||||||
|
|
||||||
static constexpr char MainScenePath[] = "res://Scenes/Levels/world.tscn";
|
|
||||||
|
|
||||||
void Main::_bind_methods() {
|
|
||||||
godot::ClassDB::bind_method(godot::D_METHOD("change_scene"), &Main::ChangeScene);
|
|
||||||
}
|
|
||||||
|
|
||||||
Main::Main() {}
|
|
||||||
|
|
||||||
Main::~Main() {}
|
|
||||||
|
|
||||||
void Main::ChangeScene() {
|
|
||||||
Ref<PackedScene> sceneData = ResourceLoader::get_singleton()->load(MainScenePath);
|
|
||||||
World* world = Object::cast_to<World>(sceneData->instantiate());
|
|
||||||
add_child(world);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace blitz
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
#include "NetworkInterface.h"
|
|
||||||
|
|
||||||
#include <blitz/protocol/PacketFactory.h>
|
|
||||||
#include <blitz/protocol/PacketSerializer.h>
|
|
||||||
#include <godot_cpp/classes/multiplayer_api.hpp>
|
|
||||||
#include <godot_cpp/classes/multiplayer_peer.hpp>
|
|
||||||
#include <godot_cpp/variant/utility_functions.hpp>
|
|
||||||
|
|
||||||
namespace blitz {
|
|
||||||
|
|
||||||
using namespace godot;
|
|
||||||
|
|
||||||
void NetworkInterface::_bind_methods() {
|
|
||||||
ClassDB::bind_method(D_METHOD("RecievePacketDataReliable", "a_PacketData"), &NetworkInterface::RecievePacketDataReliable);
|
|
||||||
protocol::PacketFactory::Init();
|
|
||||||
}
|
|
||||||
|
|
||||||
NetworkInterface::NetworkInterface() {}
|
|
||||||
|
|
||||||
NetworkInterface::~NetworkInterface() {}
|
|
||||||
|
|
||||||
void NetworkInterface::_ready() {
|
|
||||||
// TODO: unreliable
|
|
||||||
Dictionary config;
|
|
||||||
config["rpc_mode"] = MultiplayerAPI::RPC_MODE_ANY_PEER;
|
|
||||||
config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
|
|
||||||
config["call_local"] = true;
|
|
||||||
config["channel"] = 0;
|
|
||||||
rpc_config("RecievePacketDataReliable", config);
|
|
||||||
}
|
|
||||||
|
|
||||||
void NetworkInterface::BroadcastPacket(const protocol::Packet& a_Packet) {
|
|
||||||
PackedByteArray byteArray = protocol::PacketSerializer::Serialize(a_Packet);
|
|
||||||
rpc("RecievePacketDataReliable", byteArray);
|
|
||||||
}
|
|
||||||
|
|
||||||
void NetworkInterface::SendPacket(PeerID a_Peer, const protocol::Packet& a_Packet) {
|
|
||||||
PackedByteArray byteArray = protocol::PacketSerializer::Serialize(a_Packet);
|
|
||||||
rpc_id(a_Peer, "RecievePacketDataReliable", byteArray);
|
|
||||||
}
|
|
||||||
|
|
||||||
void NetworkInterface::RecievePacketDataReliable(godot::PackedByteArray a_PacketData) {
|
|
||||||
auto packet = protocol::PacketSerializer::Deserialize(a_PacketData);
|
|
||||||
if (packet) {
|
|
||||||
packet->m_Sender = get_multiplayer()->get_remote_sender_id();
|
|
||||||
Dispatch(*packet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace blitz
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <godot_cpp/variant/string.hpp>
|
|
||||||
|
|
||||||
namespace blitz {
|
|
||||||
|
|
||||||
struct PlayerInfo {
|
|
||||||
godot::String m_Name;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace blitz
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
#include "SpringArmPivot.h"
|
|
||||||
|
|
||||||
#include "Player.h"
|
|
||||||
#include <godot_cpp/classes/engine.hpp>
|
|
||||||
#include <godot_cpp/classes/input.hpp>
|
|
||||||
#include <godot_cpp/classes/input_event_mouse_motion.hpp>
|
|
||||||
#include <godot_cpp/classes/spring_arm3d.hpp>
|
|
||||||
#include <godot_cpp/core/error_macros.hpp>
|
|
||||||
#include <godot_cpp/core/math.hpp>
|
|
||||||
#include <godot_cpp/variant/utility_functions.hpp>
|
|
||||||
|
|
||||||
static const float NormalFov = 75.0;
|
|
||||||
static const float RunFov = 90.0;
|
|
||||||
static const float CameraBlend = 0.05;
|
|
||||||
|
|
||||||
namespace blitz {
|
|
||||||
|
|
||||||
void SpringArmPivot::_bind_methods() {
|
|
||||||
godot::ClassDB::bind_method(godot::D_METHOD("get_dynfov"), &SpringArmPivot::IsFovDynamic);
|
|
||||||
godot::ClassDB::bind_method(godot::D_METHOD("set_dynfov", "dynamic_fov"), &SpringArmPivot::SetDynamicFov);
|
|
||||||
ADD_PROPERTY(godot::PropertyInfo(godot::Variant::BOOL, "dynamic_fov"), "set_dynfov", "get_dynfov");
|
|
||||||
}
|
|
||||||
|
|
||||||
SpringArmPivot::SpringArmPivot() {}
|
|
||||||
|
|
||||||
SpringArmPivot::~SpringArmPivot() {}
|
|
||||||
|
|
||||||
void SpringArmPivot::_ready() {
|
|
||||||
m_SpringArm = Object::cast_to<godot::SpringArm3D>(get_child(0));
|
|
||||||
m_Camera = Object::cast_to<godot::Camera3D>(m_SpringArm->get_child(0));
|
|
||||||
DEV_ASSERT(m_SpringArm);
|
|
||||||
DEV_ASSERT(m_Camera);
|
|
||||||
if (!godot::Engine::get_singleton()->is_editor_hint()) {
|
|
||||||
godot::Input::get_singleton()->set_mouse_mode(godot::Input::MOUSE_MODE_CAPTURED);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SpringArmPivot::_unhandled_input(const godot::Ref<godot::InputEvent>& p_event) {
|
|
||||||
auto* event = Object::cast_to<godot::InputEventMouseMotion>(p_event.ptr());
|
|
||||||
if (event) {
|
|
||||||
rotate_y(-event->get_relative().x * 0.005);
|
|
||||||
m_SpringArm->rotate_x(-event->get_relative().y * 0.005);
|
|
||||||
|
|
||||||
godot::Vector3 rotationClamped = m_SpringArm->get_rotation();
|
|
||||||
rotationClamped.x = godot::UtilityFunctions::clamp(rotationClamped.x, -Math_PI / 4, Math_PI / 4);
|
|
||||||
m_SpringArm->set_rotation(rotationClamped);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SpringArmPivot::_physics_process(float delta) {
|
|
||||||
if (m_DynamicFOV) {
|
|
||||||
auto* parent = Object::cast_to<Player>(get_owner());
|
|
||||||
if (parent->is_on_floor()) {
|
|
||||||
if (godot::Input::get_singleton()->is_action_pressed("run")) {
|
|
||||||
m_Camera->set_fov(godot::UtilityFunctions::lerp(m_Camera->get_fov(), RunFov, CameraBlend));
|
|
||||||
} else {
|
|
||||||
m_Camera->set_fov(godot::UtilityFunctions::lerp(m_Camera->get_fov(), NormalFov, CameraBlend));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
m_Camera->set_fov(godot::UtilityFunctions::lerp(m_Camera->get_fov(), NormalFov, CameraBlend));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace blitz
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <godot_cpp/classes/input_event.hpp>
|
|
||||||
#include <godot_cpp/classes/node3d.hpp>
|
|
||||||
#include <godot_cpp/classes/spring_arm3d.hpp>
|
|
||||||
#include <godot_cpp/classes/camera3d.hpp>
|
|
||||||
|
|
||||||
namespace blitz {
|
|
||||||
class SpringArmPivot : public godot::Node3D {
|
|
||||||
|
|
||||||
GDCLASS(SpringArmPivot, godot::Node3D);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
static void _bind_methods();
|
|
||||||
|
|
||||||
public:
|
|
||||||
SpringArmPivot();
|
|
||||||
~SpringArmPivot();
|
|
||||||
|
|
||||||
void _ready();
|
|
||||||
void _unhandled_input(const godot::Ref<godot::InputEvent>& p_event);
|
|
||||||
void _physics_process(float delta);
|
|
||||||
|
|
||||||
void SetDynamicFov(bool fov) {
|
|
||||||
m_DynamicFOV = fov;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsFovDynamic() const {
|
|
||||||
return m_DynamicFOV;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
godot::SpringArm3D* m_SpringArm;
|
|
||||||
godot::Camera3D* m_Camera;
|
|
||||||
|
|
||||||
bool m_DynamicFOV = false;
|
|
||||||
};
|
|
||||||
} // namespace blitz
|
|
||||||
129
src/blitz/godot/NetworkInterface.cpp
Normal file
129
src/blitz/godot/NetworkInterface.cpp
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
#include <blitz/godot/NetworkInterface.h>
|
||||||
|
|
||||||
|
#include <blitz/protocol/PacketFactory.h>
|
||||||
|
#include <blitz/protocol/PacketSerializer.h>
|
||||||
|
#include <godot_cpp/classes/e_net_multiplayer_peer.hpp>
|
||||||
|
#include <godot_cpp/classes/multiplayer_api.hpp>
|
||||||
|
#include <godot_cpp/classes/packed_scene.hpp>
|
||||||
|
#include <godot_cpp/classes/resource_loader.hpp>
|
||||||
|
#include <godot_cpp/variant/utility_functions.hpp>
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
|
||||||
|
static const char ServerScenePath[] = "res://Scenes/Network/server.tscn";
|
||||||
|
|
||||||
|
using namespace godot;
|
||||||
|
|
||||||
|
void NetworkInterface::_bind_methods() {
|
||||||
|
ClassDB::bind_method(D_METHOD("RecievePacketDataReliable", "a_PacketData"), &NetworkInterface::RecievePacketDataReliable);
|
||||||
|
|
||||||
|
// server
|
||||||
|
ADD_SIGNAL(MethodInfo("player_connected", PropertyInfo(Variant::INT, "peer_id")));
|
||||||
|
ADD_SIGNAL(MethodInfo("player_disconnected", PropertyInfo(Variant::INT, "peer_id")));
|
||||||
|
|
||||||
|
// client
|
||||||
|
ADD_SIGNAL(MethodInfo("server_disconnected"));
|
||||||
|
ADD_SIGNAL(MethodInfo("local_player_connected"));
|
||||||
|
|
||||||
|
protocol::PacketFactory::Init();
|
||||||
|
}
|
||||||
|
|
||||||
|
NetworkInterface::NetworkInterface() {}
|
||||||
|
|
||||||
|
NetworkInterface::~NetworkInterface() {}
|
||||||
|
|
||||||
|
void NetworkInterface::_ready() {
|
||||||
|
// TODO: unreliable
|
||||||
|
Dictionary config;
|
||||||
|
config["rpc_mode"] = MultiplayerAPI::RPC_MODE_ANY_PEER;
|
||||||
|
config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
|
||||||
|
config["call_local"] = true;
|
||||||
|
config["channel"] = 0;
|
||||||
|
rpc_config("RecievePacketDataReliable", config);
|
||||||
|
|
||||||
|
get_multiplayer()->connect("peer_connected", callable_mp(this, &NetworkInterface::OnPlayerConnected));
|
||||||
|
get_multiplayer()->connect("peer_disconnected", callable_mp(this, &NetworkInterface::OnPlayerDisconnected));
|
||||||
|
get_multiplayer()->connect("connected_to_server", callable_mp(this, &NetworkInterface::OnConnectOk));
|
||||||
|
get_multiplayer()->connect("connection_failed", callable_mp(this, &NetworkInterface::OnConnectFail));
|
||||||
|
get_multiplayer()->connect("server_disconnected", callable_mp(this, &NetworkInterface::OnServerDisconnected));
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkInterface::BroadcastPacket(const protocol::Packet& a_Packet) {
|
||||||
|
PackedByteArray byteArray = protocol::PacketSerializer::Serialize(a_Packet);
|
||||||
|
rpc("RecievePacketDataReliable", byteArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkInterface::SendPacket(PeerID a_Peer, const protocol::Packet& a_Packet) {
|
||||||
|
PackedByteArray byteArray = protocol::PacketSerializer::Serialize(a_Packet);
|
||||||
|
rpc_id(a_Peer, "RecievePacketDataReliable", byteArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkInterface::RecievePacketDataReliable(godot::PackedByteArray a_PacketData) {
|
||||||
|
auto packet = protocol::PacketSerializer::Deserialize(a_PacketData);
|
||||||
|
if (packet) {
|
||||||
|
packet->m_Sender = get_multiplayer()->get_remote_sender_id();
|
||||||
|
Dispatch(*packet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Error NetworkInterface::JoinGame(const String& a_Address, uint16_t a_Port) {
|
||||||
|
auto* peer = memnew(ENetMultiplayerPeer);
|
||||||
|
Error error = peer->create_client(a_Address, a_Port);
|
||||||
|
if (error) {
|
||||||
|
OnConnectFail();
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
get_multiplayer()->set_multiplayer_peer(peer);
|
||||||
|
return Error::OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
Error NetworkInterface::CreateGame(uint16_t a_Port, bool a_Dedicated) {
|
||||||
|
auto* peer = memnew(ENetMultiplayerPeer);
|
||||||
|
Error error = peer->create_server(a_Port);
|
||||||
|
if (error)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
get_multiplayer()->set_multiplayer_peer(peer);
|
||||||
|
|
||||||
|
Ref<PackedScene> serverScene = ResourceLoader::get_singleton()->load(ServerScenePath);
|
||||||
|
add_child(serverScene->instantiate());
|
||||||
|
|
||||||
|
if (!a_Dedicated) {
|
||||||
|
emit_signal("local_player_connected");
|
||||||
|
emit_signal("player_connected", get_multiplayer()->get_unique_id());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Error::OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkInterface::OnPlayerConnected(PeerID a_PeerId) {
|
||||||
|
emit_signal("player_connected", a_PeerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkInterface::OnPlayerDisconnected(PeerID a_PeerId) {
|
||||||
|
emit_signal("player_disconnected", a_PeerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkInterface::OnConnectOk() {
|
||||||
|
emit_signal("local_player_connected");
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkInterface::OnConnectFail() {
|
||||||
|
ShutdownNetwork();
|
||||||
|
emit_signal("server_disconnected");
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkInterface::OnServerDisconnected() {
|
||||||
|
ShutdownNetwork();
|
||||||
|
emit_signal("server_disconnected");
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkInterface::ShutdownNetwork() {
|
||||||
|
get_multiplayer()->set_multiplayer_peer(nullptr);
|
||||||
|
if (auto* server = find_child("Server")) {
|
||||||
|
server->queue_free();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace blitz
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
#include "World.h"
|
#include <blitz/godot/World.h>
|
||||||
|
|
||||||
#include "FirstPersonPlayer.h"
|
#include <blitz/godot/NetworkInterface.h>
|
||||||
#include "NetworkInterface.h"
|
#include <client/FirstPersonPlayer.h>
|
||||||
#include "Player.h"
|
#include <client/Player.h>
|
||||||
#include <godot_cpp/classes/engine.hpp>
|
#include <godot_cpp/classes/engine.hpp>
|
||||||
#include <godot_cpp/classes/multiplayer_api.hpp>
|
#include <godot_cpp/classes/multiplayer_api.hpp>
|
||||||
#include <godot_cpp/classes/packed_scene.hpp>
|
#include <godot_cpp/classes/packed_scene.hpp>
|
||||||
@@ -26,10 +26,7 @@ void World::_ready() {
|
|||||||
m_Players = find_child("Players");
|
m_Players = find_child("Players");
|
||||||
DEV_ASSERT(m_Players);
|
DEV_ASSERT(m_Players);
|
||||||
|
|
||||||
auto* lobby = get_parent()->find_child("Lobby");
|
m_NetworkInterface = Object::cast_to<NetworkInterface>(get_parent()->find_child("Network"));
|
||||||
DEV_ASSERT(lobby);
|
|
||||||
|
|
||||||
m_NetworkInterface = Object::cast_to<NetworkInterface>(lobby->find_child("NetworkInterface"));
|
|
||||||
DEV_ASSERT(m_NetworkInterface);
|
DEV_ASSERT(m_NetworkInterface);
|
||||||
|
|
||||||
m_NetworkInterface->RegisterHandler(protocol::PacketType::PlayerJoin, *this);
|
m_NetworkInterface->RegisterHandler(protocol::PacketType::PlayerJoin, *this);
|
||||||
@@ -47,37 +44,6 @@ World::~World() {
|
|||||||
m_NetworkInterface->UnregisterHandler(*this);
|
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<Player>(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) {
|
Player* World::GetPlayerById(PlayerID a_PlayerId) {
|
||||||
String stringId = UtilityFunctions::var_to_str(a_PlayerId);
|
String stringId = UtilityFunctions::var_to_str(a_PlayerId);
|
||||||
for (int i = 0; i < m_Players->get_child_count(); i++) {
|
for (int i = 0; i < m_Players->get_child_count(); i++) {
|
||||||
39
src/client/ClientWorld.cpp
Normal file
39
src/client/ClientWorld.cpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#include <client/ClientWorld.h>
|
||||||
|
|
||||||
|
#include <blitz/godot/NetworkInterface.h>
|
||||||
|
#include <client/Player.h>
|
||||||
|
#include <godot_cpp/classes/engine.hpp>
|
||||||
|
#include <godot_cpp/classes/multiplayer_api.hpp>
|
||||||
|
|
||||||
|
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
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "FirstPersonPlayer.h"
|
#include <client/FirstPersonPlayer.h>
|
||||||
|
|
||||||
#include <godot_cpp/classes/camera3d.hpp>
|
#include <godot_cpp/classes/camera3d.hpp>
|
||||||
#include <godot_cpp/classes/engine.hpp>
|
#include <godot_cpp/classes/engine.hpp>
|
||||||
42
src/client/Main.cpp
Normal file
42
src/client/Main.cpp
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#include <client/Main.h>
|
||||||
|
|
||||||
|
#include <godot_cpp/classes/engine.hpp>
|
||||||
|
#include <godot_cpp/classes/packed_scene.hpp>
|
||||||
|
#include <godot_cpp/classes/resource_loader.hpp>
|
||||||
|
#include <godot_cpp/classes/scene_tree.hpp>
|
||||||
|
#include <godot_cpp/classes/window.hpp>
|
||||||
|
#include <godot_cpp/variant/utility_functions.hpp>
|
||||||
|
|
||||||
|
#include <client/ClientWorld.h>
|
||||||
|
#include <server/ServerWorld.h>
|
||||||
|
|
||||||
|
using namespace godot;
|
||||||
|
|
||||||
|
namespace blitz {
|
||||||
|
|
||||||
|
static constexpr char ClientWorldScenePath[] = "res://Scenes/Levels/client_world.tscn";
|
||||||
|
static constexpr char ServerWorldScenePath[] = "res://Scenes/Levels/server_world.tscn";
|
||||||
|
|
||||||
|
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(bool a_Server) {
|
||||||
|
Ref<PackedScene> sceneData;
|
||||||
|
if (a_Server)
|
||||||
|
sceneData = ResourceLoader::get_singleton()->load(ServerWorldScenePath);
|
||||||
|
else
|
||||||
|
sceneData = ResourceLoader::get_singleton()->load(ClientWorldScenePath);
|
||||||
|
|
||||||
|
add_child(sceneData->instantiate());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace blitz
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
#include "MainMenu.h"
|
#include <client/MainMenu.h>
|
||||||
|
|
||||||
|
#include <godot_cpp/classes/engine.hpp>
|
||||||
|
#include <godot_cpp/classes/multiplayer_api.hpp>
|
||||||
#include <godot_cpp/classes/resource_loader.hpp>
|
#include <godot_cpp/classes/resource_loader.hpp>
|
||||||
#include <godot_cpp/classes/scene_tree.hpp>
|
#include <godot_cpp/classes/scene_tree.hpp>
|
||||||
|
|
||||||
@@ -9,9 +11,7 @@ namespace blitz {
|
|||||||
|
|
||||||
void MainMenu::_bind_methods() {
|
void MainMenu::_bind_methods() {
|
||||||
godot::ClassDB::bind_method(godot::D_METHOD("on_connected"), &MainMenu::OnConnected);
|
godot::ClassDB::bind_method(godot::D_METHOD("on_connected"), &MainMenu::OnConnected);
|
||||||
ADD_SIGNAL(MethodInfo("create_game", PropertyInfo(Variant::INT, "port"), PropertyInfo(Variant::BOOL, "dedicated")));
|
ADD_SIGNAL(MethodInfo("change_scene_to_game", PropertyInfo(Variant::BOOL, "server")));
|
||||||
ADD_SIGNAL(MethodInfo("join_game", PropertyInfo(Variant::STRING, "address"), PropertyInfo(Variant::INT, "port")));
|
|
||||||
ADD_SIGNAL(MethodInfo("change_scene"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MainMenu::MainMenu() {}
|
MainMenu::MainMenu() {}
|
||||||
@@ -33,23 +33,49 @@ void MainMenu::_ready() {
|
|||||||
m_JoinButton->connect("pressed", callable_mp(this, &MainMenu::OnJoinPressed));
|
m_JoinButton->connect("pressed", callable_mp(this, &MainMenu::OnJoinPressed));
|
||||||
m_CreateButton->connect("pressed", callable_mp(this, &MainMenu::OnCreatePressed));
|
m_CreateButton->connect("pressed", callable_mp(this, &MainMenu::OnCreatePressed));
|
||||||
m_QuitButton->connect("pressed", callable_mp(this, &MainMenu::OnQuitPressed));
|
m_QuitButton->connect("pressed", callable_mp(this, &MainMenu::OnQuitPressed));
|
||||||
|
|
||||||
|
|
||||||
|
if (!Engine::get_singleton()->is_editor_hint()) {
|
||||||
|
m_NetworkInterface = Object::cast_to<NetworkInterface>(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() {
|
void MainMenu::OnConnected() {
|
||||||
emit_signal("change_scene");
|
emit_signal("change_scene_to_game", get_multiplayer()->is_server());
|
||||||
set_visible(false);
|
set_visible(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainMenu::OnDisconnected() {
|
||||||
|
set_visible(true);
|
||||||
|
EnableButtons();
|
||||||
|
}
|
||||||
|
|
||||||
void MainMenu::OnJoinPressed() {
|
void MainMenu::OnJoinPressed() {
|
||||||
emit_signal("join_game", "localhost", 25565);
|
DisableButtons();
|
||||||
|
m_NetworkInterface->JoinGame("localhost", 25565);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainMenu::OnCreatePressed() {
|
void MainMenu::OnCreatePressed() {
|
||||||
emit_signal("create_game", 25565, false);
|
DisableButtons();
|
||||||
|
m_NetworkInterface->CreateGame(25565);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainMenu::OnQuitPressed() {
|
void MainMenu::OnQuitPressed() {
|
||||||
get_tree()->quit();
|
get_tree()->quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainMenu::DisableButtons() {
|
||||||
|
m_JoinButton->set_disabled(true);
|
||||||
|
m_CreateButton->set_disabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainMenu::EnableButtons() {
|
||||||
|
m_JoinButton->set_disabled(false);
|
||||||
|
m_CreateButton->set_disabled(false);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace blitz
|
} // namespace blitz
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "Player.h"
|
#include <client/Player.h>
|
||||||
|
|
||||||
#include <godot_cpp/classes/engine.hpp>
|
#include <godot_cpp/classes/engine.hpp>
|
||||||
#include <godot_cpp/classes/input.hpp>
|
#include <godot_cpp/classes/input.hpp>
|
||||||
@@ -1,14 +1,11 @@
|
|||||||
#include "register_types.h"
|
#include <blitz/godot/NetworkInterface.h>
|
||||||
|
#include <client/ClientWorld.h>
|
||||||
#include "FirstPersonPlayer.h"
|
#include <client/FirstPersonPlayer.h>
|
||||||
#include "Lobby.h"
|
#include <client/Main.h>
|
||||||
#include "Main.h"
|
#include <client/MainMenu.h>
|
||||||
#include "MainMenu.h"
|
#include <client/Player.h>
|
||||||
#include "NetworkInterface.h"
|
#include <server/Server.h>
|
||||||
#include "Player.h"
|
#include <server/ServerWorld.h>
|
||||||
#include "Server.h"
|
|
||||||
#include "SpringArmPivot.h"
|
|
||||||
#include "World.h"
|
|
||||||
|
|
||||||
#include <gdextension_interface.h>
|
#include <gdextension_interface.h>
|
||||||
#include <godot_cpp/core/defs.hpp>
|
#include <godot_cpp/core/defs.hpp>
|
||||||
@@ -18,17 +15,17 @@ using namespace godot;
|
|||||||
|
|
||||||
static void RegisterClasses() {
|
static void RegisterClasses() {
|
||||||
GDREGISTER_CLASS(blitz::Player);
|
GDREGISTER_CLASS(blitz::Player);
|
||||||
GDREGISTER_CLASS(blitz::SpringArmPivot);
|
|
||||||
GDREGISTER_CLASS(blitz::FirstPersonPlayer);
|
GDREGISTER_CLASS(blitz::FirstPersonPlayer);
|
||||||
GDREGISTER_CLASS(blitz::MainMenu);
|
GDREGISTER_CLASS(blitz::MainMenu);
|
||||||
GDREGISTER_CLASS(blitz::Lobby);
|
|
||||||
GDREGISTER_CLASS(blitz::World);
|
|
||||||
GDREGISTER_CLASS(blitz::Main);
|
GDREGISTER_CLASS(blitz::Main);
|
||||||
GDREGISTER_CLASS(blitz::NetworkInterface);
|
GDREGISTER_CLASS(blitz::NetworkInterface);
|
||||||
GDREGISTER_CLASS(blitz::Server);
|
GDREGISTER_CLASS(blitz::Server);
|
||||||
|
GDREGISTER_ABSTRACT_CLASS(blitz::World);
|
||||||
|
GDREGISTER_CLASS(blitz::ClientWorld);
|
||||||
|
GDREGISTER_CLASS(blitz::ServerWorld);
|
||||||
}
|
}
|
||||||
|
|
||||||
void initialize_example_module(ModuleInitializationLevel p_level) {
|
static void initialize_blitz_module(ModuleInitializationLevel p_level) {
|
||||||
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
|
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -36,7 +33,7 @@ void initialize_example_module(ModuleInitializationLevel p_level) {
|
|||||||
RegisterClasses();
|
RegisterClasses();
|
||||||
}
|
}
|
||||||
|
|
||||||
void uninitialize_example_module(ModuleInitializationLevel p_level) {
|
static void uninitialize_blitz_module(ModuleInitializationLevel p_level) {
|
||||||
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
|
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -46,8 +43,8 @@ extern "C" GDExtensionBool GDE_EXPORT library_init(GDExtensionInterfaceGetProcAd
|
|||||||
const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization* r_initialization) {
|
const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization* r_initialization) {
|
||||||
godot::GDExtensionBinding::InitObject init_obj(p_get_proc, p_library, r_initialization);
|
godot::GDExtensionBinding::InitObject init_obj(p_get_proc, p_library, r_initialization);
|
||||||
|
|
||||||
init_obj.register_initializer(initialize_example_module);
|
init_obj.register_initializer(initialize_blitz_module);
|
||||||
init_obj.register_terminator(uninitialize_example_module);
|
init_obj.register_terminator(uninitialize_blitz_module);
|
||||||
init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE);
|
init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE);
|
||||||
|
|
||||||
return init_obj.init();
|
return init_obj.init();
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#ifndef GDEXAMPLE_REGISTER_TYPES_H
|
|
||||||
#define GDEXAMPLE_REGISTER_TYPES_H
|
|
||||||
|
|
||||||
#include <godot_cpp/core/class_db.hpp>
|
|
||||||
|
|
||||||
using namespace godot;
|
|
||||||
|
|
||||||
void initialize_example_module(ModuleInitializationLevel p_level);
|
|
||||||
void uninitialize_example_module(ModuleInitializationLevel p_level);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
#include "Server.h"
|
#include <server/Server.h>
|
||||||
|
|
||||||
#include "Lobby.h"
|
#include <blitz/godot/NetworkInterface.h>
|
||||||
#include "NetworkInterface.h"
|
|
||||||
#include <godot_cpp/classes/engine.hpp>
|
#include <godot_cpp/classes/engine.hpp>
|
||||||
|
|
||||||
using namespace godot;
|
using namespace godot;
|
||||||
@@ -19,13 +18,11 @@ void Server::_ready() {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
||||||
m_Lobby = Object::cast_to<Lobby>(get_parent());
|
m_NetworkInterface = Object::cast_to<NetworkInterface>(get_parent());
|
||||||
DEV_ASSERT(m_Lobby);
|
|
||||||
m_NetworkInterface = Object::cast_to<NetworkInterface>(m_Lobby->find_child("NetworkInterface"));
|
|
||||||
DEV_ASSERT(m_NetworkInterface);
|
DEV_ASSERT(m_NetworkInterface);
|
||||||
|
|
||||||
m_Lobby->connect("player_connected", callable_mp(this, &Server::OnPlayerConnect));
|
m_NetworkInterface->connect("player_connected", callable_mp(this, &Server::OnPlayerConnect));
|
||||||
m_Lobby->connect("player_disconnected", callable_mp(this, &Server::OnPlayerDisconnect));
|
m_NetworkInterface->connect("player_disconnected", callable_mp(this, &Server::OnPlayerDisconnect));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::OnPlayerConnect(PeerID a_PeerId) {
|
void Server::OnPlayerConnect(PeerID a_PeerId) {
|
||||||
38
src/server/ServerWorld.cpp
Normal file
38
src/server/ServerWorld.cpp
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#include <server/ServerWorld.h>
|
||||||
|
|
||||||
|
#include <blitz/godot/NetworkInterface.h>
|
||||||
|
#include <client/Player.h>
|
||||||
|
#include <godot_cpp/classes/engine.hpp>
|
||||||
|
|
||||||
|
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<Player>(m_Players->get_child(i));
|
||||||
|
DEV_ASSERT(player);
|
||||||
|
m_NetworkInterface->BroadcastPacket(
|
||||||
|
protocol::packets::PlayerPositionAndRotation({player->GetId(), player->get_position(), player->GetCameraRotation()}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace blitz
|
||||||
Reference in New Issue
Block a user