Blud_Mage/Scenes/Spawnables/template_spawnable.gd

81 lines
1.8 KiB
GDScript3
Raw Normal View History

2023-10-10 13:30:23 -04:00
class_name TemplateSpawnable
extends CharacterBody2D
@export var maxHp : int = 0
@export var attackSpeed : float = 0
@export var speed : float = 0
@export var damage : int = 0
@export var priority : int = 0
2023-10-10 14:54:21 -04:00
@export var canAttack : bool = true
2023-10-10 13:30:23 -04:00
@onready var ai : Node = $AI
@onready var animatedSprite : AnimatedSprite2D = $AnimatedSprite2D
2023-10-10 15:02:48 -04:00
@onready var attackCollision : CollisionShape2D = $Attack/AttackCollision
@onready var damageCollision : CollisionShape2D = $Attack/DamageCollision
2023-10-10 13:30:23 -04:00
var isAlive : bool = true
2023-10-10 14:54:21 -04:00
var isFacingLeft : bool = false
2023-10-10 13:30:23 -04:00
var direction : Vector2 = Vector2.ZERO
var enemy : CharacterBody2D = null
var currentHp : int = 0
var cooldown : float = 0
func _ready():
enemy = get_node("../Enemy")
currentHp = maxHp
2023-10-10 13:47:38 -04:00
animatedSprite.play("default")
2023-10-10 13:30:23 -04:00
func _process(delta):
if !isAlive:
return
if enemy != null:
2023-10-10 14:54:21 -04:00
direction = ai.getDirection(global_position, enemy.global_position)
2023-10-10 13:30:23 -04:00
else:
direction = Vector2.ZERO
2023-10-10 14:54:21 -04:00
if !isFacingLeft && velocity.x >= 0:
isFacingLeft = true
scale = Vector2(1, 1)
elif isFacingLeft && velocity.x < 0:
isFacingLeft = false
scale = Vector2(-1, 1)
2023-10-10 13:30:23 -04:00
velocity = direction * speed * delta
move_and_slide()
2023-10-10 14:54:21 -04:00
func attack():
if !canAttack || !isAlive:
return
2023-10-10 13:30:23 -04:00
animatedSprite.play("attack")
2023-10-10 14:54:21 -04:00
animatedSprite.connect("animation_finished", endAttack)
2023-10-10 13:30:23 -04:00
2023-10-10 14:54:21 -04:00
func endAttack():
2023-10-10 13:30:23 -04:00
animatedSprite.play("default")
func receive_damage(dmg):
if !isAlive:
return
if currentHp - dmg <= 0:
currentHp = 0
die()
else :
currentHp -= dmg
func die():
isAlive = false
animatedSprite.play("death")
2023-10-10 14:54:21 -04:00
animatedSprite.connect("animation_finished", fadeOut)
2023-10-10 13:30:23 -04:00
func fadeOut():
2023-10-10 14:54:21 -04:00
var tween = get_tree().create_tween()
2023-10-10 13:30:23 -04:00
tween.tween_property(animatedSprite, "modulate", Color(0,0,0,0), 1)
2023-10-10 14:54:21 -04:00
tween.tween_callback(clean)
2023-10-10 13:30:23 -04:00
func clean():
queue_free()