extends Node

# 事件注册表
# { entity: [MatcherConsumer] }
var _event_registry: Dictionary = {}

class MatcherConsumer extends RefCounted:
	var _matcher: GameEventMatcher
	var _consumer: Callable

	func _init(matcher: GameEventMatcher, consumer: Callable) -> void:
		self._matcher = matcher
		self._consumer = consumer

# 注册事件
func register_event_matcher(event_matcher: GameEventMatcher, consumer: Callable) -> void:
	if !event_matcher.entity:
		return
	var matcher_consumer = MatcherConsumer.new(event_matcher, consumer)
	if event_matcher.entity not in _event_registry:
		_event_registry[event_matcher.entity] = []
	_event_registry[event_matcher.entity].append(matcher_consumer)

# 触发事件
func trigger_event(event: GameEvent) -> void:
	if event.entity not in _event_registry:
		return
	for matcher_consumer in _event_registry[event.entity]:
		# matcher_consumer = matcher_consumer as MatcherConsumer
		if matcher_consumer._matcher.match(event):
			matcher_consumer._consumer.call(event)
