WIP: Spawn bullets on the server and sync them

This commit is contained in:
2025-01-26 18:18:35 +01:00
parent adce112be9
commit e28845b688
11 changed files with 1467 additions and 18 deletions

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)