全局功能
在铁锈战争中,全局功能用于定义跨单位的共享功能和资源。本章节将详细介绍如何配置全局资源、全局标签、内存系统等。
全局资源
全局资源在整个游戏中共享,所有玩家单位都可以访问:
[global_resource_NAME] 全局资源定义
[global_resource_Gold]
displayName:金币
displayNameShort:¥
displayColor:#FFD700
iconImage:resources/gold_icon.png
全局资源属性说明
| 属性 | 说明 | 示例 |
|---|---|---|
| displayName | 资源显示名称 | displayName:金币 |
| displayNameShort | 资源短名称 | displayNameShort:¥ |
| displayColor | 显示颜色 | displayColor:#FFD700 |
| iconImage | 资源图标 | iconImage:resources/gold_icon.png |
| hidden | 是否隐藏 | hidden:true |
| priority | 优先级 | priority:1 |
局部资源
局部资源仅在特定单位上存在:
[resource_NAME] 局部资源定义
[resource_MyResource]
displayName:自定义资源
displayNameShort:R
displayColor:#00FF00
内存系统
内存系统用于存储单位的自定义数据:
[defineUnitMemory] 内存定义
[defineUnitMemory]
experience:0
level:1
upgradePoints:0
内存属性说明
| 属性 | 说明 | 示例 |
|---|---|---|
| experience | 经验值 | experience:0 |
| level | 等级 | level:1 |
| upgradePoints | 升级点数 | upgradePoints:0 |
内存访问
可以访问和修改内存数据:
内存访问语法
# 读取内存
if memory.experience > 100
# 修改内存
addResourcesWithLogic: experience += 10
# 使用内存进行计算
autoTrigger: if memory.level * 100 <= memory.experience
# 内存数组
[defineUnitMemory]
abilities[5]:0
全局标签
全局标签用于跨单位的标记系统:
全局标签使用
# 添加全局标签
addGlobalTeamTags:tech_unlocked
# 移除全局标签
removeGlobalTeamTags:tech_unlocked
# 检查全局标签
if self.globalTeamTags(includes="tech_unlocked")
模板系统
模板系统用于代码复用:
[template_NAME] 模板定义
[template_basicTurret]
turnSpeed:2
size:10
barrelX:0
barrelY:15
[template_advancedTurret]
@copyFromSection: template_basicTurret
turnSpeed:3
size:15
模板属性说明
| 属性 | 说明 | 示例 |
|---|---|---|
| @copyFromSection | 复制节 | @copyFromSection: template_basicTurret |
| @define X: |
局部变量 | @define damage: 50 |
| @global X: |
全局变量 | @global damage: 50 |
| ${X} | 变量引用 | directDamage=${damage} |
变量系统
变量系统用于动态值计算:
变量使用
# 局部变量
@define baseDamage: 25
# 全局变量
@global maxLevel: 10
# 变量引用
[turret_1]
directDamage:${baseDamage}
# 键值引用
addResources: credits=${core.price * 2 + 10}
单位引用系统
单位引用系统用于访问其他单位:
单位引用
# 当前动作目标
if thisActionTarget.resource.credits > 100
# 事件来源
if eventSource.resource.energy > 50
# 附件
if attachment.resource.hp > 200
# 运输单位
if transporting.resource.shield > 100
# 攻击目标
if attacking.resource.ammo > 10
# 最后伤害源
if lastDamagedBy.resource.level > 5
# 父单位
if parent.resource.experience > 1000
# 活动路径点目标
if activeWaypointTarget.resource.credits > 500
标记功能
标记功能用于创建临时位置标记:
标记使用
# 获取为标记
if self.lastDamagedBy.getAsMarker() == nearestUnit()
# 获取绝对偏移
if lastDamagedBy.getOffsetAbsolute(x=10, y=20) == self
# 获取相对偏移
if lastDamagedBy.getOffsetRelative(x=10, y=20) == self
# 创建标记
if createMarker(x=100, y=100).nearestUnit == null
全局功能函数
全局功能函数提供各种计算功能:
函数使用
# 距离计算
if distance(self.x(), self.y(), memory.target.x(), memory.target.y()) < 400
# 距离平方计算
if distanceSquared(self.x(), self.y(), memory.target.x(), memory.target.y()) < 160000
# 单位间距离
if distanceBetween(self, activeWaypointTarget) < 100
# 单位间距离平方
if distanceBetweenSquared(self, self.attacking) > 10000
# 方向计算
if direction(1, 1, 10, 10) == 45
# 单位间方向
if directionBetween(self, self.customTarget1) == 90
# 随机数
if rnd(1, 100) == 50
# 数学函数
if int(4.2) == 4
if squareRoot(4) == 2
if min(5, 10) == 5
if max(5, 10) == 10
if cos(60) == 0.5
if sin(30) == 0.5
字符串处理
字符串处理功能用于文本操作:
字符串函数
# 转为字符串
if str(self.energy) + 'x' == '100x'
# 子字符串
if substring('hello', 0, 2) == 'he'
# 字符串长度
if length("string") == 6
# 大小写转换
if uppercase("abc") == "ABC"
if lowercase("ABC") == "abc"
完整全局功能示例
全局资源示例
# all-units.template文件内容
[global_resource_Gold]
displayName:金币
displayNameShort:¥
displayColor:#FFD700
iconImage:resources/gold_icon.png
displayPos:1
[global_resource_Manpower]
displayName:人力
displayNameShort:♂
displayColor:#FF6347
iconImage:resources/manpower_icon.png
displayPos:2
[resource_Experience]
displayName:经验
displayNameShort:EXP
displayColor:#9370DB
内存系统示例
[defineUnitMemory]
level:1
experience:0
upgradePoints:0
abilities[5]:0
[hiddenAction_levelUp]
autoTrigger=if memory.experience >= memory.level * 100
addResourcesWithLogic: level += 1
addResourcesWithLogic: upgradePoints += 1
spawnEffects:level_up_effect
[hiddenAction_gainExperience]
autoTrigger=if self.kills(greaterThan=memory.lastKillCount)
addResourcesWithLogic: experience += 10
addResourcesWithLogic: lastKillCount = self.kills
模板系统示例
[template_basicProjectile]
directDamage:25
life:30
speed:15
image:projectiles/bullet.png
drawType:1
color:#FFFF00
[template_advancedProjectile]
@copyFromSection: template_basicProjectile
directDamage:50
speed:20
color:#FF4500
[projectile_1]
@copyFromSection: template_advancedProjectile
注意事项
- 合理设计全局资源,避免过多影响性能
- 平衡内存使用,避免存储过多无用数据
- 正确使用模板系统,提高代码复用性
- 测试全局功能在多人游戏中的表现
- 考虑全局功能与游戏整体设计的一致性