XML 使用说明 - 定义属性集
在游戏配置文件中,您可以使用 <AttributesDef> 元素来定义一组角色或物品的属性。每个 <AttributesDef> 元素都必须包含在顶层的 <Define> 元素之内,并遵循其通用的属性定义规则。
功能介绍
<AttributesDef> 用于封装和定义一系列常见的游戏属性,如生命值、移动速度、攻击力等。这使得在游戏中创建不同类型单位或物品时,可以方便地引用或调整这些基础属性。
参数说明
所有的参数都必须通过嵌套元素的方式进行赋值。
-
<defName>(必需,字符串)- 该属性集定义的唯一标识符。在整个游戏配置中,此名称必须是全局唯一的,除非您有意覆盖现有定义。
- 作为匿名定义时,此字段可以省略。
-
<label>(可选,字符串)- 提供一个人类可读的标签,用于更好地理解该属性集定义的用途。
-
<description>(可选,字符串)- 提供更详细的文本描述,解释该属性集定义的具体内容或特殊用途。
-
<health>(可选,整数,默认为10)- 定义实体对象的生命值或耐久度。
-
<moveSpeed>(可选,浮点数,默认为1)- 定义实体对象的移动速度。
-
<attack>(可选,整数,默认为1)- 定义实体对象的攻击力。
-
<defense>(可选,整数,默认为0)- 定义实体对象的防御力。
-
<attackSpeed>(可选,浮点数,默认为2)- 定义实体对象的攻击速度,例如每秒攻击次数或攻击间隔。
-
<attackRange>(可选,浮点数,默认为3)- 定义实体对象的攻击范围。
-
<attackTargetCount>(可选,整数,默认为1)- 定义实体对象每次攻击可以命中的目标数量。
引用方式
- 当其他定义需要引用某个属性集时,始终使用该属性集定义的
defName。
示例
示例 1:定义一个基础敌人属性集
<Define>
<AttributesDef>
<defName>BasicEnemyAttributes</defName>
<label>基础敌人属性</label>
<description>小型敌人的默认属性配置。</description>
<health>50</health>
<moveSpeed>1.5</moveSpeed>
<attack>10</attack>
<defense>2</defense>
<attackSpeed>1.8</attackSpeed>
<attackRange>2.5</attackRange>
<attackTargetCount>1</attackTargetCount>
</AttributesDef>
</Define>
在其他定义中引用此属性集:BasicEnemyAttributes
示例 2:定义一个防御塔的属性集
<Define>
<AttributesDef>
<defName>DefenseTowerAttributes</defName>
<label>防御塔属性</label>
<description>用于炮塔的属性配置。</description>
<health>200</health>
<moveSpeed>0</moveSpeed> <!-- 塔不能移动 -->
<attack>25</attack>
<defense>10</defense>
<attackSpeed>1.0</attackSpeed>
<attackRange>7.0</attackRange>
<attackTargetCount>1</attackTargetCount>
</AttributesDef>
</Define>
在其他定义中引用此属性集:DefenseTowerAttributes
示例 3:在另一个定义中以内联方式定义属性集(匿名定义)
某些情况下,当一个属性集只在特定位置使用,且不需要全局引用时,可以作为另一个定义的子元素进行匿名定义,此时 <defName> 可省略。
<Define>
<UnitDef>
<defName>SpecialBossUnit</defName>
<sprite>BossImage</sprite>
<baseAttributes><!-- 匿名定义,没有 defName -->
<health>1000</health>
<moveSpeed>0.8</moveSpeed>
<attack>50</attack>
<defense>20</defense>
<attackSpeed>0.5</attackSpeed>
<attackRange>4.0</attackRange>
<attackTargetCount>3</attackTargetCount>
</baseAttributes>
</UnitDef>
</Define>
在此示例中,<AttributesDef> 没有 defName,它的属性只供 SpecialBossUnit 使用。
注意事项
defName唯一性: 强烈建议所有<AttributesDef>的defName都是全局唯一的,以避免在加载和引用时出现混淆或不确定的行为。如果存在重复的defName,后加载的定义将会覆盖先加载的。- 匿名定义: 当
<AttributesDef>作为其他定义的内部结构使用时,defName可以省略,此时它将作为一个匿名的、局部可用的属性集定义。在这种情况下,该属性集无法通过名称在外部引用。 - 默认值: 如果某个属性未在 XML 中明确定义,它将使用其默认值。在设计属性集时,可以只指定需要修改的属性,其他属性将沿用默认值。