Exemplo n.º 1
0
        /// <summary>
        /// Loads the weapon data of a building/unit type from the given element.
        /// </summary>
        /// <param name="weaponDataElem">The XML element to load from.</param>
        /// <param name="metadata">The metadata object.</param>
        private static WeaponData LoadWeaponData(XElement weaponDataElem, ScenarioMetadata metadata)
        {
            XAttribute nameAttr          = weaponDataElem.Attribute(XmlMetadataConstants.WPN_NAME_ATTR);
            XAttribute displayedNameAttr = weaponDataElem.Attribute(XmlMetadataConstants.WPN_DISPLAYEDNAME_ATTR);
            XElement   cooldownElem      = weaponDataElem.Element(XmlMetadataConstants.WPN_COOLDOWN_ELEM);
            XElement   damageElem        = weaponDataElem.Element(XmlMetadataConstants.WPN_DAMAGE_ELEM);
            XElement   damageTypeElem    = weaponDataElem.Element(XmlMetadataConstants.WPN_DAMAGETYPE_ELEM);
            XElement   incrementElem     = weaponDataElem.Element(XmlMetadataConstants.WPN_INCREMENT_ELEM);
            XElement   rangeMaxElem      = weaponDataElem.Element(XmlMetadataConstants.WPN_RANGEMAX_ELEM);
            XElement   rangeMinElem      = weaponDataElem.Element(XmlMetadataConstants.WPN_RANGEMIN_ELEM);
            XElement   splashTypeElem    = weaponDataElem.Element(XmlMetadataConstants.WPN_SPLASHTYPE_ELEM);

            if (nameAttr == null)
            {
                throw new SimulatorException("Weapon name not defined!");
            }

            WeaponTypeEnum weaponType;

            if (!EnumMap <WeaponTypeEnum, string> .TryDemap(weaponDataElem.Name.LocalName, out weaponType))
            {
                throw new SimulatorException(string.Format("Unexpected weapon type '{0}' defined in weapon data!", weaponDataElem.Name.LocalName));
            }

            WeaponData weaponData = new WeaponData(nameAttr.Value, metadata, weaponType);

            if (cooldownElem != null)
            {
                weaponData.SetCooldown(XmlHelper.LoadInt(cooldownElem.Value));
            }
            if (damageElem != null)
            {
                weaponData.SetDamage(XmlHelper.LoadInt(damageElem.Value));
            }
            if (rangeMaxElem != null)
            {
                weaponData.SetRangeMax(XmlHelper.LoadInt(rangeMaxElem.Value));
            }

            if (damageTypeElem != null)
            {
                DamageTypeEnum damageType;
                if (!EnumMap <DamageTypeEnum, string> .TryDemap(damageTypeElem.Value, out damageType))
                {
                    throw new SimulatorException(string.Format("Unexpected damage type '{0}' defined in weapon data!", damageTypeElem.Value));
                }
                weaponData.SetDamageType(damageType);
            }

            if (displayedNameAttr != null)
            {
                weaponData.SetDisplayedName(displayedNameAttr.Value);
            }
            if (incrementElem != null)
            {
                weaponData.SetIncrement(XmlHelper.LoadInt(incrementElem.Value));
            }
            if (rangeMinElem != null)
            {
                weaponData.SetRangeMin(XmlHelper.LoadInt(rangeMinElem.Value));
            }
            if (splashTypeElem != null)
            {
                SplashTypeEnum splashType;
                if (!EnumMap <SplashTypeEnum, string> .TryDemap(splashTypeElem.Value, out splashType))
                {
                    throw new SimulatorException(string.Format("Unexpected splash type '{0}' defined in weapon data!", splashTypeElem.Value));
                }
                weaponData.SetSplashType(splashType);
            }

            foreach (XElement missileElem in weaponDataElem.Elements(XmlMetadataConstants.WPN_MISSILE_ELEM))
            {
                weaponData.AddMissile(LoadMissileData(missileElem, metadata));
            }

            return(weaponData);
        }