Compare commits

8 Commits

Author SHA1 Message Date
65808e9dee i guess it's working
All checks were successful
Linux arm64 / Build (pull_request) Successful in 1m21s
2024-08-17 17:18:13 +02:00
d261672f51 change node hierarchy 2024-08-17 14:32:53 +02:00
9449b125eb add icon
All checks were successful
Linux arm64 / Build (pull_request) Successful in 1m12s
2024-08-16 19:11:30 +02:00
3769fd3ace begin network 2024-08-16 19:00:45 +02:00
65e2a0b3ce Add main menu (#1)
All checks were successful
Linux arm64 / Build (push) Successful in 1m4s
Il est caché pour l'instant parce que c'est plus rapide à debug le reste pour le moment

Reviewed-on: #1
Co-authored-by: Persson-dev <sim16.prib@gmail.com>
Co-committed-by: Persson-dev <sim16.prib@gmail.com>
2024-08-16 11:13:54 +02:00
960ce2a546 fix firstpersson shadow movement
All checks were successful
Linux arm64 / Build (push) Successful in 1m1s
2024-08-14 15:02:15 +02:00
eb8e3b0888 add cool shadows
All checks were successful
Linux arm64 / Build (push) Successful in 1m3s
2024-08-14 14:31:13 +02:00
d0028dd0f8 fix cam rotation 2024-08-14 14:03:10 +02:00
29 changed files with 4513 additions and 90 deletions

File diff suppressed because one or more lines are too long

View File

@@ -3276,7 +3276,6 @@ node_connections = [&"ground_air_transition", 0, &"iwr_blend", &"ground_air_tran
velocity = Vector3(0, -5821.84, 0) velocity = Vector3(0, -5821.84, 0)
[node name="Mesh" type="Node3D" parent="."] [node name="Mesh" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.0730165, 0)
[node name="Armature" type="Node3D" parent="Mesh"] [node name="Armature" type="Node3D" parent="Mesh"]
transform = Transform3D(0.01, 0, 0, 0, -3.57628e-09, -0.01, 0, 0.01, -3.57628e-09, 0, 0, 0) transform = Transform3D(0.01, 0, 0, 0, -3.57628e-09, -0.01, 0, 0.01, -3.57628e-09, 0, 0, 0)
@@ -3750,15 +3749,6 @@ skin = SubResource("Skin_l3wpu")
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.908729, 0) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.908729, 0)
shape = SubResource("CapsuleShape3D_mm42w") shape = SubResource("CapsuleShape3D_mm42w")
[node name="SpringArmPivot" type="SpringArmPivot" parent="."]
[node name="SpringArm3D" type="SpringArm3D" parent="SpringArmPivot"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.31667, 0)
spring_length = 2.0
margin = 0.2
[node name="Camera3D" type="Camera3D" parent="SpringArmPivot/SpringArm3D"]
[node name="AnimationPlayer" type="AnimationPlayer" parent="."] [node name="AnimationPlayer" type="AnimationPlayer" parent="."]
root_node = NodePath("../Mesh") root_node = NodePath("../Mesh")
libraries = { libraries = {

View File

@@ -1,10 +1,9 @@
[gd_scene load_steps=18 format=3 uid="uid://coue2qehpn4fr"] [gd_scene load_steps=17 format=3 uid="uid://coue2qehpn4fr"]
[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_mnexj"]
[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_fkwcn"]
[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_ux02w"]
[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_wp15n"]
[ext_resource type="PackedScene" uid="uid://d38w4ae3qj0k4" path="res://Scenes/Characters/first_person_player.tscn" id="5_8ctht"]
[sub_resource type="PanoramaSkyMaterial" id="PanoramaSkyMaterial_6c4vd"] [sub_resource type="PanoramaSkyMaterial" id="PanoramaSkyMaterial_6c4vd"]
panorama = ExtResource("1_mnexj") panorama = ExtResource("1_mnexj")
@@ -51,7 +50,7 @@ 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="Node3D"] [node name="World" type="World"]
[node name="WorldEnvironment" type="WorldEnvironment" parent="."] [node name="WorldEnvironment" type="WorldEnvironment" parent="."]
environment = SubResource("Environment_ctwiv") environment = SubResource("Environment_ctwiv")
@@ -87,4 +86,4 @@ surface_material_override/0 = SubResource("StandardMaterial3D_pfpgv")
[node name="CollisionShape3D" type="CollisionShape3D" parent="Slope/StaticBody3D"] [node name="CollisionShape3D" type="CollisionShape3D" parent="Slope/StaticBody3D"]
shape = SubResource("ConcavePolygonShape3D_rit6o") shape = SubResource("ConcavePolygonShape3D_rit6o")
[node name="FirstPersonPlayer" parent="." instance=ExtResource("5_8ctht")] [node name="Players" type="Node" parent="."]

View File

@@ -0,0 +1,41 @@
[gd_scene format=3 uid="uid://bqfqg7xwwlxd8"]
[node name="MainMenu" type="MainMenu"]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 4
size_flags_vertical = 4
[node name="Container" type="VBoxContainer" parent="."]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -228.512
offset_top = -89.5
offset_right = 228.512
offset_bottom = 89.5
grow_horizontal = 2
grow_vertical = 2
[node name="JoinButton" type="Button" parent="Container"]
layout_mode = 2
theme_override_font_sizes/font_size = 35
text = "Join Game"
[node name="CreateButton" type="Button" parent="Container"]
layout_mode = 2
theme_override_font_sizes/font_size = 35
text = "Create Game"
[node name="QuitButton" type="Button" parent="Container"]
layout_mode = 2
theme_override_font_sizes/font_size = 35
text = "Quit"

View File

@@ -0,0 +1,5 @@
[gd_scene format=3 uid="uid://clafls1xhludi"]
[node name="Lobby" type="Lobby"]
[node name="NetworkInterface" type="NetworkInterface" parent="."]

View File

@@ -0,0 +1,3 @@
[gd_scene format=3 uid="uid://us5sb4a0kq8d"]
[node name="Server" type="Server"]

15
godot/Scenes/main.tscn Normal file
View File

@@ -0,0 +1,15 @@
[gd_scene load_steps=3 format=3 uid="uid://4jt0v2b2l4rt"]
[ext_resource type="PackedScene" path="res://Scenes/Network/networking.tscn" id="1_06ibn"]
[ext_resource type="PackedScene" path="res://Scenes/Menus/mainmenu.tscn" id="2_lavg1"]
[node name="Main" type="Main"]
[node name="Lobby" parent="." instance=ExtResource("1_06ibn")]
[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"]

View File

@@ -1,5 +1,5 @@
[icons] [icons]
Blitz3 = "res://icon.svg" Blitz3 = "res://icon.png"
[configuration] [configuration]

BIN
godot/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

View File

@@ -1 +0,0 @@
<svg height="128" width="128" xmlns="http://www.w3.org/2000/svg"><rect x="2" y="2" width="124" height="124" rx="14" fill="#363d52" stroke="#212532" stroke-width="4"/><g transform="scale(.101) translate(122 122)"><g fill="#fff"><path d="M105 673v33q407 354 814 0v-33z"/><path fill="#478cbf" d="m105 673 152 14q12 1 15 14l4 67 132 10 8-61q2-11 15-15h162q13 4 15 15l8 61 132-10 4-67q3-13 15-14l152-14V427q30-39 56-81-35-59-83-108-43 20-82 47-40-37-88-64 7-51 8-102-59-28-123-42-26 43-46 89-49-7-98 0-20-46-46-89-64 14-123 42 1 51 8 102-48 27-88 64-39-27-82-47-48 49-83 108 26 42 56 81zm0 33v39c0 276 813 276 813 0v-39l-134 12-5 69q-2 10-14 13l-162 11q-12 0-16-11l-10-65H447l-10 65q-4 11-16 11l-162-11q-12-3-14-13l-5-69z"/><path d="M483 600c3 34 55 34 58 0v-86c-3-34-55-34-58 0z"/><circle cx="725" cy="526" r="90"/><circle cx="299" cy="526" r="90"/></g><g fill="#414042"><circle cx="307" cy="532" r="60"/><circle cx="717" cy="532" r="60"/></g></g></svg>

Before

Width:  |  Height:  |  Size: 950 B

View File

@@ -11,9 +11,14 @@ config_version=5
[application] [application]
config/name="Blitz3" config/name="Blitz3"
run/main_scene="res://Scenes/Levels/world.tscn" run/main_scene="res://Scenes/main.tscn"
config/features=PackedStringArray("4.2", "Forward Plus") config/features=PackedStringArray("4.2", "Forward Plus")
config/icon="res://icon.svg" config/icon="res://icon.png"
[display]
window/size/viewport_width=1920
window/size/viewport_height=1080
[input] [input]

View File

@@ -1,10 +1,12 @@
#include "FirstPersonPlayer.h" #include "FirstPersonPlayer.h"
#include <godot_cpp/classes/camera3d.hpp>
#include <godot_cpp/classes/engine.hpp> #include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/input.hpp> #include <godot_cpp/classes/input.hpp>
#include <godot_cpp/classes/input_event_mouse_motion.hpp> #include <godot_cpp/classes/input_event_mouse_motion.hpp>
#include <godot_cpp/classes/input_map.hpp> #include <godot_cpp/classes/input_map.hpp>
#include <godot_cpp/core/math.hpp> #include <godot_cpp/core/math.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
using namespace godot; using namespace godot;
@@ -32,6 +34,9 @@ static constexpr float FOV_TRANSITION = 8.0f;
static constexpr float MIN_FOV_VELOCITY = 0.5; static constexpr float MIN_FOV_VELOCITY = 0.5;
static constexpr float MAX_FOV_VELOCITY = SPRINT_SPEED * 2.0f; static constexpr float MAX_FOV_VELOCITY = SPRINT_SPEED * 2.0f;
static const float LerpValue = 0.10;
static const float AnimationBlend = 7.0;
void FirstPersonPlayer::_bind_methods() {} void FirstPersonPlayer::_bind_methods() {}
FirstPersonPlayer::FirstPersonPlayer() : m_BobTime(0) {} FirstPersonPlayer::FirstPersonPlayer() : m_BobTime(0) {}
@@ -44,9 +49,9 @@ void FirstPersonPlayer::_ready() {
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED); Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
} }
m_Head = Object::cast_to<Node3D>(find_child("Head")); m_Head = Object::cast_to<Node3D>(find_child("Head"));
DEV_ASSERT(m_Head);
m_Camera = Object::cast_to<Camera3D>(m_Head->find_child("Camera")); m_Camera = Object::cast_to<Camera3D>(m_Head->find_child("Camera"));
DEV_ASSERT(m_Camera); m_AnimationTree = Object::cast_to<AnimationTree>(find_child("AnimationTree"));
m_Mesh = Object::cast_to<Node3D>(find_child("Mesh"));
} }
void FirstPersonPlayer::_unhandled_input(const godot::Ref<godot::InputEvent>& a_Event) { void FirstPersonPlayer::_unhandled_input(const godot::Ref<godot::InputEvent>& a_Event) {
@@ -84,6 +89,8 @@ void FirstPersonPlayer::_physics_process(float a_Delta) {
UpdateFOV(a_Delta); UpdateFOV(a_Delta);
UpdateBobbing(a_Delta); UpdateBobbing(a_Delta);
UpdateAnimation(a_Delta);
move_and_slide(); move_and_slide();
} }
@@ -98,10 +105,11 @@ void FirstPersonPlayer::UpdateBobbing(float a_Delta) {
void FirstPersonPlayer::UpdateCamera(const InputEventMouseMotion& a_Event) { void FirstPersonPlayer::UpdateCamera(const InputEventMouseMotion& a_Event) {
m_Head->rotate_y(-a_Event.get_relative().x * SENSITIVITY); m_Head->rotate_y(-a_Event.get_relative().x * SENSITIVITY);
m_Mesh->rotate_y(-a_Event.get_relative().x * SENSITIVITY);
m_Camera->rotate_x(-a_Event.get_relative().y * SENSITIVITY); m_Camera->rotate_x(-a_Event.get_relative().y * SENSITIVITY);
float rotationX = m_Camera->get_rotation().x; float rotationX = m_Camera->get_rotation().x;
CLAMP(rotationX, Math::deg_to_rad(-40.0), Math::deg_to_rad(60.0)); rotationX = CLAMP(rotationX, Math::deg_to_rad(-80.0), Math::deg_to_rad(80.0));
m_Camera->set_rotation({rotationX, get_rotation().y, get_rotation().z}); m_Camera->set_rotation({rotationX, get_rotation().y, get_rotation().z});
} }
@@ -128,6 +136,14 @@ void FirstPersonPlayer::UpdatePosition(float delta) {
Math::lerp(static_cast<float>(get_velocity().z), static_cast<float>(direction.z * m_Speed), Math::lerp(static_cast<float>(get_velocity().z), static_cast<float>(direction.z * m_Speed),
static_cast<float>(delta * AIR_MOVEMENT))}); static_cast<float>(delta * AIR_MOVEMENT))});
} }
if (!direction.is_zero_approx()) {
godot::Vector3 newRotation = m_Mesh->get_rotation();
newRotation.y = godot::UtilityFunctions::lerp_angle(
newRotation.y, godot::UtilityFunctions::atan2(get_velocity().x, get_velocity().z), LerpValue);
m_Mesh->set_rotation(newRotation);
}
} }
void FirstPersonPlayer::UpdateFOV(float a_Delta) { void FirstPersonPlayer::UpdateFOV(float a_Delta) {
@@ -136,4 +152,25 @@ void FirstPersonPlayer::UpdateFOV(float a_Delta) {
m_Camera->set_fov(Math::lerp(m_Camera->get_fov(), targetFOV, a_Delta * FOV_TRANSITION)); m_Camera->set_fov(Math::lerp(m_Camera->get_fov(), targetFOV, a_Delta * FOV_TRANSITION));
} }
void FirstPersonPlayer::UpdateAnimation(float delta) {
if (is_on_floor()) {
m_AnimationTree->set("parameters/ground_air_transition/transition_request", "grounded");
if (get_velocity().length() > 0.2f) {
if (m_Speed == SPRINT_SPEED) {
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
UtilityFunctions::lerp(m_AnimationTree->get("parameters/iwr_blend/blend_amount"), 1.0, delta * AnimationBlend));
} else {
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
UtilityFunctions::lerp(m_AnimationTree->get("parameters/iwr_blend/blend_amount"), 0.0, delta * AnimationBlend));
}
} else {
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
UtilityFunctions::lerp(m_AnimationTree->get("parameters/iwr_blend/blend_amount"), -1.0, delta * AnimationBlend));
}
} else {
m_AnimationTree->set("parameters/ground_air_transition/transition_request", "air");
}
}
} // namespace blitz } // namespace blitz

View File

@@ -1,13 +1,11 @@
#pragma once #pragma once
#include <godot_cpp/classes/camera3d.hpp> #include "Player.h"
#include <godot_cpp/classes/character_body3d.hpp>
#include <godot_cpp/classes/input_event_mouse_motion.hpp> #include <godot_cpp/classes/input_event_mouse_motion.hpp>
#include <godot_cpp/classes/node3d.hpp>
namespace blitz { namespace blitz {
class FirstPersonPlayer : public godot::CharacterBody3D { class FirstPersonPlayer : public Player {
GDCLASS(FirstPersonPlayer, godot::CharacterBody3D) GDCLASS(FirstPersonPlayer, godot::CharacterBody3D)
protected: protected:
static void _bind_methods(); static void _bind_methods();
@@ -19,7 +17,7 @@ class FirstPersonPlayer : public godot::CharacterBody3D {
// Godot overrides // Godot overrides
void _unhandled_input(const godot::Ref<godot::InputEvent>&); void _unhandled_input(const godot::Ref<godot::InputEvent>&);
void _physics_process(float delta); void _physics_process(float delta);
void _ready(); void _ready() override;
private: private:
godot::Camera3D* m_Camera; godot::Camera3D* m_Camera;
@@ -31,6 +29,8 @@ class FirstPersonPlayer : public godot::CharacterBody3D {
void UpdateFOV(float delta); void UpdateFOV(float delta);
void UpdateCamera(const godot::InputEventMouseMotion&); void UpdateCamera(const godot::InputEventMouseMotion&);
void UpdatePosition(float delta); void UpdatePosition(float delta);
void UpdateAnimation(float delta);
}; };
} // namespace blitz } // namespace blitz

99
src/Lobby.cpp Normal file
View File

@@ -0,0 +1,99 @@
#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(int64_t a_PeerId) {
if (get_multiplayer()->is_server()) {
emit_signal("player_connected", a_PeerId);
}
}
void Lobby::OnPlayerDisconnected(int64_t 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 Normal file
View File

@@ -0,0 +1,32 @@
#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(int64_t a_PeerId);
void OnPlayerDisconnected(int64_t a_PeerId);
void OnConnectOk();
void OnConnectFail();
void OnServerDisconnected();
};
} // namespace blitz

33
src/Main.cpp Normal file
View File

@@ -0,0 +1,33 @@
#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

19
src/Main.h Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include <godot_cpp/classes/node.hpp>
namespace blitz {
class Main : public godot::Node {
GDCLASS(Main, godot::Node)
protected:
static void _bind_methods();
public:
Main();
~Main();
void ChangeScene();
};
} // namespace blitz

55
src/MainMenu.cpp Normal file
View File

@@ -0,0 +1,55 @@
#include "MainMenu.h"
#include <godot_cpp/classes/resource_loader.hpp>
#include <godot_cpp/classes/scene_tree.hpp>
using namespace godot;
namespace blitz {
void MainMenu::_bind_methods() {
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("join_game", PropertyInfo(Variant::STRING, "address"), PropertyInfo(Variant::INT, "port")));
ADD_SIGNAL(MethodInfo("change_scene"));
}
MainMenu::MainMenu() {}
MainMenu::~MainMenu() {}
void MainMenu::_ready() {
Node* container = find_child("Container");
DEV_ASSERT(container);
m_JoinButton = Object::cast_to<Button>(container->find_child("JoinButton"));
m_CreateButton = Object::cast_to<Button>(container->find_child("CreateButton"));
m_QuitButton = Object::cast_to<Button>(container->find_child("QuitButton"));
DEV_ASSERT(m_JoinButton);
DEV_ASSERT(m_CreateButton);
DEV_ASSERT(m_QuitButton);
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));
}
void MainMenu::OnConnected() {
emit_signal("change_scene");
set_visible(false);
}
void MainMenu::OnJoinPressed() {
emit_signal("join_game", "localhost", 25565);
}
void MainMenu::OnCreatePressed() {
emit_signal("create_game", 25565, false);
}
void MainMenu::OnQuitPressed() {
get_tree()->quit();
}
} // namespace blitz

32
src/MainMenu.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#include <godot_cpp/classes/button.hpp>
#include <godot_cpp/classes/control.hpp>
namespace blitz {
class MainMenu : public godot::Control {
GDCLASS(MainMenu, godot::Control)
protected:
static void _bind_methods();
public:
MainMenu();
~MainMenu();
// Godot overrides
void _ready() override;
private:
godot::Button* m_JoinButton;
godot::Button* m_CreateButton;
godot::Button* m_QuitButton;
void OnConnected();
void OnJoinPressed();
void OnCreatePressed();
void OnQuitPressed();
};
} // namespace blitz

57
src/NetworkInterface.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include "NetworkInterface.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("AddPlayer", "a_PlayerId", "a_PlayerName"), &NetworkInterface::AddPlayer);
ClassDB::bind_method(D_METHOD("RemovePlayer", "a_PlayerId"), &NetworkInterface::RemovePlayer);
ClassDB::bind_method(D_METHOD("SetPlayerPositionAndRotation", "a_PlayerId", "a_Position", "a_Rotation"),
&NetworkInterface::SetPlayerPositionAndRotation);
ADD_SIGNAL(MethodInfo("AddPlayer", PropertyInfo(Variant::INT, "a_PlayerId"), PropertyInfo(Variant::STRING, "a_PlayerName")));
ADD_SIGNAL(MethodInfo("RemovePlayer", PropertyInfo(Variant::INT, "a_PlayerId")));
ADD_SIGNAL(MethodInfo("SetPlayerPositionAndRotation", PropertyInfo(Variant::INT, "a_PlayerId"),
PropertyInfo(Variant::VECTOR3, "a_Position"), PropertyInfo(Variant::VECTOR3, "a_Rotation")));
}
NetworkInterface::NetworkInterface() {}
NetworkInterface::~NetworkInterface() {}
void NetworkInterface::AddPlayer(int64_t a_PlayerId, godot::String a_PlayerName) {
emit_signal("AddPlayer", a_PlayerId, a_PlayerName);
}
void NetworkInterface::RemovePlayer(int64_t a_PlayerId) {
emit_signal("RemovePlayer", a_PlayerId);
}
void NetworkInterface::SetPlayerPositionAndRotation(int64_t a_PlayerId, Vector3 a_Position, Vector3 a_Rotation) {
emit_signal("SetPlayerPositionAndRotation", a_PlayerId, a_Position, a_Rotation);
}
void NetworkInterface::_ready() {
Dictionary config;
config["rpc_mode"] = MultiplayerAPI::RPC_MODE_AUTHORITY;
config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
config["call_local"] = true;
config["channel"] = 0;
rpc_config("AddPlayer", config);
rpc_config("RemovePlayer", config);
Dictionary config2;
config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
config["channel"] = 0;
config2["rpc_mode"] = MultiplayerAPI::RPC_MODE_ANY_PEER;
config2["call_local"] = false;
rpc_config("SetPlayerPositionAndRotation", config2);
}
} // namespace blitz

22
src/NetworkInterface.h Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
#include <godot_cpp/classes/node.hpp>
namespace blitz {
class NetworkInterface : public godot::Node {
GDCLASS(NetworkInterface, godot::Node)
protected:
static void _bind_methods();
public:
NetworkInterface();
~NetworkInterface();
void AddPlayer(int64_t a_PlayerId, godot::String a_PlayerName);
void RemovePlayer(int64_t a_PlayerId);
void SetPlayerPositionAndRotation(int64_t a_PlayerId, godot::Vector3 a_Position, godot::Vector3 a_Rotation);
void _ready() override;
};
} // namespace blitz

View File

@@ -16,18 +16,19 @@ static const float AnimationBlend = 7.0;
namespace blitz { namespace blitz {
using namespace godot;
void Player::_bind_methods() {} void Player::_bind_methods() {}
Player::Player() {} Player::Player() {}
Player::~Player() {} Player::~Player() {}
void Player::_ready() { void Player::_ready() {
godot::InputMap::get_singleton()->load_from_project_settings(); godot::InputMap::get_singleton()->load_from_project_settings();
m_PlayerMesh = Object::cast_to<godot::Node3D>(get_child(0)); m_Mesh = Object::cast_to<godot::Node3D>(find_child("Mesh"));
m_SpringArmPivot = Object::cast_to<godot::Node3D>(get_child(2)); m_AnimationTree = Object::cast_to<godot::AnimationTree>(find_child("AnimationTree"));
m_AnimationTree = Object::cast_to<godot::AnimationTree>(get_child(4)); DEV_ASSERT(m_Mesh);
DEV_ASSERT(m_PlayerMesh);
DEV_ASSERT(m_SpringArmPivot);
DEV_ASSERT(m_AnimationTree); DEV_ASSERT(m_AnimationTree);
apply_floor_snap(); apply_floor_snap();
@@ -38,50 +39,6 @@ void Player::_physics_process(float delta) {
if (godot::Engine::get_singleton()->is_editor_hint()) if (godot::Engine::get_singleton()->is_editor_hint())
return; return;
auto* Input = godot::Input::get_singleton();
godot::Vector3 move_direction{0, 0, 0};
move_direction.x = Input->get_action_strength("move_right") - Input->get_action_strength("move_left");
move_direction.z = Input->get_action_strength("move_backwards") - Input->get_action_strength("move_forwards");
move_direction = move_direction.rotated({0, 1, 0}, m_SpringArmPivot->get_rotation().y);
godot::Vector3 newVelocity = get_velocity();
newVelocity.y -= Gravity * delta;
set_velocity(newVelocity);
if (Input->is_action_pressed("run"))
m_Speed = RunSpeed;
else
m_Speed = WalkSpeed;
newVelocity = get_velocity();
newVelocity.x = move_direction.x * m_Speed;
newVelocity.z = move_direction.z * m_Speed;
set_velocity(newVelocity);
if (move_direction != godot::Vector3{0, 0, 0}) {
godot::Vector3 newRotation = m_PlayerMesh->get_rotation();
newRotation.y = godot::UtilityFunctions::lerp_angle(
newRotation.y, godot::UtilityFunctions::atan2(get_velocity().x, get_velocity().z), LerpValue);
m_PlayerMesh->set_rotation(newRotation);
}
bool justLanded = is_on_floor() && m_SnapVector == godot::Vector3{0, 0, 0};
bool isJumping = is_on_floor() && Input->is_action_just_pressed("jump");
if (isJumping) {
newVelocity = get_velocity();
newVelocity.y = JumpStrength;
set_velocity(newVelocity);
m_SnapVector.zero();
} else if (justLanded) {
m_SnapVector = {0, -1, 0};
}
apply_floor_snap();
move_and_slide();
animate(delta); animate(delta);
} }
@@ -108,4 +65,13 @@ void Player::animate(float delta) {
m_AnimationTree->set("parameters/ground_air_transition/transition_request", "air"); m_AnimationTree->set("parameters/ground_air_transition/transition_request", "air");
} }
} }
Vector3 Player::GetCameraRotation() const {
return m_Mesh->get_rotation();
}
void Player::SetCameraRotation(const Vector3& a_Rotation) {
m_Mesh->set_rotation(a_Rotation);
}
} // namespace blitz } // namespace blitz

View File

@@ -5,6 +5,9 @@
#include <godot_cpp/classes/node3d.hpp> #include <godot_cpp/classes/node3d.hpp>
namespace blitz { namespace blitz {
class World;
class Player : public godot::CharacterBody3D { class Player : public godot::CharacterBody3D {
GDCLASS(Player, godot::CharacterBody3D); GDCLASS(Player, godot::CharacterBody3D);
@@ -20,18 +23,21 @@ class Player : public godot::CharacterBody3D {
void _physics_process(float delta); void _physics_process(float delta);
void animate(float delta); void animate(float delta);
private: godot::Vector3 GetCameraRotation() const;
godot::Node3D* m_PlayerMesh; void SetCameraRotation(const godot::Vector3& a_Rotation);
godot::Node3D* m_SpringArmPivot;
uint64_t GetId() const {
return m_PeerId;
}
protected:
godot::Node3D* m_Mesh;
godot::AnimationTree* m_AnimationTree; godot::AnimationTree* m_AnimationTree;
godot::Vector3 m_SnapVector; godot::Vector3 m_SnapVector;
float m_Speed; float m_Speed;
/* uint64_t m_PeerId;
@onready var player_mesh : Node3D = $Mesh
@onready var spring_arm_pivot : Node3D = $SpringArmPivot
@onready var animator : AnimationTree = $AnimationTree
*/ friend class World;
}; };
} // namespace blitz } // namespace blitz

11
src/PlayerInfo.h Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
#include <godot_cpp/variant/string.hpp>
namespace blitz {
struct PlayerInfo {
godot::String m_Name;
};
} // namespace blitz

44
src/Server.cpp Normal file
View File

@@ -0,0 +1,44 @@
#include "Server.h"
#include "Lobby.h"
#include "NetworkInterface.h"
#include <godot_cpp/classes/engine.hpp>
using namespace godot;
namespace blitz {
void Server::_bind_methods() {}
Server::Server() {}
Server::~Server() {}
void Server::_ready() {
if (Engine::get_singleton()->is_editor_hint())
return;
m_Lobby = Object::cast_to<Lobby>(get_parent());
DEV_ASSERT(m_Lobby);
m_NetworkInterface = Object::cast_to<NetworkInterface>(m_Lobby->find_child("NetworkInterface"));
DEV_ASSERT(m_NetworkInterface);
m_Lobby->connect("player_connected", callable_mp(this, &Server::OnPlayerConnect));
m_Lobby->connect("player_disconnected", callable_mp(this, &Server::OnPlayerDisconnect));
}
void Server::OnPlayerConnect(uint64_t a_PeerId) {
for (int i = 0; i < m_Peers.size(); i++) {
m_NetworkInterface->rpc_id(a_PeerId, "AddPlayer", m_Peers[i], "Aucuneidee");
}
m_Peers.push_back(a_PeerId);
m_NetworkInterface->rpc("AddPlayer", a_PeerId, "Aucuneidee");
}
void Server::OnPlayerDisconnect(uint64_t a_PeerId) {
m_Peers.erase(a_PeerId);
m_NetworkInterface->rpc("RemovePlayer", a_PeerId);
}
} // namespace blitz

31
src/Server.h Normal file
View File

@@ -0,0 +1,31 @@
#pragma once
#include <godot_cpp/classes/node.hpp>
namespace blitz {
class Lobby;
class NetworkInterface;
class Server : public godot::Node {
GDCLASS(Server, godot::Node)
protected:
static void _bind_methods();
public:
Server();
~Server();
void _ready() override;
void OnPlayerConnect(uint64_t a_PeerId);
void OnPlayerDisconnect(uint64_t a_PeerId);
private:
Lobby* m_Lobby;
NetworkInterface* m_NetworkInterface;
godot::TypedArray<uint64_t> m_Peers;
};
} // namespace blitz

112
src/World.cpp Normal file
View File

@@ -0,0 +1,112 @@
#include "World.h"
#include "FirstPersonPlayer.h"
#include "NetworkInterface.h"
#include "Player.h"
#include <godot_cpp/classes/engine.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>
using namespace godot;
namespace blitz {
static const char FirstPersonPlayerScenePath[] = "res://Scenes/Characters/first_person_player.tscn";
static const char PlayerScenePath[] = "res://Scenes/Characters/player.tscn";
void World::_bind_methods() {}
void World::_ready() {
if (Engine::get_singleton()->is_editor_hint())
return;
m_Players = find_child("Players");
DEV_ASSERT(m_Players);
auto* lobby = get_parent()->find_child("Lobby");
DEV_ASSERT(lobby);
m_NetworkInterface = Object::cast_to<NetworkInterface>(lobby->find_child("NetworkInterface"));
DEV_ASSERT(m_NetworkInterface);
m_NetworkInterface->connect("AddPlayer", callable_mp(this, &World::AddPlayer));
m_NetworkInterface->connect("RemovePlayer", callable_mp(this, &World::RemovePlayer));
m_NetworkInterface->connect("SetPlayerPositionAndRotation", callable_mp(this, &World::SetPlayerPositionAndRotation));
}
World::World() {}
World::~World() {}
void World::_process(float delta) {
#if DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint())
return;
#endif
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->rpc(
"SetPlayerPositionAndRotation", player->m_PeerId, player->get_position(), player->GetCameraRotation());
}
} else {
Player* player = GetPlayerById(get_multiplayer()->get_unique_id());
if (player)
m_NetworkInterface->rpc("SetPlayerPositionAndRotation", get_multiplayer()->get_unique_id(), player->get_position(),
player->GetCameraRotation());
}
}
Player* World::GetPlayerById(uint64_t a_PlayerId) {
String stringId = UtilityFunctions::var_to_str(a_PlayerId);
for (int i = 0; i < m_Players->get_child_count(); i++) {
Node* player = m_Players->get_child(i);
if (player->get_name() == stringId) {
return Object::cast_to<Player>(player);
}
}
return nullptr;
}
void World::AddPlayer(int32_t a_PlayerId, String a_PlayerName) {
UtilityFunctions::print("New Player with id : ", a_PlayerId);
if (a_PlayerId == get_multiplayer()->get_unique_id()) {
Ref<PackedScene> serverScene = ResourceLoader::get_singleton()->load(FirstPersonPlayerScenePath);
FirstPersonPlayer* player = Object::cast_to<FirstPersonPlayer>(serverScene->instantiate());
player->set_name(UtilityFunctions::var_to_str(a_PlayerId));
player->m_PeerId = a_PlayerId;
m_Players->add_child(player);
} else {
Ref<PackedScene> serverScene = ResourceLoader::get_singleton()->load(PlayerScenePath);
Player* player = Object::cast_to<Player>(serverScene->instantiate());
player->set_name(UtilityFunctions::var_to_str(a_PlayerId));
player->m_PeerId = a_PlayerId;
m_Players->add_child(player);
}
}
void World::RemovePlayer(int32_t a_PlayerId) {
UtilityFunctions::print("Removing Player with id : ", a_PlayerId);
Player* player = GetPlayerById(a_PlayerId);
if (player) {
player->queue_free();
}
}
void World::SetPlayerPositionAndRotation(int64_t a_PlayerId, Vector3 a_Position, Vector3 a_Rotation) {
if (a_PlayerId == get_multiplayer()->get_unique_id())
return;
Player* player = GetPlayerById(a_PlayerId);
if (player) {
player->set_position(a_Position);
player->SetCameraRotation(a_Rotation);
}
}
} // namespace blitz

34
src/World.h Normal file
View File

@@ -0,0 +1,34 @@
#pragma once
#include <godot_cpp/classes/node3d.hpp>
namespace blitz {
class Player;
class NetworkInterface;
class World : public godot::Node3D {
GDCLASS(World, godot::Node3D)
protected:
static void _bind_methods();
public:
World();
~World();
void _process(float delta);
void _ready() override;
Player* GetPlayerById(uint64_t a_PlayerId);
void AddPlayer(int32_t a_PlayerId, godot::String a_PlayerName);
void RemovePlayer(int32_t a_PlayerId);
void SetPlayerPositionAndRotation(int64_t a_PlayerId, godot::Vector3 a_Position, godot::Vector3 a_Rotation);
private:
NetworkInterface* m_NetworkInterface;
godot::Node* m_Players;
float m_PassedTime;
};
} // namespace blitz

View File

@@ -1,8 +1,14 @@
#include "register_types.h" #include "register_types.h"
#include "FirstPersonPlayer.h" #include "FirstPersonPlayer.h"
#include "Lobby.h"
#include "Main.h"
#include "MainMenu.h"
#include "NetworkInterface.h"
#include "Player.h" #include "Player.h"
#include "Server.h"
#include "SpringArmPivot.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>
@@ -10,14 +16,24 @@
using namespace godot; using namespace godot;
static void RegisterClasses() {
ClassDB::register_class<blitz::Player>();
ClassDB::register_class<blitz::SpringArmPivot>();
ClassDB::register_class<blitz::FirstPersonPlayer>();
ClassDB::register_class<blitz::MainMenu>();
ClassDB::register_class<blitz::Lobby>();
ClassDB::register_class<blitz::World>();
ClassDB::register_class<blitz::Main>();
ClassDB::register_class<blitz::NetworkInterface>();
ClassDB::register_class<blitz::Server>();
}
void initialize_example_module(ModuleInitializationLevel p_level) { void initialize_example_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return; return;
} }
ClassDB::register_class<blitz::Player>(); RegisterClasses();
ClassDB::register_class<blitz::SpringArmPivot>();
ClassDB::register_class<blitz::FirstPersonPlayer>();
} }
void uninitialize_example_module(ModuleInitializationLevel p_level) { void uninitialize_example_module(ModuleInitializationLevel p_level) {