From 98d61b0b0b332c69739446f9c3ed656a4e90567a Mon Sep 17 00:00:00 2001 From: rowan Date: Tue, 6 May 2025 15:04:14 -0500 Subject: [PATCH] add fire rate timer to weapons --- project.godot | 2 ++ resources/items/handgun.tres | 2 ++ scenes/player.tscn | 48 +++++++++++++++------------ src/equipped_weapon.ts | 22 +++++++++--- src/player.ts | 2 +- src/weapon.ts | 9 ++++- typings/scenes/node_3d.nodes.gen.d.ts | 6 +++- typings/scenes/player.nodes.gen.d.ts | 6 +++- 8 files changed, 66 insertions(+), 31 deletions(-) diff --git a/project.godot b/project.godot index 17c1542..cb86478 100644 --- a/project.godot +++ b/project.godot @@ -77,11 +77,13 @@ _dev_console_toggle={ aim={ "deadzone": 0.5, "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":2,"canceled":false,"pressed":false,"double_click":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":88,"key_label":0,"unicode":120,"location":0,"echo":false,"script":null) ] } fire={ "deadzone": 0.5, "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":90,"key_label":0,"unicode":122,"location":0,"echo":false,"script":null) ] } look_left={ diff --git a/resources/items/handgun.tres b/resources/items/handgun.tres index 7de15a9..67b7f6d 100644 --- a/resources/items/handgun.tres +++ b/resources/items/handgun.tres @@ -8,4 +8,6 @@ name = &"" description = "" max_quantity = 99 scene = "uid://ddgak6clk2i2p" +fire_rate = 0.5 +capacity = 9 metadata/_custom_type_script = "uid://tv71bu1y658n" diff --git a/scenes/player.tscn b/scenes/player.tscn index 9083f61..90dae7a 100644 --- a/scenes/player.tscn +++ b/scenes/player.tscn @@ -132,15 +132,15 @@ script = ExtResource("2_pdrhn") [node name="Mesh" parent="." instance=ExtResource("7_fjrip")] [node name="Skeleton3D" parent="Mesh/AuxScene/Scene/Armature" index="0"] -bones/0/position = Vector3(-1.06432, 98.5161, 1.28562) -bones/0/rotation = Quaternion(0.0437049, -0.455054, -0.0441828, 0.888293) -bones/2/rotation = Quaternion(-0.0551638, 0.0563445, 0.0314511, 0.99639) -bones/4/rotation = Quaternion(0.0240218, 0.0676321, 0.00773834, 0.997391) -bones/6/rotation = Quaternion(0.0374848, 0.0679606, 0.00519961, 0.99697) -bones/8/rotation = Quaternion(-0.419926, 0.506888, -0.571468, -0.490053) -bones/10/rotation = Quaternion(0.378262, 0.0908406, -0.817036, 0.42558) -bones/12/rotation = Quaternion(-2.37487e-08, -1.04156e-07, -0.25919, 0.965826) -bones/14/rotation = Quaternion(-0.217025, 0.380649, 0.184106, 0.879836) +bones/0/position = Vector3(-1.34296, 99.233, 1.4959) +bones/0/rotation = Quaternion(0.0438457, -0.453154, -0.0426107, 0.889333) +bones/2/rotation = Quaternion(-0.0580086, 0.0574482, 0.0299733, 0.996211) +bones/4/rotation = Quaternion(0.023142, 0.0682831, 0.00805779, 0.997365) +bones/6/rotation = Quaternion(0.0366486, 0.0685247, 0.00561944, 0.99696) +bones/8/rotation = Quaternion(-0.412947, 0.510846, -0.570029, -0.493537) +bones/10/rotation = Quaternion(0.366708, 0.091387, -0.824325, 0.421499) +bones/12/rotation = Quaternion(-3.39933e-08, 5.02926e-08, -0.244577, 0.96963) +bones/14/rotation = Quaternion(-0.218999, 0.389096, 0.162682, 0.879874) bones/16/rotation = Quaternion(0.491661, 0.0186145, 0.04132, 0.869607) bones/18/rotation = Quaternion(0.583947, 9.22009e-08, 0.0475734, 0.810396) bones/20/rotation = Quaternion(0.435138, -7.46222e-08, 0.03545, 0.899666) @@ -156,12 +156,12 @@ bones/44/rotation = Quaternion(0.430986, -3.23344e-08, 0.0351118, 0.901675) bones/48/rotation = Quaternion(0.349343, 0.0283774, -0.150582, 0.92438) bones/50/rotation = Quaternion(-0.112075, -0.188988, 0.210821, 0.952511) bones/52/rotation = Quaternion(-0.271577, -0.018948, 0.408217, 0.871347) -bones/56/rotation = Quaternion(0.100429, 0.0667491, -0.0240202, 0.992412) -bones/58/rotation = Quaternion(0.00594193, 0.164804, 0.0913867, 0.982066) -bones/61/rotation = Quaternion(-0.518834, -0.556127, 0.578182, -0.295363) -bones/63/rotation = Quaternion(0.254827, -0.255068, 0.509372, 0.781373) -bones/65/rotation = Quaternion(2.09548e-09, -1.99941e-08, 0.659355, 0.751832) -bones/67/rotation = Quaternion(-0.169227, -0.371956, -0.363468, 0.837199) +bones/56/rotation = Quaternion(0.103598, 0.0666441, -0.0240849, 0.992092) +bones/58/rotation = Quaternion(0.00749621, 0.164595, 0.0896287, 0.982252) +bones/61/rotation = Quaternion(-0.515646, -0.558738, 0.572809, -0.306285) +bones/63/rotation = Quaternion(0.255191, -0.248185, 0.504518, 0.786602) +bones/65/rotation = Quaternion(-4.35393e-08, 4.86605e-09, 0.655273, 0.755392) +bones/67/rotation = Quaternion(-0.18199, -0.388089, -0.336697, 0.838393) bones/69/rotation = Quaternion(0.128107, -0.173846, 0.152522, 0.964419) bones/71/rotation = Quaternion(-0.0940495, 0.0712352, 0.0315575, 0.992514) bones/73/rotation = Quaternion(0.0268879, 0.0186427, 0.218404, 0.97531) @@ -177,17 +177,17 @@ bones/97/rotation = Quaternion(0.173011, 1.41736e-08, -0.0177296, 0.98476) bones/101/rotation = Quaternion(0.324573, -0.049477, 0.0773928, 0.94139) bones/103/rotation = Quaternion(0.491491, -5.96046e-07, -0.0503663, 0.869425) bones/105/rotation = Quaternion(0.273046, -6.9337e-07, -0.0279808, 0.961594) -bones/109/rotation = Quaternion(-0.126597, 0.13207, 0.981033, 0.0640646) -bones/111/rotation = Quaternion(-0.233629, -0.0744139, 0.00367423, 0.969467) -bones/113/rotation = Quaternion(0.516729, 0.12195, 0.0692569, 0.844584) +bones/109/rotation = Quaternion(-0.127311, 0.113785, 0.983303, 0.0629229) +bones/111/rotation = Quaternion(-0.199275, -0.0738388, 0.000908459, 0.977157) +bones/113/rotation = Quaternion(0.503553, 0.125908, 0.0705691, 0.851823) bones/115/rotation = Quaternion(0.335242, -6.35498e-08, 2.67434e-07, 0.942132) -bones/119/rotation = Quaternion(0.126352, 0.264053, 0.94116, -0.168904) -bones/121/rotation = Quaternion(-0.273629, -0.0831775, 0.0214499, 0.957992) -bones/123/rotation = Quaternion(0.429761, -0.0809993, -0.0474114, 0.898052) +bones/119/rotation = Quaternion(0.124542, 0.250518, 0.945838, -0.164682) +bones/121/rotation = Quaternion(-0.248302, -0.0864232, 0.020511, 0.964602) +bones/123/rotation = Quaternion(0.421959, -0.0804281, -0.0458337, 0.901877) bones/125/rotation = Quaternion(0.335241, -2.57727e-08, 5.75265e-09, 0.942132) [node name="RightHand" parent="Mesh/AuxScene/Scene/Armature/Skeleton3D" index="2"] -transform = Transform3D(0.259487, 0.484082, -0.835662, 0.944169, 0.0547265, 0.324883, 0.203003, -0.873309, -0.442854, -3.09716, 140.733, 59.0089) +transform = Transform3D(0.288251, 0.477446, -0.830034, 0.9427, 0.0105974, 0.333473, 0.168011, -0.878596, -0.447034, -2.98473, 141.91, 59.327) [node name="Node3D" type="Node3D" parent="Mesh/AuxScene/Scene/Armature/Skeleton3D/RightHand" index="0"] @@ -213,8 +213,12 @@ script = ExtResource("6_jscba") [node name="EquippedWeapon" type="Node" parent="."] script = ExtResource("6_eg2om") +starting_weapon = "uid://c15pu3ela0g6k" _transform_parent = NodePath("../Mesh/AuxScene/Scene/Armature/Skeleton3D/RightHand/Node3D") +[node name="FireRate" type="Timer" parent="EquippedWeapon"] +one_shot = true + [node name="AnimationTree" type="AnimationTree" parent="."] root_node = NodePath("../Mesh") tree_root = SubResource("AnimationNodeStateMachine_5lvsk") diff --git a/src/equipped_weapon.ts b/src/equipped_weapon.ts index 07432a8..02002c2 100644 --- a/src/equipped_weapon.ts +++ b/src/equipped_weapon.ts @@ -1,6 +1,6 @@ -import { GArray, Node, Node3D, PackedScene, Signal1, Vector3 } from 'godot' +import { GArray, Node, Node3D, PackedScene, Signal1, Timer, Vector3 } from 'godot' import { export_file, signal } from 'godot.annotations' -import { export_node } from './annotations' +import { export_node, onready } from './annotations' import Weapon from './weapon' import AsyncResourceLoader from './async_resource_loader' @@ -21,6 +21,9 @@ export default class EquippedWeapon extends Node { @signal() unequipped!: Signal1 + @onready('FireRate') + _fire_rate_timer!: Timer + has_equipped_weapon(): boolean { return this._has_equipped_weapon } @@ -41,7 +44,6 @@ export default class EquippedWeapon extends Node { } _parent_scene_to_transform(scene: PackedScene) { - console.log('loaded', scene.resource_name) const children: GArray = this.transform_parent.get_children() for (const child of children) { child.queue_free() @@ -53,8 +55,8 @@ export default class EquippedWeapon extends Node { } equip(weapon: Weapon) { - console.log('weapon scene', weapon, weapon.scene) - AsyncResourceLoader.instance.load(weapon.scene, 'PackedScene').then(scene => this._parent_scene_to_transform(scene)) + AsyncResourceLoader.instance.load(weapon.scene, 'PackedScene') + .then(scene => this._parent_scene_to_transform(scene)) if (this._has_equipped_weapon) { this.unequip() @@ -62,7 +64,17 @@ export default class EquippedWeapon extends Node { this._has_equipped_weapon = true this._equipped_weapon = weapon + this._fire_rate_timer.wait_time = weapon.fire_rate this.equipped.emit(weapon) } + + fire(): boolean { + if (this._fire_rate_timer.time_left <= 0) { + this._fire_rate_timer.start() + return true + } else { + return false + } + } } diff --git a/src/player.ts b/src/player.ts index 089198e..f3b3158 100644 --- a/src/player.ts +++ b/src/player.ts @@ -66,7 +66,7 @@ export default class Player extends CharacterBody3D { } is_firing() { - return this._wants_to_fire && this.is_aiming() + return this._wants_to_fire && this.is_aiming() && this.equipped_weapon.fire() } private _can_act: boolean = true diff --git a/src/weapon.ts b/src/weapon.ts index 1889eb7..16a4dfa 100644 --- a/src/weapon.ts +++ b/src/weapon.ts @@ -1,8 +1,15 @@ +import { Variant } from 'godot' import { export_scene } from './annotations' import Item from './item' -import { export_file } from 'godot.annotations' +import { export_ } from 'godot.annotations' export default class Weapon extends Item { @export_scene() readonly scene!: string + + @export_(Variant.Type.TYPE_FLOAT) + readonly fire_rate!: number + + @export_(Variant.Type.TYPE_INT) + readonly capacity!: number } diff --git a/typings/scenes/node_3d.nodes.gen.d.ts b/typings/scenes/node_3d.nodes.gen.d.ts index 73b52aa..10c236f 100644 --- a/typings/scenes/node_3d.nodes.gen.d.ts +++ b/typings/scenes/node_3d.nodes.gen.d.ts @@ -52,7 +52,11 @@ declare module "godot" { } >, Inventory: Node<{}>, - EquippedWeapon: Node<{}>, + EquippedWeapon: Node< + { + FireRate: Timer<{}>, + } + >, AnimationTree: AnimationTree<{}>, } >, diff --git a/typings/scenes/player.nodes.gen.d.ts b/typings/scenes/player.nodes.gen.d.ts index 7d1a0ea..3a7cc95 100644 --- a/typings/scenes/player.nodes.gen.d.ts +++ b/typings/scenes/player.nodes.gen.d.ts @@ -37,7 +37,11 @@ declare module "godot" { } >, Inventory: Node<{}>, - EquippedWeapon: Node<{}>, + EquippedWeapon: Node< + { + FireRate: Timer<{}>, + } + >, AnimationTree: AnimationTree<{}>, }, }