Browse Source

update AI client

master
Roger 5 years ago
parent
commit
469b942fbb
  1. 0
      makefile
  2. 129
      zinx_app_demo/mmo_game/client_AI_robot.go
  3. 22
      zinx_app_demo/mmo_game/core/player.go

0
GNUmakefile → makefile

129
zinx_app_demo/mmo_game/client_AI_robot.go

@ -9,8 +9,8 @@ import (
"io"
"math/rand"
"net"
"os"
"os/signal"
"runtime"
"sync"
"time"
)
@ -31,17 +31,17 @@ type TcpClient struct {
}
func (this *TcpClient) Unpack(headdata []byte) (head *Message, err error) {
headbuf := bytes.NewReader(headdata)
headBuf := bytes.NewReader(headdata)
head = &Message{}
// 读取Len
if err = binary.Read(headbuf, binary.LittleEndian, &head.Len); err != nil {
if err = binary.Read(headBuf, binary.LittleEndian, &head.Len); err != nil {
return nil, err
}
// 读取MsgId
if err = binary.Read(headbuf, binary.LittleEndian, &head.MsgId); err != nil {
if err = binary.Read(headBuf, binary.LittleEndian, &head.MsgId); err != nil {
return nil, err
}
@ -77,15 +77,15 @@ func (this *TcpClient) Pack(msgId uint32, dataBytes []byte) (out []byte, err err
func (this *TcpClient) SendMsg(msgID uint32, data proto.Message) {
// 进行编码
binary_data, err := proto.Marshal(data)
binaryData, err := proto.Marshal(data)
if err != nil {
fmt.Println(fmt.Sprintf("marshaling error: %s", err))
return
}
sendData, err := this.Pack(msgID, binary_data)
sendData, err := this.Pack(msgID, binaryData)
if err == nil {
this.conn.Write(sendData)
_, _ = this.conn.Write(sendData)
} else {
fmt.Println(err)
}
@ -109,8 +109,8 @@ func (this *TcpClient) AIRobotAction() {
x := this.X
z := this.Z
randpos := rand.Intn(2)
if randpos == 0 {
randPos := rand.Intn(2)
if randPos == 0 {
x -= float32(rand.Intn(10))
z -= float32(rand.Intn(10))
} else {
@ -132,14 +132,14 @@ func (this *TcpClient) AIRobotAction() {
}
//移动方向角度
randv := rand.Intn(2)
randV := rand.Intn(2)
v := this.V
if randv == 0 {
if randV == 0 {
v = 25
} else {
v = 335
}
//封装Postsition消息
//封装Position消息
msg := &pb.Position{
X: x,
Y: this.Y,
@ -164,7 +164,7 @@ func (this *TcpClient) DoMsg(msg *Message) {
//解析proto
syncpid := &pb.SyncPid{}
proto.Unmarshal(msg.Data, syncpid)
_ = proto.Unmarshal(msg.Data, syncpid)
//给当前客户端ID进行赋值
this.Pid = syncpid.Pid
@ -173,7 +173,7 @@ func (this *TcpClient) DoMsg(msg *Message) {
//解析proto
bdata := &pb.BroadCast{}
proto.Unmarshal(msg.Data, bdata)
_ = proto.Unmarshal(msg.Data, bdata)
//初次玩家上线 广播位置消息
if bdata.Tp == 2 && bdata.Pid == this.Pid {
@ -197,14 +197,16 @@ func (this *TcpClient) DoMsg(msg *Message) {
func (this *TcpClient) Start() {
go func() {
for {
//read per head data
headdata := make([]byte, 8)
//读取服务端发来的数据 ==》 SyncPid
//1.读取8字节
//第一次读取,读取数据头
headData := make([]byte, 8)
if _, err := io.ReadFull(this.conn, headdata); err != nil {
if _, err := io.ReadFull(this.conn, headData); err != nil {
fmt.Println(err)
return
}
pkgHead, err := this.Unpack(headdata)
pkgHead, err := this.Unpack(headData)
if err != nil {
return
}
@ -221,47 +223,74 @@ func (this *TcpClient) Start() {
}
}()
select {
case <-this.isOnline:
//自动AI业务
go func() {
for {
this.AIRobotAction()
time.Sleep(3 * time.Second)
}
}()
// 10s后,断开连接
for {
select {
case <-this.isOnline:
go func() {
for {
this.AIRobotAction()
time.Sleep(time.Second)
}
}()
case <-time.After(time.Second * 10):
_ = this.conn.Close()
return
}
}
}
func NewTcpClient(ip string, port int) *TcpClient {
addrStr := fmt.Sprintf("%s:%d", ip, port)
conn, err := net.Dial("tcp", addrStr)
if err == nil {
client := &TcpClient{
conn: conn,
Pid: 0,
X: 0,
Y: 0,
Z: 0,
V: 0,
isOnline: make(chan bool),
}
return client
} else {
if err != nil {
panic(err)
}
client := &TcpClient{
conn: conn,
Pid: 0,
X: 0,
Y: 0,
Z: 0,
V: 0,
isOnline: make(chan bool),
}
return client
}
func main() {
for i := 0; i < 1000; i++ {
client := NewTcpClient("127.0.0.1", 8999)
client.Start()
time.Sleep(1 * time.Second)
}
// 开启一个waitgroup,同时运行3个goroutine
runtime.GOMAXPROCS(runtime.NumCPU())
var wg sync.WaitGroup
wg.Add(3)
go func() {
defer wg.Done()
for i := 0; i < 10; i++ {
client := NewTcpClient("127.0.0.1", 8999)
client.Start()
}
}()
go func() {
defer wg.Done()
for i := 0; i < 10; i++ {
client := NewTcpClient("127.0.0.1", 8999)
client.Start()
}
}()
go func() {
defer wg.Done()
for i := 0; i < 10; i++ {
client := NewTcpClient("127.0.0.1", 8999)
client.Start()
}
}()
// close
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
sig := <-c
fmt.Println("=======", sig)
fmt.Println("AI robot start")
wg.Wait()
fmt.Println("AI robot exit")
}

22
zinx_app_demo/mmo_game/core/player.go

@ -172,7 +172,7 @@ func (p *Player) UpdatePos(x float32, y float32, z float32, v float32) {
//把pid添加到新的aoi格子中去
WorldMgrObj.AoiMgr.AddPidToGrid(int(p.Pid), newGid)
p.OnExchangeAoiGrid(oldGid, newGid)
_ = p.OnExchangeAoiGrid(oldGid, newGid)
}
//组装protobuf协议,发送位置给周围玩家
@ -216,7 +216,7 @@ func (p *Player) OnExchangeAoiGrid(oldGid, newGid int) error {
}
//------ > 处理视野消失 <-------
offline_msg := &pb.SyncPid{
offlineMsg := &pb.SyncPid{
Pid: p.Pid,
}
@ -233,13 +233,13 @@ func (p *Player) OnExchangeAoiGrid(oldGid, newGid int) error {
players := WorldMgrObj.GetPlayersByGid(grid.GID)
for _, player := range players {
//让自己在其他玩家的客户端中消失
player.SendMsg(201, offline_msg)
player.SendMsg(201, offlineMsg)
//将其他玩家信息 在自己的客户端中消失
another_offline_msg := &pb.SyncPid{
anotherOfflineMsg := &pb.SyncPid{
Pid: player.Pid,
}
p.SendMsg(201, another_offline_msg)
p.SendMsg(201, anotherOfflineMsg)
time.Sleep(200 * time.Millisecond)
}
}
@ -254,11 +254,11 @@ func (p *Player) OnExchangeAoiGrid(oldGid, newGid int) error {
}
}
online_msg := &pb.BroadCast{
onlineMsg := &pb.BroadCast{
Pid: p.Pid,
Tp: 2,
Data: &pb.BroadCast_P{
&pb.Position{
P: &pb.Position{
X: p.X,
Y: p.Y,
Z: p.Z,
@ -273,14 +273,14 @@ func (p *Player) OnExchangeAoiGrid(oldGid, newGid int) error {
for _, player := range players {
//让自己出现在其他人视野中
player.SendMsg(200, online_msg)
player.SendMsg(200, onlineMsg)
//让其他人出现在自己的视野中
another_online_msg := &pb.BroadCast{
anotherOnlineMsg := &pb.BroadCast{
Pid: player.Pid,
Tp: 2,
Data: &pb.BroadCast_P{
&pb.Position{
P: &pb.Position{
X: player.X,
Y: player.Y,
Z: player.Z,
@ -290,7 +290,7 @@ func (p *Player) OnExchangeAoiGrid(oldGid, newGid int) error {
}
time.Sleep(200 * time.Millisecond)
p.SendMsg(200, another_online_msg)
p.SendMsg(200, anotherOnlineMsg)
}
}

Loading…
Cancel
Save