From 20517af12ee819ac48bdbf6e575c85f31241f369 Mon Sep 17 00:00:00 2001 From: teatov Date: Tue, 25 Feb 2025 00:47:40 +1000 Subject: [PATCH] add attack cooldown and hit window --- scenes/player.tscn | 2 +- scripts/debug/debug_collision_shapes.gd | 8 +++++ scripts/player/player_attack.gd | 40 +++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/scenes/player.tscn b/scenes/player.tscn index 105fd61..926c95e 100644 --- a/scenes/player.tscn +++ b/scenes/player.tscn @@ -10,7 +10,7 @@ height = 1.8 [sub_resource type="CylinderShape3D" id="CylinderShape3D_qsqht"] -height = 0.43 +height = 0.5 radius = 2.0 [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_m1xj5"] diff --git a/scripts/debug/debug_collision_shapes.gd b/scripts/debug/debug_collision_shapes.gd index f9bf6e7..9ef2903 100644 --- a/scripts/debug/debug_collision_shapes.gd +++ b/scripts/debug/debug_collision_shapes.gd @@ -3,6 +3,8 @@ extends RefCounted const MARGIN = 0.01 +var is_visible: bool = true + var _mesh_nodes: Array[MeshInstance3D] = [] @@ -56,6 +58,12 @@ func init(children: Array[Node], parent: Node, material: Material) -> void: Debugger.mode_changed.connect(_on_debugger_mode_changed) +func set_visibility(visible: bool) -> void: + is_visible = visible + for node in _mesh_nodes: + node.visible = is_visible + + func _on_debugger_mode_changed(mode: Debugger.Mode) -> void: for mesh_node in _mesh_nodes: mesh_node.visible = mode == Debugger.Mode.FULL diff --git a/scripts/player/player_attack.gd b/scripts/player/player_attack.gd index ef85f75..dd5a088 100644 --- a/scripts/player/player_attack.gd +++ b/scripts/player/player_attack.gd @@ -3,6 +3,8 @@ extends Area3D signal attacked +const COOLDOWN_TIME: float = 0.3 +const HIT_WINDOW_TIME: float = 0.25 const SWOOP_EFFECT_TIME: float = 0.25 @export var _collision_debug_material: Material @@ -10,6 +12,8 @@ const SWOOP_EFFECT_TIME: float = 0.25 var _debug_collision_shapes := DebugCollisionShapes.new() +var _cooldown_timer: float +var _hit_window_timer: float var _swoop_effect_timer: float @onready var _swoop_mesh: MeshInstance3D = $SwoopMesh @@ -29,23 +33,50 @@ func _unhandled_input(event: InputEvent) -> void: func _process(delta: float) -> void: - if _swoop_effect_timer >= 0: + if _cooldown_timer > 0: + _cooldown_timer -= delta + + if _hit_window_timer > 0: + _hit_window_timer -= delta + elif _debug_collision_shapes.is_visible: + _debug_collision_shapes.set_visibility(false) + + if _swoop_effect_timer > 0: _swoop_effect_timer -= delta - else: - _swoop_effect_timer = 0 + (_swoop_mesh.material_override as StandardMaterial3D).albedo_color = Color( 1, 1, 1, _swoop_effect_timer / SWOOP_EFFECT_TIME ) _swoop_mesh.visible = _swoop_effect_timer > 0 + Debugger.text("_cooldown_timer", _cooldown_timer, 2) + Debugger.text("_hit_window_timer", _hit_window_timer, 2) + Debugger.text("_swoop_effect_timer", _swoop_effect_timer, 2) + Debugger.vector( + "fghdh", + global_position, + global_position + global_basis.z.rotated(Vector3.UP, _attack_max_angle) * 2 + ) + Debugger.vector( + "fghdh2", + global_position, + global_position + global_basis.z.rotated(Vector3.UP, -_attack_max_angle) * 2 + ) + func _physics_process(_delta: float) -> void: monitoring = Input.is_action_just_pressed("attack") func _attack() -> void: + if _cooldown_timer > 0: + return + attacked.emit() + _cooldown_timer = COOLDOWN_TIME + _hit_window_timer = HIT_WINDOW_TIME _swoop_effect_timer = SWOOP_EFFECT_TIME + _debug_collision_shapes.set_visibility(true) func _hit_projectile(projectile: Projectile) -> void: @@ -60,5 +91,8 @@ func _hit_projectile(projectile: Projectile) -> void: func _on_body_entered(node: Node3D) -> void: + if _hit_window_timer <= 0: + return + if node is Projectile: _hit_projectile(node as Projectile)