refactor(fight/effect): 统一效果初始化接口,重构效果类型管理并优化参数传递逻辑

This commit is contained in:
1
2025-09-24 16:36:32 +00:00
parent e909200799
commit 266a328159
14 changed files with 146 additions and 204 deletions

View File

@@ -0,0 +1,72 @@
package input
import (
"blazing/logic/service/fight/info"
)
type Effect interface {
OnBattleStart() bool //战斗开始
OnTurnStart(opp *Input) //回合开始
UseSkill(opp *Input) bool //使用技能 可以取消用技能节点
SkillUseEnd(opp *Input)
// OnSkillPP() bool //技能PP减少节点
// BeforeMultiHit() bool //多段攻击前
// BeforeHit() bool //命中前
// OnCritPreDamage() bool //暴击判定成功且伤害计算前触发
// PreDamage() bool // 技能伤害计算前触发(增伤 / 减伤等)
// OnBeforeCalculateDamage() bool // 最终伤害计算前触发
// OnDamage() bool // 造成伤害时触发
//使用技能 可以取消用技能节点
AfterAttr(t *info.BattlePetEntity) //在获取属性前,比如重写对方属性AfterAttr
BeferAttr(t *info.BattlePetEntity) //在获取属性后,比如视为对方属性
SetArgs(input *Input,param ...int)
IsCrit(opp *Input, skill *info.SkillEntity) //是否暴击
CalculateDamage(opp *Input, skill *info.SkillEntity) //击判定成功且伤害计算前触发
OnBeforeCalculateDamage(opp *Input, skill *info.SkillEntity) // 最终伤害计算前触发
// Shield() bool // 护盾值变化时触发
// PostDamage() bool // 伤害结算后触发(血量扣除后)
IsHit(opp *Input, skill *info.SkillEntity) //闪避率计算,,实际上是修改命中的判断
TakeHit(opp *Input, skill *info.SkillEntity) //闪避率计算,,实际上是修改命中的判断
//() bool // 暴击伤害结算后触发
OnSkill(opp *Input, skill *info.SkillEntity) //闪避率计算,,实际上是修改命中的判断
OnMiss() bool // 技能未命中时触发
// OnHit() bool // 技能命中时触发
// OnMiss() bool // 技能未命中时触发
// AfterAttacked() bool // 被攻击后触发(受击判定)
// SetOwner(bool)
// OnDefeat() bool // 精灵被击败时触发
TurnEnd(opp *Input) //闪避率计算,,实际上是修改命中的判断
// // 堆叠Stack相关触发
// OnStackBefore() bool // 堆叠效果前触发
// OnStack() bool // 堆叠效果触发
// OnBeforeConsumeStack() bool // 消耗堆叠前触发
// OnConsumeStack() bool // 消耗堆叠时触发
// // 治疗相关触发
// OnBeforeHeal() bool // 治疗前触发
// OnHeal() bool // 治疗生效时触发
// // 精灵切换相关触发
// OnSwitchIn() bool // 精灵出战 / 上场时触发
// OnSwitchOut() bool // 精灵下场时触发
// OnOwnerSwitchIn() bool // 所属玩家精灵出战时触发
// OnOwnerSwitchOut() bool // 所属玩家精灵下场时触发
// PreBattleEnd() bool //战斗结束前
// OnBattleEnd() bool //战斗结束
//回合数,然后次数另外维护
Duration(...int) int
Alive() bool
Stack(...int) int
GetMaxStack() int
NotALive()
GetOwner() bool // 技能属主,比如寄生和镇魂歌,属主是对方)
//GetSkill() *BattleSkillEntity //获得技能ctx
}

View File

@@ -28,7 +28,7 @@ type Input struct {
func NewInput(c common.FightI, p common.PlayerI) *Input {
ret := &Input{FightC: c, Player: p}
ret.Effects = utils.NewOrderedMap[int, Effect]()
t := GetDamageEffect(0)
t := Geteffect(EffectType.Damage, 0)
ret.AddEffect(t) //添加默认基类,实现继承
p.SetFightC(c) //给玩家设置战斗容器
@@ -73,10 +73,10 @@ func (i *Input) GetStatusBonus() float64 {
maxBonus := 1.0 // 默认无状态倍率
for statusIdx := 0; statusIdx < 20; statusIdx++ {
t, ok := GetStatusEffect(statusIdx)
t := Geteffect(EffectType.Status, statusIdx)
// 检查状态是否存在数组中值为1表示存在该状态
if ok && t.Stack() > 0 {
if t.Effect.Stack() > 0 {
if bonus, exists := statusBonuses[info.EnumBattleStatus(statusIdx)]; exists && bonus > maxBonus {
maxBonus = bonus
}

View File

@@ -7,95 +7,39 @@ import (
"reflect"
"github.com/mohae/deepcopy"
"github.com/tnnmigga/enum"
)
type Effect interface {
OnBattleStart() bool //战斗开始
// 战斗结束原因枚举
type EnumEffectType int
OnTurnStart(opp *Input) //回合开始
UseSkill(opp *Input) bool //使用技能 可以取消用技能节点
SkillUseEnd(opp *Input)
// OnSkillPP() bool //技能PP减少节点
// BeforeMultiHit() bool //多段攻击前
// BeforeHit() bool //命中前
// OnCritPreDamage() bool //暴击判定成功且伤害计算前触发
// PreDamage() bool // 技能伤害计算前触发(增伤 / 减伤等)
// OnBeforeCalculateDamage() bool // 最终伤害计算前触发
// OnDamage() bool // 造成伤害时触发
//使用技能 可以取消用技能节点
SetInput(input *Input)
AfterAttr(t *info.BattlePetEntity) //在获取属性前,比如重写对方属性AfterAttr
BeferAttr(t *info.BattlePetEntity) //在获取属性后,比如视为对方属性
SetArgs(param ...int)
IsCrit(opp *Input, skill *info.SkillEntity) //是否暴击
CalculateDamage(opp *Input, skill *info.SkillEntity) //击判定成功且伤害计算前触发
OnBeforeCalculateDamage(opp *Input, skill *info.SkillEntity) // 最终伤害计算前触发
// Shield() bool // 护盾值变化时触发
// PostDamage() bool // 伤害结算后触发(血量扣除后)
IsHit(opp *Input, skill *info.SkillEntity) //闪避率计算,,实际上是修改命中的判断
TakeHit(opp *Input, skill *info.SkillEntity) //闪避率计算,,实际上是修改命中的判断
//() bool // 暴击伤害结算后触发
OnSkill(opp *Input, skill *info.SkillEntity) //闪避率计算,,实际上是修改命中的判断
// OnHit() bool // 技能命中时触发
// OnMiss() bool // 技能未命中时触发
// AfterAttacked() bool // 被攻击后触发(受击判定)
// SetOwner(bool)
// OnDefeat() bool // 精灵被击败时触发
TurnEnd(opp *Input) //闪避率计算,,实际上是修改命中的判断
// // 堆叠Stack相关触发
// OnStackBefore() bool // 堆叠效果前触发
// OnStack() bool // 堆叠效果触发
// OnBeforeConsumeStack() bool // 消耗堆叠前触发
// OnConsumeStack() bool // 消耗堆叠时触发
// // 治疗相关触发
// OnBeforeHeal() bool // 治疗前触发
// OnHeal() bool // 治疗生效时触发
// // 精灵切换相关触发
// OnSwitchIn() bool // 精灵出战 / 上场时触发
// OnSwitchOut() bool // 精灵下场时触发
// OnOwnerSwitchIn() bool // 所属玩家精灵出战时触发
// OnOwnerSwitchOut() bool // 所属玩家精灵下场时触发
// PreBattleEnd() bool //战斗结束前
// OnBattleEnd() bool //战斗结束
//回合数,然后次数另外维护
Duration(...int) int
Alive() bool
Stack(...int) int
GetMaxStack() int
NotALive()
GetOwner() bool // 技能属主,比如寄生和镇魂歌,属主是对方)
//GetSkill() *BattleSkillEntity //获得技能ctx
}
var EffectType = enum.New[struct {
Skill EnumEffectType `enum:"1000000"` //技能
Prop EnumEffectType `enum:"2000000"` //属性
Status EnumEffectType `enum:"3000000"` //状态
Damage EnumEffectType `enum:"4000000"` //伤害
}]()
var NodeM = make(map[int]Effect, 0)
func InitSkillEffect(id int, t Effect) {
func InitEffect(etype EnumEffectType, id int, t Effect) {
NodeM[id+1000000] = t
NodeM[id+int(etype)] = t
}
func Geteffect(id int) (Effect, bool) {
func Geteffect(etype EnumEffectType, id int) *EffectID {
//todo 获取前GetEffect
ret, ok := NodeM[id]
ret, ok := NodeM[id+int(etype)]
if ok {
//todo 获取前GetEffect
eff := deepcopy.Copy(ret).(Effect)
return eff, ok
return &EffectID{
ID: id + int(etype),
Effect: eff,
}
//todo 获取后GetEffect
}
return nil, false
//todo 获取后GetEffect
}
func InitPropEffect(id int, t Effect) {
NodeM[id+2000000] = t
return &EffectID{}
}
// * battle_lv: atk(0), def(1), sp_atk(2), sp_def(3), spd(4), accuracy(5)
@@ -123,47 +67,11 @@ func (c *Input) GetProp(id int, istue bool) int {
return realValue
}
func InitDamageEffect(id int, t Effect) {
NodeM[id+4000000] = t
}
func (c *Input) GetEffect(etype EnumEffectType, id int) Effect {
rer, _ := c.Effects.Load(id + int(etype))
// 1为红伤
func GetDamageEffect(id int) *EffectID {
id1 := id + 4000000
ret, ok := Geteffect(id1)
if ok {
//todo 获取前GetEffect
return &EffectID{
ID: id1,
Effect: ret,
}
//todo 获取后GetEffect
}
return &EffectID{}
}
func (c *Input) GetDamageEffect(id int) int {
id1 := id + 4000000
rer, ok := c.Effects.Load(id1)
if ok {
return rer.Stack()
}
return 0
}
func GetSkillEffect(id int) *EffectID {
id1 := id + 1000000
ret, ok := Geteffect(id1)
if ok {
return &EffectID{
ID: id1,
Effect: ret,
}
}
return &EffectID{}
return rer
}
type EffectID struct {
@@ -171,36 +79,6 @@ type EffectID struct {
Effect Effect
}
func GetPropEffect(id int) Effect {
ret, ok := Geteffect(id + 2000000)
if ok {
//todo 获取前GetEffect
return ret
//todo 获取后GetEffect
}
return nil
}
func InitStatusEffect(id int, t Effect) {
NodeM[id+3000000] = t
}
func GetStatusEffect(id int) (Effect, bool) {
ret, ok := Geteffect(id + 3000000)
if ok {
//todo 获取前GetEffect
return ret, true
//todo 获取后GetEffect
}
return nil, false
}
func (c *Input) GetStatusEffect(id int) (Effect, bool) {
rer, ok := c.Effects.Load(id + 3000000)
return rer, ok
}
func (c *Input) GetCurrAttr(id int) *model.PetInfo {
//todo 获取前GetEffect
@@ -232,7 +110,7 @@ func (c *Input) AddEffect(e *EffectID) {
}
//todo 免疫
//TODO 先激活
e.Effect.SetInput(c)
// 如果已有同 ID 的效果,尝试叠加
_, ok := c.Effects.Load(e.ID)
if !ok {