The_Dark_Side_of_Earth/player/sword.gd
2025-10-14 15:05:34 +02:00

50 lines
1.5 KiB
GDScript

extends Area2D
var damage = 20
var facing = -1
var facing_mult = 1
var dmg_id
var swing_knockback = 1000
var apply_swing_knockback = true
func _ready() -> void:
get_parent().attack.connect(swing)
func swing(dir_str) -> void:
# Each swing is a new damage instance
dmg_id = Global.next_dmg_id
facing = get_parent().facing * facing_mult
# Adjust the orientation depending on direction of the player,
# direction of the sword and whether it was an upward strike
if dir_str == "up":
scale.x = abs(scale.x)
scale.y = abs(scale.y) * -facing
rotation = PI/2 * facing_mult
else:
scale.x = abs(scale.x) * -facing
scale.y = abs(scale.y)
rotation = 0
# Sprite is visible during swing
$Sprite.visible = true
# Wait for the physics to register the new orientation of the sword before starting the swing
await get_tree().physics_frame
await get_tree().physics_frame
# Upslashes do not knockback you when you hit
if dir_str == "up":
apply_swing_knockback = false
$SlashTimer.start()
func end_slash():
# Reset swing-specific values to default
$Sprite.visible = false
apply_swing_knockback = true
func _process(_delta: float) -> void:
if $SlashTimer.time_left > 0:
# Hurt all overlapping enemies
for area in get_overlapping_areas():
var hurt_dir = -get_parent().get_node("EarthAligner").global_from_local(Vector2(facing, 0)).rotated(-facing*rotation)
if area.hurt(damage, hurt_dir, dmg_id) and apply_swing_knockback:
get_parent().reset_to_velocity += Vector2(swing_knockback * -facing, 0)
apply_swing_knockback = false