The_Dark_Side_of_Earth/player/sword.gd

51 lines
1.5 KiB
GDScript3
Raw Normal View History

2025-09-16 14:59:40 +02:00
extends Area2D
var damage = 20
var facing = -1
2025-09-17 18:23:42 +02:00
var facing_mult = 1
var dmg_id
2025-10-02 15:08:59 +02:00
var swing_knockback = 1000
var apply_swing_knockback = true
2025-09-16 14:59:40 +02:00
func _ready() -> void:
2025-09-17 18:23:42 +02:00
get_parent().attack.connect(swing)
2025-09-16 14:59:40 +02:00
func swing(dir_str) -> void:
2025-10-14 15:05:34 +02:00
# Each swing is a new damage instance
dmg_id = Global.next_dmg_id
2025-10-14 15:05:34 +02:00
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)
2025-10-14 15:05:34 +02:00
scale.y = abs(scale.y) * -facing
2025-10-02 18:31:42 +02:00
rotation = PI/2 * facing_mult
else:
2025-10-14 15:05:34 +02:00
scale.x = abs(scale.x) * -facing
scale.y = abs(scale.y)
rotation = 0
2025-10-14 15:05:34 +02:00
# 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
2025-10-14 15:05:34 +02:00
# Upslashes do not knockback you when you hit
if dir_str == "up":
apply_swing_knockback = false
2025-10-14 15:05:34 +02:00
$SlashTimer.start()
2025-09-16 14:59:40 +02:00
2025-10-14 15:05:34 +02:00
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
2025-09-16 14:59:40 +02:00
for area in get_overlapping_areas():
2025-10-14 15:05:34 +02:00
var hurt_dir = -get_parent().get_node("EarthAligner").global_from_local(Vector2(facing, 0)).rotated(-facing*rotation)
2025-10-02 15:08:59 +02:00
if area.hurt(damage, hurt_dir, dmg_id) and apply_swing_knockback:
2025-10-14 15:05:34 +02:00
get_parent().reset_to_velocity += Vector2(swing_knockback * -facing, 0)
2025-10-02 15:08:59 +02:00
apply_swing_knockback = false