private IEnumerator startCountDown()
        {
            for (second = 2; second > 0; second--)
            {
                if (UDPChat.instance.gameState != "stop")
                {
                    instance.countDownText.gameObject.SetActive(true);
                    instance.countDownText.SetText(message + second.ToString());
                    yield return(new WaitForSeconds(1));
                }
                else
                {
                    instance.countDownText.SetText("");
                    instance.countDownText.gameObject.SetActive(false);
                    second = 5;
                    break;
                }

                // Load "GameScene" when countdown ends
                if (second == 1)
                {
                    LobbyList.addRolesToPlayerList();
                    DontDestroyOnLoad(udp);
                    SceneManager.LoadScene("GameScene", LoadSceneMode.Single);
                }
            }
        }
        //// Role Buttons Function
        public void roleButtonFunc(string roleName)
        {
            object[] roleInfo = new object[3] {
                ProtocolLabels.roleSelected,
                UDPChat.clientNo,
                roleName
            };

            string msg = MessageMaker.makeMessage(roleInfo);

            UDPChat.instance.roleName = roleName;

            if (UDPChat.instance.isServer)
            {
                // Server must set roleName to lobby list by itself
                LobbyList.setRolePref(roleName);

                UDPChat.instance.Send(msg);
            }
            else
            {
                UDPChat.instance.connection.Send(msg,
                                                 new IPEndPoint(UDPChat.instance.serverIp, Globals.port));
            }
        }
        public void exitButtonFunc()
        {
            // Set UDP settings as default
            if (!UDPChat.instance.isServer)
            {
                UDPChat.instance.clientDisconnected();
            }

            UDPChat.instance.connection.Close();
            resetUDP();

            // Set UI elements to show Main Menu
            LobbyList.clearLobbyList();

            // Set everything default state
            UDPChat.instance.readyStatement = "N";
            stopGame();

            roleButtonSection.SetActive(true);
            lockButton.transform.GetChild(0).gameObject.GetComponent <Text>().text = "Lock";

            lobbyUI.SetActive(false);
            startButton.SetActive(false);
            mainUI.SetActive(true);
        }
Beispiel #4
0
        void Start()
        {
            instance = this;

            for (int i = 0; i < playerSections.Length; i++)
            {
                textList.Add(playerSections[i].playerText);
                imageList.Add(playerSections[i].roleImage);

                if (SceneManager.GetActiveScene().name == "MainScene")
                {
                    stateList.Add(playerSections[i].stateText);
                }
            }
        }
Beispiel #5
0
        public void Awake()
        {
            instance = this;
            username = MainSceneEventHandler.instance.usernameInput.text;

            if (serverIp == null)
            {
                this.isServer = true;
                connection    = new UdpConnectedClient();

                LobbyList.setPlayerName(username);
                LobbyList.setReadyStatement("N");
            }
            else
            {
                connection = new UdpConnectedClient(ip: serverIp);
                AddClient(new IPEndPoint(serverIp, Globals.port));
            }
        }
        internal void lockPref()
        {
            UDPChat.instance.readyStatement = "R"; // "R" means "Ready"
            LobbyList.setReadyStatement("R", UDPChat.clientNo);

            object[] readyInfo = new object[3] {
                ProtocolLabels.clientReady,
                UDPChat.clientNo,
                "R"
            };

            string readyMsg = MessageMaker.makeMessage(readyInfo);

            UDPChat.instance.Send(readyMsg);

            roleButtonSection.SetActive(false);

            lockButton.transform.GetChild(0).gameObject.GetComponent <Text>().text = "Unlock";
        }
        internal void unlockPref()
        {
            const string NOT_READY = "N";

            UDPChat.instance.readyStatement = NOT_READY;
            LobbyList.setReadyStatement(NOT_READY, UDPChat.clientNo);

            object[] unreadyInfo = new object[3] {
                ProtocolLabels.clientReady,
                UDPChat.clientNo,
                NOT_READY
            };

            string unreadyMsg = MessageMaker.makeMessage(unreadyInfo);

            UDPChat.instance.Send(unreadyMsg);

            roleButtonSection.SetActive(true);

            lockButton.transform.GetChild(0).gameObject.GetComponent <Text>().text = "Lock";
        }
Beispiel #8
0
        public static void Handle(string message, IPEndPoint ipEndpoint)
        {
            string[] sections    = message.Split(';');
            string   messageType = sections[0];

            Debug.Log("Handler: " + message);

            if (UDPChat.instance.isServer)
            {
                switch (messageType)
                {
                case ProtocolLabels.joinRequest:

                    if (UDPChat.instance.playerList.ToArray().Length == 4 ||
                        UDPChat.instance.gameState == "start")
                    {
                        object[] rejectMsg = new object[2] {
                            ProtocolLabels.joinRequest, "rejected"
                        };

                        string rejMsg = MessageMaker.makeMessage(rejectMsg);
                        UDPChat.instance.connection.Send(rejMsg, ipEndpoint);
                    }
                    else
                    {
                        // Add player informations to lobby
                        LobbyList.setPlayerName(sections[1]);

                        int newClientNo = UDPChat.instance.playerList.IndexOf(sections[1]);
                        LobbyList.setReadyStatement("N", newClientNo);

                        // give info about itself to client to update it
                        // and set its clientInfo

                        object[] nameMsg = new object[5] {
                            ProtocolLabels.clientInfo,
                            UDPChat.instance.username,
                            newClientNo,
                            UDPChat.instance.roleName,
                            UDPChat.instance.readyStatement
                        };

                        string infoMsg = MessageMaker.makeMessage(nameMsg);
                        UDPChat.instance.connection.Send(infoMsg, ipEndpoint);
                    }

                    break;

                case ProtocolLabels.roleSelected:

                    LobbyList.setRolePref(sections[2], System.Int32.Parse(sections[1]));

                    UDPChat.instance.Send(message);
                    break;

                case ProtocolLabels.clientReady:

                    LobbyList.setReadyStatement(sections[2], System.Int32.Parse(sections[1]));
                    break;

                case ProtocolLabels.clientLeft:

                    int leftClientNo     = System.Int32.Parse(sections[1]);
                    int playerListLength = UDPChat.instance.playerList.ToArray().Length;

                    LobbyList.refreshLobbyList(leftClientNo, playerListLength);

                    UDPChat.RemoveClient(ipEndpoint);

                    MainSceneEventHandler.stopGame();

                    // send this message to inform other clients
                    object[] exitMsgParts = new object[3] {
                        ProtocolLabels.clientLeft, sections[1],
                        playerListLength
                    };

                    string exitMsg = MessageMaker.makeMessage(exitMsgParts);

                    UDPChat.instance.Send(exitMsg);
                    break;

                case ProtocolLabels.playerMove:
                    float[] coordinates = new float[3] {
                        float.Parse(sections[2]),
                        float.Parse(sections[3]),
                        float.Parse(sections[4])
                    };

                    GameSceneEventHandler.movePlayerObj(System.Int32.Parse(sections[1]), coordinates);
                    break;

                case ProtocolLabels.playerRot:

                    GameSceneEventHandler.rotatePlayerObj(System.Int32.Parse(sections[1]),
                                                          float.Parse(sections[2]),
                                                          float.Parse(sections[3]));
                    break;

                default:
                    break;
                }
            }
            else
            {
                switch (messageType)
                {
                case ProtocolLabels.joinRequest:
                    // means you cannot join lobby, set UI normal
                    MainSceneEventHandler.instance.exitButtonFunc();
                    break;

                case ProtocolLabels.clientInfo:

                    if (UDPChat.clientNo == 0)
                    {
                        //add server
                        LobbyList.setPlayerName(sections[1]);
                        LobbyList.setRolePref(sections[3]);
                        LobbyList.setReadyStatement(sections[4]);

                        //client settings
                        UDPChat.clientNo = System.Int32.Parse(sections[2]);

                        LobbyList.setPlayerName(UDPChat.instance.username,
                                                UDPChat.clientNo);

                        LobbyList.setReadyStatement("N", UDPChat.clientNo);

                        object[] clientMsg = new object[3] {
                            ProtocolLabels.newClient,
                            UDPChat.instance.username,
                            UDPChat.clientNo
                        };

                        string othersMsg = MessageMaker.makeMessage(clientMsg);
                        UDPChat.instance.Send(othersMsg);
                    }
                    else
                    {
                        LobbyList.setPlayerName(sections[1], System.Int32.Parse(sections[2]));
                        LobbyList.setRolePref(sections[3], System.Int32.Parse(sections[2]));
                        LobbyList.setReadyStatement(sections[4], System.Int32.Parse(sections[2]));
                    }

                    break;

                case ProtocolLabels.newClient:

                    LobbyList.setPlayerName(sections[1], System.Int32.Parse(sections[2]));

                    object[] nameMsg = new object[5] {
                        ProtocolLabels.clientInfo,
                        UDPChat.instance.username,
                        UDPChat.clientNo,
                        UDPChat.instance.roleName,
                        UDPChat.instance.readyStatement
                    };

                    string infoMsg = MessageMaker.makeMessage(nameMsg);
                    UDPChat.instance.connection.Send(infoMsg, ipEndpoint);
                    break;

                case ProtocolLabels.roleSelected:

                    LobbyList.setRolePref(sections[2], System.Int32.Parse(sections[1]));
                    break;

                case ProtocolLabels.clientReady:

                    LobbyList.setReadyStatement(sections[2], System.Int32.Parse(sections[1]));
                    break;

                case ProtocolLabels.clientLeft:

                    int leftClientNo = System.Int32.Parse(sections[1]);

                    LobbyList.refreshLobbyList(leftClientNo, System.Int32.Parse(sections[2]));

                    MainSceneEventHandler.stopGame();     // To stop countdown
                    break;

                case ProtocolLabels.gameAction:
                    UDPChat.instance.gameState = sections[1];

                    if (UDPChat.instance.gameState == "start")
                    {
                        MainSceneEventHandler.startGame();
                    }
                    else
                    {
                        MainSceneEventHandler.stopGame();
                    }

                    break;

                case ProtocolLabels.playerMove:
                    float[] coordinates = new float[3] {
                        float.Parse(sections[2]),
                        float.Parse(sections[3]),
                        float.Parse(sections[4])
                    };

                    GameSceneEventHandler.movePlayerObj(System.Int32.Parse(sections[1]), coordinates);

                    break;

                case ProtocolLabels.playerRot:

                    GameSceneEventHandler.rotatePlayerObj(System.Int32.Parse(sections[1]),
                                                          float.Parse(sections[2]),
                                                          float.Parse(sections[3]));
                    break;

                default:
                    break;
                }
            }
        }