Refactored sword code

This commit is contained in:
RealMelwei 2025-10-14 15:05:34 +02:00
parent 2a346363eb
commit 14e7fc057f
3 changed files with 39 additions and 20 deletions

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=16 format=3 uid="uid://cxo6bq26huau7"]
[gd_scene load_steps=18 format=3 uid="uid://cxo6bq26huau7"]
[ext_resource type="PackedScene" uid="uid://cmaovvr15b3qk" path="res://player/player.tscn" id="2_1bvp3"]
[ext_resource type="Texture2D" uid="uid://d3fpq76anm4t7" path="res://world/Background Prototype/Background prototype.png" id="3_kek77"]
@ -15,6 +15,8 @@
[ext_resource type="PackedScene" uid="uid://cqn67nwyrtq3k" path="res://ui/journal/journal.tscn" id="10_w48qg"]
[ext_resource type="PackedScene" uid="uid://cpe4s6vsn0ujd" path="res://enemies/boss/boss.tscn" id="11_efxa6"]
[ext_resource type="Script" uid="uid://gul4u5tw1vxk" path="res://bg_image.gd" id="13_vivmo"]
[ext_resource type="PackedScene" uid="uid://bwtdls58ajair" path="res://items/unique_items/upslash/upslash.tscn" id="16_yaehf"]
[ext_resource type="PackedScene" uid="uid://bbpf28ohayd8n" path="res://items/unique_items/backslash/backslash.tscn" id="17_074og"]
[node name="main" type="Node2D"]
@ -104,6 +106,12 @@ colors = Array[Color]([Color(0, 0.6441987, 0.6693053, 1), Color(0.90588236, 0.15
script = ExtResource("10_efxa6")
boss = ExtResource("11_efxa6")
[node name="Upslash" parent="." instance=ExtResource("16_yaehf")]
position = Vector2(264, -3143)
[node name="Backslash" parent="." instance=ExtResource("17_074og")]
position = Vector2(-48, -3137)
[connection signal="active_item_changed" from="Player" to="UIOverlay/ItemUI" method="_on_player_active_item_changed"]
[connection signal="health_changed" from="Player" to="UIOverlay/Healthbar" method="_on_player_health_changed"]
[connection signal="max_hp_changed" from="Player" to="UIOverlay/Healthbar" method="_on_player_max_hp_changed"]

View file

@ -1,7 +1,4 @@
extends Area2D
var anim_sprite: AnimatedSprite2D
var slash_duration = 0.1
var slash_timer = 0
var damage = 20
var facing = -1
var facing_mult = 1
@ -10,36 +7,44 @@ var swing_knockback = 1000
var apply_swing_knockback = true
func _ready() -> void:
anim_sprite = $AnimatedSprite2D
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
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
scale.y = abs(scale.y) * -facing
rotation = PI/2 * facing_mult
else:
scale.x = abs(scale.x) * facing
scale.x = abs(scale.x) * -facing
scale.y = abs(scale.y)
rotation = 0
anim_sprite.visible = true
# 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
slash_timer = slash_duration
$SlashTimer.start()
func _process(delta: float) -> void:
if slash_timer > 0:
slash_timer = max(0, slash_timer-delta)
if(slash_timer == 0):
anim_sprite.visible = false
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
else:
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

View file

@ -28,9 +28,15 @@ rotation = 1.5708
scale = Vector2(-0.635, -1.1)
shape = SubResource("CapsuleShape2D_e4ynd")
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
[node name="Sprite" type="AnimatedSprite2D" parent="."]
visible = false
modulate = Color(1, 1, 1, 0.6862745)
position = Vector2(-25.644447, 0.61333346)
scale = Vector2(-0.385, 0.231)
sprite_frames = SubResource("SpriteFrames_fahsa")
[node name="SlashTimer" type="Timer" parent="."]
wait_time = 0.1
one_shot = true
[connection signal="timeout" from="SlashTimer" to="." method="end_slash"]