- 新增 AI_player 结构体和相关方法,用于创建和管理 AI 玩家 - 重构 FightC 结构体,增加 Input 结构体用于封装玩家输入 - 优化战斗流程,包括回合处理、技能使用、伤害计算等 - 改进广播机制,使用函数回调替代直接调用方法 - 优化玩家和 AI 的动作处理逻辑
189 lines
5.1 KiB
Go
189 lines
5.1 KiB
Go
package info
|
||
|
||
import (
|
||
"time"
|
||
|
||
"github.com/tnnmigga/enum"
|
||
)
|
||
|
||
// EnumPlayerOperation 玩家操作枚举类型
|
||
type EnumPlayerOperation int
|
||
|
||
// 定义读秒倒计时期间玩家可执行的操作枚举
|
||
var PlayerOperations = enum.New[struct {
|
||
SystemGiveUp EnumPlayerOperation `enum:"-1"` // 系统选择放弃出手(比如没有PP)
|
||
SelectSkill EnumPlayerOperation `enum:"0"` // 选择技能-6到6
|
||
ActiveSwitch EnumPlayerOperation `enum:"2"` // 主动切换(中切)
|
||
UsePotion EnumPlayerOperation `enum:"3"` // 使用药剂(捕捉、逃跑等)
|
||
Escape EnumPlayerOperation `enum:"4"` // 逃跑(等级最高,以及掉线)
|
||
PlayerOffline EnumPlayerOperation `enum:"5"` // 玩家掉线
|
||
// BeExpelledSwitch EnumPlayerOperation `enum:"6"` // 被驱逐切换
|
||
}]()
|
||
|
||
// Compare 比较两个1v1战斗动作的执行优先级(核心逻辑)
|
||
func Compare(a, b BattleActionI) (BattleActionI, BattleActionI) {
|
||
// 动作本身的优先级比较
|
||
p1 := b.Priority() - a.Priority()
|
||
if p1 > 0 { // 对手优先级更高
|
||
return b, a
|
||
} else if p1 < 0 {
|
||
return a, b
|
||
}
|
||
_, ok := b.(*SystemGiveUpAction)
|
||
|
||
if ok {
|
||
return a, b
|
||
}
|
||
|
||
bskill := b.(*SelectSkillAction)
|
||
askill := a.(*SelectSkillAction)
|
||
// 如果是选择技能操作,进一步比较技能优先级和宠物属性
|
||
|
||
p2 := bskill.Skill.Priority - askill.Skill.Priority
|
||
if p2 > 0 {
|
||
return b, a
|
||
} else if p2 < 0 {
|
||
return a, b
|
||
}
|
||
|
||
// 比较宠物相关属性(假设Value(4)返回速度相关值)
|
||
p2 = int(bskill.PetInfo.Speed()) - int(askill.PetInfo.Speed())
|
||
if p2 > 0 {
|
||
return b, a
|
||
} else if p2 < 0 {
|
||
return a, b
|
||
}
|
||
|
||
return a, b // 速度相同时,发起方优先
|
||
}
|
||
|
||
// BattleActionI 战斗动作接口
|
||
type BattleActionI interface {
|
||
GetPlayerID() uint32
|
||
Priority() int // 优先级
|
||
|
||
}
|
||
|
||
// SelectSkillAction 选择技能的战斗动作
|
||
type SelectSkillAction struct {
|
||
PlayerID uint32 // 玩家ID
|
||
Skill *BattleSkillEntity // 使用的技能
|
||
PetInfo *BattlePetEntity // 使用技能的宠物
|
||
Attack AttackValue
|
||
}
|
||
|
||
func (s *SelectSkillAction) GetPlayerID() uint32 {
|
||
return s.PlayerID
|
||
}
|
||
|
||
// Priority 返回动作优先级
|
||
func (s *SelectSkillAction) Priority() int {
|
||
return int(PlayerOperations.SelectSkill)
|
||
}
|
||
|
||
// ActiveSwitchAction 主动切换宠物的战斗动作
|
||
type ActiveSwitchAction struct {
|
||
PlayerID uint32 // 玩家ID
|
||
|
||
Reason ChangePetInfo
|
||
// CurrentPet BattlePetEntity // 当前在场宠物
|
||
// TargetPet BattlePetEntity // 要切换上场的宠物
|
||
// SwitchReason string // 切换原因
|
||
}
|
||
|
||
// Priority 返回动作优先级
|
||
func (a *ActiveSwitchAction) Priority() int {
|
||
return int(PlayerOperations.ActiveSwitch)
|
||
}
|
||
|
||
func (a *ActiveSwitchAction) GetPlayerID() uint32 {
|
||
return a.PlayerID
|
||
// fmt.Printf("玩家[%d]主动切换宠物:从%s切换到%s(原因:%s)\n",
|
||
// // a.PlayerID, a.CurrentPet.Name, a.TargetPet.Name, a.SwitchReason)
|
||
}
|
||
|
||
// UsePotionAction 使用药剂的战斗动作
|
||
type UsePotionAction struct {
|
||
PlayerID uint32 // 玩家ID
|
||
PotionID uint32 // 药剂ID
|
||
PotionName string // 药剂名称
|
||
TargetPet BattlePetEntity // 药剂作用目标宠物
|
||
}
|
||
type PlayerI interface {
|
||
SendPack(b []byte) error
|
||
}
|
||
|
||
// Priority 返回动作优先级
|
||
func (u *UsePotionAction) Priority() int {
|
||
return int(PlayerOperations.UsePotion)
|
||
}
|
||
|
||
// EscapeAction 逃跑的战斗动作
|
||
type EscapeAction struct {
|
||
PlayerID uint32 // 玩家ID
|
||
Reason FightOverInfo
|
||
Our PlayerI
|
||
Opp PlayerI
|
||
}
|
||
|
||
func (e *EscapeAction) GetPlayerID() uint32 {
|
||
return e.PlayerID
|
||
}
|
||
func (e *EscapeAction) GetInfo() FightOverInfo {
|
||
return e.Reason
|
||
}
|
||
|
||
// Priority 返回动作优先级
|
||
func (e *EscapeAction) Priority() int {
|
||
return int(PlayerOperations.Escape)
|
||
}
|
||
|
||
// SystemGiveUpAction 系统强制放弃出手的动作
|
||
type SystemGiveUpAction struct {
|
||
PlayerID uint32 // 玩家ID
|
||
Reason string // 放弃原因(如没有PP值、宠物全部倒下等)
|
||
LastPet BattlePetEntity // 最后在场的宠物
|
||
}
|
||
|
||
func (s *SystemGiveUpAction) GetPlayerID() uint32 {
|
||
return s.PlayerID
|
||
}
|
||
|
||
// Priority 返回动作优先级
|
||
func (s *SystemGiveUpAction) Priority() int {
|
||
return int(PlayerOperations.SystemGiveUp)
|
||
}
|
||
|
||
// Broadcast 广播系统放弃的动作信息
|
||
func (s *SystemGiveUpAction) Broadcast() {
|
||
// fmt.Printf("玩家[%d]被系统强制放弃出手,原因:%s(最后在场宠物:%s)\n",
|
||
// s.PlayerID, s.Reason, s.LastPet.Name)
|
||
}
|
||
|
||
// PlayerOfflineAction 玩家掉线的战斗动作
|
||
type PlayerOfflineAction struct {
|
||
PlayerID uint32 // 掉线玩家ID
|
||
OfflineTime time.Time // 掉线时间
|
||
Reason FightOverInfo
|
||
}
|
||
|
||
// Priority 返回动作优先级
|
||
func (p *PlayerOfflineAction) Priority() int {
|
||
return int(PlayerOperations.PlayerOffline)
|
||
}
|
||
|
||
func (p *PlayerOfflineAction) GetPlayerID() uint32 {
|
||
return p.PlayerID
|
||
}
|
||
|
||
// Broadcast 广播玩家掉线信息
|
||
func (p *PlayerOfflineAction) Broadcast() {
|
||
// /status := "未重连"
|
||
// if p.IsReconnect {
|
||
// status = "已重连"
|
||
// }
|
||
// fmt.Printf("玩家[%d]在[%s]掉线,当前在场宠物:%s,状态:%s\n",
|
||
// p.PlayerID, p.OfflineTime.Format("2006-01-02 15:04:05"),
|
||
// p.CurrentPet.Name, status)
|
||
}
|