From 760d943172930f2f9d89b4d21ea5c750aa349ba6 Mon Sep 17 00:00:00 2001 From: yuki Date: Fri, 14 Nov 2025 00:56:23 -0300 Subject: [PATCH] properly spawn player hit --- scenes/player/player.gd | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/scenes/player/player.gd b/scenes/player/player.gd index d819eb6..0d07910 100644 --- a/scenes/player/player.gd +++ b/scenes/player/player.gd @@ -24,17 +24,9 @@ func _physics_process(_delta: float) -> void: var is_moving: bool = velocity.length() > 10 if Input.is_action_pressed("hit_left") and not is_hitting: - if not is_hitting: - sprite.play('up') - sprite.frame = 1 - sprite.pause() - - var hit: Area2D = preload("res://scenes/hit/hit.tscn").instantiate() - add_child(hit) - - hit.timer.connect("timeout", _on_hit_end) - - is_hitting = true + hit("left") + elif Input.is_action_pressed("hit_right") and not is_hitting: + hit("right") if is_moving: if abs(velocity.x) > abs(velocity.y): @@ -56,5 +48,29 @@ func get_movement_vector() -> Vector2: var y_mov: float = Input.get_action_strength('move_down') - Input.get_action_strength('move_up') return Vector2(x_mov, y_mov) +func hit(dir: String) -> void: + if not is_hitting: + sprite.play('up') + sprite.frame = 1 + sprite.pause() + + var sprite_texture: Texture2D = sprite.sprite_frames.get_frame_texture('up', 1) + var hit_node: Area2D = preload("res://scenes/hit/hit.tscn").instantiate() + + # set position according to frame width and height + @warning_ignore("integer_division") + hit_node.global_position = global_position + Vector2( + (sprite_texture.get_width()/2)*(-1 if dir == "left" else 1), + sprite_texture.get_height()*-1 + ) + + # flip entire node horizontally if spawning left + hit_node.scale = Vector2(-1 if dir == "left" else 1, 1) + get_parent().add_child(hit_node) + + hit_node.timer.connect("timeout", _on_hit_end) + + is_hitting = true + func _on_hit_end() -> void: is_hitting = false