WIP: Add basic ragdoll

This commit is contained in:
2025-01-27 14:51:05 +01:00
parent 66cbb87eaa
commit 642d84c0ad
7 changed files with 350 additions and 24 deletions

View File

@ -17,6 +17,7 @@ var mouse_captured: bool = false
@onready var first_view_camera_mount: Node3D = $FirstPersonCameraMount
@onready var first_view_camera: Camera3D = $FirstPersonCameraMount/Camera
@onready var model_mount: Node3D = $ModelMount
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
@ -42,33 +43,50 @@ func _ready() -> void:
err = _add_legs_to_first_view()
if err != OK:
print("Error occured: " + str(err))
var world_model := _add_world_model()
if world_model == null:
print("Error occured: " + "couldn't load the world model")
_enable_camera()
else:
var err := _add_world_model()
if err != OK:
print("Error occured: " + str(err))
var world_model := _add_world_model()
if world_model == null:
print("Error occured: " + "couldn't load the world model")
_hide_camera_mount()
_capture_mouse()
# -- Add a world model to the player, that should be seen by other players
# -- on the server
func _add_world_model() -> Error :
func _add_world_model() -> Node3D :
# -- TODO: It should not be hardcoded
var path := "res://scenes/characters/blue/dummy.tscn"
if not ResourceLoader.exists(path):
return ERR_DOES_NOT_EXIST
push_error(ERR_DOES_NOT_EXIST)
return null
var scene: PackedScene = ResourceLoader.load(path)
if not scene.can_instantiate():
return ERR_CANT_OPEN
push_error(ERR_CANT_OPEN)
return null
var node: Node3D = scene.instantiate()
model = node
model_mount.add_child(node)
return OK
if _is_current_player():
node.make_invisible()
first_view_camera.cull_mask &= ~(1 << 1)
return node
func _hide_camera_mount() :
func _hide_camera_mount():
first_view_camera_mount.visible = 0
hud.visible = 0
func make_node_invisible_for_camera(node: Node3D, camera: Camera3D):
if node and camera:
# Set the node to Layer 2 (or any other layer you want)
node.visibility_layer = 1 << 1 # Assign the node to Layer 2
# Disable Layer 2 on the camera's culling mask (this makes it invisible to this camera)
camera.cull_mask &= ~(1 << 1) # Disable Layer 2 on this camera
var current_gun: String = "ak"
@onready var gun_mount: Node3D = $FirstPersonCameraMount/GunMount
@ -172,7 +190,11 @@ func _process(delta: float) -> void:
health_indicator.text = str(health)
fps_indicator.text = str(Engine.get_frames_per_second())
if health == 0:
alive = false
model.reparent(get_tree().get_root())
model.die()
queue_free()
func _physics_process(delta: float) -> void:
# Add the gravity.
@ -203,8 +225,8 @@ func _physics_process(delta: float) -> void:
first_view_legs_anim.stop()
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()
if alive:
move_and_slide()
# -- TODO: It shouldn't be hardcoded
var bullet = load("res://scenes/weapon/bullet.tscn")