WIP: Better player input system

This commit is contained in:
Nikolai Rodionov 2025-01-29 22:59:36 +01:00
parent d024f217d0
commit 67e81abad9
Signed by: allanger
GPG Key ID: 09F8B434D0FDD99B
19 changed files with 391 additions and 63 deletions

View File

@ -15,8 +15,6 @@ func _process(delta: float) -> void:
func _on_head_collision_body_part_hit(dam: Variant) -> void:
#print("head is hit" + str(dam))
#print(owner_placeholder)
#if owner_placeholder.has_method("take_damage"):
if owner_placeholder:
owner_placeholder.take_damage(dam)

View File

@ -1419,12 +1419,12 @@ shape = SubResource("SphereShape3D_axep3")
[node name="PhysicalBoneSimulator3D" type="PhysicalBoneSimulator3D" parent="Body/Armature/Skeleton3D"]
[node name="Physical Bone mixamorig_Hips" type="PhysicalBone3D" parent="Body/Armature/Skeleton3D/PhysicalBoneSimulator3D"]
transform = Transform3D(82.5044, -54.0559, -16.4615, 55.0482, 83.4649, 1.8195, 12.756, -10.5629, 98.619, 1.31498, 0.290624, -87.4646)
transform = Transform3D(82.5044, -54.0559, -16.4615, 55.0482, 83.4649, 1.8195, 12.756, -10.5629, 98.619, 1.31498, 0.290625, -87.4646)
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -8.35312e-08, -5.58794e-09, 0.0499956)
body_offset = Transform3D(100, -0.0695305, -0.00860751, -1.19209e-05, 12.2744, -99.2438, 0.0700493, 99.2438, 12.2744, 0.00043869, 4.96175, -0.613665)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -9.28935e-08, -8.84756e-09, 0.0499956)
body_offset = Transform3D(100, -0.0695305, -0.00860786, -1.28746e-05, 12.2744, -99.2438, 0.0700493, 99.2438, 12.2744, 0.000439644, 4.96175, -0.613665)
mass = 20.0
bone_name = "mixamorig_Hips"
joint_constraints/bias = 0.3
@ -1436,12 +1436,12 @@ transform = Transform3D(0.01, 0, 0, 0, -4.37114e-10, 0.01, 0, -0.01, -4.37114e-1
shape = SubResource("CapsuleShape3D_0ptuo")
[node name="Physical Bone mixamorig_Spine" type="PhysicalBone3D" parent="Body/Armature/Skeleton3D/PhysicalBoneSimulator3D"]
transform = Transform3D(80.9412, -58.546, -4.56913, 57.6046, 80.6695, -13.1964, 11.4119, 8.0493, 99.0201, 2.40599, 0.973749, -98.2036)
transform = Transform3D(80.9412, -58.5461, -4.56913, 57.6046, 80.6695, -13.1964, 11.4119, 8.04929, 99.0201, 2.40599, 0.973749, -98.2036)
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 4.2222e-09, 2.2377e-09, 0.05866)
body_offset = Transform3D(100, 7.62939e-06, 9.05991e-06, 4.76837e-06, -1.90735e-06, -100, -1.52588e-05, 100, -3.8147e-06, -9.53674e-07, 5.866, 0)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 3.66277e-09, 1.11885e-09, 0.0586601)
body_offset = Transform3D(100, 1.52588e-05, 1.00136e-05, 4.76837e-06, 0, -100, -1.52588e-05, 100, -1.90735e-06, -9.53674e-07, 5.866, 0)
mass = 20.0
bone_name = "mixamorig_Spine"
joint_constraints/bias = 0.3
@ -1453,12 +1453,12 @@ transform = Transform3D(0.0318071, 3.86358e-14, 4.9738e-14, -2.13163e-14, -1.629
shape = SubResource("CapsuleShape3D_126jt")
[node name="Physical Bone mixamorig_Head" type="PhysicalBone3D" parent="Body/Armature/Skeleton3D/PhysicalBoneSimulator3D"]
transform = Transform3D(-97.8019, 4.47329, 20.366, -15.8369, -79.4709, -58.5966, 13.5638, -60.5339, 78.4325, -13.8193, 23.4986, -143.635)
transform = Transform3D(-97.8019, 4.4733, 20.366, -15.8369, -79.4709, -58.5966, 13.5638, -60.5339, 78.4325, -13.8193, 23.4986, -143.635)
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -3.90306e-08, -1.39698e-07, 0.0981531)
body_offset = Transform3D(-100, -0.00220013, -0.000778198, -5.24521e-06, 33.8066, -94.1122, 0.00232792, -94.1122, -33.8066, 7.24792e-05, 9.23741, 3.31821)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -5.81038e-08, -1.37836e-07, 0.0981531)
body_offset = Transform3D(-100, -0.00220108, -0.000778198, -5.24521e-06, 33.8066, -94.1122, 0.00232792, -94.1122, -33.8066, 7.05719e-05, 9.23741, 3.31821)
mass = 7.0
bone_name = "mixamorig_Head"
joint_constraints/bias = 0.3
@ -1474,8 +1474,8 @@ transform = Transform3D(86.1165, 14.0252, -48.8594, -45.4995, 64.1262, -61.787,
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 3.65892e-08, 2.39336e-07, 0.137023)
body_offset = Transform3D(100, -3.05176e-05, -2.67029e-05, 2.09808e-05, 100, -7.62939e-06, 1.71661e-05, -3.8147e-06, 100, 0, -2.28882e-05, -13.7023)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1.55782e-09, 2.39336e-07, 0.137023)
body_offset = Transform3D(100, -3.43323e-05, -2.67029e-05, 2.47955e-05, 100, -7.62939e-06, 1.71661e-05, -3.8147e-06, 100, 3.8147e-06, -2.28882e-05, -13.7023)
mass = 3.0
bone_name = "mixamorig_LeftArm"
joint_constraints/bias = 0.3
@ -1491,8 +1491,8 @@ transform = Transform3D(-48.2566, -12.058, 86.752, -78.7312, -37.4258, -48.997,
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.47239e-07, 6.31263e-08, 0.138072)
body_offset = Transform3D(100, 3.05176e-05, -4.19617e-05, -3.8147e-05, 0, -100, -3.8147e-06, 100, 9.53674e-06, 3.05176e-05, 13.8072, -7.62939e-06)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -3.28799e-07, 6.04928e-08, 0.138072)
body_offset = Transform3D(100, 3.05176e-05, -3.8147e-05, -3.8147e-05, 0, -100, -3.8147e-06, 100, 1.14441e-05, 3.8147e-05, 13.8072, -7.62939e-06)
mass = 3.0
bone_name = "mixamorig_LeftForeArm"
joint_constraints/bias = 0.3
@ -1504,11 +1504,11 @@ transform = Transform3D(0.01, 0, 0, 0, -4.37114e-10, 0.01, 0, -0.01, -4.37114e-1
shape = SubResource("CapsuleShape3D_2k34c")
[node name="Physical Bone mixamorig_LeftHand" type="PhysicalBone3D" parent="Body/Armature/Skeleton3D/PhysicalBoneSimulator3D"]
transform = Transform3D(9.11563, -98.6768, 13.4089, 23.9481, -10.8976, -96.4766, 96.6612, 12.0056, 22.6379, -10.9685, 56.2242, -106.442)
transform = Transform3D(9.11563, -98.6768, 13.4089, 23.9481, -10.8976, -96.4766, 96.6612, 12.0056, 22.6379, -10.9684, 56.2242, -106.442)
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1.24797e-07, -1.22003e-07, 0.0264902)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -8.19564e-08, -9.40636e-08, 0.0264903)
body_offset = Transform3D(-58.5197, 57.9894, 56.6805, 1.62125e-05, 69.899, -71.5132, -81.0891, -41.8493, -40.9047, -1.50148, 1.89441, 1.08356)
bone_name = "mixamorig_LeftHand"
joint_constraints/bias = 0.3
@ -1524,7 +1524,7 @@ transform = Transform3D(30.7246, 72.8896, 61.1809, 93.7855, -34.092, -6.48192, 1
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1.63344e-08, 1.16326e-07, 0.137023)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1.63344e-08, 1.16325e-07, 0.137023)
body_offset = Transform3D(100, -5.72205e-06, 1.19209e-05, 6.67572e-06, 1.07288e-05, -100, -1.90735e-06, 100, 2.64645e-05, 0, 13.7023, -1.52588e-05)
mass = 3.0
bone_name = "mixamorig_RightArm"
@ -1541,8 +1541,8 @@ transform = Transform3D(24.3552, 37.4063, 89.4852, -25.0101, 91.5653, -31.4688,
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.40737e-07, -2.80688e-08, 0.138072)
body_offset = Transform3D(100, -1.14441e-05, 8.58307e-06, -3.8147e-06, 100, 9.53674e-06, 5.72205e-06, -5.72205e-06, 100, -1.52588e-05, 1.49012e-06, -13.8072)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.40737e-07, -4.23738e-08, 0.138072)
body_offset = Transform3D(100, -1.14441e-05, 8.58307e-06, -3.8147e-06, 100, 9.53674e-06, 5.72205e-06, -5.72205e-06, 100, -1.52588e-05, 2.92063e-06, -13.8072)
mass = 3.0
bone_name = "mixamorig_RightForeArm"
joint_constraints/bias = 0.3
@ -1558,8 +1558,8 @@ transform = Transform3D(89.0767, -24.8074, -38.0781, 5.05123, 88.6726, -45.9527,
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.86265e-08, 2.31434e-07, 0.0264903)
body_offset = Transform3D(-58.5197, -57.9893, -56.6805, -1.19209e-05, 69.8991, -71.5131, 81.0891, -41.8492, -40.9048, 1.5015, 1.89439, 1.08359)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -6.51926e-09, 2.77068e-07, 0.0264903)
body_offset = Transform3D(-58.5197, -57.9893, -56.6805, -1.33514e-05, 69.8991, -71.5131, 81.0891, -41.8492, -40.9048, 1.5015, 1.89439, 1.08359)
bone_name = "mixamorig_RightHand"
joint_constraints/bias = 0.3
joint_constraints/damping = 1.0
@ -1574,8 +1574,8 @@ transform = Transform3D(-96.1774, -26.2683, 7.73901, -23.4203, 64.2554, -72.957,
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 3.78612e-08, -7.74374e-09, 0.202997)
body_offset = Transform3D(100, -2.86102e-05, 9.53674e-06, 7.62939e-06, 7.62939e-06, -100, 3.71933e-05, 100, 3.8147e-06, -5.72205e-06, 20.2997, 0)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 3.39893e-08, -7.74373e-09, 0.202997)
body_offset = Transform3D(100, -3.8147e-05, 1.14441e-05, 7.62939e-06, 7.62939e-06, -100, 4.19617e-05, 100, 3.8147e-06, -5.72205e-06, 20.2997, 0)
mass = 5.0
bone_name = "mixamorig_LeftUpLeg"
joint_constraints/bias = 0.3
@ -1591,8 +1591,8 @@ transform = Transform3D(-97.5216, -11.6193, -18.8291, -22.0483, 58.1427, 78.3154
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -9.03053e-09, -1.30455e-08, 0.210495)
body_offset = Transform3D(100, 4.76837e-06, 1.33514e-05, 5.72205e-06, 0, -100, -9.53674e-07, 100, 1.52588e-05, -1.90735e-06, 21.0495, -1.90735e-06)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -5.01575e-09, -2.10752e-08, 0.210495)
body_offset = Transform3D(100, 4.76837e-06, 1.14441e-05, 5.72205e-06, 0, -100, -9.53674e-07, 100, 1.90735e-05, -1.90735e-06, 21.0495, -1.90735e-06)
mass = 5.0
bone_name = "mixamorig_LeftLeg"
joint_constraints/bias = 0.3
@ -1604,12 +1604,12 @@ transform = Transform3D(0.01, 0, 0, 0, -4.37114e-10, 0.01, 0, -0.01, -4.37114e-1
shape = SubResource("CapsuleShape3D_vwdij")
[node name="Physical Bone mixamorig_LeftFoot" type="PhysicalBone3D" parent="Body/Armature/Skeleton3D/PhysicalBoneSimulator3D"]
transform = Transform3D(-93.6712, -34.5296, 5.78095, -34.9451, 91.207, -21.4506, 2.13418, -22.1132, -97.501, 11.7385, 3.12728, -14.5678)
transform = Transform3D(-93.6712, -34.5296, 5.78095, -34.9451, 91.207, -21.4506, 2.13419, -22.1132, -97.501, 11.7385, 3.12728, -14.5678)
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -7.66258e-09, 5.6483e-08, 0.078608)
body_offset = Transform3D(100, 5.72205e-06, -2.38419e-06, -2.86102e-06, -1.52588e-05, -100, -5.72205e-06, 100, -3.24249e-05, 9.53674e-07, 7.8608, -3.09944e-06)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -8.41225e-09, 6.36356e-08, 0.078608)
body_offset = Transform3D(100, 5.72205e-06, -1.43051e-06, -1.90735e-06, -1.52588e-05, -100, -5.72205e-06, 100, -3.24249e-05, 9.53674e-07, 7.8608, -3.8147e-06)
bone_name = "mixamorig_LeftFoot"
joint_constraints/bias = 0.3
joint_constraints/damping = 1.0
@ -1624,8 +1624,8 @@ transform = Transform3D(-83.6898, -51.5224, 18.4786, -50.5592, 59.8312, -62.161,
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.31228e-08, 7.74372e-09, 0.202997)
body_offset = Transform3D(100, -1.52588e-05, 7.62939e-06, 5.72205e-06, -3.8147e-06, -100, 1.14441e-05, 100, -3.8147e-06, -2.86102e-06, 20.2997, 0)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 2.08665e-08, 7.74372e-09, 0.202997)
body_offset = Transform3D(100, -1.52588e-05, 3.8147e-06, 5.72205e-06, -3.8147e-06, -100, 1.14441e-05, 100, -3.8147e-06, -2.86102e-06, 20.2997, 0)
mass = 5.0
bone_name = "mixamorig_RightUpLeg"
joint_constraints/bias = 0.3
@ -1641,8 +1641,8 @@ transform = Transform3D(-91.8175, -23.5086, -31.8888, -36.0103, 83.0805, 42.437,
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 5.27453e-09, -2.4944e-07, 0.210495)
body_offset = Transform3D(100, -1.71661e-05, 1.33514e-05, -9.53674e-07, 4.19617e-05, -100, 1.90735e-05, 100, 6.86646e-05, -3.33786e-06, 21.0495, 1.04904e-05)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.40577e-08, -2.67006e-07, 0.210495)
body_offset = Transform3D(100, -1.71661e-05, 1.14441e-05, -9.53674e-07, 4.19617e-05, -100, 1.90735e-05, 100, 7.24792e-05, -3.8147e-06, 21.0495, 1.14441e-05)
mass = 5.0
bone_name = "mixamorig_RightLeg"
joint_constraints/bias = 0.3
@ -1658,7 +1658,7 @@ transform = Transform3D(-88.192, -17.3351, 43.8367, -46.8549, 42.4444, -77.4796,
collision_layer = 4
collision_mask = 4
joint_type = 1
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -5.24153e-08, -2.70381e-09, 0.078608)
joint_offset = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -5.24152e-08, -2.70387e-09, 0.078608)
body_offset = Transform3D(100, 1.14441e-05, -7.62939e-06, -7.62939e-06, -1.90735e-05, -100, 0, 100, -5.72205e-05, 5.84126e-06, 7.8608, 4.76837e-06)
bone_name = "mixamorig_RightFoot"
joint_constraints/bias = 0.3

View File

@ -1,7 +1,8 @@
[gd_scene load_steps=11 format=3 uid="uid://b10lpwfjgxds4"]
[gd_scene load_steps=13 format=3 uid="uid://b10lpwfjgxds4"]
[ext_resource type="Script" path="res://scripts/player/player_input_controller.gd" id="1_q75ai"]
[ext_resource type="Texture2D" uid="uid://oopj5mj1vdp0" path="res://assets/crosshairs/crosshair_default.png" id="2_lsd7c"]
[ext_resource type="Script" path="res://scenes/characters/player_input_controller.gd" id="3_n3epd"]
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_jc50w"]
margin = 0.5
@ -18,6 +19,12 @@ properties/1/replication_mode = 1
properties/2/path = NodePath(".:health")
properties/2/spawn = true
properties/2/replication_mode = 2
properties/3/path = NodePath(".:velocity")
properties/3/spawn = true
properties/3/replication_mode = 1
properties/4/path = NodePath(".:owner_id")
properties/4/spawn = true
properties/4/replication_mode = 1
[sub_resource type="Animation" id="Animation_falg4"]
length = 0.001
@ -83,11 +90,23 @@ font_size = 70
font_size = 100
font_color = Color(0.756874, 0, 0.223924, 1)
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_eour5"]
properties/0/path = NodePath(".:input_direction")
properties/0/spawn = false
properties/0/replication_mode = 2
properties/1/path = NodePath(".:jumping")
properties/1/spawn = false
properties/1/replication_mode = 2
[node name="CharacterPlaceholder" type="CharacterBody3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.799455, 0)
collision_layer = 9
collision_mask = 9
script = ExtResource("1_q75ai")
health = null
owner_id = null
jump_height = null
camera_sens = null
[node name="Collision" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.0150437, 0)
@ -163,3 +182,8 @@ label_settings = SubResource("LabelSettings_1j1uq")
[node name="ModelMount" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.790262, 0)
[node name="PlayerInput" type="MultiplayerSynchronizer" parent="."]
root_path = NodePath(".")
replication_config = SubResource("SceneReplicationConfig_eour5")
script = ExtResource("3_n3epd")

View File

@ -0,0 +1,35 @@
extends MultiplayerSynchronizer
@export var jumping := false
@export var input_direction := Vector2()
@onready var camera_mount = $"../FirstPersonCameraMount"
@onready var placeholder = $".."
func _ready() -> void:
pass
#set_process(get_multiplayer_authority() == multiplayer.get_unique_id())
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
# Add the gravity.
if multiplayer.get_unique_id() == get_multiplayer_authority():
input_direction = Input.get_vector("move_left", "move_right", "move_forward", "move_backwards")
set_input_direction.rpc(input_direction)
if Input.is_action_just_pressed("jump"):
jump.rpc_id(1)
var camera_sens: float = 0.002
func _input(event):
if multiplayer.get_unique_id() == get_multiplayer_authority():
if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
placeholder.rotate_y(-event.relative.x * camera_sens)
@rpc("any_peer", "call_local", "reliable")
func set_input_direction(direction: Vector2):
input_direction = direction
@rpc("any_peer", "call_local", "reliable")
func jump():
jumping = true

View File

@ -20,11 +20,11 @@ func _ready() -> void:
err = _add_player_spawner()
if err != OK:
print("Couldn't load player spawner")
push_error("Couldn't load player spawner")
err = _add_object_spawner()
if err != OK:
print("Couldn't load object spawner")
push_error("Couldn't load object spawner")
# add objects spawner
if multiplayer.is_server():
_spawn_player(1)

View File

@ -19,12 +19,13 @@ func _get_root() -> Node3D:
# -- Spawn a player node and sync it across all peers
func spawn_players(spawn_location: SpawnController, id: int) -> Error:
if multiplayer.is_server():
var char : CharacterBody3D = null
var char : Node3D = null
var player_data: PlayerState = GameServerManager.players[id]
char = ResourceLoader.load("res://scenes/characters/placeholder.tscn").instantiate()
char = ResourceLoader.load("res://scenes/player/placeholder.tscn").instantiate()
char.name = "PlayerPlaceholder_" + str(player_data.id)
char.owner_id = id
var position: Vector3 = spawn_location.get_spawner(SpawnController.Sides.BLUE)
char.global_position = position
char.set_server_position(position)
_get_root().add_child(char)
return OK
return ERR_UNAUTHORIZED

View File

@ -6,7 +6,7 @@
script = ExtResource("1_2hsyd")
[node name="MultiplayerSpawner" type="MultiplayerSpawner" parent="."]
_spawnable_scenes = PackedStringArray("res://scenes/characters/placeholder.tscn")
_spawnable_scenes = PackedStringArray("res://scenes/characters/placeholder.tscn", "res://scenes/player/placeholder.tscn")
spawn_path = NodePath("../Players")
spawn_limit = 10

View File

@ -31,9 +31,7 @@ func _get_available_spawn(spawn_set: Node3D) -> Node3D:
for spawn: Node3D in spawn_set.get_children():
if not spawn.busy:
spawns.push_back(spawn)
print(spawns.size())
var random_index: int = randi_range(0, spawns.size() - 1)
print(random_index)
return spawns[random_index]
return null
@ -41,13 +39,11 @@ func _get_available_spawn(spawn_set: Node3D) -> Node3D:
func get_spawner(team: Sides) -> Vector3:
match team:
Sides.BLUE:
print("Getting a blue spawner")
var spawn := _get_available_spawn(blue_spawners)
return spawn.choose_spawn_location()
Sides.RED:
print("red")
return Vector3(0,0,0)
_:
print("хз")
return Vector3(0,0,0)
# Get all spawners for each team

View File

@ -962,7 +962,7 @@ script = ExtResource("2_1ss1b")
[node name="Blue" type="Node3D" parent="SpawnLocations"]
[node name="Spawn4" type="Node3D" parent="SpawnLocations/Blue"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 53.1487, -0.0093751, -8.68195)
transform = Transform3D(-0.332276, 0, 0.943182, 0, 1, 0, -0.943182, 0, -0.332276, 53.1487, -0.0093751, -8.68195)
[node name="Spawn1" type="Node3D" parent="SpawnLocations/Blue"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 52.9098, -0.00937462, 10.3532)

View File

@ -0,0 +1,106 @@
extends Node3D
@export var owner_id: int = 0
@export var initial_position: Vector3 = Vector3(0, 0, 0)
# -- Components
@onready var client_node: CharacterBody3D = $PlayerControlledNode
@onready var server_node: CharacterBody3D = $ServerControlledNode
@export var character_speed: int = 5
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
set_multiplayer_authority(1)
client_node.set_multiplayer_authority(owner_id)
$PlayerControlledNode/CameraMount.set_multiplayer_authority(owner_id)
switch_players_camera.rpc_id(owner_id)
client_node.global_position = server_node.global_position
client_node.rotation.y = server_node.rotation.y
client_node.rotation.x = server_node.rotation.x
if multiplayer.is_server():
$Timer.start()
if owner_id != multiplayer.get_unique_id():
client_node.queue_free()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
#if desired_player_state.globa_position != real_player_state.globa_position:
#desired_player_state.globa_position = real_player_state.globa_position
pass
func _physics_process(delta: float) -> void:
pass
@rpc("authority", "call_local", "reliable")
func switch_players_camera():
$PlayerControlledNode/CameraMount/Camera3D.make_current()
func set_server_position(position: Vector3):
initial_position = position
func _on_Timer_timeout():
$Timer.start()
func _on_timer_timeout() -> void:
if multiplayer.is_server():
verify_position.rpc_id(owner_id)
verify_rotation.rpc_id(owner_id)
$Timer.start()
pass # Replace with function body.
@rpc("authority", "call_local")
func verify_position():
var desired_position: Vector3 = client_node.global_position
send_position.rpc_id(1, desired_position.x, desired_position.y, desired_position.z)
@rpc("any_peer", "call_local")
func send_position(x: float, y: float, z: float):
var desired_position: Vector3 = Vector3(x, y, z)
if multiplayer.is_server():
var real_position: Vector3 = server_node.global_position
var difference: Vector3 = desired_position - real_position
if is_vector_a_lower_than_b(difference, Vector3(0.1, 0.1, 0.1)):
print("trustin")
server_node.global_position = desired_position
else:
print("not trustin")
adjust_position.rpc_id(owner_id, real_position.x, real_position.y, real_position.z)
@rpc("authority", "call_local")
func adjust_position(x: float, y: float, z: float):
var desired_position: Vector3 = Vector3(x, y, z)
if owner_id == multiplayer.get_unique_id():
push_warning("player position is not valid, adjusting")
client_node.global_position = desired_position
@rpc("authority", "call_local")
func verify_rotation():
var desired_rotation: Vector3 = client_node.rotation
send_rotation.rpc_id(1, desired_rotation.x, desired_rotation.y, desired_rotation.z)
@rpc("any_peer", "call_local")
func send_rotation(x: float, y: float, z: float):
var desired_rotation: Vector3 = Vector3(x, y, z)
if multiplayer.is_server():
var real_rotation: Vector3 = server_node.rotation
var difference: Vector3 = desired_rotation - real_rotation
if is_vector_a_lower_than_b(difference, Vector3(0.1, 0.1, 0.1)):
server_node.rotation = desired_rotation
else:
adjust_rotation.rpc_id(owner_id, real_rotation.x, real_rotation.y, real_rotation.z)
@rpc("any_peer", "call_local")
func adjust_rotation(x: float, y: float, z: float):
var desired_rotation: Vector3 = Vector3(x, y, z)
if owner_id == multiplayer.get_unique_id():
push_warning("player rotation is not valid, adjusting")
client_node.rotation = desired_rotation
func is_vector_a_lower_than_b(vec_a: Vector3, vec_b: Vector3) -> bool:
return vec_a.x < vec_b.x and vec_a.y < vec_b.y and vec_a.z < vec_b.z

View File

@ -0,0 +1,60 @@
[gd_scene load_steps=6 format=3 uid="uid://bmm3brgvr4d86"]
[ext_resource type="Script" path="res://scenes/player/player_input_controller.gd" id="1_m6tul"]
[ext_resource type="Script" path="res://scenes/player/placeholder.gd" id="1_ts455"]
[ext_resource type="Script" path="res://scenes/player/server_player_controller.gd" id="3_f1bhn"]
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_taqso"]
radius = 0.4
height = 1.6
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_7vlrn"]
properties/0/path = NodePath("ServerControlledNode:position")
properties/0/spawn = true
properties/0/replication_mode = 1
properties/1/path = NodePath(".:owner_id")
properties/1/spawn = true
properties/1/replication_mode = 2
properties/2/path = NodePath("ServerControlledNode:rotation")
properties/2/spawn = true
properties/2/replication_mode = 1
properties/3/path = NodePath(".:initial_position")
properties/3/spawn = true
properties/3/replication_mode = 2
[node name="Placeholder" type="Node3D"]
script = ExtResource("1_ts455")
[node name="PlayerControlledNode" type="CharacterBody3D" parent="."]
collision_layer = 2
script = ExtResource("1_m6tul")
[node name="CollisionShape3D" type="CollisionShape3D" parent="PlayerControlledNode"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.786919, 0)
shape = SubResource("CapsuleShape3D_taqso")
[node name="CameraMount" type="Node3D" parent="PlayerControlledNode"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.29568, -0.240438)
[node name="Camera3D" type="Camera3D" parent="PlayerControlledNode/CameraMount"]
[node name="Node3D" type="Node3D" parent="PlayerControlledNode"]
[node name="ServerControlledNode" type="CharacterBody3D" parent="."]
collision_layer = 4
script = ExtResource("3_f1bhn")
[node name="CollisionShape3D" type="CollisionShape3D" parent="ServerControlledNode"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.786919, 0)
shape = SubResource("CapsuleShape3D_taqso")
[node name="CSGBox3D" type="CSGBox3D" parent="ServerControlledNode"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.91303, 0)
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
replication_config = SubResource("SceneReplicationConfig_7vlrn")
[node name="Timer" type="Timer" parent="."]
wait_time = 0.1
[connection signal="timeout" from="Timer" to="." method="_on_timer_timeout"]

View File

@ -0,0 +1,69 @@
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# This script is supposed to control the node that is rendered on the
# client side, and send the changes to the server node
# ---------------------------------------------------------------------
extends CharacterBody3D
@export var jumping := false
@export var input_direction := Vector2()
@onready var camera_mount = $CameraMount
@onready var camera = $CameraMount/Camera3D
@onready var placeholder: Node3D = $'..'
var paused := false
#func _ready() -> void:
var look_dir: Vector2
func _ready() -> void:
global_position = $"..".initial_position
func _input(event):
if multiplayer.get_unique_id() == get_multiplayer_authority():
if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
look_dir = event.relative * 1
rotation.y -= look_dir.x * camera_sens * 1.0
camera_mount.rotation.x = clamp(camera_mount.rotation.x - look_dir.y * camera_sens * 1.0, -1.5, 1.5)
server_node.set_rotation_y.rpc_id(1, rotation.y)
server_node.set_rotation_x.rpc_id(1, rotation.x)
@onready var server_node: ServerControlledPlayer = $"../ServerControlledNode"
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if multiplayer.get_unique_id() == get_multiplayer_authority():
if !paused:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
server_node.set_input_direction.rpc_id(1, input_direction)
input_direction = Input.get_vector("move_left", "move_right", "move_forward", "move_backwards")
if Input.is_action_just_pressed("jump"):
jump.rpc_id(1)
func _physics_process(delta: float) -> void:
if not is_on_floor():
velocity += get_gravity() * delta
if multiplayer.get_unique_id() == get_multiplayer_authority():
var direction := (transform.basis * Vector3(input_direction.x, 0, input_direction.y)).normalized()
if is_on_floor():
if direction:
velocity.x = direction.x * placeholder.character_speed
velocity.z = direction.z * placeholder.character_speed
else:
velocity.x = move_toward(velocity.x, 0, placeholder.character_speed)
velocity.z = move_toward(velocity.z, 0, placeholder.character_speed)
move_and_slide()
var camera_sens: float = 0.002
@rpc("authority", "reliable")
func set_current_rotation(rotation: Vector3):
rotation = rotation
@rpc("any_peer", "reliable")
func set_current_position(x: float, y: float, z: float):
global_position = Vector3(x, y ,z)
@rpc("any_peer", "call_local", "reliable")
func jump():
jumping = true

View File

@ -0,0 +1,37 @@
class_name ServerControlledPlayer
extends CharacterBody3D
func _ready() -> void:
global_position = $"..".initial_position
@export_category("ServerControlledPlayer")
@onready var placeholder: Node3D = $'..'
var input_direction := Vector2()
var input_rotation_y
@rpc("call_local", "any_peer", "unreliable_ordered")
func set_input_direction(new_input_direction: Vector2):
input_direction = new_input_direction
@rpc("call_local", "any_peer", "unreliable_ordered")
func set_rotation_y(new_rotation_y: float):
rotation.y = new_rotation_y
@rpc("call_local", "any_peer", "unreliable_ordered")
func set_rotation_x(new_rotation_x: float):
$"..".rotation.x = new_rotation_x
func _physics_process(delta: float) -> void:
if not is_on_floor():
velocity += get_gravity() * delta
var direction := (transform.basis * Vector3(input_direction.x, 0, input_direction.y)).normalized()
#if is_on_floor():
if direction:
velocity.x = direction.x * placeholder.character_speed
velocity.z = direction.z * placeholder.character_speed
else:
velocity.x = move_toward(velocity.x, 0, placeholder.character_speed)
velocity.z = move_toward(velocity.z, 0, placeholder.character_speed)
move_and_slide()

View File

@ -60,7 +60,6 @@ func remove_multiplayer_peer():
func load_game():
var chosen_map_index = $Maps.get_selected_items()[0]
var chosen_map = $Maps.get_item_text(chosen_map_index)
print(chosen_map)
var path_tmpl := "res://scenes/maps/maps/%s"
var path := path_tmpl % chosen_map
get_tree().change_scene_to_file(path)

View File

@ -34,7 +34,7 @@ func _on_create_button_pressed() -> void:
GameServerManager.current_map = path
var err := GameServerManager.create_server(player_manager)
if err != OK:
print("couldn't create a server")
push_error("couldn't create a server")
$UI.hide()
change_level.call_deferred(load(path))

View File

@ -30,7 +30,6 @@ func _process(delta):
rigid_body_3d.visible = false
particles.emitting = true
#if ray.get_collider().is_in_group("body"):
#print("head")
await get_tree().create_timer(1.0).timeout
queue_free()

View File

@ -39,7 +39,6 @@ func _ready() -> void:
add_child(dummy)
enable_camera()
capture_mouse()
#print("I am " + str(multiplayer.get_unique_id()) + "I'm controling " + str($"../MultiplayerSynchronizer".get_multiplayer_authority()))
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseMotion:

View File

@ -29,7 +29,6 @@ func _on_player_connected(id):
func _ready() -> void:
multiplayer.peer_connected.connect(_on_player_connected)
multiplayer.connected_to_server.connect(_on_connected_ok)
print("started")
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.

View File

@ -22,13 +22,16 @@ var model: Node3D = null
# -- This node is supposed to be spawned per player, and since each
# -- player has an id, it is used for giving a node a name. So we can
# -- use it here to tell a controlled node from the rest models
@onready var owner_id: int = str($".".name).to_int()
@onready var owner_id_detected: int = str($".".name).to_int()
@export var owner_id: int = 0
var multiplayer_id: int = 0
# -- Character state
var alive: bool = true
func _ready() -> void:
owner_id = owner_id_detected
$PlayerInput.set_multiplayer_authority(owner_id)
player_synchronizer.set_multiplayer_authority(1)
multiplayer_id = multiplayer.get_unique_id()
# -- Separate logic for player and other models that are controlled
@ -39,19 +42,19 @@ func _ready() -> void:
if _is_current_player():
var err := _add_first_view_model()
if err != OK:
print("Error occured: " + str(err))
push_error("Error occured: " + str(err))
err = _add_legs_to_first_view()
if err != OK:
print("Error occured: " + str(err))
push_error("Error occured: " + str(err))
var world_model := _add_world_model()
if world_model == null:
print("Error occured: " + "couldn't load the world model")
push_error("Error occured: " + "couldn't load the world model")
_enable_camera()
else:
var world_model := _add_world_model()
if world_model == null:
print("Error occured: " + "couldn't load the world model")
push_error("Error occured: " + "couldn't load the world model")
_hide_camera_mount()
_capture_mouse()
@ -182,10 +185,17 @@ func _unhandled_input(event: InputEvent) -> void:
#if Input.is_action_just_pressed("reload"): $Body/UpperTorso/CameraMount/Camera.reload()
func _rotate_camera(sens_mod: float = 1.0) -> void:
pass
#if str($"..".name).to_int() == multiplayer.get_unique_id():
rotation.y -= look_dir.x * camera_sens * sens_mod
first_view_camera_mount.rotation.x = clamp(first_view_camera_mount.rotation.x - look_dir.y * camera_sens * sens_mod, -1.5, 1.5)
#@export var player := 1:
#set(id):
#player = id
#$PlayerInput.set_multiplayer_authority(id)
@onready var input = $PlayerInput
@onready var hud = $FirstPersonCameraMount/HUD
@onready var health_indicator = $FirstPersonCameraMount/HUD/HealthIndicator
@onready var fps_indicator = $FirstPersonCameraMount/HUD/FPS
@ -209,16 +219,15 @@ func _physics_process(delta: float) -> void:
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
if input.jumping and is_on_floor():
velocity.y = JUMP_VELOCITY
input.jumping = false
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
#var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
# -- It shouldn't be possible to change direction during the jumps
if is_on_floor():
var input_dir := Input.get_vector("move_left", "move_right", "move_forward", "move_backwards")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
var direction := (transform.basis * Vector3(input.input_direction.x, 0, input.input_direction.y)).normalized()
if direction:
gun_mount_anim.play("move")
if first_view_legs_anim != null:
@ -274,7 +283,6 @@ func _get_camera_collision():
var intersection = get_world_3d().direct_space_state.intersect_ray(new_intersection)
if not intersection.is_empty():
var collision_point = intersection.position
print("gotcha")
return collision_point
else:
return ray_end
@ -286,12 +294,10 @@ func _hit_scan_collision(collision_point):
var new_intersection = PhysicsRayQueryParameters3D.create(ray_origin, ray_end)
var bullet_collision = get_world_3d().direct_space_state.intersect_ray(new_intersection)
if bullet_collision:
print(bullet_collision)
_hit_scan_damage(bullet_collision.collider)
func _hit_scan_damage(Collider):
#if Collider.is_in_group("target") and Collider.has_method("take_damage"):
print("damaged")
func take_damage(dam: int):
var new_health = health - dam