Files
netease-modsdk-wiki/docs/mconline/10-addon教程/第07章:自定义生物/课程16.练习2-会攻击侵犯领地的凶狠水鸭.md
boybook 760c2dd9ad 2.6
2025-12-01 20:59:16 +08:00

171 lines
5.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
front:
hard: 进阶
time: 20分钟
---
# 练习2会攻击侵犯领地的凶狠水鸭
#### 作者:境界
凶狠水鸭示例包下载:下载[示例包](https://g79.gdl.netease.com/guidedemo-case7v2.zip)。
```
{
"format_version": "1.16.0",
"minecraft:entity": {
"description": {
"identifier": "design:green_head_duck",
"is_spawnable": true,
"is_summonable": true,
"is_experimental": false,
"runtime_identifier": "design:green_head_duck"
},
"component_groups": {
},
"components": {
"minecraft:breathable": {
"total_supply": 15,
"suffocate_time": 0
},
"minecraft:collision_box": {
"width": 0.6,
"height": 1.0
},
"minecraft:nameable": {
},
"minecraft:health": {
"value": 4,
"max": 4
},
"minecraft:hurt_on_condition": {
"damage_conditions": [
{
"filters": { "test": "in_lava", "subject": "self", "operator": "==", "value": true },
"cause": "lava",
"damage_per_tick": 4
}
]
},
"minecraft:movement": {
"value": 0.25
},
"minecraft:damage_sensor": {
"triggers": {
"cause": "fall",
"deals_damage": false
}
},
"minecraft:behavior.rise_to_liquid_level": {
"priority": 0,
"liquid_y_offset": -0.5,
"rise_delta": 0.01,
"sink_delta": 0.01
},
"minecraft:leashable": {
"soft_distance": 4.0,
"hard_distance": 6.0,
"max_distance": 10.0
},
"minecraft:balloonable": {
"mass": 0.5
},
"minecraft:navigation.walk": {
"can_path_over_water": true,
"can_sink": false,
"avoid_damage_blocks": true
},
"minecraft:movement.basic": {
},
"minecraft:jump.static": {
},
"minecraft:can_climb": {
},
"minecraft:despawn": {
"despawn_from_distance": {}
},
"minecraft:behavior.panic": {
"priority": 1,
"speed_multiplier": 1.5
},
"minecraft:behavior.tempt": {
"priority": 4,
"speed_multiplier": 1.0,
"items": [
"wheat_seeds",
"beetroot_seeds",
"melon_seeds",
"pumpkin_seeds"
]
},
"minecraft:type_family": {
"family": [ "duck", "mob" ]
},
"minecraft:behavior.melee_attack": {
"priority": 3,
"speed_multiplier": 1,
"track_target": false
},
"minecraft:behavior.nearest_attackable_target": {
"priority": 2,
"reselect_targets": true,
"must_see": false,
"entity_types": [
{
"filters": {
"all_of": [
{ "test": "is_family", "subject": "other", "value": "mob" },
{ "test": "is_family", "subject": "other", "operator": "not", "value": "duck"}
]
},
"max_dist": 5
}
]
},
"minecraft:attack": {
"damage": 1
},
"minecraft:behavior.random_stroll": {
"priority": 6,
"speed_multiplier": 1.0
},
"minecraft:behavior.look_at_player": {
"priority": 7,
"look_distance": 6.0,
"probability": 0.02
},
"minecraft:behavior.random_look_around": {
"priority": 8
},
"minecraft:physics": {
},
"minecraft:pushable": {
"is_pushable": true,
"is_pushable_by_piston": true
}
},
"events": {
}
}
}
```
攻击侵犯领地的行为,可以想象成为靠近水鸭多近会引起水鸭的攻击。因此在设置一个生物拥有近战能力的时候,需要用三个行为进行组合:
1minecraft:attack它的damage值可以接受一个数组也可以接受一个整数数值。数组情况下可以让伤害在数组内由大到小取一个随机伤害而一个固定数值则代表的是固定伤害。
2minecraft:behavior.nearest_attackable_target它的entity_types接受一连串的生物目标我们在目标选择器中同时将目标设置为mob但又不是duck即所有带有mob标签却不带有duck标签的生物。max_dist代表在多远距离内会被当作目标must_see意为是否必须看到才会当作目标reselect_targets会在一群目标里不断选择离自己最近的目标。
3minecraft:behavior.melee_attack,它的speed_multiplier接收一个浮点数值即保持进攻状态时速度会给予多少的加成1为100%则保持原速1.5则是150%。track_target接收一个布尔值若设置为true则会不停跟着目标即使走出攻击范围false则会在目标离开攻击范围后停止行为。
特别注意由于minecraft:type_family组件用来标记生物的家族类型原版鸡携带的家族标签为"mob“和"chicken"理论上所有原版的生物都带有这个mob标签我们将水鸭的标签保留了mob标签也是为了允许其他原版生物在处理一些需要判断生物标签的行为如寻找目标的时候能够将水鸭纳入目标范围内更加的贴近原版的生态。除此之外我们将chicken改为duck一个是强调实体携带duck标签另一个是为了在自身寻找目标的行为当中将同为duck标签的实体剔除出目标范围。若保持原来鸡的写法则会变成需要剔除chicken标签的实体那样就会包括了鸡这类生物不符合实际需求。