Browse Source

修正AI动作多个go的问题

master
aceld 5 years ago
parent
commit
b7dc9368eb
  1. 23
      zinx_app_demo/mmo_game/client_AI_robot.go

23
zinx_app_demo/mmo_game/client_AI_robot.go

@ -27,6 +27,7 @@ type TcpClient struct {
Z float32
V float32
Pid int32
isOnline chan bool
}
func (this *TcpClient) Unpack(headdata []byte) (head *Message, err error) {
@ -183,13 +184,9 @@ func (this *TcpClient) DoMsg(msg *Message) {
this.Z = bdata.GetP().Z
this.V = bdata.GetP().V
fmt.Println(fmt.Sprintf("player ID: %d online.. at(%f,%f,%f,%f)", bdata.Pid, this.X, this.Y, this.Z, this.V))
//自动AI业务
go func() {
for {
this.AIRobotAction()
time.Sleep(3 * time.Second)
}
}()
//玩家已经成功上线
this.isOnline <- true
} else if bdata.Tp == 1 {
fmt.Println(fmt.Sprintf("世界聊天,玩家%d说的话是: %s", bdata.Pid, bdata.GetContent()))
@ -223,6 +220,17 @@ func (this *TcpClient) Start() {
this.DoMsg(pkgHead)
}
}()
select {
case <-this.isOnline:
//自动AI业务
go func() {
for {
this.AIRobotAction()
time.Sleep(3 * time.Second)
}
}()
}
}
func NewTcpClient(ip string, port int) *TcpClient {
@ -236,6 +244,7 @@ func NewTcpClient(ip string, port int) *TcpClient {
Y: 0,
Z: 0,
V: 0,
isOnline: make(chan bool),
}
return client
} else {

Loading…
Cancel
Save