go_dreamfactory/modules/parkour/ai.go
2023-10-17 16:09:23 +08:00

125 lines
3.0 KiB
Go

package parkour
import (
"fmt"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"go_dreamfactory/modules/parkour/ai"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"sync"
)
/*
AI 逻辑组件
*/
type aiComp struct {
cbase.ModuleCompBase
service core.IService
module *Parkour
conf []*cfg.GameBuzkashiGradeData
lock sync.RWMutex
ais map[string][]*ai.AI
}
//组件初始化接口
func (this *aiComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, options)
this.module = module.(*Parkour)
this.service = service
this.ais = make(map[string][]*ai.AI)
return
}
func (this *aiComp) Start() (err error) {
err = this.ModuleCompBase.Start()
if this.conf, err = this.module.configure.getGameBuzkashiGrades(); err != nil {
return
}
// timewheel.AddCron(time.Second*3, this.aihandle)
return
}
//创建战斗ai
func (this *aiComp) createAi(battleid string, btype pb.RaceType, users []*pb.DBRaceMember) (err error) {
var (
ok bool
ais []*ai.AI
conf *cfg.GameBukashiAiData
)
if battleid == "" || users == nil || len(users) == 0 {
err = fmt.Errorf("battleid:%s users:%v parameter exceptions", battleid, users)
return
}
this.lock.RLock()
_, ok = this.ais[battleid]
this.lock.RUnlock()
if ok {
err = fmt.Errorf("battle:%s already exists", battleid)
return
}
ais = make([]*ai.AI, len(users))
for i, v := range users {
if conf, err = this.module.configure.getgameBukashiAiDataByDan(int32(btype), v.Dan); err != nil {
return
}
ais[i] = ai.NewAI(battleid, v.Uid, conf)
}
this.lock.Lock()
this.ais[battleid] = ais
this.lock.Unlock()
return
}
//移除AI
func (this *aiComp) removeAi(battleid string) {
this.lock.Lock()
delete(this.ais, battleid)
this.lock.Unlock()
}
//ai自动化
// func (this *aiComp) aihandle(task *timewheel.Task, args ...interface{}) {
// var (
// ok bool
// ais map[string][]*AI = make(map[string][]*AI)
// )
// this.lock.RLock()
// if len(this.ais) > 0 {
// ok = true
// for k, v := range this.ais {
// ais[k] = v
// }
// }
// this.lock.RUnlock()
// if ok {
// for id, member := range ais {
// for _, ai := range member {
// if time.Now().Sub(ai.LastTime) > ai.Interval {
// switch ai.Handle[ai.CurrIndex] {
// case AIHandle_Avoid0: //躲避障碍 失败
// go this.module.avoid(id, ai.UId, nil)
// break
// case AIHandle_Avoid1: //躲避障碍 成功
// go this.module.avoid(id, ai.UId, this.conf[1])
// break
// case AIHandle_Avoid2: //躲避障碍 完美
// go this.module.avoid(id, ai.UId, this.conf[0])
// break
// case AIHandle_Shots0: //射门 失败
// break
// case AIHandle_Shots1: //射门 成功
// go this.module.shot(id, ai.UId)
// break
// }
// ai.LastTime = time.Now()
// ai.CurrIndex++
// ai.CurrIndex = ai.CurrIndex % int32(len(ai.Handle))
// }
// }
// }
// }
// }