51 lines
1.4 KiB
GDScript
51 lines
1.4 KiB
GDScript
class_name Target
|
|
extends RigidBody3D
|
|
|
|
@export var max_health: int = 3
|
|
var current_health: int = max_health
|
|
signal damaged(amount: int)
|
|
@export var explosion_scene: PackedScene
|
|
@export var explosion_lifetime: float = 3.0
|
|
@export var reward_gold: int = 1
|
|
|
|
func is_dead() -> bool:
|
|
return current_health <= 0
|
|
|
|
func take_damage(amount: int) -> void:
|
|
if amount <= 0:
|
|
return
|
|
if is_dead():
|
|
return
|
|
var was_alive := not is_dead()
|
|
current_health = max(0, current_health - amount)
|
|
emit_signal("damaged", amount)
|
|
if was_alive and is_dead():
|
|
die()
|
|
|
|
|
|
func die() -> void:
|
|
print("Target destroyed!")
|
|
# Erhöhe das Gold des Spielers beim Tod dieses Targets
|
|
if reward_gold > 0 and GameManager:
|
|
GameManager.increase_player_gold(reward_gold)
|
|
_spawn_explosion()
|
|
queue_free()
|
|
|
|
|
|
func _spawn_explosion() -> void:
|
|
if explosion_scene == null:
|
|
return
|
|
var explosion := explosion_scene.instantiate()
|
|
var tree := get_tree()
|
|
var parent: Node = tree.current_scene if tree.current_scene != null else tree.root
|
|
parent.add_child(explosion)
|
|
if explosion is Node3D:
|
|
(explosion as Node3D).global_transform = global_transform
|
|
# Optional: Partikel/Animation starten, falls der Effekt so etwas anbietet
|
|
if explosion.has_method("start"):
|
|
explosion.call("start")
|
|
# Aufräumen, falls der Effekt sich nicht selbst entfernt
|
|
if explosion_lifetime > 0.0:
|
|
var timer := tree.create_timer(explosion_lifetime)
|
|
timer.timeout.connect(explosion.queue_free)
|