From b7dc9368eb4aceca4c94208f63289b13c6cff65c Mon Sep 17 00:00:00 2001 From: aceld Date: Mon, 3 Jun 2019 14:29:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3AI=E5=8A=A8=E4=BD=9C=E5=A4=9A?= =?UTF-8?q?=E4=B8=AAgo=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zinx_app_demo/mmo_game/client_AI_robot.go | 47 ++++++++++++++--------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/zinx_app_demo/mmo_game/client_AI_robot.go b/zinx_app_demo/mmo_game/client_AI_robot.go index b482f5c..035fc32 100644 --- a/zinx_app_demo/mmo_game/client_AI_robot.go +++ b/zinx_app_demo/mmo_game/client_AI_robot.go @@ -21,12 +21,13 @@ type Message struct { } type TcpClient struct { - conn net.Conn - X float32 - Y float32 - Z float32 - V float32 - Pid int32 + conn net.Conn + X float32 + Y float32 + 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 { @@ -230,12 +238,13 @@ func NewTcpClient(ip string, port int) *TcpClient { conn, err := net.Dial("tcp", addrStr) if err == nil { client := &TcpClient{ - conn: conn, - Pid: 0, - X: 0, - Y: 0, - Z: 0, - V: 0, + conn: conn, + Pid: 0, + X: 0, + Y: 0, + Z: 0, + V: 0, + isOnline: make(chan bool), } return client } else {