90 lines
1.9 KiB
GDScript
90 lines
1.9 KiB
GDScript
class_name Item
|
|
extends Node3D
|
|
|
|
const DEFAULT_MAX_HEALTH: int = 10
|
|
signal name_changed(item_name: String)
|
|
signal health_changed(current_health: int, maximum_health: int)
|
|
signal state_changed(new_state: States)
|
|
signal recalculate_navigation_map(requester: Node3D)
|
|
enum States {
|
|
hidden,
|
|
idle,
|
|
closed,
|
|
opened,
|
|
looted,
|
|
destroyed
|
|
}
|
|
|
|
@export var item_name: String:
|
|
get:
|
|
return _item_name
|
|
set(value):
|
|
if _item_name == value:
|
|
return
|
|
_item_name = value
|
|
if _initialized:
|
|
name_changed.emit(value)
|
|
|
|
@export var maximum_health: int = DEFAULT_MAX_HEALTH:
|
|
get:
|
|
return _maximum_health
|
|
set(value):
|
|
value = max(1, value)
|
|
if _maximum_health == value:
|
|
return
|
|
_maximum_health = value
|
|
if _health > _maximum_health:
|
|
_health = _maximum_health
|
|
if _initialized:
|
|
health_changed.emit(_health, _maximum_health)
|
|
|
|
@export var health: int = DEFAULT_MAX_HEALTH:
|
|
get:
|
|
return _health
|
|
set(value):
|
|
value = clamp(value, 0, maximum_health)
|
|
if _health == value:
|
|
return
|
|
_health = value
|
|
if _initialized:
|
|
health_changed.emit(_health, maximum_health)
|
|
if _health <= 0:
|
|
state = States.destroyed
|
|
|
|
@export var state: States = States.idle:
|
|
get:
|
|
return _state
|
|
set(value):
|
|
if _state == value:
|
|
return
|
|
_state = value
|
|
if _initialized:
|
|
state_changed.emit(value)
|
|
|
|
var _initialized: bool = false
|
|
var _item_name: String = ""
|
|
var _maximum_health: int = DEFAULT_MAX_HEALTH
|
|
var _health: int = DEFAULT_MAX_HEALTH
|
|
var _state: States = States.idle
|
|
|
|
|
|
func _ready() -> void:
|
|
_initialized = true
|
|
name_changed.emit(item_name)
|
|
health_changed.emit(health, maximum_health)
|
|
|
|
|
|
func apply_damage(amount: int) -> void:
|
|
if amount <= 0:
|
|
return
|
|
health = health - amount
|
|
|
|
|
|
func take_damage(damage_amount: int) -> void:
|
|
# Rückwärtskompatibel: Bitte künftig apply_damage verwenden.
|
|
apply_damage(damage_amount)
|
|
|
|
|
|
func destroy_item() -> void:
|
|
recalculate_navigation_map.emit(self)
|
|
queue_free() |