/********************************************************************************
    *  ------------------------------- COMBAT SECTION ---------------------------------
    *********************************************************************************
    *  -This section contains all functions related to player and CPU combat
    ********************************************************************************/

    public void TriggerCombat(GameObject trigger)
    {
        //Fix sorting order
        NumberOfSpritesBehind = 0;
        GetComponent <SpriteRenderer>().sortingLayerName = DefaultSortingLayer;
        GetComponent <SpriteRenderer>().sortingOrder     = DefaultSortingOrder;
        DontDestroyOnLoad(objectSpawner);
        objectSpawner.HideObjects();
        //move player to combat instance
        //save current position
        _combatPosition = transform.position;
        if (IsTest)
        {
            nextLevel = new SceneManagerInstance(GameObject.FindGameObjectWithTag("SceneManager").GetComponent <SceneManager>());
        }
        else
        {
            nextLevel.name = Application.loadedLevelName;
        }
        //save portal as combat details for loading combat
        combatDetails = trigger;
        //removeFromSceneManager(other.gameObject);
        //protect components
        DoNotDestroy();
        //Heal
        PostBattleRecovery();
        //load fight
        RemoveFromSceneManager(trigger);
        Application.LoadLevel("Combat");
    }
    public void TriggerSceneChange(SceneManager sceneManager, string levelName, int spawnPointID)
    {
        if (objectSpawner)
        {
            objectSpawner.CleanObjects();
            Destroy(objectSpawner);
        }
        //Load the scene manager if one is defined
        if (sceneManager != null)
        {
            nextLevel = new SceneManagerInstance(sceneManager);
        }
        //Fix sorting layer and order
        GetComponent <SpriteRenderer>().sortingLayerName = DefaultSortingLayer;
        GetComponent <SpriteRenderer>().sortingOrder     = DefaultSortingOrder;
        NumberOfSpritesBehind = 0;
        //Get spawning position
        string newLevel = levelName;

        _targetSpawnID = spawnPointID;
        //Load level
        Application.LoadLevel(newLevel);
        //Notify event system that location has changed
        GetComponent <EventManager>().Event(new List <object>()
        {
            GameAction.IN_AREA, newLevel
        });
        //Save all this to the database
        networkManager.DBSwitchInstance();
        networkManager.UpdatePlayerStat("CurrentScene", levelName);
        networkManager.UpdatePlayerStat("CurrentSceneSpawn", spawnPointID.ToString());
        if (sceneManager != null && sceneManager.name != null)
        {
            networkManager.UpdatePlayerStat("CurrentSceneManager", sceneManager.name);
        }
        else
        {
            networkManager.UpdatePlayerStat("CurrentSceneManager", "");
        }
    }
    public void SpawnRandomObjectsFromSpawnPoints(List <GameObject> objects, List <float> spawnRates, SceneManagerInstance manager)
    {
        foreach (GameObject location in spawnPoints)
        {
            if (manager.globalSpawnRate < Random.Range(0f, 1f))
            {
                continue;
            }
            List <int> attemptedSpawns = new List <int>();
            for (int i = 0; i < objects.Count; i++)
            {
                int index;
                while (attemptedSpawns.Contains(index = Random.Range(0, objects.Count)))
                {
                }

                if (spawnRates[index] >= Random.Range(0f, 1f))
                {
                    //Debug.Log(location.transform.position);
                    GameObject obj = Instantiate(objects[index]);
                    obj.transform.position = location.transform.position;
                    spawnedObjects.Add(obj);
                    spawnedLocations.Add(location.transform.position);
                    spawnedRates.Add(1);
                    break;
                }
                attemptedSpawns.Add(index);
                if (attemptedSpawns.Count == objects.Count)
                {
                    break;
                }
            }
            attemptedSpawns.Clear();
        }
        manager.objects         = spawnedObjects;
        manager.objectLocations = spawnedLocations;
        foreach (GameObject o in spawnedObjects)
        {
            DontDestroyOnLoad(o);
        }
    }
    public void SpawnRandomObjects(List <GameObject> objects, List <Vector3> objectLocations, List <float> spawnRates, SceneManagerInstance manager)
    {
        //foreach(Vector3 location in objectLocations)
        for (int k = 0; k < objectLocations.Count; k++)
        {
            List <int> attemptedSpawns = new List <int>();
            for (int i = 0; i < objects.Count; i++)
            {
                int index;
                while (attemptedSpawns.Contains(index = Random.Range(0, objects.Count)))
                {
                }

                if (spawnRates[index] < Random.Range(0f, 1f))
                {
                    objects[index].transform.position = objectLocations[index];
                    spawnedObjects.Add(Instantiate(objects[index]));
                    spawnedLocations.Add(objectLocations[index]);
                    spawnedRates.Add(1);
                    break;
                }
                attemptedSpawns.Add(index);
                if (attemptedSpawns.Count == objects.Count)
                {
                    break;
                }
            }
        }
        manager.objects         = spawnedObjects;
        manager.objectLocations = spawnedLocations;
        foreach (GameObject o in spawnedObjects)
        {
            DontDestroyOnLoad(o);
        }
    }