refactor: 将战斗初始化逻辑移动到新文件并优化AI技能选择
This commit is contained in:
@@ -4,18 +4,15 @@ import (
|
||||
"blazing/common/data"
|
||||
"blazing/common/socket/errorcode"
|
||||
"blazing/common/utils"
|
||||
"blazing/cool"
|
||||
"blazing/modules/config/service"
|
||||
"blazing/modules/player/model"
|
||||
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"blazing/logic/service/common"
|
||||
"blazing/logic/service/fight/action"
|
||||
"blazing/logic/service/fight/info"
|
||||
"blazing/logic/service/fight/input"
|
||||
"blazing/logic/service/player"
|
||||
"blazing/logic/service/user"
|
||||
"math/rand"
|
||||
"sync"
|
||||
@@ -265,94 +262,6 @@ func initfightready(in *input.Input) (info.FightUserInfo, []info.ReadyFightPetIn
|
||||
return userindo, t
|
||||
}
|
||||
|
||||
// 创建新战斗,邀请方和被邀请方,或者玩家和野怪方
|
||||
func NewFight(p1, p2 common.PlayerI, fn func(info.FightOverInfo)) (*FightC, errorcode.ErrorCode) {
|
||||
|
||||
fmt.Println("NewFight", p1.GetInfo().UserID)
|
||||
f := &FightC{}
|
||||
f.ownerID = p1.GetInfo().UserID
|
||||
|
||||
f.Switch = make(map[uint32]*action.ActiveSwitchAction)
|
||||
f.callback = fn //战斗结束的回调
|
||||
f.quit = make(chan struct{})
|
||||
f.over = make(chan struct{})
|
||||
f.StartTime = time.Now()
|
||||
seed := f.StartTime.UnixNano() ^ int64(p1.GetInfo().UserID) ^ int64(p2.GetInfo().UserID) // ^ int64(f.Round) // 用异或运算混合多维度信息
|
||||
f.rand = rand.New(rand.NewSource(seed))
|
||||
f.Info = p1.Getfightinfo()
|
||||
|
||||
//这里应该挪到玩家初始化执行
|
||||
|
||||
f.ReadyInfo.Status = f.Info.Status
|
||||
var err errorcode.ErrorCode
|
||||
f.Our, err = f.initplayer(p1)
|
||||
if err > 0 {
|
||||
return nil, err
|
||||
|
||||
}
|
||||
|
||||
f.Opp, err = f.initplayer(p2)
|
||||
if err > 0 {
|
||||
return nil, err
|
||||
|
||||
}
|
||||
|
||||
f.ReadyInfo.OurInfo, f.ReadyInfo.OurPetList = initfightready(f.Our)
|
||||
f.ReadyInfo.OpponentInfo, f.ReadyInfo.OpponentPetList = initfightready(f.Opp)
|
||||
var loadtime time.Duration = 120 * time.Second
|
||||
//说明是PVE
|
||||
|
||||
f.Broadcast(func(ff *input.Input) {
|
||||
ff.SetOPP(f.GetInputByPlayer(ff.Player, true))
|
||||
|
||||
})
|
||||
f.FightStartOutboundInfo = f.buildFightStartInfo()
|
||||
if f.Info.Status == info.BattleMode.FIGHT_WITH_NPC {
|
||||
|
||||
f.Opp.Finished = true //PVE 默认boss数据直接加载完成
|
||||
loadtime = 60 * time.Second
|
||||
//f.handleNPCFightSpecial(&fightStartInfo)
|
||||
|
||||
if f.Opp.Player.(*player.AI_player).CanCapture > 0 {
|
||||
f.Opp.CanCapture = f.Opp.Player.(*player.AI_player).CanCapture
|
||||
f.FightStartOutboundInfo.Info2.Catchable = 1 //可以捕捉就置1
|
||||
}
|
||||
f.Opp.AttackValue.Prop = f.Opp.Player.(*player.AI_player).Prop
|
||||
f.FightStartOutboundInfo.Info2.Prop = f.Opp.AttackValue.Prop
|
||||
}
|
||||
|
||||
f.Broadcast(func(ff *input.Input) {
|
||||
|
||||
ff.Player.SendPackCmd(2503, &f.ReadyInfo)
|
||||
if p, ok := ff.Player.(*player.Player); ok {
|
||||
p.Service.Info.Save(*p.Info)
|
||||
}
|
||||
})
|
||||
|
||||
cool.Cron.AfterFunc(loadtime, func() {
|
||||
fmt.Println(f.Our.UserID, "战斗超时结算")
|
||||
if !f.Our.Finished || !f.Opp.Finished { //如果有任一没有加载完成
|
||||
f.closefight = true //阻止继续添加action
|
||||
f.Reason = info.BattleOverReason.PlayerOffline
|
||||
switch {
|
||||
case !f.Opp.Finished: //邀请方没加载完成 先判断邀请方,如果都没加载完成,就算做房主胜利
|
||||
f.WinnerId = f.Our.Player.GetInfo().UserID
|
||||
case !f.Our.Finished: //被邀请方没加载完成
|
||||
f.WinnerId = f.Opp.Player.GetInfo().UserID
|
||||
}
|
||||
f.Broadcast(func(ff *input.Input) {
|
||||
//todo 将血量和技能pp传回enterturn
|
||||
|
||||
ff.Player.SendPackCmd(2506, &f.FightOverInfo)
|
||||
ff.Player.QuitFight()
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
return f, 0
|
||||
}
|
||||
|
||||
// 被击败的ID
|
||||
func (f *FightC) IsWin(c *input.Input) bool {
|
||||
|
||||
|
||||
@@ -43,8 +43,17 @@ func (our *Input) GetAction() {
|
||||
s.DamageValue = our.CalculatePower(our.Opp, s)
|
||||
|
||||
// 判断是否能秒杀(伤害 >= 对方当前生命值)
|
||||
if uint32(s.DamageValue.IntPart()) >= our.Opp.CurrentPet.Info.Hp { // 假设oppPet.HP为对方当前剩余生命值
|
||||
usedskill = s
|
||||
if s.DamageValue.Cmp(our.Opp.CurrentPet.GetHP()) != -1 { // 假设oppPet.HP为对方当前剩余生命值
|
||||
|
||||
if usedskill != nil {
|
||||
if s.DamageValue.Cmp(usedskill.DamageValue) != -1 {
|
||||
usedskill = s
|
||||
}
|
||||
|
||||
} else {
|
||||
usedskill = s
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
if usedskill == nil {
|
||||
|
||||
102
logic/service/fight/new.go
Normal file
102
logic/service/fight/new.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package fight
|
||||
|
||||
import (
|
||||
"blazing/common/socket/errorcode"
|
||||
"blazing/cool"
|
||||
"blazing/logic/service/common"
|
||||
"blazing/logic/service/fight/action"
|
||||
"blazing/logic/service/fight/info"
|
||||
"blazing/logic/service/fight/input"
|
||||
"blazing/logic/service/player"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 创建新战斗,邀请方和被邀请方,或者玩家和野怪方
|
||||
func NewFight(p1, p2 common.PlayerI, fn func(info.FightOverInfo)) (*FightC, errorcode.ErrorCode) {
|
||||
|
||||
fmt.Println("NewFight", p1.GetInfo().UserID)
|
||||
f := &FightC{}
|
||||
f.ownerID = p1.GetInfo().UserID
|
||||
|
||||
f.Switch = make(map[uint32]*action.ActiveSwitchAction)
|
||||
f.callback = fn //战斗结束的回调
|
||||
f.quit = make(chan struct{})
|
||||
f.over = make(chan struct{})
|
||||
f.StartTime = time.Now()
|
||||
seed := f.StartTime.UnixNano() ^ int64(p1.GetInfo().UserID) ^ int64(p2.GetInfo().UserID) // ^ int64(f.Round) // 用异或运算混合多维度信息
|
||||
f.rand = rand.New(rand.NewSource(seed))
|
||||
f.Info = p1.Getfightinfo()
|
||||
|
||||
//这里应该挪到玩家初始化执行
|
||||
|
||||
f.ReadyInfo.Status = f.Info.Status
|
||||
var err errorcode.ErrorCode
|
||||
f.Our, err = f.initplayer(p1)
|
||||
if err > 0 {
|
||||
return nil, err
|
||||
|
||||
}
|
||||
|
||||
f.Opp, err = f.initplayer(p2)
|
||||
if err > 0 {
|
||||
return nil, err
|
||||
|
||||
}
|
||||
|
||||
f.ReadyInfo.OurInfo, f.ReadyInfo.OurPetList = initfightready(f.Our)
|
||||
f.ReadyInfo.OpponentInfo, f.ReadyInfo.OpponentPetList = initfightready(f.Opp)
|
||||
var loadtime time.Duration = 120 * time.Second
|
||||
//说明是PVE
|
||||
|
||||
f.Broadcast(func(ff *input.Input) {
|
||||
ff.SetOPP(f.GetInputByPlayer(ff.Player, true))
|
||||
|
||||
})
|
||||
f.FightStartOutboundInfo = f.buildFightStartInfo()
|
||||
if f.Info.Status == info.BattleMode.FIGHT_WITH_NPC {
|
||||
|
||||
f.Opp.Finished = true //PVE 默认boss数据直接加载完成
|
||||
loadtime = 60 * time.Second
|
||||
//f.handleNPCFightSpecial(&fightStartInfo)
|
||||
|
||||
if f.Opp.Player.(*player.AI_player).CanCapture > 0 {
|
||||
f.Opp.CanCapture = f.Opp.Player.(*player.AI_player).CanCapture
|
||||
f.FightStartOutboundInfo.Info2.Catchable = 1 //可以捕捉就置1
|
||||
}
|
||||
f.Opp.AttackValue.Prop = f.Opp.Player.(*player.AI_player).Prop
|
||||
f.FightStartOutboundInfo.Info2.Prop = f.Opp.AttackValue.Prop
|
||||
}
|
||||
|
||||
f.Broadcast(func(ff *input.Input) {
|
||||
|
||||
ff.Player.SendPackCmd(2503, &f.ReadyInfo)
|
||||
if p, ok := ff.Player.(*player.Player); ok {
|
||||
p.Service.Info.Save(*p.Info)
|
||||
}
|
||||
})
|
||||
|
||||
cool.Cron.AfterFunc(loadtime, func() {
|
||||
fmt.Println(f.Our.UserID, "战斗超时结算")
|
||||
if !f.Our.Finished || !f.Opp.Finished { //如果有任一没有加载完成
|
||||
f.closefight = true //阻止继续添加action
|
||||
f.Reason = info.BattleOverReason.PlayerOffline
|
||||
switch {
|
||||
case !f.Opp.Finished: //邀请方没加载完成 先判断邀请方,如果都没加载完成,就算做房主胜利
|
||||
f.WinnerId = f.Our.Player.GetInfo().UserID
|
||||
case !f.Our.Finished: //被邀请方没加载完成
|
||||
f.WinnerId = f.Opp.Player.GetInfo().UserID
|
||||
}
|
||||
f.Broadcast(func(ff *input.Input) {
|
||||
//todo 将血量和技能pp传回enterturn
|
||||
|
||||
ff.Player.SendPackCmd(2506, &f.FightOverInfo)
|
||||
ff.Player.QuitFight()
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
return f, 0
|
||||
}
|
||||
Reference in New Issue
Block a user