Example #1
0
        public override void Execute(Level level)
        {
            ClientAvatar         ca  = level.GetPlayerAvatar();
            GameObject           go  = level.GameObjectManager.GetGameObjectByID(BuildingId);
            Building             b   = (Building)go;
            UnitUpgradeComponent uuc = b.GetUnitUpgradeComponent();
            int unitLevel            = ca.GetUnitUpgradeLevel(this.UnitData);

            if (uuc.CanStartUpgrading(this.UnitData))
            {
                int          cost = this.UnitData.GetUpgradeCost(unitLevel);
                ResourceData rd   = this.UnitData.GetUpgradeResource(unitLevel);
                if (ca.HasEnoughResources(rd, cost))
                {
                    ca.SetResourceCount(rd, ca.GetResourceCount(rd) - cost);
                    uuc.StartUpgrading(this.UnitData);
                }
            }
        }
Example #2
0
        internal override void Execute()
        {
            Level level = this.Device.GameMode.Level;

            if (this.Unit != null)
            {
                GameObject gameObject = level.GameObjectManager.Filter.GetGameObjectById(this.BuildingId);
                if (gameObject != null)
                {
                    if (gameObject is Building)
                    {
                        Building             building             = (Building)gameObject;
                        UnitUpgradeComponent unitUpgradeComponent = building.UnitUpgradeComponent;

                        if (unitUpgradeComponent != null)
                        {
                            int unitLevel = level.Player.GetUnitUpgradeLevel(this.Unit);

                            ResourceData resourceData = this.UnitType == 1 ? ((SpellData)this.Unit).UpgradeResourceData : ((CharacterData)this.Unit).UpgradeResourceData;
                            int          upgradeCost  = this.UnitType == 1 ? ((SpellData)this.Unit).UpgradeCost[unitLevel] : ((CharacterData)this.Unit).UpgradeCost[unitLevel];

                            if (resourceData != null)
                            {
                                if (level.Player.Resources.GetCountByData(resourceData) >= upgradeCost)
                                {
                                    if (unitUpgradeComponent.CanStartUpgrading(this.Unit))
                                    {
                                        level.Player.Resources.Remove(resourceData, upgradeCost);
                                        unitUpgradeComponent.StartUpgrading(this.Unit);
                                    }
                                    else
                                    {
                                        Logging.Error(this.GetType(),
                                                      "Unable to upgrade the unit. The UnitUpgradeComponent probably training other unit.");
                                    }
                                }
                                else
                                {
                                    Logging.Error(this.GetType(),
                                                  "Unable to upgrade the unit. The player doesn't have enough resources.");
                                }
                            }
                            else
                            {
                                Logging.Error(this.GetType(),
                                              "Unable to upgrade the unit. The resources data is null.");
                            }
                        }
                        else
                        {
                            Logging.Error(this.GetType(),
                                          "Unable to upgrade the unit. The game object doesn't contain a UnitUpgradeComponent.");
                        }
                    }
                    else
                    {
                        Logging.Error(this.GetType(),
                                      "Unable to upgrade the unit. The game object is not valid or not exist");
                    }
                }
                else
                {
                    Logging.Error(this.GetType(), "Unable to upgrade the unit. The game object is null");
                }
            }
            else
            {
                Logging.Error(this.GetType(), "Unable to to upgrade the unit. The unit data is null");
            }
        }