2026-02-22 17:50:03 +01:00
|
|
|
extends Node2D
|
|
|
|
|
class_name Battler
|
|
|
|
|
|
2026-03-11 17:12:53 +01:00
|
|
|
@export var sprite_texture : Texture
|
2026-02-22 17:50:03 +01:00
|
|
|
@export var battler_stats : BattlerStats
|
|
|
|
|
@export_enum("Plant", "Pollution") var side = 0
|
|
|
|
|
|
|
|
|
|
@onready var attack_cooldown : Timer = $Attack
|
|
|
|
|
|
2026-03-11 17:12:53 +01:00
|
|
|
@onready var floating_text_packed : PackedScene = load("res://Objects/floating_text.tscn")
|
2026-02-22 17:50:03 +01:00
|
|
|
|
|
|
|
|
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:
|
2026-03-11 17:12:53 +01:00
|
|
|
$Sprite2D.texture = sprite_texture
|
2026-02-22 17:50:03 +01:00
|
|
|
set_side()
|
|
|
|
|
set_battler_stats()
|
|
|
|
|
_attack_ready = true
|
|
|
|
|
|
|
|
|
|
|
2026-03-11 17:12:53 +01:00
|
|
|
|
2026-02-22 17:50:03 +01:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
2026-03-11 17:12:53 +01:00
|
|
|
func get_hurt(damage : int) -> void:
|
|
|
|
|
life -= Formulas.calculate_damage(damage, armor)
|
|
|
|
|
instantiate_floating_text(damage)
|
2026-02-22 17:50:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func death() -> void:
|
|
|
|
|
queue_free()
|
|
|
|
|
|
|
|
|
|
|
2026-03-11 17:12:53 +01:00
|
|
|
func instantiate_floating_text(text_value) -> void:
|
|
|
|
|
var floating_text : Control = floating_text_packed.instantiate()
|
|
|
|
|
add_child(floating_text)
|
|
|
|
|
floating_text.text = str(text_value)
|
|
|
|
|
floating_text.position.y -= $Sprite2D.texture.get_height()/2.0 * $Sprite2D.scale.y + 30.0
|
|
|
|
|
floating_text.position.x -= 10.0
|
|
|
|
|
|
|
|
|
|
|
2026-02-22 17:50:03 +01:00
|
|
|
func _on_attack_timeout() -> void:
|
|
|
|
|
_attack_ready = true
|