refactor: 重构战斗系统为统一动作包结构
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful

This commit is contained in:
xinian
2026-04-06 00:58:23 +08:00
committed by cnb
parent 141ba67014
commit f433a26a6d
12 changed files with 683 additions and 158 deletions

View File

@@ -2,7 +2,6 @@ package controller
import (
"blazing/common/socket/errorcode"
"blazing/modules/player/model"
"blazing/logic/service/fight"
"blazing/logic/service/fight/info"
@@ -31,7 +30,7 @@ func (h Controller) UseSkill(data *UseSkillInInfo, c *player.Player) (result *fi
if err := h.checkFightStatus(c); err != 0 {
return nil, err
}
go c.FightC.UseSkill(c, data.SkillId)
h.dispatchFightActionEnvelope(c, buildLegacyUseSkillEnvelope(data))
return nil, 0
}
@@ -41,31 +40,7 @@ func (h Controller) UseSkillAt(data *UseSkillAtInboundInfo, c *player.Player) (r
if err := h.checkFightStatus(c); err != 0 {
return nil, err
}
actorIndex := int(data.ActorIndex)
targetIndex := int(data.TargetIndex)
targetRelation := data.TargetRelation
// 前端未显式给 relation 时,按 AtkType 兜底3=自己1=己方,其他按对方。
if targetRelation > fight.SkillTargetAlly {
switch data.AtkType {
case 3:
targetRelation = fight.SkillTargetSelf
case 1:
targetRelation = fight.SkillTargetAlly
default:
targetRelation = fight.SkillTargetOpponent
}
}
switch targetRelation {
case fight.SkillTargetSelf:
targetIndex = actorIndex
go c.FightC.UseSkillAt(c, data.SkillId, actorIndex, fight.EncodeTargetIndex(targetIndex, false))
case fight.SkillTargetAlly:
go c.FightC.UseSkillAt(c, data.SkillId, actorIndex, fight.EncodeTargetIndex(targetIndex, false))
default:
go c.FightC.UseSkillAt(c, data.SkillId, actorIndex, fight.EncodeTargetIndex(targetIndex, true))
}
h.dispatchFightActionEnvelope(c, buildIndexedUseSkillEnvelope(data))
return nil, 0
}
@@ -74,8 +49,7 @@ func (h Controller) Escape(data *EscapeFightInboundInfo, c *player.Player) (resu
if err := h.checkFightStatus(c); err != 0 {
return nil, err
}
go c.FightC.Over(c, model.BattleOverReason.PlayerEscape)
h.dispatchFightActionEnvelope(c, buildLegacyEscapeEnvelope())
return nil, 0
}
@@ -84,7 +58,7 @@ func (h Controller) ChangePet(data *ChangePetInboundInfo, c *player.Player) (res
if err := h.checkFightStatus(c); err != 0 {
return nil, err
}
go c.FightC.ChangePet(c, data.CatchTime)
h.dispatchFightActionEnvelope(c, buildLegacyChangeEnvelope(data))
return nil, -1
}
@@ -123,7 +97,7 @@ func (h Controller) UsePetItemInboundInfo(data *UsePetItemInboundInfo, c *player
}
}
go c.FightC.UseItem(c, data.CatchTime, data.ItemId)
h.dispatchFightActionEnvelope(c, buildLegacyUseItemEnvelope(data))
return nil, -1
}
@@ -132,6 +106,6 @@ func (h Controller) FightChat(data *ChatInfo, c *player.Player) (result *fight.N
if err := h.checkFightStatus(c); err != 0 {
return nil, err
}
go c.FightC.Chat(c, data.Message)
h.dispatchFightActionEnvelope(c, buildChatEnvelope(data))
return nil, -1
}

View File

@@ -0,0 +1,79 @@
package controller
import (
"blazing/modules/player/model"
"blazing/logic/service/fight"
"blazing/logic/service/player"
)
// dispatchFightActionEnvelope 把控制器层收到的统一动作结构分发回现有 FightI 接口。
func (h Controller) dispatchFightActionEnvelope(c *player.Player, envelope fight.FightActionEnvelope) {
if c == nil || c.FightC == nil {
return
}
switch envelope.ActionType {
case fight.FightActionTypeSkill:
go c.FightC.UseSkillAt(c, envelope.SkillID, envelope.ActorIndex, envelope.EncodedTargetIndex())
case fight.FightActionTypeItem:
go c.FightC.UseItemAt(c, envelope.CatchTime, envelope.ItemID, envelope.ActorIndex, envelope.EncodedTargetIndex())
case fight.FightActionTypeChange:
go c.FightC.ChangePetAt(c, envelope.CatchTime, envelope.ActorIndex)
case fight.FightActionTypeEscape:
go c.FightC.Over(c, model.BattleOverReason.PlayerEscape)
case fight.FightActionTypeChat:
go c.FightC.Chat(c, envelope.Chat)
}
}
// buildLegacyUseSkillEnvelope 把旧 2405 技能包映射成统一动作结构。
func buildLegacyUseSkillEnvelope(data *UseSkillInInfo) fight.FightActionEnvelope {
if data == nil {
return fight.NewSkillActionEnvelope(0, 0, 0, fight.SkillTargetOpponent, 0)
}
return fight.NewSkillActionEnvelope(data.SkillId, 0, 0, fight.SkillTargetOpponent, 0)
}
// buildIndexedUseSkillEnvelope 把 7505 多战位技能包映射成统一动作结构。
func buildIndexedUseSkillEnvelope(data *UseSkillAtInboundInfo) fight.FightActionEnvelope {
if data == nil {
return fight.NewSkillActionEnvelope(0, 0, 0, fight.SkillTargetOpponent, 0)
}
return fight.NewSkillActionEnvelope(
data.SkillId,
int(data.ActorIndex),
int(data.TargetIndex),
data.TargetRelation,
data.AtkType,
)
}
// buildLegacyUseItemEnvelope 把旧 2406 道具包映射成统一动作结构。
func buildLegacyUseItemEnvelope(data *UsePetItemInboundInfo) fight.FightActionEnvelope {
if data == nil {
return fight.NewItemActionEnvelope(0, 0, 0, 0, fight.SkillTargetOpponent)
}
return fight.NewItemActionEnvelope(data.CatchTime, data.ItemId, 0, 0, fight.SkillTargetOpponent)
}
// buildLegacyChangeEnvelope 把旧 2407 切宠包映射成统一动作结构。
func buildLegacyChangeEnvelope(data *ChangePetInboundInfo) fight.FightActionEnvelope {
if data == nil {
return fight.NewChangeActionEnvelope(0, 0)
}
return fight.NewChangeActionEnvelope(data.CatchTime, 0)
}
// buildLegacyEscapeEnvelope 构造旧 2410 逃跑包对应的统一动作结构。
func buildLegacyEscapeEnvelope() fight.FightActionEnvelope {
return fight.NewEscapeActionEnvelope()
}
// buildChatEnvelope 把战斗聊天包映射成统一动作结构。
func buildChatEnvelope(data *ChatInfo) fight.FightActionEnvelope {
if data == nil {
return fight.NewChatActionEnvelope("")
}
return fight.NewChatActionEnvelope(data.Message)
}