Example #1
0
 void Awake()
 {
     SettingsScript.SetDefaultSettingsIfNotSetted();
     PhotonNetwork.AutomaticallySyncScene = true;
 }
Example #2
0
        private void Update()
        {
            if (LoadSaveScript.loadLevel != "")
            {
                LoadSaveScript.loadLevelTimer -= Time.fixedDeltaTime;
                if (LoadSaveScript.loadLevelTimer <= 0)
                {
                    musicSource.Stop();
                    if (singleplayer)
                    {
                        SceneManager.LoadSceneAsync(LoadSaveScript.loadLevel);
                    }
                    else
                    {
                        PhotonNetwork.LoadLevel(LoadSaveScript.loadLevel);
                    }
                    LoadSaveScript.loadLevel = "";
                }
                return;
            }

            if (timerActive && !gameStarted)
            {
                timerToStart     -= Time.deltaTime;
                sendMessageTimer -= Time.deltaTime;
                if (sendMessageTimer <= 0)
                {
                    AddMessasgeToChat(String.Format("Game will start in {0:F0}", timerToStart));
                    sendMessageTimer = 1.0f;
                }
                if (timerToStart <= 0)
                {
                    AddMessasgeToChat(String.Format("Game started..."));
                    gameStarted = true;

                    if (PhotonNetwork.InRoom)
                    {
                        PhotonNetwork.CurrentRoom.IsOpen    = false;
                        PhotonNetwork.CurrentRoom.IsVisible = false;
                    }

                    UI.document.Run("CreateLoadingScreen", "Level initialization");
                    LoadSaveScript.loadLevel      = "Levels/Map1";
                    LoadSaveScript.loadLevelTimer = 0.7f;
                    return;
                }
            }
            string className = "";
            var    element   = PowerUI.CameraPointer.All[0].ActiveOver;

            if (element != null)
            {
                className = element.className;
            }

            if (UnityEngine.Input.GetMouseButtonUp(0) || UnityEngine.Input.GetKeyDown(KeyCode.Return) || UnityEngine.Input.GetKeyDown(KeyCode.Escape))
            {
                bool changed = SettingsScript.ChangeTabOrSaveSettings(className, windowSettings: "settingsContainer", saveClassName: "saveSettings", errorClassName: "messageSettings", mainMenu: true);
                if (changed)
                {
                    musicSource.volume = PlayerPrefs.GetFloat("musicMenuVolume");
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    return;
                }

                if (className.Contains("singleplayer") && UnityEngine.Input.GetMouseButtonUp(0))
                {
                    singleplayer = true;
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    GameInfo.playerSpectate = false;
                    GameInfo.NPCList.Clear();
                    UI.document.innerHTML = RoomHTMLFile.text;
                    GameInfo.SetReady(false);
                    GameInfo.mapSize = GameInfo.defaultMapSize;
                    GameInfo.mapSeed = UnityEngine.Random.Range(0, 1000);
                    UpdateRoomView();
                    return;
                }
                if (className.Contains("load") && UnityEngine.Input.GetMouseButtonUp(0))
                {
                    singleplayer = true;
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    UI.document.innerHTML = LoadingHTMLFile.text;
                    LoadSaveScript.UpdateSaveList();

                    foreach (Dom.Element loadingButton in UI.document.getElementsByClassName("loadingButton"))
                    {
                        loadingButton.onclick = GameMenuBehavior.LoadFileClick;
                    }
                    return;
                }
                if (className.Contains("multiplayer") && UnityEngine.Input.GetMouseButtonUp(0))
                {
                    singleplayer = false;
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    if (PlayerPrefs.GetString("username") == "")
                    {
                        CreateConnectDialog();
                    }
                    else
                    {
                        Connect(PlayerPrefs.GetString("username"));
                    }
                }
                if (className.Contains("menuElement settings") && UnityEngine.Input.GetMouseButtonUp(0))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    UI.document.innerHTML = SettingsHTMLFile.text;
                    SettingsScript.CreateSettings("settingsContainer", true);
                    return;
                }
                if (className.Contains("exit") && UnityEngine.Input.GetMouseButtonUp(0))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    Application.Quit();
                    return;
                }
                else if ((className.Contains("ConnectDialog") && UnityEngine.Input.GetMouseButtonUp(0)) ||
                         UI.document.getElementsByClassName("ConnectDialog").length > 0 && UnityEngine.Input.GetKeyDown(KeyCode.Return))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    string error = SettingsScript.SaveSettings();
                    if (error != "")
                    {
                        UI.document.getElementsByClassName("error")[0].innerText = error;
                    }
                    else
                    {
                        DeleteDialog();
                        Connect(PlayerPrefs.GetString("username"));
                    }
                }
                else if (className.Contains("backToMenu") && UI.document.getElementsByClassName("settingsBlock").length > 0 && UnityEngine.Input.GetMouseButtonUp(0) || UnityEngine.Input.GetKeyDown(KeyCode.Escape))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    UI.document.innerHTML = MenuHTMLFile.text;
                    return;
                }

                // Singleplayer
                else if (singleplayer && ((className.Contains("backToMenu") && UnityEngine.Input.GetMouseButtonUp(0)) || UnityEngine.Input.GetKeyDown(KeyCode.Escape)))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    gameStarted           = false;
                    UI.document.innerHTML = MenuHTMLFile.text;
                }
                else if (singleplayer && (className.Contains("setReady") && UnityEngine.Input.GetMouseButtonUp(0)))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    GameInfo.SetReady(!GameInfo.isPlayerReady);
                    UpdateRoomView();
                }

                else if (((className.Contains("backToMenu") && UnityEngine.Input.GetMouseButtonUp(0)) || UnityEngine.Input.GetKeyDown(KeyCode.Escape)) && !PhotonNetwork.InRoom)
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    PhotonNetwork.Disconnect();
                    CreateMessage("Disconnecting...");
                }
                else if (className.Contains("refreshMenu") && UnityEngine.Input.GetMouseButtonUp(0))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    UpdateLobbyListView();
                }
                else if (className.Contains("restoreSettings") && UnityEngine.Input.GetMouseButtonUp(0))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    SettingsScript.SetDefaultSettingsIfNotSetted(force: true);
                    SettingsScript.CreateSettings("settingsContainer", true);
                    musicSource.volume = PlayerPrefs.GetFloat("musicMenuVolume");
                    var messageDiv = UI.document.getElementsByClassName("messageSettings")[0];
                    messageDiv.innerHTML   = "Default settings are set!";
                    messageDiv.style.color = "green";
                    return;
                }

                else if (className.Contains("createRoomDialog") && UnityEngine.Input.GetMouseButtonUp(0))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    CreateRoomMenu();
                }

                else if (className.Contains("roomInLobby") && UnityEngine.Input.GetMouseButtonUp(0))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    CreateMessage(String.Format("Connecting to \"{0}\"...", element.innerText));
                    PhotonNetwork.JoinRoom(element.innerText);
                }

                else if (className.Contains("deleteDialog") && UnityEngine.Input.GetMouseButtonUp(0))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    DeleteDialog();
                }

                else if ((className.Contains("RoomDialog") && UnityEngine.Input.GetMouseButtonUp(0)) || UI.document.getElementsByClassName("inputMaxplayers").length > 0 && UnityEngine.Input.GetKeyDown(KeyCode.Return))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    string roomName   = UI.document.getElementsByClassName("inputName")[0].innerText;
                    string maxplayers = UI.document.getElementsByClassName("inputMaxplayers")[0].innerText;
                    if (!Regex.Match(maxplayers, @"^[2-9][0-9]*$", RegexOptions.IgnoreCase).Success)
                    {
                        UI.document.getElementsByClassName("error")[0].innerText = "Wrong maxplayers value";
                    }
                    else if (roomName.Length <= 0)
                    {
                        UI.document.getElementsByClassName("error")[0].innerText = "Empty room name";
                    }
                    else
                    {
                        DeleteDialog();
                        CreateMessage("Creating...");
                        PhotonNetwork.JoinOrCreateRoom(roomName, new RoomOptions {
                            MaxPlayers = (byte)int.Parse(maxplayers)
                        }, TypedLobby.Default);
                    }
                }

                else if ((PhotonNetwork.InRoom || singleplayer) && ((className.Contains("backToMenu") && UnityEngine.Input.GetMouseButtonUp(0)) || UnityEngine.Input.GetKeyDown(KeyCode.Escape)))
                {
                    timerActive = false;
                    PhotonNetwork.LeaveRoom();
                    CreateMessage("Leaving...");
                }
                else if (PhotonNetwork.InRoom && (UnityEngine.Input.GetKeyDown(KeyCode.Return) || (className.Contains("chatSend") && UnityEngine.Input.GetMouseButtonUp(0))))
                {
                    string chatInput = UI.document.getElementsByClassName("chatInput")[0].getAttribute("value");
                    if (chatInput != null && chatInput.Length > 0)
                    {
                        UI.document.getElementsByClassName("chatInput")[0].setAttribute("value", "");
                        GetComponent <PhotonView>().RPC(
                            "AddMessasgeToChat", PhotonTargets.All, new string[1] {
                            String.Format("{0}: {1}", PhotonNetwork.NickName, chatInput)
                        }
                            );
                    }
                }
                else if (PhotonNetwork.InRoom && (className.Contains("setReady") && UnityEngine.Input.GetMouseButtonUp(0)))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    GameInfo.SetReady(!GameInfo.isPlayerReady);
                }

                // Create NPC
                else if (GameInfo.IsMasterClient() && (className.Contains("CreateNPC") && UnityEngine.Input.GetMouseButtonUp(0)))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    GameInfo.CreateNPC(GameInfo.NPCList.Count + 1 % 9);
                    if (singleplayer)
                    {
                        UpdateRoomView();
                    }
                    return;
                }
                // Kick
                else if (GameInfo.IsMasterClient() && (className.Contains("kick") && UnityEngine.Input.GetMouseButtonUp(0)))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    if (element.parentElement.className.Contains("NPC"))
                    {
                        int index = int.Parse(new Regex(@"NPCINDEX(\d+)").Match(element.className).Groups[1].Value);
                        GameInfo.DeleteNPC(index);
                        if (singleplayer)
                        {
                            UpdateRoomView();
                        }
                        return;
                    }
                    else
                    {
                        string kickUsername = element.parentElement.textContent;
                        AddMessasgeToChat(String.Format("{0} kicked user {1}", PhotonNetwork.LocalPlayer.NickName, kickUsername));
                        GetComponent <PhotonView>().RPC("KickPlayer", PhotonTargets.All, kickUsername);
                        return;
                    }
                }
                if ((className.Contains("spectator") && UnityEngine.Input.GetMouseButtonUp(0)) && element.parentElement.innerText.Contains(GameInfo.GetNickName()))
                {
                    soundsSource.PlayOneShot(clickSound, PlayerPrefs.GetFloat("interfaceVolume"));
                    GameInfo.SetSpectator(!GameInfo.playerSpectate);
                    if (singleplayer)
                    {
                        UpdateRoomView();
                    }
                    return;
                }

                // Change map size
                var mapElements = UI.document.getElementsByClassName("MapSize");
                if (mapElements.length > 0)
                {
                    int newSize = int.Parse(mapElements[0].id);
                    if (newSize != GameInfo.mapSize)
                    {
                        GameInfo.SetMapSize(newSize);
                        if (singleplayer)
                        {
                            UpdateRoomView();
                        }
                        return;
                    }
                }

                // Change team
                if (UI.document.getElementsByAttribute("name", "teamSelect").length > 0)
                {
                    foreach (Dom.Element selectElement in UI.document.getElementsByAttribute("name", "teamSelect"))
                    {
                        int teamSelect = int.Parse(selectElement.id);
                        if (selectElement.className.Contains(GameInfo.GetNickName()))
                        {
                            if (GameInfo.playerTeam != teamSelect)
                            {
                                GameInfo.SetTeam(teamSelect);
                            }
                        }
                        else if (GameInfo.IsMasterClient() && selectElement.parentElement.className.Contains("NPC"))
                        {
                            // Debug.Log("NPC (" + selectElement.parentElement.className + "): " + teamSelect);
                            int index = int.Parse(new Regex(@"NPCINDEX(\d+)").Match(selectElement.parentElement.className).Groups[1].Value);
                            if (GameInfo.NPCList[index] != teamSelect)
                            {
                                GameInfo.ChangeNPCTeam(index, teamSelect);
                            }
                        }
                    }
                }
            }
        }