82 lines
2.4 KiB
GDScript
82 lines
2.4 KiB
GDScript
extends CharacterBody2D
|
|
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
|
|
var current_xp = 0
|
|
var max_xp = 5
|
|
var level = 1
|
|
var speed = 200
|
|
var damage: int = 10
|
|
var strength = 3
|
|
var attacks = false
|
|
|
|
func _physics_process(delta):
|
|
var direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
|
|
velocity = direction * speed
|
|
move_and_slide()
|
|
if attacks == true:
|
|
return
|
|
if direction == Vector2.ZERO:
|
|
animated_sprite_2d.play("idle")
|
|
elif abs(direction.x) >= abs(direction.y):
|
|
if direction.x < 0:
|
|
animated_sprite_2d.play("walk_left")
|
|
else:
|
|
animated_sprite_2d.play("walk_right")
|
|
else:
|
|
if direction.y < 0:
|
|
animated_sprite_2d.play("walk_up")
|
|
else:
|
|
animated_sprite_2d.play("walk_down")
|
|
|
|
|
|
|
|
|
|
func attack():
|
|
if attacks:
|
|
return
|
|
for body in $MeleeArea.get_overlapping_bodies():
|
|
if body.is_in_group("enemies"):
|
|
attacks = true
|
|
var dir = global_position.direction_to(body.global_position)
|
|
if dir == Vector2.ZERO:
|
|
animated_sprite_2d.play("idle")
|
|
elif abs(dir.x) >= abs(dir.y):
|
|
if dir.x < 0:
|
|
animated_sprite_2d.play("attack_left")
|
|
else:
|
|
animated_sprite_2d.play("attack_right")
|
|
else:
|
|
if dir.y < 0:
|
|
animated_sprite_2d.play("attack_up")
|
|
else:
|
|
animated_sprite_2d.play("attack_down")
|
|
animated_sprite_2d.speed_scale = 0.5 / $AttackSpeed.wait_time * 1.4
|
|
var wait_time = $AttackSpeed.wait_time
|
|
await get_tree().create_timer(wait_time / 2).timeout
|
|
body.take_damage(strength)
|
|
var knockback_dir = global_position.direction_to(body.global_position)
|
|
var enemy_tween = create_tween()
|
|
enemy_tween.tween_property(body, "global_position", body.global_position + knockback_dir * 20, 0.1).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_QUAD)
|
|
var player_tween = create_tween()
|
|
player_tween.tween_property(self, "global_position", global_position - knockback_dir * 10, 0.1).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_QUAD)
|
|
await animated_sprite_2d.animation_finished
|
|
animated_sprite_2d.speed_scale = 1
|
|
attacks = false
|
|
break
|
|
|
|
|
|
|
|
func _on_attack_speed_timeout() -> void:
|
|
if $MeleeArea.get_overlapping_bodies().any(func(b): return b.is_in_group("enemies")):
|
|
attack()
|
|
else:
|
|
$AttackSpeed.call_deferred("stop")
|
|
pass # Replace with function body.
|
|
|
|
|
|
|
|
func _on_melee_area_body_entered(body: Node2D) -> void:
|
|
if body.is_in_group("enemies"):
|
|
if $AttackSpeed.is_stopped():
|
|
attack()
|
|
$AttackSpeed.start()
|