- Third-Person Spieler mit WASD-Bewegung und Kamerasteuerung (RMB + Mausrad-Zoom) - HP-System mit Healthbar und Aktionsleiste (Slots 1-9) - Autoattack-System: Linksklick markiert Ziel, Rechtsklick markiert + greift an - Waffensystem-Basis: Schaden basiert auf ausgerüsteter Waffe (unbewaffnet = 1) - Gegner-KI: läuft auf Spieler zu, greift bei Reichweite an, zeigt HP-Label bei Markierung - Ressourcen-Klassen: Attack und Weapon Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
79 lines
1.8 KiB
GDScript
79 lines
1.8 KiB
GDScript
# Enemy.gd
|
|
# Steuert den Gegner: KI-Bewegung zum Spieler, Angriff, HP, Zielanzeige
|
|
extends CharacterBody3D
|
|
|
|
const SPEED = 3.0
|
|
const GRAVITY = 9.8
|
|
const ATTACK_DAMAGE = 5
|
|
const ATTACK_RANGE = 1.5
|
|
const ATTACK_COOLDOWN = 2.0
|
|
|
|
var max_hp = 50
|
|
var current_hp = 50
|
|
var target = null # Ziel des Gegners (normalerweise der Spieler)
|
|
var can_attack = true
|
|
|
|
@onready var health_label = $HealthLabel
|
|
|
|
func _ready():
|
|
health_label.visible = false
|
|
_update_label()
|
|
|
|
# HP-Label Text aktualisieren
|
|
func _update_label():
|
|
health_label.text = str(current_hp) + " / " + str(max_hp)
|
|
|
|
# HP-Label anzeigen (wenn Gegner markiert wird)
|
|
func show_health():
|
|
health_label.visible = true
|
|
|
|
# HP-Label verstecken (wenn Markierung aufgehoben wird)
|
|
func hide_health():
|
|
health_label.visible = false
|
|
|
|
# Schaden nehmen und Label aktualisieren
|
|
func take_damage(amount):
|
|
current_hp -= amount
|
|
_update_label()
|
|
if current_hp <= 0:
|
|
die()
|
|
|
|
# Gegner aus der Szene entfernen
|
|
func die():
|
|
print("Gegner besiegt!")
|
|
queue_free()
|
|
|
|
func _physics_process(delta):
|
|
if not is_on_floor():
|
|
velocity.y -= GRAVITY * delta
|
|
|
|
if target == null:
|
|
move_and_slide()
|
|
return
|
|
|
|
var distance = global_position.distance_to(target.global_position)
|
|
|
|
if distance <= ATTACK_RANGE:
|
|
# In Reichweite: angreifen
|
|
velocity.x = 0
|
|
velocity.z = 0
|
|
if can_attack:
|
|
_attack()
|
|
else:
|
|
# Direkt auf Ziel zubewegen
|
|
var direction = (target.global_position - global_position)
|
|
direction.y = 0
|
|
direction = direction.normalized()
|
|
velocity.x = direction.x * SPEED
|
|
velocity.z = direction.z * SPEED
|
|
look_at(Vector3(target.global_position.x, global_position.y, target.global_position.z))
|
|
|
|
move_and_slide()
|
|
|
|
# Angriff mit Cooldown
|
|
func _attack():
|
|
can_attack = false
|
|
target.take_damage(ATTACK_DAMAGE)
|
|
print("Gegner greift an!")
|
|
await get_tree().create_timer(ATTACK_COOLDOWN).timeout
|
|
can_attack = true
|