Exemplo n.º 1
0
    /// <summary>
    /// 外部修改移动方向
    /// </summary>
    /// <param name="dir"></param>
    public void SetNetDirection(ActorMoveDirection dir)
    {
        float distance = Vector3.Distance(SerializeHelper.BackVector(dir.position), transform.position);

        //Debug.LogError("相差长度:" + distance);
        netMove = dir;
    }
Exemplo n.º 2
0
 public void BeStop()
 {
     netMove    = new ActorMoveDirection();
     lastMove   = new ActorMoveDirection();
     netRotate  = new ActorRotateDirection();
     lastRotate = new ActorRotateDirection();
 }
Exemplo n.º 3
0
    private ActorMoveDirection lastMove; //记录上一次发送给服务器的移动值
    public void UIMove(Vector3 moveDirection, float moveSpeed)
    {
        if (DataController.instance.ActorList[myIndex].CurState == RoomActorState.Dead)
        {
            return;
        }
        NetVector3 move   = DataController.BackNetLimetByType(moveDirection);
        float      _speed = DataController.BackNetLimetByType(moveSpeed);
        NetVector3 pos    = DataController.BackNetLimetByType(transform.position);

        ActorMoveDirection tempMove = new ActorMoveDirection()
        {
            userIndex  = DataController.instance.MyLocateIndex,
            frameIndex = GameManager.instance.frameIndex,
            position   = pos,
            direction  = move,
            speed      = _speed
        };

        if (
            lastMove == null ||
            move.x != lastMove.direction.x ||
            move.y != lastMove.direction.y ||
            move.z != lastMove.direction.z ||
            _speed != lastMove.speed
            )
        {
            lastMove = tempMove;
            byte[] sendData = SerializeHelper.Serialize <ActorMoveDirection>(tempMove);
            //SocketManager.instance.SendSave((byte)MessageConvention.moveDirection, sendData, false);
            UDPManager.instance.SendSave((byte)MessageConvention.moveDirection, sendData);
            if (move.y > 0)
            {
                Debug.LogError("已发送跳跃");
            }
        }
    }
Exemplo n.º 4
0
    public byte[] SelectMessage(byte[] data, EndPoint endPoint)
    {
        string strPoint = endPoint.ToString();

        byte[]       newBuffer = null;
        MessageXieYi xieyi     = MessageXieYi.FromBytes(data);

        if (xieyi == null)
        {
            return(newBuffer);
        }

        byte[]             tempMessageContent = xieyi.MessageContent;
        ActorMoveDirection moveDirection      = null;
        SingleRoom         room  = null;
        UDPLogin           login = null;

        //该处RoomList没有加锁
        if (ServerDataManager.instance.allRoom.RoomList.ContainsKey(allUDPs[strPoint].roomID))
        {
            room = ServerDataManager.instance.allRoom.RoomList[allUDPs[strPoint].roomID];
        }

        switch ((MessageConvention)xieyi.XieYiFirstFlag)
        {
        case MessageConvention.setUDP:
            login       = SerializeHelper.Deserialize <UDPLogin>(tempMessageContent);
            login.login = strPoint;
            allUDPs[strPoint].roomID = login.roomID;
            allUDPs[strPoint].unique = login.unique;
            Log4Debug("UDP login 房间号:" + login.roomID);
            newBuffer = SerializeHelper.Serialize <UDPLogin>(login);
            break;

        case MessageConvention.moveDirection:
            moveDirection = SerializeHelper.Deserialize <ActorMoveDirection>(tempMessageContent);
            if (room.ActorList[moveDirection.userIndex].CurState != RoomActorState.Dead)
            {
                //Log4Debug("将历史帧:" + moveDirection.frameIndex + "保存到" + (moveDirection.frameIndex + room.RoomInfo.frameInterval) + "/" + room.RoomInfo.FrameIndex);
                room.SetRecondFrame(xieyi.ToBytes());
                //Log4Debug("站位:" + moveDirection.userIndex + " 更新了方向:" + "["
                //    + moveDirection.direction.x + ","
                //    + moveDirection.direction.y + ","
                //    + moveDirection.direction.z + "]"
                //    + "/速度:" + moveDirection.speed);
            }
            else
            {
                Log4Debug("死亡用户不更新移动。");
            }
            break;

        case MessageConvention.rotateDirection:
            ActorRotateDirection netRotation = SerializeHelper.Deserialize <ActorRotateDirection>(tempMessageContent);
            if (room.ActorList[netRotation.userIndex].CurState != RoomActorState.Dead)
            {
                room.SetRecondFrame(xieyi.ToBytes());
                //Log4Debug("站位:" + netRotation.userIndex + " 更新了旋转:" + netRotation.rotateY);
            }
            else
            {
                Log4Debug("死亡用户不更新旋转。");
            }
            break;

        case MessageConvention.jump:
            ActorJump netJump = SerializeHelper.Deserialize <ActorJump>(tempMessageContent);
            if (room.ActorList[netJump.userIndex].CurState != RoomActorState.Dead)
            {
                room.SetRecondFrame(xieyi.ToBytes());
            }
            else
            {
                Log4Debug("死亡用户不更新跳跃。");
            }
            break;

        case MessageConvention.shootBullet:
            ShootInfo shootInfo = SerializeHelper.Deserialize <ShootInfo>(tempMessageContent);
            if (room.ActorList[shootInfo.userIndex].CurState != RoomActorState.Dead)
            {
                room.SetRecondFrame(xieyi.ToBytes());
                //Log4Debug("站位:" + netRotation.userIndex + " 更新了旋转:" + netRotation.rotateY);
            }
            else
            {
                Log4Debug("死亡用户不更新射击。");
            }
            break;

        case MessageConvention.bulletInfo:
            //
            BulletInfo bulletInfo = SerializeHelper.Deserialize <BulletInfo>(xieyi.MessageContent);
            room.UpdateBulletInfo(bulletInfo);    //更新
            //room.SetRecondFrame(xieyi.ToBytes());
            break;

        default:
            Log4Debug("检查协议->" + (MessageConvention)xieyi.XieYiFirstFlag);
            break;
        }

        byte[] sendBuffer = null;
        if (newBuffer != null)//用户需要服务器返回值给自己的话
        {
            xieyi      = new MessageXieYi(xieyi.XieYiFirstFlag, xieyi.XieYiSecondFlag, newBuffer);
            sendBuffer = xieyi.ToBytes();
        }
        return(sendBuffer);
    }
Exemplo n.º 5
0
    /// <summary>
    /// tcp
    /// </summary>
    /// <param name="xieyi"></param>
    /// <param name="userToken"></param>
    /// <returns></returns>
    public byte[] SelectMessage(MessageXieYi xieyi, AsyncUserToken userToken)
    {
        JObject json = null;

        byte[]             newBuffer          = null;
        byte[]             tempMessageContent = xieyi.MessageContent;
        int                roomID             = userToken.userInfo.RoomID;
        SingleRoom         room            = GetSingleRoomByID(roomID);
        Register           login           = null;
        RoomActorUpdate    roomActorUpdate = null;
        ActorMoveDirection moveDirection   = null;

        if (userToken == null)
        {
            Log4Debug("该用户已被清理,不处理接收数据。");
            return(newBuffer);
        }
        if (allRoom.RoomList.ContainsKey(userToken.userInfo.RoomID))
        {
            room = allRoom.RoomList[userToken.userInfo.RoomID];
        }

        //Log4Debug("/" + xieyi.MessageContentLength);
        //try
        {
            switch ((MessageConvention)xieyi.XieYiFirstFlag)
            {
            case MessageConvention.login:
                login     = SerializeHelper.Deserialize <Register>(tempMessageContent);
                newBuffer = Login(login, userToken);
                break;

            case MessageConvention.getHeartBeatTime:
                HeartbeatTime hbTime = new HeartbeatTime()
                {
                    time = AsyncIOCPServer.HeartbeatSecondTime
                };
                newBuffer = SerializeHelper.Serialize <HeartbeatTime>(hbTime);
                break;

            case MessageConvention.reConnectCheck:
                //检查是否是掉线用户
                ReconnctInfo rcInfo = new ReconnctInfo();
                if (OffLineRooms.ContainsKey(userToken.userInfo.Register.userID))
                {
                    int offRoomID = OffLineRooms[userToken.userInfo.Register.userID];
                    userToken.userInfo.RoomID = offRoomID;
                    allRoom.RoomList[offRoomID].ReConnect(userToken);

                    int removeCount = 0;
                    OffLineRooms.TryRemove(userToken.userInfo.Register.userID, out removeCount);
                    rcInfo.isReconnect = true;
                }
                else
                {
                    rcInfo.isReconnect = false;
                }
                newBuffer = SerializeHelper.Serialize <ReconnctInfo>(rcInfo);
                break;

            case MessageConvention.reConnectIndex:
                room.GetReConnectFrameData(userToken.userInfo.UniqueID);
                break;

            case MessageConvention.heartBeat:
                //心跳检测客户端传递服务器时间
                //Log4Debug("接收到 " + userToken.userInfo.Register.name + " 的心跳检测");
                //newBuffer = SerializeHelper.DateTimeToBytes(DateTime.Now);
                //newBuffer = SerializeHelper.Serialize<HeartbeatTime>(new HeartbeatTime() { time = AsyncIOCPServer.HeartbeatSecondTime });
                break;

            case MessageConvention.updateName:
                string updateName = SerializeHelper.ConvertToString(tempMessageContent);
                Log4Debug("修改人物信息:" + updateName);
                newBuffer = UpdateName(userToken, updateName);
                break;

            case MessageConvention.createRoom:
                json      = JObject.Parse(SerializeHelper.ConvertToString(xieyi.MessageContent));
                newBuffer = allRoom.CreateNewRoom(json, userToken);
                break;

            case MessageConvention.joinRoom:
                json      = JObject.Parse(SerializeHelper.ConvertToString(xieyi.MessageContent));
                newBuffer = allRoom.JoinRoom(json, userToken);
                break;

            case MessageConvention.updateRoom:
                Log4Debug("更新房间");
                json      = JObject.Parse(SerializeHelper.ConvertToString(xieyi.MessageContent));
                newBuffer = allRoom.UpdateRoom(json);
                break;

            case MessageConvention.getRoomInfo:
                newBuffer = SerializeHelper.Serialize <RoomInfo>(room.RoomInfo);
                break;

            case MessageConvention.getRoommateInfo:
                Log4Debug("请求房间人物信息:" + SerializeHelper.ConvertToString(tempMessageContent));
                roomActorUpdate = new RoomActorUpdate();
                roomActorUpdate.SetSendInfo(SerializeHelper.ConvertToString(tempMessageContent));
                //
                newBuffer = SerializeHelper.Serialize <List <RoomActor> >(new List <RoomActor>(room.ActorList.Values));
                break;

            case MessageConvention.quitRoom:
                QuitInfo qInfo = SerializeHelper.Deserialize <QuitInfo>(tempMessageContent);
                if (room != null)
                {
                    room.CheckQuit(userToken, qInfo);
                }
                break;

            case MessageConvention.updateActorAnimation:
                ActorNetAnimation netAnimation = new ActorNetAnimation();
                netAnimation.SetSendInfo(SerializeHelper.ConvertToString(tempMessageContent));
                //Log4Debug("set->" + netAnimation.animationIndex + "");
                room.UpdateAnimation(netAnimation);    //更新
                break;

            case MessageConvention.updateActorState:
                roomActorUpdate = new RoomActorUpdate();
                roomActorUpdate.SetSendInfo(SerializeHelper.ConvertToString(tempMessageContent));
                Log4Debug("站位:" + userToken.userInfo.UniqueID + "/" + userToken.userInfo.Register.name
                          + "用户站位:" + roomActorUpdate.userIndex +
                          "请求修改状态为->" + (RoomActorState)int.Parse(roomActorUpdate.update));
                //
                room.UpdateState(roomActorUpdate);    //更新
                break;

            case MessageConvention.prepareLocalModel:
                roomActorUpdate = new RoomActorUpdate();
                roomActorUpdate.SetSendInfo(SerializeHelper.ConvertToString(tempMessageContent));
                //
                room.UpdatePrepare(roomActorUpdate, userToken);    //更新
                break;

            case MessageConvention.getPreGameData:

                break;

            case MessageConvention.startGaming:

                break;

            case MessageConvention.endGaming:

                break;

            case MessageConvention.frameData:
                FrameInfo frame = SerializeHelper.Deserialize <FrameInfo>(tempMessageContent);
                newBuffer = room.GetBoardFrame(frame.frameIndex);
                //Log4Debug("用户" + userToken.userInfo.Register.name + "/请求帧数据:" + frame.frameIndex + "/" + room.RoomInfo.FrameIndex + "数据总长:" + newBuffer.Length);
                break;

            //case MessageConvention.setUDP:
            //    UDPLogin loginUDP = SerializeHelper.Deserialize<UDPLogin>(tempMessageContent);
            //    Log4Debug("收到登录UDP账号:" + loginUDP.login);
            //    room.UpdateUDP(userToken.userInfo.UniqueID, loginUDP);
            //    break;
            default:
                Log4Debug("TCP是否判断该协议:" + (MessageConvention)xieyi.XieYiFirstFlag);
                break;
            }
        }
        //catch (Exception error)
        //{
        //    Log4Debug("处理逻辑错误:" + error.Message);
        //}
        //  创建一个发送缓冲区。
        byte[] sendBuffer = null;
        if (newBuffer != null)//用户需要服务器返回值给自己的话
        {
            xieyi      = new MessageXieYi(xieyi.XieYiFirstFlag, xieyi.XieYiSecondFlag, newBuffer);
            sendBuffer = xieyi.ToBytes();
        }
        return(sendBuffer);
    }
Exemplo n.º 6
0
    /// <summary>
    /// 解析所有帧操作指令并在本客户端复现
    /// </summary>
    /// <param name="xieyi"></param>
    private void SelectFrameInfo(MessageXieYi xieyi)
    {
        byte[]          tempMessageContent = xieyi.MessageContent;
        string          messageInfo        = "";
        CharacterCommon member             = null;
        BuffInfo        buffInfo           = null;

        switch ((MessageConvention)xieyi.XieYiFirstFlag)
        {
        case MessageConvention.moveDirection:
            ActorMoveDirection moveDir = SerializeHelper.Deserialize <ActorMoveDirection>(tempMessageContent);
            member = GameManager.instance.memberGroup[moveDir.userIndex];
            //用上次移动操作到这次操作的时间,算出当前位置,并移动到该点
            //member.transform.DOMove(SerializeHelper.BackVector(moveDir.position), DataController.FrameFixedTime);
            //member.SetPosition(SerializeHelper.BackVector(moveDir.position));
            member.SetNetDirection(moveDir);
            break;

        case MessageConvention.rotateDirection:
            ActorRotateDirection rotateDir = SerializeHelper.Deserialize <ActorRotateDirection>(tempMessageContent);
            //判断用户
            //Debug.LogError("玩家接收方向移动:" + messageInfo);
            member = GameManager.instance.memberGroup[rotateDir.userIndex];
            member.SetNetDirection(rotateDir);
            break;

        case MessageConvention.jump:
            ActorJump netJump = SerializeHelper.Deserialize <ActorJump>(tempMessageContent);
            member = GameManager.instance.memberGroup[netJump.userIndex];
            member.SetJump();
            break;

        case MessageConvention.shootBullet:
            ShootInfo shootInfo = SerializeHelper.Deserialize <ShootInfo>(tempMessageContent);
            member = GameManager.instance.memberGroup[shootInfo.userIndex];
            if (shootInfo.userIndex != DataController.instance.MyLocateIndex)    //自身在收到服务器消息之前已旋转
            {
                member.ShowBullet(shootInfo);
            }
            break;

        case MessageConvention.bulletInfo:
            BulletInfo bulletInfo = SerializeHelper.Deserialize <BulletInfo>(tempMessageContent);
            switch (bulletInfo.shootTag)
            {
            case ShootTag.Box:
                BoxManager.instance.SetBulletInfo(bulletInfo);
                break;

            case ShootTag.Character:
                UpdateMemberShoot(xieyi);
                break;

            case ShootTag.Wall:
                Debug.Log("射中Wall:" + bulletInfo.shootInfo);
                break;

            case ShootTag.Buff:
                Debug.LogError("Buff不算是子弹");
                break;
            }

            break;

        case MessageConvention.createBuff:
            buffInfo = SerializeHelper.Deserialize <BuffInfo>(tempMessageContent);
            GameObject obj = PoolManager.instance.GetPoolObjByType(PreLoadType.Buff, GameManager.instance.transBuff);
            obj.transform.position = BoxManager.instance.GetBoxInfoByIndex(buffInfo.boxIndex).transform.position;
            MagicBuff buff = obj.GetComponent <MagicBuff>();
            buff.Init(buffInfo);
            break;

        case MessageConvention.getBuff:
            buffInfo = SerializeHelper.Deserialize <BuffInfo>(tempMessageContent);
            BoxManager.instance.SetBuffData(buffInfo);
            break;
        }
    }