Files
gd-playground/Game/Runtime/BattleState.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

120 lines
4.8 KiB
GDScript

class_name BattleState
extends RefCounted
const BattleEventBusScript = preload("res://Game/Infrastructure/BattleEventBus.gd")
const BattlePhaseScript = preload("res://Game/Runtime/BattlePhase.gd")
const BattleEventTypeScript = preload("res://Game/Runtime/Queue/BattleEventType.gd")
const BattleEventQueueScript = preload("res://Game/Runtime/Queue/BattleEventQueue.gd")
const DamageHandlerScript = preload("res://Game/Runtime/Handlers/DamageHandler.gd")
const BlockHandlerScript = preload("res://Game/Runtime/Handlers/BlockHandler.gd")
const WeakHandlerScript = preload("res://Game/Runtime/Handlers/WeakHandler.gd")
const DrawHandlerScript = preload("res://Game/Runtime/Handlers/DrawHandler.gd")
const DiscardHandlerScript = preload("res://Game/Runtime/Handlers/DiscardHandler.gd")
const EndTurnFlowHandlerScript = preload("res://Game/Runtime/Handlers/EndTurnFlowHandler.gd")
const PlayCardFlowHandlerScript = preload("res://Game/Runtime/Handlers/PlayCardFlowHandler.gd")
const TriggerDispatchHandlerScript = preload("res://Game/Runtime/Handlers/TriggerDispatchHandler.gd")
const IntentStateScript = preload("res://Game/Runtime/IntentState.gd")
var event_bus = BattleEventBusScript.new()
var event_queue = BattleEventQueueScript.new()
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
var _damage_handler = DamageHandlerScript.new()
var _block_handler = BlockHandlerScript.new()
var _weak_handler = WeakHandlerScript.new()
var _draw_handler = DrawHandlerScript.new()
var _discard_handler = DiscardHandlerScript.new()
var _end_turn_flow_handler = EndTurnFlowHandlerScript.new()
var _play_card_flow_handler = PlayCardFlowHandlerScript.new()
var _trigger_dispatch_handler = TriggerDispatchHandlerScript.new()
var player
var enemy
var draw_pile: Array = []
var hand: Array = []
var discard_pile: Array = []
var max_energy: int = 3
var energy: int = 3
var turn_index: int = 1
var is_player_turn: bool = true
var phase: int = BattlePhaseScript.PLAYER_DRAW
var enemy_intent = IntentStateScript.attack(6)
func setup(seed_value: int = 12345) -> void:
rng.seed = seed_value
_register_core_handlers()
func _register_core_handlers() -> void:
event_queue.register_handler(BattleEventTypeScript.DAMAGE_REQUESTED, _damage_handler.handle)
event_queue.register_handler(BattleEventTypeScript.BLOCK_REQUESTED, _block_handler.handle)
event_queue.register_handler(BattleEventTypeScript.WEAK_APPLIED, _weak_handler.handle)
event_queue.register_handler(BattleEventTypeScript.CARD_PLAY_REQUESTED, _play_card_flow_handler.handle)
event_queue.register_handler(BattleEventTypeScript.CARD_DRAW_REQUESTED, _draw_handler.handle)
event_queue.register_handler(BattleEventTypeScript.CARD_DISCARD_REQUESTED, _discard_handler.handle)
event_queue.register_handler(BattleEventTypeScript.TURN_END_REQUESTED, _end_turn_flow_handler.handle)
event_queue.register_handler(BattleEventTypeScript.CARD_DRAWN, _trigger_dispatch_handler.handle)
event_queue.register_handler(BattleEventTypeScript.DAMAGE_APPLIED, _trigger_dispatch_handler.handle)
event_queue.register_handler(BattleEventTypeScript.PHASE_ENTERED, _trigger_dispatch_handler.handle)
event_queue.register_handler(BattleEventTypeScript.PHASE_EXITED, _trigger_dispatch_handler.handle)
func shuffle_draw_pile() -> void:
for i in range(draw_pile.size() - 1, 0, -1):
var j := rng.randi_range(0, i)
var tmp = draw_pile[i]
draw_pile[i] = draw_pile[j]
draw_pile[j] = tmp
func draw_cards(count: int) -> void:
event_queue.enqueue_event(BattleEventTypeScript.CARD_DRAW_REQUESTED, {
"count": max(0, count),
"owner": player
}, "BattleState.draw_cards")
event_queue.run(self, event_bus)
func discard_hand() -> void:
event_queue.enqueue_event(BattleEventTypeScript.CARD_DISCARD_REQUESTED, {
"all_hand": true
}, "BattleState.discard_hand")
event_queue.run(self, event_bus)
func start_player_turn() -> void:
is_player_turn = true
energy = max_energy
set_phase(BattlePhaseScript.PLAYER_DRAW)
discard_hand()
draw_cards(5)
set_phase(BattlePhaseScript.PLAYER_PLAY)
event_bus.publish("turn_started", {"side": "player", "turn": turn_index})
func end_player_turn() -> void:
is_player_turn = false
player.tick_end_of_turn()
event_bus.publish("turn_started", {"side": "enemy", "turn": turn_index})
func start_next_turn() -> void:
turn_index += 1
start_player_turn()
func set_phase(new_phase: int) -> void:
if phase == new_phase:
return
var old_phase: int = phase
event_bus.publish(BattleEventTypeScript.PHASE_EXITED, {
"phase": old_phase,
"phase_text": BattlePhaseScript.to_text(old_phase)
})
phase = new_phase
event_bus.publish(BattleEventTypeScript.PHASE_ENTERED, {
"phase": phase,
"phase_text": BattlePhaseScript.to_text(phase)
})
event_bus.publish(BattleEventTypeScript.PHASE_CHANGED, {
"from_phase": old_phase,
"to_phase": phase,
"phase": phase,
"phase_text": BattlePhaseScript.to_text(phase)
})