コード例 #1
0
    /// <summary>
    /// Iterates all abilities on the
    /// </summary>
    protected void setupAbilities()
    {
        if (info.isUnit || info.isPseudoUnit)
        {
            _accumulatedModifier = new UnitAbility();
        }
        else if (info.isBuilding)
        {
            _accumulatedModifier = new BuildingAbility();
        }
        else
        {
            _accumulatedModifier = new ResourceAbility();
        }

        foreach (EntityAbility ability in info.abilities)
        {
            // Try to get class with this name
            string abilityName = ability.ability.Replace(" ", "");
            if (abilityName.Length == 0)
            {
                abilityName = ability.name.Replace(" ", "");
            }

            Ability newAbility = null;
            try
            {
                var constructor = Type.GetType(abilityName).
                                  GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(UnitAbility), typeof(GameObject) }, null);
                if (constructor == null)
                {
                    // Invalid constructor, use GenericAbility
                    newAbility = new GenericAbility(ability, gameObject);
                }
                else
                {
                    // Class found, use that!
                    newAbility = (Ability)constructor.Invoke(new object[2] {
                        ability, gameObject
                    });
                }
            }
            catch (Exception /*e*/)
            {
                // No such class, use the GenericAbility class
                newAbility = new GenericAbility(ability, gameObject);
            }

            newAbility.register(Ability.Actions.ENABLED, onAbilityToggled);
            newAbility.register(Ability.Actions.DISABLED, onAbilityToggled);

            _abilities.Add(newAbility);
        }
    }