Exemple #1
0
        /**
         * <summary>Removes the current Player from the scene, and re-instantiates the correct Player prefab.</summary>
         * <param name = "ref_player">The new Player prefab to instantiate</param>
         * <param name = "ID">The ID number to assign the new Player</param>
         * <param name = "resetReferences">If True, then any references to the Player prefab in other AC scripts will be updated</param>
         * <param name = "_rotation">The new Player's rotation</param>
         * <param name = "keepInventory">If True, then the inventory items of the previous player (if there is one) will be transferred onto the new one, replacing any items held by the new one.</param>
         */
        public static void ResetPlayer(Player ref_player, int ID, bool resetReferences, Quaternion _rotation, bool keepInventory = false)
        {
            // Delete current player(s)
            if (GameObject.FindGameObjectsWithTag(Tags.player) != null)
            {
                foreach (GameObject playerOb in GameObject.FindGameObjectsWithTag(Tags.player))
                {
                    if (playerOb != null)
                    {
                        if (playerOb.GetComponent <Player>())
                        {
                            playerOb.GetComponent <Player>().ReleaseHeldObjects();
                        }
                        DestroyImmediate(playerOb);
                    }
                }
            }

            // Load new player
            if (ref_player)
            {
                SettingsManager settingsManager = AdvGame.GetReferences().settingsManager;

                Player newPlayer = (Player)Instantiate(ref_player, Vector3.zero, _rotation);
                newPlayer.ID   = ID;
                newPlayer.name = ref_player.name;
                playerPrefab   = newPlayer;
                DontDestroyOnLoad(newPlayer);

                if (KickStarter.runtimeInventory)
                {
                    KickStarter.runtimeInventory.SetNull();
                    KickStarter.runtimeInventory.RemoveRecipes();

                    // Clear inventory
                    if (settingsManager.playerSwitching == PlayerSwitching.Allow && !settingsManager.shareInventory)
                    {
                        if (!keepInventory)
                        {
                            KickStarter.runtimeInventory.localItems.Clear();
                        }
                    }

                    if (KickStarter.saveSystem != null && KickStarter.saveSystem.DoesPlayerDataExist(ID, false))
                    {
                        bool loadNewInventory = !settingsManager.shareInventory;
                        if (settingsManager.playerSwitching == PlayerSwitching.DoNotAllow || (!settingsManager.shareInventory && keepInventory))
                        {
                            loadNewInventory = false;
                        }
                        saveSystem.AssignPlayerData(ID, loadNewInventory);
                    }

                    // Menus
                    foreach (AC.Menu menu in PlayerMenus.GetMenus())
                    {
                        foreach (MenuElement element in menu.elements)
                        {
                            if (element is MenuInventoryBox)
                            {
                                MenuInventoryBox invBox = (MenuInventoryBox)element;
                                invBox.ResetOffset();
                            }
                        }
                    }
                }

                newPlayer.Initialise();
                if (KickStarter.eventManager != null)
                {
                    KickStarter.eventManager.Call_OnSetPlayer(newPlayer);
                }
            }

            // Reset player references
            if (resetReferences)
            {
                KickStarter.playerMovement.AssignFPCamera();
                KickStarter.stateHandler.IgnoreNavMeshCollisions();
                KickStarter.stateHandler.GatherObjects(false);
                KickStarter.stateHandler.UpdateAllMaxVolumes();
                _Camera[] cameras = FindObjectsOfType(typeof(_Camera)) as _Camera[];
                foreach (_Camera camera in cameras)
                {
                    camera.ResetTarget();
                }
            }
        }