extends Node

var entities: Dictionary = {}

func register(entity: Node, base: PortalStats) -> void:
  var thresholds: Array[float] = base.thresholds.duplicate()
  var triggered: Array[bool] = []
  triggered.resize(thresholds.size())
  triggered.fill(false)
  entities[entity] = {
    "base": base,
    "health": base.max_health,
    "max_health": base.max_health,
    "alive": true,
    "spawn_count": base.spawn_count,
    "thresholds": thresholds,
    "triggered": triggered,
  }

func deregister(entity: Node) -> void:
  entities.erase(entity)

func get_stat(entity: Node, key: String) -> Variant:
  if entity in entities:
    return entities[entity].get(key)
  return null

func set_stat(entity: Node, key: String, value: Variant) -> void:
  if entity in entities:
    entities[entity][key] = value

func is_alive(entity: Node) -> bool:
  if entity in entities:
    return entities[entity]["alive"]
  return false

func set_health(entity: Node, value: float) -> void:
  if entity not in entities:
    return
  entities[entity]["health"] = value
  var max_health: float = entities[entity]["max_health"]
  EventBus.health_changed.emit(entity, value, max_health)
  if value <= 0 and entities[entity]["alive"]:
    entities[entity]["alive"] = false
    EventBus.entity_died.emit(entity)
