82 lines
1.4 KiB
GDScript
82 lines
1.4 KiB
GDScript
extends Node2D
|
|
class_name Battler
|
|
|
|
@export var battler_stats : BattlerStats
|
|
@export_enum("Plant", "Pollution") var side = 0
|
|
|
|
@onready var attack_cooldown : Timer = $Attack
|
|
|
|
|
|
var life : int = 1 :
|
|
set(value):
|
|
life = value
|
|
print(name, " life : ", life)
|
|
if life <= 0:
|
|
death()
|
|
|
|
|
|
var damage : int = 1
|
|
var armor : int = 1
|
|
var speed : float = 1.0
|
|
|
|
|
|
var target : Battler
|
|
var _attack_ready : bool = false
|
|
|
|
|
|
func _ready() -> void:
|
|
set_side()
|
|
set_battler_stats()
|
|
_attack_ready = true
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
if target:
|
|
if _attack_ready:
|
|
attack()
|
|
else:
|
|
call_deferred("get_new_target")
|
|
|
|
|
|
func set_battler_stats() -> void:
|
|
life = battler_stats.base_life
|
|
damage = battler_stats.base_damage
|
|
armor = battler_stats.base_armor
|
|
speed = battler_stats.base_speed
|
|
|
|
attack_cooldown.wait_time = 1/speed
|
|
|
|
|
|
func set_side() -> void:
|
|
match side:
|
|
0:
|
|
add_to_group("Plant")
|
|
1:
|
|
add_to_group("Pollution")
|
|
_:
|
|
add_to_group("Plant")
|
|
|
|
|
|
func get_new_target() -> void:
|
|
if is_in_group("Plant"):
|
|
target = get_tree().get_first_node_in_group("Pollution")
|
|
if is_in_group("Pollution"):
|
|
target = get_tree().get_first_node_in_group("Plant")
|
|
|
|
|
|
func attack() -> void:
|
|
_attack_ready = false
|
|
target.get_hurt(damage)
|
|
attack_cooldown.start()
|
|
|
|
|
|
func get_hurt(hurt : int) -> void:
|
|
life -= clamp(hurt - armor, 1, 9999)
|
|
|
|
|
|
func death() -> void:
|
|
queue_free()
|
|
|
|
|
|
func _on_attack_timeout() -> void:
|
|
_attack_ready = true
|