Add per-weapon scene and better first person view

This commit is contained in:
2025-01-25 17:24:57 +01:00
parent 1f1f2918c9
commit 1c8458048c
20 changed files with 3761 additions and 15 deletions

View File

@ -58,17 +58,26 @@ func _add_world_model() -> Error :
model_mount.add_child(node)
return OK
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
var path := "res://scenes/characters/first_person_view.tscn"
# -- TODO: It should not be hardcoded
# Define a format string with placeholder '%s'
var path_tmpl := "res://scenes/weapon/guns/%s/with_hands.tscn"
var path := path_tmpl % current_gun
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()
first_view_camera.add_child(node)
node.scale = Vector3(0.03,0.03,0.03)
node.position = Vector3(0.02, -0.03, -0.07)
gun_with_hands = node
gun_mount.add_child(node)
return OK
func _enable_camera():
@ -101,6 +110,7 @@ var jump_vel: Vector3 # Jumping velocity
@export_range(0.1, 3.0, 0.1) var jump_height: float = 1 # m
@export_range(0.1, 3.0, 0.1, "or_greater") var camera_sens: float = 1
func _unhandled_input(event: InputEvent) -> void:
if _is_current_player():
if event is InputEventMouseMotion:
@ -135,9 +145,12 @@ func _physics_process(delta: float) -> void:
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()
if direction:
gun_mount_anim.play("move")
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
if gun_mount_anim.is_playing():
gun_mount_anim.stop()
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
@ -148,11 +161,13 @@ var bullet = load("res://scenes/weapon/bullet.tscn")
@onready var shooting_raycast: RayCast3D = $FirstPersonCameraMount/RayCast3D
@onready var bullet_starting_point: Node3D = $FirstPersonCameraMount/BulletStartingPoint
# --find the gun node and exec shoot
func _shoot():
print("shooting")
var root := get_tree().get_root()
#if !gun_anim.is_playing():
#gun_anim.play("Shoot")
gun_with_hands.shoot()
#gun_mount_anim.play("shooting")
var node: Node3D = bullet.instantiate()
node.position = bullet_starting_point.global_position
node.transform.basis = bullet_starting_point.global_transform.basis