// Use this for initialization
    void Start()
    {
        PlayerPrefs.SetInt("sessionBookmark", 1);
        combatInProgress = true;
        //PlayerPrefs.DeleteAll();
        //levelLoader.techBonus = { 0,0,0,0};
        phaseID = 0;

        playerLoader.initializeParty();
        List <Chara> tempList = playerLoader.GetPlayerCharacters();

        foreach (Chara chara in tempList)
        {
            allChara.Add(chara);
        }
        for (int i = 0; i < 5; i++)
        {
            ActiveEnemy.Add(transform.GetChild(i).gameObject);
        }
        foreach (GameObject go in ActiveEnemy)
        {
            EnemyChara echara = go.GetComponent <EnemyChara>();
            echara.chara.Initialize();
            echara.chara.isEnemy = true;
            allChara.Add(echara.chara);
        }
        LoadEnemy();

        UpdateHPUI();
        Combat();
        VictoryCheck();
    }
    public void saveEnemies()
    {
        string saveData = "";

        foreach (GameObject go in combatEngine.ActiveEnemy)
        {
            EnemyChara eChara = go.GetComponent <EnemyChara>();
            saveData += eChara.chara.name + '#'; //index 0
            saveData += eChara.enemyID + "#";
            saveData += eChara.chara.HPcurr + "#";

            //saves battle data
            saveData += eChara.chara.efExtend + "#";
            saveData += eChara.chara.efPoison + "#";
            saveData += eChara.chara.efRecov + "#"; //index 5
            saveData += eChara.chara.efShield + "#";
            saveData += eChara.chara.efStunRate + "#";
            saveData += eChara.chara.efTauntRate + "#";
            if (eChara.chara.efTauntTarget != null)
            {
                saveData += eChara.chara.efTauntTarget.name + "#";
            }
            else
            {
                saveData += "none" + "#";
            }

            //saves status effect
            if (eChara.chara.statusEffectList != null)
            {
                int seIndex = 0;
                foreach (StatusEffect se in eChara.chara.statusEffectList)
                {
                    saveData += se.StatusID + "^";
                    saveData += se.level + "^";
                    saveData += se.duration;
                    //check if the status effect is the last in the list
                    //if index+1==seList.count
                    if (seIndex + 1 < eChara.chara.statusEffectList.Count)
                    {
                        saveData += "&";
                    }
                    seIndex++;
                }
            }

            saveData += "#"; //index 10

            //save action in sequence
            saveData += eChara.chara.seqIndex + "#";
            saveData += eChara.chara.actIndex;
            saveData += '\n';
        }
        //Debug.Log(saveData);
        PlayerPrefs.SetString("enemyDetails", saveData);
    }
    public EnemyChara SpawnEnemy(EnemyChara spawnChara)
    {
        //another way to spawn enemy
        foreach (GameObject go in ActiveEnemy)
        {
            EnemyChara eChara = go.GetComponent <EnemyChara>();
            //if dead/empty
            if (eChara.chara.HPcurr <= 0)
            {
                //initialize that component as the correct spawn

                eChara.enemyID       = spawnChara.enemyID;
                eChara.goldPrize     = spawnChara.goldPrize;
                eChara.chara.isEnemy = true;

                eChara.chara.sprite    = spawnChara.chara.sprite;
                eChara.chara.baseHPmax = spawnChara.chara.baseHPmax;
                eChara.chara.HPmax     = spawnChara.chara.HPmax;
                eChara.chara.HPcurr    = spawnChara.chara.HPcurr;

                eChara.chara.MPmax  = spawnChara.chara.MPmax;
                eChara.chara.MPcurr = spawnChara.chara.MPcurr;

                eChara.chara.name = spawnChara.chara.name;

                eChara.chara.atk     = spawnChara.chara.atk;
                eChara.chara.baseAtk = spawnChara.chara.baseAtk;

                eChara.chara.spd     = spawnChara.chara.spd;
                eChara.chara.baseSpd = spawnChara.chara.baseSpd;

                eChara.chara.def      = spawnChara.chara.def;
                eChara.chara.baseDef  = spawnChara.chara.baseDef;
                eChara.chara.sequence = spawnChara.chara.sequence;
                eChara.chara.Initialize();
                UpdateHPUI();
                return(eChara);
            }
        }
        return(null);
    }
    public EnemyChara SpawnEnemy(int ID)
    {
        //Debug.Log("Spawned: "+ID);
        //add enemy here :D
        //Step 1, assign an idle ActiveEnemy object
        //Step 2, get the object's EnemyChara component
        //Step 3, copy the enemyChara component values
        //step 4, initialize the new enemy
        foreach (GameObject go in ActiveEnemy)
        {
            EnemyChara eChara = go.GetComponent <EnemyChara>();
            //if dead/empty
            if (eChara.chara.HPcurr <= 0)
            {
                //GetCorrectEnemyByID
                int correctIndex = -1;
                for (int i = 0; i < EnemyList.Length; i++)
                {
                    if (EnemyList[i].GetComponent <EnemyChara>().enemyID == ID)
                    {
                        correctIndex = i;
                        break;
                    }
                }
                EnemyChara sourceChara;
                if (correctIndex == -1)
                {
                    //this is an empty enemy
                    sourceChara = EnemyList[0].GetComponent <EnemyChara>();
                }
                else
                {
                    sourceChara = EnemyList[correctIndex].GetComponent <EnemyChara>();
                    //initialize that component as the correct spawn
                }

                eChara.enemyID       = ID;
                eChara.chara.isEnemy = true;
                eChara.goldPrize     = sourceChara.goldPrize;
                if (correctIndex == -1)
                {
                    eChara.chara.sprite = emptySprite;
                }
                else
                {
                    eChara.chara.sprite = sourceChara.chara.sprite;
                }
                eChara.chara.baseHPmax = sourceChara.chara.baseHPmax;
                eChara.chara.HPmax     = sourceChara.chara.HPmax;
                eChara.chara.HPcurr    = sourceChara.chara.HPcurr;

                eChara.chara.MPmax  = sourceChara.chara.MPmax;
                eChara.chara.MPcurr = sourceChara.chara.MPcurr;

                eChara.chara.name = sourceChara.chara.name;

                eChara.chara.atk     = sourceChara.chara.atk;
                eChara.chara.baseAtk = sourceChara.chara.baseAtk;

                eChara.chara.spd     = sourceChara.chara.spd;
                eChara.chara.baseSpd = sourceChara.chara.baseSpd;

                eChara.chara.def      = sourceChara.chara.def;
                eChara.chara.baseDef  = sourceChara.chara.baseDef;
                eChara.chara.sequence = sourceChara.chara.sequence;
                eChara.chara.Initialize();
                UpdateHPUI();
                return(eChara);
            }
        }
        return(null);
    }
 // Update is called once per frame
 private void Start()
 {
     enemy = GetComponent <EnemyChara>();
 }
    public void loadEnemies()
    {
        if (semester < 4)
        {
            bgImage.sprite = bgImageList[0];
        }
        else if (semester < 7)
        {
            bgImage.sprite = bgImageList[1];
        }
        else
        {
            bgImage.sprite = bgImageList[2];
        }

        //Debug.Log(combatEngine.combatInProgress);
        if (combatEngine.combatInProgress)
        {
            //load already there units
            string   enemyData = PlayerPrefs.GetString("enemyDetails", "");
            string[] enemyUnit = enemyData.Split('\n');
            if (enemyUnit.Length <= 5)
            {
                //error handling: data not found
                combatEngine.combatInProgress = false;
                loadEnemies();
            }
            else
            {
                //data found, load 5 enemies
                for (int i = 0; i < 5; i++)
                {
                    string[] enemyDetail = enemyUnit[i].Split('#');
                    //Debug.Log("Loading data "+i+" found qty "+enemyDetail.Length);
                    //string debugger = "";
                    //foreach(string s in enemyDetail)
                    //{
                    //    debugger += s;
                    //    debugger += " ";
                    //}
                    //Debug.Log("Debugger: "+debugger);
                    //loads id, stats, moves based on id
                    EnemyChara eChara = combatEngine.SpawnEnemy(int.Parse(enemyDetail[1]));
                    //loads name
                    eChara.chara.name = enemyDetail[0];

                    //loads currhp
                    eChara.chara.HPcurr = int.Parse(enemyDetail[2]);
                    //loads battle stats
                    eChara.chara.efExtend    = int.Parse(enemyDetail[3]);
                    eChara.chara.efPoison    = int.Parse(enemyDetail[4]);
                    eChara.chara.efRecov     = int.Parse(enemyDetail[5]);
                    eChara.chara.efShield    = int.Parse(enemyDetail[6]);
                    eChara.chara.efStunRate  = int.Parse(enemyDetail[7]);
                    eChara.chara.efTauntRate = int.Parse(enemyDetail[8]);
                    string targetName = enemyDetail[9];
                    if (targetName != "none")
                    {
                        foreach (Chara chara in combatEngine.GetAllChara())
                        {
                            if (chara.name == targetName)
                            {
                                eChara.chara.efTauntTarget = chara;
                            }
                        }
                    }

                    //loads status effect
                    if (enemyDetail[10].Length > 0)
                    {
                        string[] seData = enemyDetail[10].Split('&');
                        foreach (string seSet in seData)
                        {
                            string[] seDetail = seSet.Split('^');

                            /*1 = AtkUP
                             *  2 = SpdUP
                             *  3 = DefUP
                             *  4 = AtkDOWN
                             *  5 = SpdDOWN
                             *  6 = DefDOWN
                             *  7 = HPDOwn
                             *  11 = Poison(Overload)
                             *  12 = Shield
                             *  13 = Extend
                             *  14 = Regen
                             *  15 = Stun
                             *  16 = Taunt*/
                            StatusEffect tempSE;
                            //set SE according to ID
                            switch (int.Parse(seDetail[0]))
                            {
                            case 1:
                                tempSE = new Ef_AtkUP();
                                break;

                            case 2:
                                tempSE = new Ef_SpdUP();
                                break;

                            case 3:
                                tempSE = new Ef_DefUP();
                                break;

                            case 4:
                                tempSE = new Ef_AtkDOWN();
                                break;

                            case 5:
                                tempSE = new Ef_SpdDOWN();
                                break;

                            case 6:
                                tempSE = new Ef_DefDOWN();
                                break;

                            case 7:
                                tempSE = new Ef_HPDOWN();
                                break;

                            case 11:
                                tempSE = new Ef_Poison();
                                break;

                            case 12:
                                tempSE = new Ef_Shield();
                                break;

                            case 13:
                                tempSE = new Ef_Extend();
                                break;

                            case 14:
                                tempSE = new Ef_Regen();
                                break;

                            case 15:
                                tempSE = new Ef_Stun();
                                break;

                            case 16:
                                tempSE = new Ef_Taunt();
                                break;

                            default:
                                //in case buff not found, turn to regen
                                tempSE = new Ef_Regen();
                                break;
                            }
                            tempSE.InitializeSE(int.Parse(seDetail[1]), int.Parse(seDetail[2]));
                            eChara.chara.statusEffectList.Add(tempSE);
                        }
                    }
                    //loads action/sequence stats
                    eChara.chara.seqIndex = int.Parse(enemyDetail[11]);
                    eChara.chara.actIndex = int.Parse(enemyDetail[12]);
                }
            }
        }
        else
        {
            //spawn new units
            int maxRange = 0;
            foreach (SpawnGroup sg in semesterList[semester - 1].spawnGroup)
            {
                maxRange += sg.value;
            }
            //roll rng
            int rng       = Random.Range(0, maxRange);
            int tempIndex = 0;
            while (rng > semesterList[semester - 1].spawnGroup[tempIndex].value)
            {
                rng -= semesterList[semester - 1].spawnGroup[tempIndex].value;
                tempIndex++;
            }
            foreach (EnemyChara eChara in semesterList[semester - 1].spawnGroup[tempIndex].eCharaList)
            {
                combatEngine.SpawnEnemy(eChara);
            }
            combatEngine.combatInProgress = true;
        }
        saveEnemies();
    }