// -------------------------------------------- Transfers space marine from this ship to target ship

    private void TransferMarine(spaceMarine m, int i, p_ship target)
    {
        print("In Transfer Marine");
        target.AddEnemyMarine(m);
        target.IncEnemyMarine();

        marines.RemoveAt(i);

        numSpaceMarines--;
    }
    public void OnClick()
    {
        p_ship frigate = new p_ship(numComponents);
        //frigate.shipObject
        GameObject a = Instantiate(shipPrefab) as GameObject;

        // GameObject a = new GameObject p_ship(numComponents);//) as GameObject;
        //GameObject a = Resources.Load("Prefab/p_ship_Cruiser") as GameObject;
        a.transform.position = new Vector3(-0.507f, 2.51f, -3);
        frigate.shipObject   = a;
    }//*/
 // ----------------------------------------- Returns how many more engines the target ship has over this ship
 private int HasMoreEngines(p_ship target)
 {
     if (this.GetNumEngine() >= target.GetNumEngine())
     {
         return(0);
     }
     else
     {
         return(target.GetNumEngine() - this.GetNumEngine());
     }
 }
 public void LaunchMarineAssault(p_ship target)
 {
     if (numAssaults == 0)
     {
         numAssaults++;
         StartCoroutine(MarineAssault(target));
     }
     else
     {
         StartCoroutine(ErrorBox("You have used all you marine assaults for this turn"));
     }
 }
 // --------------------------------------------------- This ship attacks ship at the target parameter
 public void LaunchAttack(p_ship target)
 {
     if (numAttacksRemaining > 0)
     {
         numAttacksRemaining--;
         StartCoroutine(Attack(target));
     }
     else
     {
         StartCoroutine(ErrorBox("You have used all you attacks for this turn"));
     }
 }
    //determines the amount of missiles that actually hit the ship
    private Tuple <bool, int> MissileDeflect(int AMLoc, p_ship target)
    {
        int roll = UnityEngine.Random.Range(1, 6);

        if (target.arrComponents[AMLoc].getLevel() >= roll)
        {
            return(Tuple.Create(true, roll));
        }
        else
        {
            return(Tuple.Create(false, roll));
        }
    }
    private void DmgAssesmentHandler(p_ship target)
    {
        string disabledComponent = "";

        GC.GetComponent <DamageAssesmentUI>().PopUp();
        GC.GetComponent <DamageAssesmentUI>().SetArmorText(target.numArmor);
        GC.GetComponent <DamageAssesmentUI>().SetCritText(target.numCriticalHits);

        for (int i = 0; i < target.arrComponents.Count; i++)
        {
            if (target.arrComponents[i].getActivity() == false)
            {
                disabledComponent = disabledComponent + target.arrComponents[i].toString();
            }
        }
        GC.GetComponent <DamageAssesmentUI>().SetDisabledComponents(disabledComponent);
    }
    //  -------------------------------------- Space Marine Combat

    private IEnumerator MarineAssault(p_ship target)
    {
        int  disadvantage    = 0;
        bool boardSuccessful = false;

        disadvantage = HasMoreEngines(target);


        //Boarding process
        int i = 0;

        while (i < numActiveMarines && marines.Count >= 1)
        {
            Tuple <bool, int> temp;
            if (marines[i] != null && marines[(i)].GetActivity() == true)
            {
                temp = MarineRoll(disadvantage, (i));
                if (temp.Item1 == true)
                {
                    RollUIHandler(temp.Item2, marines[i].getLevel(), "marine assault success");
                    TransferMarine(marines[(i)], (i), target);
                    yield return(new WaitForSeconds(3f));

                    GC.GetComponent <RollPopUp>().Close();
                }
                else if (!temp.Item1)
                {
                    if (target.HasShields())
                    {
                        RollUIHandler(temp.Item2, marines[i].getLevel(), "marine assault fail destroyed");
                        yield return(new WaitForSeconds(3f));

                        GC.GetComponent <RollPopUp>().Close();
                        marines.RemoveAt(i);
                        numSpaceMarines--;
                        numActiveMarines--;
                    }
                    else
                    {
                        RollUIHandler(temp.Item2, marines[i].getLevel(), "marine assault fail");
                        yield return(new WaitForSeconds(3f));

                        GC.GetComponent <RollPopUp>().Close();
                        marines[(i)].ToggleActive();
                        numActiveMarines--;
                        i++;
                    }
                }
            }
            else
            {
                print("Null found");
            }
        }
        print("post board");
        if (target.GetNumSpaceMarines() > 0)
        {
            print("target has greater than 0 space marines");
            print(target.GetNumEnemy());

            // Each player rolls until one side no longer has any space marines
            do
            {
                int friendlyRoll = 0, enemyRoll = 0;
                friendlyRoll = UnityEngine.Random.Range(1, 6);
                enemyRoll    = UnityEngine.Random.Range(1, 6);

                if (friendlyRoll > enemyRoll)
                {
                    RollUIHandler(friendlyRoll, enemyRoll, "marine vs marine success");
                    yield return(new WaitForSeconds(3f));

                    GC.GetComponent <RollPopUp>().Close();
                    target.marines.RemoveAt(0);
                    target.DecMarine();
                }
                else if (friendlyRoll < enemyRoll)
                {
                    RollUIHandler(friendlyRoll, enemyRoll, "marine vs marine fail");
                    yield return(new WaitForSeconds(3f));

                    GC.GetComponent <RollPopUp>().Close();
                    target.enemyMarines.RemoveAt(0);
                    target.DecEnemyMarine();
                }
                else
                {
                    RollUIHandler(friendlyRoll, enemyRoll, "marine vs marine tie");
                    yield return(new WaitForSeconds(3f));

                    GC.GetComponent <RollPopUp>().Close();
                    target.enemyMarines.RemoveAt(0);
                    target.marines.RemoveAt(0);
                    target.DecEnemyMarine();
                    target.DecMarine();
                }
            }while (target.GetNumSpaceMarines() > 0 && target.GetNumEnemy() > 0);

            if ((target.GetNumEnemy() > 0) == true && target.GetNumSpaceMarines() == 0)
            {
                boardSuccessful = true;
            }
        }
        else
        {
            print("board successful");
            print(target.marines.Count);
            print(target.GetNumSpaceMarines());
            boardSuccessful = true;
        }

        if (boardSuccessful == true)
        {
            MarineAssaultAssesmentHandler("Enemy ship was captured!");
            yield return(new WaitForSeconds(5f));

            GC.GetComponent <MarineAssaultAssesment>().Close();
            target.gameObject.tag = this.gameObject.tag;
            GC.GetComponent <GameController>().RecompileList();
            print("Ship Captured");
        }
        else
        {
            MarineAssaultAssesmentHandler("Enemy ship was not captured!");
            yield return(new WaitForSeconds(5f));

            GC.GetComponent <MarineAssaultAssesment>().Close();
            print("Failed Attempt");
            print(target.GetNumSpaceMarines());
            print(target.GetNumEnemy());
        }
    }
    private IEnumerator Attack(p_ship target)
    {
        int beamHit    = 0; // number of beam weapons that need to be calculated against shields
        int missileHit = 0; //number of missiles that need top be calculated against anitmissiles
        int dmgHits    = 0; //number of hits that actually make it through sheilds and anti missile

        print("In attack");
        print(target.numArmor);

        //RollUIHandler(3, 4);

        //------------------------------------------ Beam


        for (int i = 0; i < arrComponents.Count; i++)
        {
            Tuple <bool, int> temp;
            GC.GetComponent <RollPopUp>().Close();
            if (arrComponents[i] is p_beamWeapon && arrComponents[i].getActivity() == true)
            {
                temp = FireBeam(i);
                if (temp.Item1 == true)
                {
                    beamHit++;

                    RollUIHandler(temp.Item2, arrComponents[i].getLevel(), "beam hit");
                    yield return(new WaitForSeconds(3f));

                    GC.GetComponent <RollPopUp>().Close();
                }
                else
                {
                    RollUIHandler(temp.Item2, arrComponents[i].getLevel(), "beam miss");
                    yield return(new WaitForSeconds(3f));

                    GC.GetComponent <RollPopUp>().Close();
                }
            }
        }
        print(beamHit);

        //------------------------------------------- Missile

        for (int i = 0; i < arrComponents.Count; i++)
        {
            Tuple <bool, int> temp;
            if (arrComponents[i] is p_missileLauncher && arrComponents[i].getActivity() == true)
            {
                temp = FireMissile(i);
                if (temp.Item1 == true)
                {
                    missileHit++;

                    RollUIHandler(temp.Item2, arrComponents[i].getLevel(), "missile hit");
                    yield return(new WaitForSeconds(3f));

                    GC.GetComponent <RollPopUp>().Close();
                }
                else
                {
                    RollUIHandler(temp.Item2, arrComponents[i].getLevel(), "missile miss");
                    yield return(new WaitForSeconds(3f));

                    GC.GetComponent <RollPopUp>().Close();
                }
            }
        }

        print(missileHit);

        //------------------------------------------- Shields

        if (target.HasShields() == true)
        {
            int shieldGenLoc = 0;
            for (int i = 0; i < target.arrComponents.Count; i++)
            {
                if (target.arrComponents[i] is p_sheildGenerator && arrComponents[i].getActivity() == true)
                {
                    shieldGenLoc = i;
                    break;
                }
            }

            for (int i = 0; i < beamHit; i++)
            {
                Tuple <bool, int> temp;
                temp = ShieldDeflect(shieldGenLoc, target);

                if (temp.Item1 == true)
                {
                    dmgHits++;
                    RollUIHandler(arrComponents[shieldGenLoc].getLevel(), temp.Item2, "shield break");
                    yield return(new WaitForSeconds(3f));

                    GC.GetComponent <RollPopUp>().Close();
                }
                else
                {
                    RollUIHandler(arrComponents[shieldGenLoc].getLevel(), temp.Item2, "shield deflect");
                    yield return(new WaitForSeconds(3f));

                    GC.GetComponent <RollPopUp>().Close();
                }
            }
        }
        else
        {
            dmgHits = beamHit;
        }

        //------------------------------------------- Anti-Missile

        if (target.HasAntiMissile() == true)
        {
            Tuple <bool, int> temp;
            for (int i = 0; i < target.arrComponents.Count; i++)
            {
                if (missileHit <= 0)
                {
                    break;
                }
                else if (target.arrComponents[i] is p_antiMissile && arrComponents[i].getActivity() == true)
                {
                    temp = MissileDeflect(i, target);

                    if (temp.Item1 == true)
                    {
                        dmgHits++;
                        missileHit--;
                        RollUIHandler(arrComponents[i].getLevel(), temp.Item2, "anti-missile hit");
                        yield return(new WaitForSeconds(3f));

                        GC.GetComponent <RollPopUp>().Close();
                    }
                    else
                    {
                        missileHit--;
                        RollUIHandler(arrComponents[i].getLevel(), temp.Item2, "anti-missile blocked");
                        yield return(new WaitForSeconds(3f));

                        GC.GetComponent <RollPopUp>().Close();
                    }
                }
            }

            if (missileHit > 0)
            {
                dmgHits = dmgHits + missileHit;
            }
        }
        else
        {
            dmgHits = dmgHits + missileHit;
        }

        print("dmg Hits " + dmgHits);

        //--------------------------------------- Armor

        if (dmgHits > target.numArmor)
        {
            target.numArmor = 0;

            do
            {
                int marker = UnityEngine.Random.Range(0, maxComponents);

                if (marker >= target.arrComponents.Count)
                {
                    print("Critical Hit!");
                    target.numCriticalHits--;
                    dmgHits--;
                }
                else if (target.arrComponents[marker].getActivity() == true)
                {
                    target.arrComponents[marker].ToggleActivity();
                    dmgHits--;
                    print("Component: " + target.arrComponents[marker].ToString() + " inactive");
                }
                else
                {
                    target.arrComponents.RemoveAt(marker);
                    dmgHits--;
                    print("Component: " + target.arrComponents[marker].ToString() + " Destroyed");
                }
            }while (dmgHits != 0 && target.numCriticalHits != 0);
        }
        else
        {
            print("Armor Before: " + target.numArmor);
            target.numArmor = target.numArmor - dmgHits;
            print("Armor After: " + target.numArmor);
        }

        // -------------------------------------- Critical Hits

        DmgAssesmentHandler(target);
        yield return(new WaitForSeconds(5f));

        GC.GetComponent <DamageAssesmentUI>().Close();

        if (target.numCriticalHits <= 0)
        {
            DmgAssesmentHandler(target);
            yield return(new WaitForSeconds(5f));

            GC.GetComponent <DamageAssesmentUI>().Close();
            Destroy(target.gameObject);
            GC.GetComponent <GameController>().RecompileList();
            print("Target Destroyed");
        }
    }