Beispiel #1
0
    public static void Fight(Troop atkTroop, Troop defTroop)
    {
        int  atkPwr      = atkTroop.AtkPower();
        int  defPwr      = defTroop.DefPower();
        bool attackerWin = (atkPwr >= defPwr);

        Troop           survivors = new Troop();
        List <UnitType> army      = new List <UnitType>();
        int             armySize  = 0;
        Troop           winner    = (attackerWin) ? atkTroop : defTroop;

        for (int i = 0; i < winner.unitList.Count; i++)
        {
            armySize += winner.unitList[i].amount;
            for (int u = 0; u < winner.unitList[i].amount; u++)
            {
                army.Add(winner.unitList[i].type);
            }
        }

        Debug.Log("Army size:  " + armySize);
        UnitType type;

        Debug.Log("Fighting...");
        int stronger = (attackerWin) ? atkPwr : defPwr;
        int weaker   = (attackerWin) ? defPwr : atkPwr;

        while (stronger >= weaker)
        {
            int index = Random.Range(0, armySize);
            type = army[index];
            army.RemoveAt(index);
            survivors.AddUnit(type, 1);
            stronger -= Unit.AtkPower(type);
            armySize--;
        }
        Debug.Log("Survivors");
        survivors.Print();

        if (attackerWin)
        {
            atkTroop.KillUnits(survivors);
            defTroop.KillUnits(null);
        }
        else
        {
            atkTroop.KillUnits(null);
            defTroop.KillUnits(survivors);
        }
    }