2023-10-10 13:30:23 -04:00
|
|
|
class_name TemplateSpawnable
|
|
|
|
extends CharacterBody2D
|
|
|
|
|
2023-10-10 17:21:39 -04:00
|
|
|
const ATTACK_COOLDOWN : float = 100
|
|
|
|
|
2023-10-10 13:30:23 -04:00
|
|
|
@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 18:29:06 -04:00
|
|
|
@export var expReward : int = 0
|
2023-10-10 17:21:39 -04:00
|
|
|
@export var cost : int = 0
|
|
|
|
@export var minSpawnRange : float = 0
|
2023-10-10 14:54:21 -04:00
|
|
|
@export var canAttack : bool = true
|
2023-10-10 13:30:23 -04:00
|
|
|
|
2023-10-10 18:29:06 -04:00
|
|
|
|
2023-10-10 13:30:23 -04:00
|
|
|
@onready var ai : Node = $AI
|
|
|
|
@onready var animatedSprite : AnimatedSprite2D = $AnimatedSprite2D
|
2023-10-10 17:21:39 -04:00
|
|
|
@onready var attackArea : Area2D = $AttackArea
|
|
|
|
@onready var damageCollision : CollisionShape2D = $DamageArea/DamageCollision
|
2023-10-10 19:38:24 -04:00
|
|
|
@onready var bodyCollision : CollisionShape2D = $BodyCollision
|
|
|
|
@onready var spawnableUI : Control = $UIContainer/SpawnableUI
|
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 19:38:24 -04:00
|
|
|
spawnableUI.setHP(currentHp, maxHp)
|
|
|
|
spawnableUI.visible = true
|
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
|
2023-10-10 17:21:39 -04:00
|
|
|
|
2023-10-10 13:30:23 -04:00
|
|
|
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 17:21:39 -04:00
|
|
|
if !isFacingLeft && direction.x < 0:
|
2023-10-10 14:54:21 -04:00
|
|
|
isFacingLeft = true
|
2023-10-10 19:38:24 -04:00
|
|
|
attackArea.scale = Vector2(-1,1)
|
|
|
|
damageCollision.scale = Vector2(-1,1)
|
|
|
|
animatedSprite.flip_h = isFacingLeft
|
2023-10-10 17:21:39 -04:00
|
|
|
elif isFacingLeft && direction.x > 0:
|
|
|
|
isFacingLeft = false
|
2023-10-10 19:38:24 -04:00
|
|
|
attackArea.scale = Vector2(1,1)
|
|
|
|
damageCollision.scale = Vector2(1,1)
|
|
|
|
animatedSprite.flip_h = isFacingLeft
|
2023-10-10 14:54:21 -04:00
|
|
|
|
2023-10-10 13:30:23 -04:00
|
|
|
velocity = direction * speed * delta
|
|
|
|
move_and_slide()
|
2023-10-10 17:21:39 -04:00
|
|
|
|
|
|
|
if cooldown > 0:
|
|
|
|
if cooldown - attackSpeed * delta <= 0:
|
2023-10-10 18:29:06 -04:00
|
|
|
cooldown = 0
|
2023-10-10 17:21:39 -04:00
|
|
|
else:
|
|
|
|
cooldown -= attackSpeed * delta
|
|
|
|
attemptAttack()
|
2023-10-10 13:30:23 -04:00
|
|
|
|
2023-10-10 17:21:39 -04:00
|
|
|
func attemptAttack():
|
|
|
|
if !canAttack || !isAlive || cooldown > 0:
|
2023-10-10 14:54:21 -04:00
|
|
|
return
|
2023-10-10 17:21:39 -04:00
|
|
|
if attackArea.overlaps_body(enemy):
|
|
|
|
attack()
|
|
|
|
|
|
|
|
func attack():
|
|
|
|
cooldown = ATTACK_COOLDOWN
|
2023-10-10 13:30:23 -04:00
|
|
|
animatedSprite.play("attack")
|
2023-10-10 17:21:39 -04:00
|
|
|
damageCollision.disabled = false
|
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 17:21:39 -04:00
|
|
|
damageCollision.disabled = true
|
2023-10-10 18:29:06 -04:00
|
|
|
animatedSprite.disconnect("animation_finished", endAttack)
|
2023-10-10 13:30:23 -04:00
|
|
|
animatedSprite.play("default")
|
|
|
|
|
|
|
|
func receive_damage(dmg):
|
2023-10-10 22:31:20 -04:00
|
|
|
print(currentHp - dmg)
|
2023-10-10 13:30:23 -04:00
|
|
|
if !isAlive:
|
|
|
|
return
|
|
|
|
if currentHp - dmg <= 0:
|
|
|
|
currentHp = 0
|
2023-10-10 19:38:24 -04:00
|
|
|
call_deferred("die")
|
2023-10-10 13:30:23 -04:00
|
|
|
else :
|
|
|
|
currentHp -= dmg
|
2023-10-10 19:38:24 -04:00
|
|
|
spawnableUI.setHP(currentHp, maxHp)
|
2023-10-10 13:30:23 -04:00
|
|
|
|
|
|
|
func die():
|
2023-10-10 20:26:50 -04:00
|
|
|
enemy.remove_foe(self)
|
2023-10-10 19:38:24 -04:00
|
|
|
bodyCollision.disabled = true
|
2023-10-10 17:21:39 -04:00
|
|
|
damageCollision.disabled = true
|
2023-10-10 19:38:24 -04:00
|
|
|
spawnableUI.visible = false
|
|
|
|
isAlive = false
|
2023-10-10 17:21:39 -04:00
|
|
|
animatedSprite.stop()
|
2023-10-10 13:30:23 -04:00
|
|
|
animatedSprite.play("death")
|
2023-10-10 17:21:39 -04:00
|
|
|
animatedSprite.disconnect("animation_finished", endAttack)
|
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()
|
2023-10-10 17:21:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
func _on_damage_area_body_entered(body):
|
|
|
|
if body == enemy:
|
2023-10-10 18:29:06 -04:00
|
|
|
body.receive_damage(damage)
|