WIP: Spawn bullets on the server and sync them

This commit is contained in:
Nikolai Rodionov 2025-01-26 18:18:35 +01:00
parent adce112be9
commit e28845b688
Signed by: allanger
GPG Key ID: 09F8B434D0FDD99B
11 changed files with 1467 additions and 18 deletions

View File

@ -16,7 +16,6 @@ func _process(delta: float) -> void:
func _on_head_collision_body_part_hit(dam: Variant) -> void:
print("head is hit" + str(dam))
owner_placeholder.take_damage(dam)
pass # Replace with function body.

View File

@ -1048,7 +1048,7 @@ bones/24/name = "mixamorig_LeftHandRing2"
bones/24/parent = 23
bones/24/rest = Transform3D(1, 1.61871e-05, -9.45085e-08, -1.61871e-05, 1, 3.29224e-06, 9.45618e-08, -3.29224e-06, 1, 6.3543e-07, 3.60118, -1.47875e-05)
bones/24/enabled = true
bones/24/position = Vector3(-3.30613e-06, 3.60119, 1.50863e-07)
bones/24/position = Vector3(-3.30613e-06, 3.60119, 1.50864e-07)
bones/24/rotation = Quaternion(0.563924, -3.30111e-07, -0.0577891, 0.823803)
bones/24/scale = Vector3(1, 1, 1)
bones/25/name = "mixamorig_LeftHandRing3"
@ -1077,7 +1077,7 @@ bones/28/parent = 27
bones/28/rest = Transform3D(1, 0.000736602, -7.57697e-07, -0.000736602, 1, 9.24707e-06, 7.64508e-07, -9.24651e-06, 1, 1.0844e-06, 4.13666, 1.41886e-05)
bones/28/enabled = true
bones/28/position = Vector3(-9.08385e-06, 4.13665, 5.47372e-06)
bones/28/rotation = Quaternion(0.433129, -6.53828e-07, -0.0443853, 0.900239)
bones/28/rotation = Quaternion(0.433129, -6.53827e-07, -0.0443853, 0.900239)
bones/28/scale = Vector3(1, 1, 1)
bones/29/name = "mixamorig_LeftHandPinky3"
bones/29/parent = 28
@ -1126,7 +1126,7 @@ bones/35/parent = 34
bones/35/rest = Transform3D(0.894428, 0.447212, -8.12113e-07, -0.387297, 0.774597, -0.500001, -0.223606, 0.447215, 0.866025, 3.00297, 3.7888, 2.16716)
bones/35/enabled = true
bones/35/position = Vector3(3.00297, 3.7888, 2.16716)
bones/35/rotation = Quaternion(0.227577, -0.0598799, -0.162411, 0.958252)
bones/35/rotation = Quaternion(0.227577, -0.0598799, -0.162411, 0.958251)
bones/35/scale = Vector3(1, 1, 1)
bones/36/name = "mixamorig_RightHandThumb2"
bones/36/parent = 35
@ -1210,14 +1210,14 @@ bones/47/parent = 34
bones/47/rest = Transform3D(1, 9.66599e-06, -1.40744e-07, -9.66599e-06, 1, -2.81994e-06, 1.40717e-07, 2.81994e-06, 1, -2.21663, 12.147, -0.00999829)
bones/47/enabled = true
bones/47/position = Vector3(-2.21664, 12.147, -0.00998812)
bones/47/rotation = Quaternion(0.0911117, 0.11369, 0.100504, 0.984211)
bones/47/rotation = Quaternion(0.0911117, 0.11369, 0.100504, 0.984212)
bones/47/scale = Vector3(1, 1, 1)
bones/48/name = "mixamorig_RightHandRing2"
bones/48/parent = 47
bones/48/rest = Transform3D(1, -1.60845e-05, 3.26056e-09, 1.60845e-05, 1, 1.32059e-06, -3.2818e-09, -1.32059e-06, 1, -2.50026e-07, 3.6012, -7.75122e-07)
bones/48/enabled = true
bones/48/position = Vector3(-2.50026e-07, 3.6012, -7.75122e-07)
bones/48/rotation = Quaternion(0.836665, 8.70473e-07, 0.068157, 0.543457)
bones/48/rotation = Quaternion(0.836665, 8.70474e-07, 0.068157, 0.543457)
bones/48/scale = Vector3(1, 1, 1)
bones/49/name = "mixamorig_RightHandRing3"
bones/49/parent = 48
@ -1301,14 +1301,14 @@ bones/60/parent = 0
bones/60/rest = Transform3D(-0.999981, -0.00611455, -7.52684e-05, 0.00611501, -0.999901, -0.0126672, 2.1931e-06, -0.0126674, 0.99992, -9.12503, -6.6556, -0.0553527)
bones/60/enabled = true
bones/60/position = Vector3(-9.12503, -6.6556, -0.0553527)
bones/60/rotation = Quaternion(-0.0339456, 0.334676, 0.92684, -0.166754)
bones/60/rotation = Quaternion(-0.0339455, 0.334676, 0.92684, -0.166754)
bones/60/scale = Vector3(1, 1, 1)
bones/61/name = "mixamorig_RightLeg"
bones/61/parent = 60
bones/61/rest = Transform3D(0.99993, -0.0118315, -0.000580536, 0.0118448, 0.999271, 0.0362854, 0.000150802, -0.0362897, 0.999341, 3.28565e-07, 40.5995, 1.49012e-08)
bones/61/enabled = true
bones/61/position = Vector3(3.28565e-07, 40.5995, 1.49012e-08)
bones/61/rotation = Quaternion(-0.581827, -0.0846291, -0.0166473, 0.808727)
bones/61/rotation = Quaternion(-0.581827, -0.0846291, -0.0166473, 0.808726)
bones/61/scale = Vector3(1, 1, 1)
bones/62/name = "mixamorig_RightFoot"
bones/62/parent = 61
@ -1344,7 +1344,7 @@ surface_material_override/0 = SubResource("StandardMaterial3D_j3emx")
[node name="HitCollisions" type="Node3D" parent="Body/Armature/Skeleton3D"]
[node name="BoneAttachment3D" type="BoneAttachment3D" parent="Body/Armature/Skeleton3D"]
transform = Transform3D(0.978017, -0.176546, -0.110972, 0.15839, 0.282801, 0.94601, -0.135631, -0.942791, 0.304547, -11.8203, 17.7472, -135.936)
transform = Transform3D(0.978017, -0.176546, -0.110973, 0.15839, 0.282801, 0.946011, -0.135631, -0.942791, 0.304547, -11.8203, 17.7472, -135.936)
bone_name = "mixamorig_Head"
bone_idx = 5

View File

@ -88,7 +88,7 @@ shape = SubResource("CapsuleShape3D_jc50w")
replication_config = SubResource("SceneReplicationConfig_tl74a")
[node name="FirstPersonCameraMount" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.609154, 0)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.609154, -0.209324)
[node name="Camera" type="Camera3D" parent="FirstPersonCameraMount"]
fov = 40.0

View File

@ -2,8 +2,15 @@ extends CSGBox3D
var health = 5
@export var color: Color = Color(0, 0, 0)
var colors = [Color(1.0, 0.0, 0.0, 1.0),
Color(0.0, 1.0, 0.0, 1.0),
Color(0.0, 0.0, 1.0, 0.0)]
func take_damage():
health -= 1
color = Color(randf(), randf(), randf())
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
@ -11,5 +18,6 @@ func _ready() -> void:
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
material_override.albedo_color = color
if health < 1:
queue_free()

View File

@ -26,7 +26,13 @@ func _ready() -> void:
$MultiplayerSpawner.spawn(char)
players.add_child(char)
func spawn_bullet(position):
print("spawning a bullet")
var node: Node3D = ResourceLoader.load("res://scenes/weapon/bullet.tscn").instantiate()
node.position = position.global_position
node.transform.basis = position.global_transform.basis
$BulletSpawner.spawn(node)
$Bullets.add_child(node)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,11 @@
extends CSGBox3D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass

View File

@ -1,6 +1,6 @@
extends Node3D
const SPEED = 715
const SPEED = 2
@onready var mesh = $RigidBody3D/MeshInstance3D
@onready var rigid_body_3d: RigidBody3D = $RigidBody3D

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=8 format=3 uid="uid://b0agqeg53tey5"]
[gd_scene load_steps=9 format=3 uid="uid://b0agqeg53tey5"]
[ext_resource type="Script" path="res://scenes/weapon/bullet.gd" id="1_th28m"]
@ -30,7 +30,16 @@ linear_accel_max = 6.67
[sub_resource type="BoxMesh" id="BoxMesh_mfkmi"]
size = Vector3(0.05, 0.05, 0.05)
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_b8q4c"]
properties/0/path = NodePath(".:position")
properties/0/spawn = true
properties/0/replication_mode = 1
properties/1/path = NodePath(".:rotation")
properties/1/spawn = true
properties/1/replication_mode = 1
[node name="Bullet" type="Node3D"]
transform = Transform3D(20, 0, 0, 0, 20, 0, 0, 0, 20, 0, 0, 0)
script = ExtResource("1_th28m")
[node name="RigidBody3D" type="RigidBody3D" parent="."]
@ -69,4 +78,7 @@ explosiveness = 1.0
process_material = SubResource("ParticleProcessMaterial_58cgi")
draw_pass_1 = SubResource("BoxMesh_mfkmi")
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
replication_config = SubResource("SceneReplicationConfig_b8q4c")
[connection signal="timeout" from="RigidBody3D/Timer" to="." method="_on_timer_timeout"]

View File

@ -1,6 +1,6 @@
extends Node3D
const SPEED = 715
const SPEED = 10
@onready var mesh = $Object_10

View File

@ -38,6 +38,10 @@ func _ready() -> void:
var err := _add_first_view_model()
if err != OK:
print("Error occured: " + str(err))
err = _add_legs_to_first_view()
if err != OK:
print("Error occured: " + str(err))
_enable_camera()
else:
var err := _add_world_model()
@ -64,6 +68,7 @@ var current_gun: String = "ak"
@onready var gun_mount: Node3D = $FirstPersonCameraMount/GunMount
@onready var gun_mount_anim: AnimationPlayer = $FirstPersonCameraMount/GunMount/AnimationPlayer
var gun_with_hands: Node3D = null
# -- Add the first person view to pthe player
func _add_first_view_model() -> Error :
# -- TODO: It should not be hardcoded
@ -81,6 +86,30 @@ func _add_first_view_model() -> Error :
gun_with_hands = node
gun_mount.add_child(node)
return OK
var first_view_legs_anim: AnimationPlayer = null
# -- Add a world model to the player, that should be seen by other players
# -- on the server
func _add_legs_to_first_view() -> Error :
# -- TODO: It should not be hardcoded
var path := "res://scenes/characters/blue/dummy.tscn"
if not ResourceLoader.exists(path):
return ERR_DOES_NOT_EXIST
var scene: PackedScene = ResourceLoader.load(path)
if not scene.can_instantiate():
return ERR_CANT_OPEN
var node: Node3D = scene.instantiate()
var skeleton: Skeleton3D = node.find_child("Skeleton3D")
var bone := skeleton.find_bone("mixamorig_Spine")
if bone != -1:
skeleton.set_bone_pose_scale(bone, Vector3(0, 0, 0))
model_mount.add_child(node)
var animation_node: AnimationPlayer = node.find_child("AnimationPlayer")
if animation_node != null:
first_view_legs_anim = animation_node
return OK
func _enable_camera():
first_view_camera.make_current()
@ -154,11 +183,15 @@ func _physics_process(delta: float) -> void:
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
gun_mount_anim.play("move")
if first_view_legs_anim != null:
first_view_legs_anim.play("Run Forward")
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
if gun_mount_anim.is_playing():
gun_mount_anim.stop()
if first_view_legs_anim != null and first_view_legs_anim.is_playing():
first_view_legs_anim.stop()
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
@ -171,6 +204,7 @@ var bullet = load("res://scenes/weapon/bullet.tscn")
@onready var aim_ray: RayCast3D = $FirstPersonCameraMount/BulletStartingPoint/AimRay
# --find the gun node and exec shoot
func _shoot():
_send_shot_to_server.rpc_id(1, aim_ray.global_position)
if aim_ray.is_colliding():
var collider := aim_ray.get_collider()
if collider != null and collider.is_in_group("target"):
@ -180,6 +214,12 @@ func _shoot():
var root := get_tree().get_root()
gun_with_hands.shoot()
@rpc("any_peer", "call_local", "unreliable_ordered")
func _send_shot_to_server(start_position):
# -- TODO: Should not be hardcoded
var world: Node3D = find_parent("ElTest")
world.spawn_bullet(bullet_starting_point)
func _get_camera_collision():
var viewport = get_viewport().size
var ray_origin = first_view_camera.project_ray_origin(viewport / 2)