Files
gd-playground/Game/Runtime/Queue/BattleEventQueue.gd
54shitaimzf e465f1cbb0 feat: 基础卡牌场景与卡牌逻辑可扩展框架,以及todo文档
- Added BattleState class to manage battle flow, including turn management and event handling.
- Introduced BuffInstance class to represent buffs applied to combatants.
- Created CardInstance class to handle card definitions and cost calculations.
- Developed CombatantState class to manage combatant attributes and actions.
- Implemented EffectRegistry to apply effects based on event specifications.
- Added various handlers (BlockHandler, DamageHandler, DrawHandler, etc.) to process specific events.
- Created IntentPlanner and IntentState classes to manage enemy actions and intents.
- Established a queue system for handling battle events with BattleEventQueue and BattleEventTask.
- Introduced triggers for applying effects based on game events (e.g., OnCardDrawnGainBlockTrigger).
- Added necessary UID files for new scripts to ensure proper resource management.
2026-04-22 21:58:15 +08:00

68 lines
1.8 KiB
GDScript

class_name BattleEventQueue
extends RefCounted
const DebugLogger = preload("res://Game/Infrastructure/Diagnostics/DebugLogger.gd")
const BattleEventTaskScript = preload("res://Game/Runtime/Queue/BattleEventTask.gd")
var _queue: Array = []
var _handlers: Dictionary = {}
var _is_running: bool = false
var _last_chain_id: int = 0
func register_handler(event_type: String, handler: Callable) -> void:
if not _handlers.has(event_type):
_handlers[event_type] = []
_handlers[event_type].append(handler)
func enqueue(task) -> void:
_queue.append(task)
func enqueue_event(event_type: String, payload: Dictionary = {}, producer: String = "") -> void:
_last_chain_id += 1
enqueue(BattleEventTaskScript.new(event_type, payload, _last_chain_id, producer))
func clear() -> void:
_queue.clear()
func is_running() -> bool:
return _is_running
func run(state, event_bus, max_steps: int = 512) -> void:
if _is_running:
return
_is_running = true
var step_count: int = 0
while not _queue.is_empty():
step_count += 1
if step_count > max_steps:
DebugLogger.error("BattleEventQueue", "超过最大执行步数,强制中断 max_steps=%d" % max_steps)
_queue.clear()
break
var task = _queue.pop_front()
task.queue_index = step_count - 1
event_bus.publish("event_task_executing", {
"event_type": task.event_type,
"chain_id": task.chain_id,
"queue_index": task.queue_index,
"producer": task.producer
})
var handlers: Array = _handlers.get(task.event_type, [])
for handler in handlers:
var follow_up = handler.call(task, state)
_append_follow_ups(follow_up)
_is_running = false
func _append_follow_ups(follow_up) -> void:
if follow_up == null:
return
if follow_up is Object and follow_up.get_script() == BattleEventTaskScript:
enqueue(follow_up)
return
if follow_up is Array:
for item in follow_up:
_append_follow_ups(item)