# 练习2:会攻击侵犯领地的凶狠水鸭
# 作者:境界
凶狠水鸭示例包下载:下载示例包 (opens new window)。
{
"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": {
}
}
}
攻击侵犯领地的行为,可以想象成为靠近水鸭多近会引起水鸭的攻击。因此在设置一个生物拥有近战能力的时候,需要用三个行为进行组合:
1)minecraft:attack,它的damage值可以接受一个数组,也可以接受一个整数数值。数组情况下,可以让伤害在数组内由大到小取一个随机伤害,而一个固定数值则代表的是固定伤害。
2)minecraft:behavior.nearest_attackable_target,它的entity_types接受一连串的生物目标,我们在目标选择器中,同时将目标设置为mob,但又不是duck,即所有带有mob标签却不带有duck标签的生物。max_dist代表在多远距离内会被当作目标,must_see意为是否必须看到才会当作目标,reselect_targets会在一群目标里不断选择离自己最近的目标。
3)minecraft: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标签的实体,那样就会包括了鸡这类生物,不符合实际需求。
进阶
20分钟