Exemplo n.º 1
0
    /*
     * 进入房间房主建立tcp服务器,成员连接服务器
     * 成员将自身player加入playerList发送给服务器
     * 各成员本地修正玩家数目,房主通过maneger更新房间人数
     * 成员修改准备状态/离开房间时发送playList
     * */
    protected override void OnInit()
    {
        contentPane       = UIPackage.CreateObject("BattleCity8102", "Room_Window").asCom;
        roomInfoComponent = contentPane.GetChild("frame").asCom.GetChild("roomInfoFrame").asCom;
        roomInfoComponent.GetChild("hostTextField").asTextField.text = MainUI.currentRoom.roomHost;
        roomInfoComponent.GetChild("mapTextField").asTextField.text  = MainUI.currentRoom.roomMap;
        roomInfoComponent.GetChild("modeTextField").asTextField.text = MainUI.currentRoom.roomMode;

        inputTextField = contentPane.GetChild("frame").asCom.GetChild("inputTextField").asTextInput;
        message        = new List <string>();
        messageList    = contentPane.GetChild("frame").asCom.GetChild("messageList").asList;
        messageList.SetVirtual();
        messageList.itemRenderer = RenderMessageList;
        messageList.lineCount    = 50;

        //房主创建聊天服务器
        Debug.Log("isHost:" + MainUI.player.isHost);
        if (MainUI.player.isHost)
        {
            ChatRoomServer.StartChatServer();
        }
        try
        {
            ChatRoom.ConnectToServer(MainUI.currentRoom.hostIP);
        }
        catch (System.Exception e)
        {
            Debug.Log(e.Message);
            Dispose();
            return;
        }

        //在此处将本地玩家添加进MainUI.currentRoom.currentPlayer
        using (MemoryStream ms = new MemoryStream())
        {
            BinaryFormatter bf = new BinaryFormatter();
            MainUI.currentRoom.currentPlayer.Add(MainUI.player);
            bf.Serialize(ms, MainUI.currentRoom.currentPlayer);
            byte[] data = new byte[ms.Length];
            System.Buffer.BlockCopy(ms.GetBuffer(), 0, data, 0, (int)ms.Length);
            ChatRoom.SendMessage(data);
        }
        roomInfoComponent.GetChild("playerNumTextField").asTextField.text = MainUI.currentRoom.currentPlayer.Count + "/" + MainUI.currentRoom.limitNum;
        ChatRoom.messageDelegate += MessageHandler;

        playerList = contentPane.GetChild("frame").asCom.GetChild("playerList").asList;
        playerList.scrollPane.touchEffect = false;
        playerList.SetVirtual();
        playerList.itemRenderer = RenderListItem;
        playerList.numItems     = MainUI.currentRoom.limitNum;

        //发送按钮按下,发送玩家名+inputTextField.text
        sendMessageButton = contentPane.GetChild("frame").asCom.GetChild("sendMessageButton").asButton;
        sendMessageButton.onClick.Add(() =>
        {
            if (inputTextField.text.Equals(""))
            {
                return;
            }
            byte[] data = Encoding.UTF8.GetBytes((MainUI.player.name + ":" + inputTextField.text));
            ChatRoom.SendMessage(data);
            inputTextField.text = "";
        });

        //准备按钮按下,发送playerList
        readyButton = contentPane.GetChild("frame").asCom.GetChild("readyButton").asButton;
        readyButton.onClick.Add(() =>
        {
            if (contentPane.GetChild("frame").asCom.GetChild("readyButton").asButton.selected)
            {
                MainUI.player.isReady = true;
            }
            else
            {
                MainUI.player.isReady = false;
            }
            foreach (var player in MainUI.currentRoom.currentPlayer)
            {
                if (player == MainUI.player)
                {
                    player.isReady = MainUI.player.isReady;
                }
            }
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, MainUI.currentRoom.currentPlayer);
                byte[] data = new byte[ms.Length];
                System.Array.Copy(ms.GetBuffer(), 0, data, 0, ms.Length);
                ChatRoom.SendMessage(data);
            }
        });

        //开始按钮按下,游戏开始
        startButton = contentPane.GetChild("frame").asCom.GetChild("startButton").asButton;
        startButton.onClick.Add(() =>
        {
            //判断准备状态,载入游戏
            foreach (var player in MainUI.currentRoom.currentPlayer)
            {
                // 跳过对房主的判断
                if (player.isHost)
                {
                    continue;
                }
                // 有玩家未准备,退出方法
                if (!player.isReady)
                {
                    startButton.selected = false;
                    return;
                }
            }
            // 异步加载战斗场景
            this.Dispose();
            LoadNewScene();
        });

        if (MainUI.player.isHost)
        {
            readyButton.visible = false;
        }
        else
        {
            startButton.visible = false;
        }
    }