From ec7b08812d32b4acde1a141674216adb545e613c Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Fri, 18 Jul 2025 16:21:30 +0300 Subject: [PATCH] some glue code between player and weapon --- base/assets/guns/placeholder/placeholder.tres | 34 ++++---- base/scenes/player.tscn | 84 ++----------------- base/scenes/weapons/weapon_base.tscn | 12 ++- base/scripts/player/player.gd | 72 ++++++++++------ .../weapons/fire_mode/auto_fire_mode.gd | 1 - base/scripts/weapons/weapon_base.gd | 21 ++++- 6 files changed, 100 insertions(+), 124 deletions(-) diff --git a/base/assets/guns/placeholder/placeholder.tres b/base/assets/guns/placeholder/placeholder.tres index 642f69b..39f8c9c 100644 --- a/base/assets/guns/placeholder/placeholder.tres +++ b/base/assets/guns/placeholder/placeholder.tres @@ -5,23 +5,6 @@ [ext_resource type="Texture2D" uid="uid://bwfkjfpk4dwx0" path="res://base/assets/guns/placeholder/shoot2.png" id="10_mjqfp"] [ext_resource type="Texture2D" uid="uid://bp1frp3sm8bic" path="res://base/assets/guns/placeholder/shoot3.png" id="11_0d256"] -[sub_resource type="Animation" id="Animation_ma1q3"] -resource_name = "static" -length = 0.001 -step = 0.0 -tracks/0/type = "value" -tracks/0/imported = false -tracks/0/enabled = true -tracks/0/path = NodePath(".:texture") -tracks/0/interp = 1 -tracks/0/loop_wrap = true -tracks/0/keys = { -"times": PackedFloat32Array(0), -"transitions": PackedFloat32Array(1), -"update": 1, -"values": [ExtResource("1_yheqn")] -} - [sub_resource type="Animation" id="Animation_i1xqq"] length = 0.001 tracks/0/type = "value" @@ -54,6 +37,23 @@ tracks/0/keys = { "values": [ExtResource("1_yheqn"), ExtResource("9_6fi3a"), ExtResource("10_mjqfp"), ExtResource("11_0d256")] } +[sub_resource type="Animation" id="Animation_ma1q3"] +resource_name = "static" +length = 0.001 +step = 0.0 +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath(".:texture") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0), +"transitions": PackedFloat32Array(1), +"update": 1, +"values": [ExtResource("1_yheqn")] +} + [resource] _data = { &"RESET": SubResource("Animation_i1xqq"), diff --git a/base/scenes/player.tscn b/base/scenes/player.tscn index 60ffbd8..cccfab7 100644 --- a/base/scenes/player.tscn +++ b/base/scenes/player.tscn @@ -1,83 +1,10 @@ -[gd_scene load_steps=11 format=3 uid="uid://dwx5tcatj35gu"] +[gd_scene load_steps=4 format=3 uid="uid://dwx5tcatj35gu"] [ext_resource type="Script" uid="uid://dts8lbivkgsmj" path="res://base/scripts/player/player.gd" id="1_1w3ab"] -[ext_resource type="Texture2D" uid="uid://cf7avgppv4kfk" path="res://base/assets/guns/placeholder/static.png" id="1_7s1wv"] -[ext_resource type="Texture2D" uid="uid://cfw6p5g680c55" path="res://base/assets/guns/placeholder/shoot1.png" id="2_7ptt8"] -[ext_resource type="Texture2D" uid="uid://bwfkjfpk4dwx0" path="res://base/assets/guns/placeholder/shoot2.png" id="3_1w3ab"] -[ext_resource type="Texture2D" uid="uid://bp1frp3sm8bic" path="res://base/assets/guns/placeholder/shoot3.png" id="4_gt0rj"] +[ext_resource type="PackedScene" uid="uid://bb6ovrbusyxpi" path="res://base/scenes/weapons/weapon_base.tscn" id="2_ma1q3"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_jjqxs"] -[sub_resource type="Animation" id="Animation_i1xqq"] -length = 0.001 -tracks/0/type = "value" -tracks/0/imported = false -tracks/0/enabled = true -tracks/0/path = NodePath(".:texture") -tracks/0/interp = 1 -tracks/0/loop_wrap = true -tracks/0/keys = { -"times": PackedFloat32Array(0), -"transitions": PackedFloat32Array(1), -"update": 1, -"values": [null] -} - -[sub_resource type="Animation" id="Animation_8sdfx"] -resource_name = "shoot" -length = 0.30001 -step = 0.1 -tracks/0/type = "value" -tracks/0/imported = false -tracks/0/enabled = true -tracks/0/path = NodePath(".:texture") -tracks/0/interp = 1 -tracks/0/loop_wrap = true -tracks/0/keys = { -"times": PackedFloat32Array(0, 0.1, 0.2), -"transitions": PackedFloat32Array(1, 1, 1), -"update": 1, -"values": [ExtResource("2_7ptt8"), ExtResource("3_1w3ab"), ExtResource("4_gt0rj")] -} -tracks/1/type = "method" -tracks/1/imported = false -tracks/1/enabled = true -tracks/1/path = NodePath("../..") -tracks/1/interp = 1 -tracks/1/loop_wrap = true -tracks/1/keys = { -"times": PackedFloat32Array(0.3), -"transitions": PackedFloat32Array(1), -"values": [{ -"args": [], -"method": &"shoot_animation_ended" -}] -} - -[sub_resource type="Animation" id="Animation_ma1q3"] -resource_name = "static" -length = 0.001 -step = 0.0 -tracks/0/type = "value" -tracks/0/imported = false -tracks/0/enabled = true -tracks/0/path = NodePath(".:texture") -tracks/0/interp = 1 -tracks/0/loop_wrap = true -tracks/0/keys = { -"times": PackedFloat32Array(0), -"transitions": PackedFloat32Array(1), -"update": 1, -"values": [ExtResource("1_7s1wv")] -} - -[sub_resource type="AnimationLibrary" id="AnimationLibrary_7ptt8"] -_data = { -&"RESET": SubResource("Animation_i1xqq"), -&"shoot": SubResource("Animation_8sdfx"), -&"static": SubResource("Animation_ma1q3") -} - [node name="Player" type="CharacterBody3D"] collision_layer = 2 collision_mask = 3 @@ -104,7 +31,8 @@ size_flags_vertical = 3 stretch_mode = 6 [node name="AnimationPlayer" type="AnimationPlayer" parent="HUD/Weapon"] -libraries = { -&"": SubResource("AnimationLibrary_7ptt8") -} autoplay = "static" + +[node name="WeaponContainer" type="Node3D" parent="."] + +[node name="Weapon" parent="WeaponContainer" instance=ExtResource("2_ma1q3")] diff --git a/base/scenes/weapons/weapon_base.tscn b/base/scenes/weapons/weapon_base.tscn index 7139e85..36dd56c 100644 --- a/base/scenes/weapons/weapon_base.tscn +++ b/base/scenes/weapons/weapon_base.tscn @@ -1,13 +1,19 @@ -[gd_scene load_steps=4 format=3 uid="uid://bb6ovrbusyxpi"] +[gd_scene load_steps=6 format=3 uid="uid://bb6ovrbusyxpi"] [ext_resource type="Script" uid="uid://bf0q3xe6oavqa" path="res://base/scripts/weapons/weapon_base.gd" id="1_rwatx"] +[ext_resource type="Script" path="res://base/scripts/weapons/fire_mode/single_fire_mode.gd" id="2_bt0tf"] [ext_resource type="Script" uid="uid://bvurg687pt06s" path="res://base/scripts/weapons/barrel.gd" id="2_n0n7m"] [ext_resource type="AnimationLibrary" uid="uid://cw8bt4hqxpk55" path="res://base/assets/guns/placeholder/placeholder.tres" id="2_p83j3"] -[node name="Weapon" type="Node3D" node_paths=PackedStringArray("barrel")] +[sub_resource type="Resource" id="Resource_ij06d"] +script = ExtResource("2_bt0tf") +fire_delay = 0.3 +metadata/_custom_type_script = "uid://ckexgw71dd5jc" + +[node name="Weapon" type="Node3D"] script = ExtResource("1_rwatx") -barrel = NodePath("Barrel") uses_hands = Array[int]([1]) +fire_mode = SubResource("Resource_ij06d") animation_library = ExtResource("2_p83j3") [node name="Barrel" type="Node3D" parent="."] diff --git a/base/scripts/player/player.gd b/base/scripts/player/player.gd index 8fdb0a6..7685b37 100644 --- a/base/scripts/player/player.gd +++ b/base/scripts/player/player.gd @@ -9,17 +9,22 @@ var queue: CommandQueue = CommandQueue.new() @onready var camera: Camera3D = $"Camera" @onready var weapon_player: AnimationPlayer = $"HUD/Weapon/AnimationPlayer" +@onready var weapons: Node3D = $"WeaponContainer" + +var current_weapon: Weapon -# Called when the node enters the scene tree for the first time. func _ready() -> void: queue.command_pushed.connect(on_queue_command_pushed) queue.command_popped.connect(on_queue_command_popped) - weapon_player.play("static") + + current_weapon = weapons.get_child(0) as Weapon + + weapon_player.add_animation_library("current", current_weapon.animation_library) + weapon_player.play("current/static") + Input.mouse_mode = Input.MOUSE_MODE_CAPTURED - func _process(_delta: float) -> void: - # TODO: use proper weapon system, this is just for testing command queue if Input.is_action_pressed("shoot") \ and queue.current_command(CommandQueue.Side.RIGHT) \ == CommandQueue.DEFAULT_COMMAND: @@ -30,8 +35,15 @@ func _process(_delta: float) -> void: CommandQueue.Side.LEFT, CommandQueue.Side.RIGHT ]) + + for side in CommandQueue.Side.values(): + var command = queue.current_command(side) + match side: + CommandQueue.Side.LEFT: + handle_current_left_command(command) + CommandQueue.Side.RIGHT: + handle_current_right_command(command) -# Called every frame. 'delta' is the elapsed time since the previous frame. func _physics_process(delta: float) -> void: var direction = Vector3.ZERO direction.z = Input.get_axis("move_forward", "move_backward") @@ -62,22 +74,6 @@ func on_queue_command_pushed(sides, commands): CommandQueue.Side.RIGHT: handle_new_right_command(commands[i]) -func handle_new_left_command(command: CommandQueue.Command): - match command: - CommandQueue.Command.NONE: - pass - _: - print('New command %s is not implemented for left hand.' % str(command)) - -func handle_new_right_command(command: CommandQueue.Command): - match command: - CommandQueue.Command.NONE: - pass - CommandQueue.Command.SHOOT: - weapon_player.play('shoot') - _: - print('New command %s is not implemented for right hand.' % str(command)) - func shoot_animation_ended(): queue.pop() @@ -89,18 +85,46 @@ func on_queue_command_popped(commands): CommandQueue.Side.RIGHT: handle_ended_right_command(commands[i]) +func handle_new_left_command(command: CommandQueue.Command): + match command: + CommandQueue.Command.NONE: + pass + _: + print('New command %s is not implemented for left hand.' % command) + +func handle_new_right_command(command: CommandQueue.Command): + match command: + CommandQueue.Command.NONE | CommandQueue.Command.SHOOT: + pass + _: + print('New command %s is not implemented for right hand.' % command) + func handle_ended_left_command(command: CommandQueue.Command): match command: CommandQueue.Command.NONE: pass _: - print('Ended command %s is not implemented for left hand.' % str(command)) + print('Ended command %s is not implemented for left hand.' % command) func handle_ended_right_command(command: CommandQueue.Command): match command: CommandQueue.Command.NONE: pass CommandQueue.Command.SHOOT: - weapon_player.play('static') + weapon_player.play('current/static') _: - print('Ended command %s is not implemented for right hand.' % str(command)) + print('Ended command %s is not implemented for right hand.' % command) + +func handle_current_left_command(command: CommandQueue.Command): + match command: + CommandQueue.Command.NONE: + pass + _: + print('Current command %s is not implemented for left hand.' % command) + +func handle_current_right_command(command: CommandQueue.Command): + match command: + CommandQueue.Command.NONE: + pass + _: + print('Current command %s is not implemented for right hand.' % command) diff --git a/base/scripts/weapons/fire_mode/auto_fire_mode.gd b/base/scripts/weapons/fire_mode/auto_fire_mode.gd index cf9f4a6..0d9f661 100644 --- a/base/scripts/weapons/fire_mode/auto_fire_mode.gd +++ b/base/scripts/weapons/fire_mode/auto_fire_mode.gd @@ -18,7 +18,6 @@ func _on_fire_begin(_tree : SceneTree) -> void: cooldown = false check_unfinished_timer(cooldown_timer,on_cooldown_timeout) - func _process(tree : SceneTree) -> void: if can_fire() : return diff --git a/base/scripts/weapons/weapon_base.gd b/base/scripts/weapons/weapon_base.gd index 53c16f7..3db1384 100644 --- a/base/scripts/weapons/weapon_base.gd +++ b/base/scripts/weapons/weapon_base.gd @@ -2,7 +2,7 @@ extends Node3D class_name Weapon -@export var barrel: Barrel +@onready var barrel = $"Barrel" @export var uses_hands: Array[CommandQueue.Side] @export var max_ammo: int = 7 @@ -12,3 +12,22 @@ var ammo: int = max_ammo @export var fire_mode: BaseFireMode ## Weapon animation library. Should contain "static", "fire", "reload" animations @export var animation_library: AnimationLibrary + +var is_firing: bool = false + +func _ready() -> void: + fire_mode.barrel = barrel + +## Begin to fire +func request_fire() -> void: + fire_mode.on_fire_begin(get_tree()) + is_firing = true + +func _process(_dt) -> void: + if is_firing: + fire_mode._process(get_tree()) + +## End fire +func end_fire() -> void: + fire_mode.on_fire_end(get_tree()) + is_firing = false