public static CombatUnitManagerMetaData Load(bool processing = false)
    {
        if (!SaveLoadManager.SharedManager.IsFileExist <CombatUnitManagerMetaData>())
        {
            CombatUnitManagerMetaData newData = new CombatUnitManagerMetaData();

            newData.availableCombatUnit = new List <CombatUnitType>();
            newData.numberOfCombatUnit  = new List <int>();

            newData.producingStartTime = DateTime.Now;
            newData.producingEndTime   = DateTime.Now;

            newData.Save();

            return(newData);
        }

        CombatUnitManagerMetaData data = SaveLoadManager.SharedManager.Load <CombatUnitManagerMetaData> ();

        if (processing)
        {
            data.ProcessData();

            data.Save();
        }


        return(data);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Cancel attack by id.
    /// </summary>
    /// <returns><c>true</c> if this instance cancel attack the specified tskId; otherwise, <c>false</c>.</returns>
    /// <param name="tskId">Tsk identifier.</param>
    public bool CancelAttack(string tskId)
    {
        AttackInfo attackInfo = FindAttackTask(tskId);

        if (attackInfo != null)
        {
            CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load();


            foreach (CombatUnit unit in attackInfo.attackerUnit.Keys)
            {
                data.AddCombatUnit(unit.unitType, attackInfo.attackerUnit[unit]);
            }

            _trackAttackTask.Remove(attackInfo);

            Save();

            return(true);
        }

        attackInfo = FindAttackTaskInComplete(tskId);

        if (attackInfo != null)
        {
            _completeAttackTask.Remove(attackInfo);

            Save();

            return(true);
        }

        return(false);
    }
Ejemplo n.º 3
0
    public override void CompleteTaskInstant()
    {
        base.CompleteTaskInstant();

        CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load();

        data.FinishProducingUnit();

        CompleteTask();
    }
Ejemplo n.º 4
0
    /// <summary>
    /// Checks the task complete.
    /// </summary>
    /// <returns>The task complete.</returns>
    IEnumerator CheckTaskComplete()
    {
        bool taskComplete = false;

        CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load();

        while (!taskComplete)
        {
            taskComplete = data.IsProduceUnitComplete();

            yield return(new WaitForSeconds(1f));
        }

        CompleteTask();
    }
Ejemplo n.º 5
0
    /// <summary>
    /// Gets the total quantity for each type of unit.
    /// </summary>
    /// <returns>The total quantity.</returns>
    public int GetUnitTotalQuantity()
    {
        CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load();

        _unitTypeAndNumber = data.GetAllUnit();

        int retVal = 0;

        foreach (CombatUnitType unitType in _unitTypeAndNumber.Keys)
        {
            retVal += _unitTypeAndNumber[unitType];
        }

        return(retVal);
    }
Ejemplo n.º 6
0
    /// <summary>
    /// Gets the type of the unit quantity for unit type.
    /// Return 0 if can't find unit type
    /// </summary>
    /// <returns>The unit quantity for type.</returns>
    /// <param name="type">Type.</param>
    public int GetUnitQuantityForType(CombatUnitType type)
    {
        CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load();

        _unitTypeAndNumber = data.GetAllUnit();

        foreach (CombatUnitType unitType in _unitTypeAndNumber.Keys)
        {
            if (type == unitType)
            {
                return(_unitTypeAndNumber[unitType]);
            }
        }

        return(0);
    }
Ejemplo n.º 7
0
    /// <summary>
    /// Updates combat unit manager.
    /// </summary>
    void UpdateCombatUnitManager()
    {
        CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load(true);

        maxCombatUnit      = data.maxCombatUnit;
        currentCombatUnit  = data.CurrentCombatUnit;
        _unitTypeAndNumber = data.GetAllUnit();

        if ((data.producingUnit) && (_task == null))
        {
            _task = ProduceUnitTask.CreateTask(data.producingUnitType, data.producingAmount, OnProducingUnitComplete, OnTimeLeftToComplete, data.producingDuration);

            TaskManager.Instance.AddTask(_task);
        }
        else
        {
            _task = null;
        }
    }
Ejemplo n.º 8
0
    /// <summary>
    /// Gets the current produced number of combat unit.
    /// Filter true will filt out unit type that current number is 0
    /// </summary>
    /// <returns>The avaliable combat unit number.</returns>
    /// <param name="filter">If set to <c>true</c> filter.</param>
    public Dictionary <CombatUnit, int> GetAvaliableCombatUnitNumber(bool filter = true)
    {
        CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load(false);

        Dictionary <CombatUnitType, int> dic = data.GetAllUnit(filter);

        Dictionary <CombatUnit, int> retDic = new Dictionary <CombatUnit, int>();

        //keep combat unit defination order
        for (int i = 0; i < combatUnitInfo.Count; i++)
        {
            if (dic.ContainsKey(combatUnitInfo[i].unitType))
            {
                retDic.Add(combatUnitInfo[i], dic[combatUnitInfo[i].unitType]);
            }
        }

        return(retDic);
    }
Ejemplo n.º 9
0
    /// <summary>
    /// Adds the attack task.
    /// </summary>
    /// <returns><c>true</c>, if attack task was added, <c>false</c> otherwise.</returns>
    /// <param name="tskId">Tsk identifier.</param>
    /// <param name="tId">T identifier.</param>
    /// <param name="tName">T name.</param>
    /// <param name="tUnit">T unit.</param>
    /// <param name="aUnit">A unit.</param>
    /// <param name="taskDuration">Task duration.</param>
    public bool AddAttackTask(string tskId, string tId, string tName, Dictionary <CombatUnit, int> tUnit, Dictionary <CombatUnit, int> aUnit, int taskDuration,
                              Dictionary <ResourceType, float> resourceAward)
    {
        CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load();

        foreach (CombatUnit unit in aUnit.Keys)
        {
            if (!data.DecreaseCombatUnit(unit.unitType, aUnit[unit]))
            {
                Debug.LogError("Unable to decrease combat unit of type " + unit.unitType.ToString());

                return(false);
            }
        }


        AttackInfo newInfo = new AttackInfo(tskId, tId, tName, tUnit, aUnit, taskDuration, resourceAward);

        _trackAttackTask.Add(newInfo);

        Save();

        return(true);
    }
Ejemplo n.º 10
0
    /// <summary>
    /// Instants produce combat unit.
    /// </summary>
    /// <returns><c>true</c>, if produce combat unit was instanted, <c>false</c> otherwise.</returns>
    /// <param name="unitType">Unit type.</param>
    /// <param name="produceAmount">Produce amount.</param>
    public bool InstantProduceCombatUnit(CombatUnitType unitType, int produceAmount)
    {
        if ((combatUnitInfo != null) && (_task == null))
        {
            CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load();

            CombatUnit unitInfo = null;

            for (int i = 0; i < combatUnitInfo.Count; i++)
            {
                if (unitType == combatUnitInfo[i].unitType)
                {
                    unitInfo = combatUnitInfo[i];

                    break;
                }
            }

            if (unitInfo != null)
            {
                bool canProduceUnit = true;

                for (int i = 0; i < unitInfo.costResourceTypes.Count; i++)
                {
                    if (!Player.Instance.IsResourceEnough(unitInfo.costResourceTypes[i], unitInfo.costResources[i] * produceAmount))
                    {
                        EventManager.GetInstance().ExecuteEvent <EventAlert>(new EventAlert("Warning", "Not enough resources"));

                        canProduceUnit = false;

                        break;
                    }
                }

                if (canProduceUnit)
                {
                    Dictionary <ResourceType, float> resourceCost = new Dictionary <ResourceType, float>();

                    for (int i = 0; i < unitInfo.costResourceTypes.Count; i++)
                    {
                        resourceCost.Add(unitInfo.costResourceTypes[i], unitInfo.costResources[i]);
                    }

                    if (data.ProduceCombatUnit(unitInfo.unitType, produceAmount, 0, resourceCost))
                    {
                        List <ResourceType> costType = new List <ResourceType>();
                        foreach (ResourceType type in resourceCost.Keys)
                        {
                            costType.Add(type);
                        }

                        EventManager.GetInstance().ExecuteEvent <EventProduceCombatUnit>(new EventProduceCombatUnit(costType));

                        return(true);
                    }
                }
            }
            else
            {
                Debug.LogError(gameObject.name + " unable to produce combat unit, no unit definition");
            }
        }

        return(false);
    }
Ejemplo n.º 11
0
    /// <summary>
    /// Produces combat unit.
    /// </summary>
    /// <returns><c>true</c>, if combat unit was produced, <c>false</c> otherwise.</returns>
    /// <param name="unitType">Unit type.</param>
    /// <param name="produceAmount">Produce amount.</param>
    public bool ProduceCombatUnit(CombatUnitType unitType, int produceAmount)
    {
        if ((combatUnitInfo != null) && (_task == null))
        {
            CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load();

            CombatUnit unitInfo = null;

            for (int i = 0; i < combatUnitInfo.Count; i++)
            {
                if (unitType == combatUnitInfo[i].unitType)
                {
                    unitInfo = combatUnitInfo[i];

                    break;
                }
            }

            if (unitInfo != null)
            {
                //todo:check if resource is enough
                bool canProduceUnit = true;

                for (int i = 0; i < unitInfo.costResourceTypes.Count; i++)
                {
                    if (!Player.Instance.IsResourceEnough(unitInfo.costResourceTypes[i], unitInfo.costResources[i] * produceAmount))
                    {
                        EventManager.GetInstance().ExecuteEvent <EventAlert>(new EventAlert("Warning", "Not enough resources"));

                        canProduceUnit = false;

                        break;
                    }
                }

                if (canProduceUnit)
                {
                    Dictionary <ResourceType, float> resourceCost = new Dictionary <ResourceType, float>();

                    for (int i = 0; i < unitInfo.costResourceTypes.Count; i++)
                    {
                        resourceCost.Add(unitInfo.costResourceTypes[i], unitInfo.costResources[i]);
                    }

                    if (data.ProduceCombatUnit(unitInfo.unitType, produceAmount, unitInfo.generateDuration * produceAmount, resourceCost))
                    {
                        _task = ProduceUnitTask.CreateTask(unitInfo.unitType, produceAmount, OnProducingUnitComplete, OnTimeLeftToComplete, unitInfo.generateDuration * produceAmount);

                        TaskManager.Instance.AddTask(_task);

                        //todo:post produce unit event
                        List <ResourceType> costType = new List <ResourceType>();
                        foreach (ResourceType type in resourceCost.Keys)
                        {
                            costType.Add(type);
                        }

                        Architecture archi = GetComponent <Architecture> ();

                        if (archi.isSelected)
                        {
                            UIArchitectureMenuController.Instance.GetMenu(transform, ArchitectureMenuType.OnTask);
                        }

                        EventManager.GetInstance().ExecuteEvent <EventProduceCombatUnit>(new EventProduceCombatUnit(costType));

                        return(true);
                    }
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                Debug.LogError(gameObject.name + " unable to produce combat unit, no unit definition");
            }
        }

        return(false);
    }
Ejemplo n.º 12
0
    /// <summary>
    /// Calculates the battle result.
    /// </summary>
    public void CalculateResult()
    {
        BattleReportMetaData reportData = BattleReportMetaData.Load();

        int fAttackerSum = GetAttackerSum();
        int fDefenderSum = GetDefenderSum();

        if ((fAttackerSum - fDefenderSum) > 0)       //attacker win, defender defeat
        {
            Debug.Log("Attacker " + GetAttackerSum() + " vs " + targetName + " " + GetDefenderSum() + " win");

            float ratio = (float)fDefenderSum * 100f / (float)fAttackerSum / 100f;

            CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load();

            //calculate unit lost for attacker
            List <UnitLostInfo> attackerLostInfo = new List <UnitLostInfo>();

            foreach (CombatUnit unit in attackerUnit.Keys)
            {
                int lostUnit = Mathf.Clamp((int)(ratio * attackerUnit[unit]), 1, attackerUnit[unit]);

                UnitLostInfo info = new UnitLostInfo(unit.unitType, attackerUnit[unit], lostUnit);

                //return unit
                data.AddCombatUnit(unit.unitType, info.to);

                attackerLostInfo.Add(info);
            }

            //calculate unit lost for defender
            List <UnitLostInfo> targetLostInfo = new List <UnitLostInfo>();

            foreach (CombatUnit unit in targetUnit.Keys)
            {
                float penalty = Mathf.Clamp((1 - ratio) * 10f, 1f, 10f);

                int lostUnit = Mathf.Clamp((int)(ratio * penalty * targetUnit[unit]), 1, targetUnit[unit]);

                //TODO:Decrease ai player unit

                targetLostInfo.Add(new UnitLostInfo(unit.unitType, targetUnit[unit], lostUnit));
            }

            result = AttackResult.Win;

            reportData.AddBattleReport(result, targetName, "Player", attackerLostInfo, targetLostInfo, resourceAward);

            //add resource
            PlayerResourceStorageMetaData pData = PlayerResourceStorageMetaData.Load();

            foreach (ResourceType type in resourceAward.Keys)
            {
                pData.AddResourceForType(type, resourceAward[type]);
            }
        }
        else if ((fAttackerSum - fDefenderSum) < 0)       //attacker defeat, defender win
        {
            Debug.Log("Attacker " + GetAttackerSum() + " vs " + targetName + " " + GetDefenderSum() + " fail");

            float ratio = (float)fAttackerSum * 100f / (float)fDefenderSum / 100f;

            CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load();

            //calculate unit lost for attacker
            List <UnitLostInfo> attackerLostInfo = new List <UnitLostInfo>();

            foreach (CombatUnit unit in attackerUnit.Keys)
            {
                float penalty = Mathf.Clamp((1 - ratio) * 10f, 1f, 10f);

                int lostUnit = Mathf.Clamp((int)(ratio * penalty * attackerUnit[unit]), 1, attackerUnit[unit]);

                UnitLostInfo info = new UnitLostInfo(unit.unitType, attackerUnit[unit], lostUnit);

                //return unit
                data.AddCombatUnit(unit.unitType, info.to);

                attackerLostInfo.Add(info);
            }

            //calculate unit lost for defender
            List <UnitLostInfo> targetLostInfo = new List <UnitLostInfo>();

            foreach (CombatUnit unit in targetUnit.Keys)
            {
                int lostUnit = Mathf.Clamp((int)(ratio * targetUnit[unit]), 1, targetUnit[unit]);

                //TODO:Decrease ai player unit

                targetLostInfo.Add(new UnitLostInfo(unit.unitType, targetUnit[unit], lostUnit));
            }

            result = AttackResult.Lost;

            reportData.AddBattleReport(result, targetName, "Player", attackerLostInfo, targetLostInfo);
        }
        else        //draw
        {
            Debug.Log("Attacker " + GetAttackerSum() + " vs " + targetName + " " + GetDefenderSum() + " draw");

            float ratio = (float)fAttackerSum * 100f / (float)fDefenderSum / 100f * 0.5f;

            CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load();

            //calculate unit lost for attacker
            List <UnitLostInfo> attackerLostInfo = new List <UnitLostInfo>();

            foreach (CombatUnit unit in attackerUnit.Keys)
            {
                int lostUnit = Mathf.Clamp((int)(ratio * 0.3f * attackerUnit[unit]), 1, attackerUnit[unit]);

                UnitLostInfo info = new UnitLostInfo(unit.unitType, attackerUnit[unit], lostUnit);

                //return unit
                data.AddCombatUnit(unit.unitType, info.to);

                attackerLostInfo.Add(info);
            }

            //calculate unit lost for defender
            List <UnitLostInfo> targetLostInfo = new List <UnitLostInfo>();

            foreach (CombatUnit unit in targetUnit.Keys)
            {
                int lostUnit = Mathf.Clamp((int)(ratio * 0.3f * targetUnit[unit]), 1, targetUnit[unit]);

                //TODO:Decrease ai player unit

                targetLostInfo.Add(new UnitLostInfo(unit.unitType, targetUnit[unit], lostUnit));
            }

            result = AttackResult.Draw;

            reportData.AddBattleReport(result, targetName, "Player", attackerLostInfo, targetLostInfo);
        }
    }