PVP/Objects/battler.gd
2026-03-11 17:12:53 +01:00

95 lines
1.9 KiB
GDScript

extends Node2D
class_name Battler
@export var sprite_texture : Texture
@export var battler_stats : BattlerStats
@export_enum("Plant", "Pollution") var side = 0
@onready var attack_cooldown : Timer = $Attack
@onready var floating_text_packed : PackedScene = load("res://Objects/floating_text.tscn")
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:
$Sprite2D.texture = sprite_texture
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(damage : int) -> void:
life -= Formulas.calculate_damage(damage, armor)
instantiate_floating_text(damage)
func death() -> void:
queue_free()
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
func _on_attack_timeout() -> void:
_attack_ready = true