234 lines
6.1 KiB
Go
234 lines
6.1 KiB
Go
package battle
|
||
|
||
import "fmt"
|
||
|
||
// ==================== 基础类型 ====================
|
||
type BaseEvent string
|
||
type HookPhase string
|
||
type LifeType string
|
||
|
||
const (
|
||
Before HookPhase = "before"
|
||
After HookPhase = "after"
|
||
|
||
TurnBased LifeType = "turn_based"
|
||
CountBased LifeType = "count_based"
|
||
Permanent LifeType = "permanent"
|
||
)
|
||
|
||
type EffectContext struct {
|
||
Actor string
|
||
Target string
|
||
Skill string
|
||
Extra map[string]interface{}
|
||
Success bool
|
||
}
|
||
|
||
type Effect struct {
|
||
Name string
|
||
Trigger string
|
||
LifeType LifeType
|
||
Duration int
|
||
Phase HookPhase
|
||
Handler func(ctx *EffectContext) bool
|
||
}
|
||
|
||
type EffectNode struct {
|
||
Name string
|
||
Exec func(ctx *EffectContext, next func())
|
||
}
|
||
|
||
type EffectManager struct {
|
||
nodes []*EffectNode
|
||
effects map[string][]*Effect
|
||
}
|
||
|
||
func NewEffectManager(nodes []*EffectNode) *EffectManager {
|
||
return &EffectManager{
|
||
nodes: nodes,
|
||
effects: make(map[string][]*Effect),
|
||
}
|
||
}
|
||
|
||
func (m *EffectManager) RegisterEffect(node string, eff *Effect) {
|
||
m.effects[node] = append(m.effects[node], eff)
|
||
}
|
||
|
||
func (m *EffectManager) ExecuteNode(nodeName string, ctx *EffectContext) {
|
||
if effs, ok := m.effects[nodeName]; ok {
|
||
for _, eff := range effs {
|
||
if eff.Handler != nil {
|
||
eff.Handler(ctx)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
func (m *EffectManager) RunBattle(ctx *EffectContext) {
|
||
var i int
|
||
var execNext func()
|
||
execNext = func() {
|
||
if i >= len(m.nodes) {
|
||
return
|
||
}
|
||
node := m.nodes[i]
|
||
i++
|
||
node.Exec(ctx, execNext)
|
||
}
|
||
execNext()
|
||
}
|
||
|
||
// ==================== 节点定义 ====================
|
||
func BuildBattleNodes() []*EffectNode {
|
||
return []*EffectNode{
|
||
{Name: "战斗开始", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("OnBattleStart")
|
||
next()
|
||
}},
|
||
{Name: "登场/切换", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("OnSwitchIn / OnSwitchOut / OnTransform")
|
||
next()
|
||
}},
|
||
{Name: "回合开始", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("TurnStart")
|
||
next()
|
||
}},
|
||
{Name: "操作阶段", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("选择技能 / 使用道具 / 切换")
|
||
next()
|
||
}},
|
||
{Name: "先手判定", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("先手权判定")
|
||
next()
|
||
}},
|
||
{Name: "先手出手-前置", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("BeforeUseSkillCheck / BeforeHit / OnMiss")
|
||
next()
|
||
}},
|
||
{Name: "先手出手-技能命中", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("技能命中判定 / 初始伤害公式 / 红伤数值计算")
|
||
next()
|
||
}},
|
||
{Name: "先手出手-技能效果结算", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("技能效果结算 / 魂印 / 套装 / 回合类效果")
|
||
next()
|
||
}},
|
||
{Name: "先手出手-伤害结算", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("最终伤害生效 / 体力扣除")
|
||
next()
|
||
}},
|
||
{Name: "先手出手-行动结束", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("行动后效果 / 额外行动 / 机盖弹伤 / 出手结束效果")
|
||
next()
|
||
}},
|
||
{Name: "后手出手", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("后手节点同先手节点")
|
||
next()
|
||
}},
|
||
{Name: "回合结束后①", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("回合结束后通用时点① / 桃园回血 / 回合扣减①")
|
||
next()
|
||
}},
|
||
{Name: "回合结束后②", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("回合结束后通用时点② / 战争猎魔 / 16年魂印续航")
|
||
next()
|
||
}},
|
||
{Name: "死亡判定", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("死亡结算 / 保护机制")
|
||
next()
|
||
}},
|
||
{Name: "击败/未击败判定", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("击败触发效果 / 未击败触发效果")
|
||
next()
|
||
}},
|
||
{Name: "进入下回合", Exec: func(ctx *EffectContext, next func()) {
|
||
fmt.Println("进入下回合,流程重新开始")
|
||
next()
|
||
}},
|
||
}
|
||
}
|
||
|
||
// ==================== 注册45个原始效果 ====================
|
||
func RegisterOriginalEffects(manager *EffectManager) {
|
||
// 战斗开始类
|
||
manager.RegisterEffect("战斗开始", &Effect{
|
||
Name: "天生特性",
|
||
LifeType: Permanent,
|
||
Phase: Before,
|
||
Handler: func(ctx *EffectContext) bool {
|
||
fmt.Println(ctx.Actor, "天生特性触发")
|
||
return true
|
||
},
|
||
})
|
||
manager.RegisterEffect("战斗开始", &Effect{
|
||
Name: "战斗开始印记",
|
||
LifeType: Permanent,
|
||
Phase: Before,
|
||
Handler: func(ctx *EffectContext) bool {
|
||
fmt.Println(ctx.Actor, "战斗开始印记触发")
|
||
return true
|
||
},
|
||
})
|
||
|
||
// 登场/切换类
|
||
manager.RegisterEffect("登场/切换", &Effect{
|
||
Name: "登场魂印",
|
||
LifeType: CountBased,
|
||
Duration: 2,
|
||
Phase: Before,
|
||
Handler: func(ctx *EffectContext) bool {
|
||
fmt.Println(ctx.Actor, "登场魂印触发")
|
||
return true
|
||
},
|
||
})
|
||
|
||
// 回合开始
|
||
manager.RegisterEffect("回合开始", &Effect{
|
||
Name: "回合开始Buff",
|
||
LifeType: TurnBased,
|
||
Duration: 3,
|
||
Phase: Before,
|
||
Handler: func(ctx *EffectContext) bool {
|
||
fmt.Println(ctx.Actor, "回合开始Buff生效")
|
||
return true
|
||
},
|
||
})
|
||
|
||
// 先手出手节点示例
|
||
manager.RegisterEffect("先手出手-前置", &Effect{
|
||
Name: "先手准备效果",
|
||
LifeType: CountBased,
|
||
Duration: 1,
|
||
Phase: Before,
|
||
Handler: func(ctx *EffectContext) bool {
|
||
fmt.Println(ctx.Actor, "先手前置效果触发")
|
||
return true
|
||
},
|
||
})
|
||
manager.RegisterEffect("先手出手-技能效果结算", &Effect{
|
||
Name: "技能附加效果",
|
||
LifeType: CountBased,
|
||
Duration: 2,
|
||
Phase: After,
|
||
Handler: func(ctx *EffectContext) bool {
|
||
fmt.Println(ctx.Actor, "技能附加效果生效")
|
||
return true
|
||
},
|
||
})
|
||
|
||
// 回合结束后①
|
||
manager.RegisterEffect("回合结束后①", &Effect{
|
||
Name: "回合结束Buff",
|
||
LifeType: TurnBased,
|
||
Duration: 3,
|
||
Phase: After,
|
||
Handler: func(ctx *EffectContext) bool {
|
||
fmt.Println(ctx.Actor, "回合结束后Buff触发")
|
||
return true
|
||
},
|
||
})
|
||
|
||
// 你可以继续按此格式,将原45个效果依次映射到16节点
|
||
// 节点选择 Before/After,LifeType选择 TurnBased/CountBased/Permanent
|
||
}
|