搬运一批Bedrock wiki内容,完善翻译

This commit is contained in:
boybook
2025-03-20 00:13:44 +08:00
parent ead7392a76
commit 4896c1a4f2
163 changed files with 33930 additions and 1464 deletions

View File

@@ -1,8 +1,8 @@
---
title: Entity Timers
category: Tutorials
title: 实体计时器
category: 教程
tags:
- intermediate
- 中级
mentions:
- SirLich
- Joelant05
@@ -13,32 +13,36 @@ mentions:
- zheaEvyline
---
Time-based interactions are extremely useful tools for map making. This article hopes to provide an extensive list which details the many ways which timers can be made. For convenience, this page will be split up into two main sections: component-based timers and animation-based timers. Each has their own advantages and disadvantages, which will be outlined in their respective sections.
You might also find useful [Scoreboard Timers](/commands/scoreboard-timers).
# 实体计时器
## Component-based timers
<!--@include: @/wiki/bedrock-wiki-mirror.md-->
Component-based timers are done inside the entity.json file of the behavior pack. They have the distinct advantage of persisting upon the entity being reloaded, but are limited by the number of timing components (duplicate components replace each other, which means defining multiple timers using the `minecraft:timer` component isn't possible).
基于时间的交互是地图制作的超实用工具。本文旨在提供一份详尽的指南,详解多种创建计时器的方法。为方便阅读,本页将分为两大部分:组件计时器和动画计时器。每种方式都有其独特优劣,我们将在对应章节详细探讨。
### minecraft:timer
你可能也会对[计分板计时器](/commands/scoreboard-timers)感兴趣。
This is the simplest but most effective component for triggering events after an elapsed amount of time. The component [minecraft:timer](https://bedrock.dev/docs/1.14.0.0/1.14.30.2/Entities#minecraft:timer) provides three main ways in which the amount of time before the event can be defined:
## 基于组件的计时器
- Exact timing: an exact amount of time after which the event will fire is defined (e.g. 3.4 seconds)
- Random interval: an interval is defined in which the event will fire at a random time inside that interval (e.g. between 3 to 5 seconds)
- Weighted random choice: a number of times are defined and assigned weights, one of which will be chosen for the event to fire (e.g. a 20% chance for the event to fire at 5 seconds, and an 80% chance to fire at 20 seconds)
组件计时器通过行为包的`entity.json`文件实现。其最大优点是实体重载时仍能保持计时状态,但受限于可用的计时组件数量(重复组件会互相覆盖,意味着无法通过`minecraft:timer`组件创建多个独立计时器)。
In the vanilla Behavior Pack, this component is used in all kinds of circumstances. For example:
### minecraft:timer组件
- The dolphin can only spend 20 seconds on land before it dries out
- Bees will perish between 10 and 60 seconds after stinging
- The wandering trader will only stay for either 2400 or 3600 seconds
这是最简单却最高效的延时触发事件组件。[minecraft:timer](https://bedrock.dev/docs/1.14.0.0/1.14.30.2/Entities#minecraft:timer)提供三种主要时间设定方式:
A simple example which triggers an event after 5.6 seconds:
- **精确计时**固定时间后触发事件例如3.4秒)
- **随机区间**在指定时间区间内随机触发例如3到5秒之间
- **权重随机选择**定义多组时间选项配比权重随机选择其中一个时间触发例如20%概率5秒触发80%概率20秒触发
<CodeHeader></CodeHeader>
在官方行为包中,该组件被广泛应用。例如:
```json
- 海豚在陆地超过20秒后会脱水
- 蜜蜂在蜇人后10-60秒随机死亡
- 流浪商人停留时间为2400秒或3600秒
基础示例5.6秒后触发事件):
::: code-group
```json [实体组件]
"minecraft:timer": {
"time": 5.6,
"time_down_event": {
@@ -46,31 +50,19 @@ A simple example which triggers an event after 5.6 seconds:
}
}
```
:::
A more complex example which triggers an event after a randomized amount of delay using weighted values:
进阶示例(使用权重系统随机延时触发):
<CodeHeader></CodeHeader>
```json
::: code-group
```json [实体组件]
"minecraft:timer": {
"looping": false, //true will fires event after every execution, false will fire event only once.
"looping": false, //true表示循环执行false表示仅执行一次
"random_time_choices": [
{
"weight": 25,
"value": 0.5 //Half a second of delay
},
{
"weight": 25,
"value": 10 //Ten seconds of delay
},
{
"weight": 25,
"value": 30 //Thirty seconds of delay
},
{
"weight": 25,
"value": 120 //2 minutes of delay
}
{"weight":25, "value":0.5}, //0.5秒延时
{"weight":25, "value":10}, //10秒延时
{"weight":25, "value":30}, //30秒延时
{"weight":25, "value":120} //2分钟延时
],
"time_down_event": {
"event": "wiki:event",
@@ -78,69 +70,59 @@ A more complex example which triggers an event after a randomized amount of dela
}
}
```
:::
A particularly useful way to handle time events is using a single, looping `minecraft:timer` component and processing the events on each tick (or however often you decide to fire the timer). This is done by using the `randomize` parameter in events, where a weight may be used determine how often other events will be run. This can get you a lot of extra mileage out of a single timer component.
高效利用技巧:通过循环触发`minecraft:timer`,配合事件中的`randomize`参数实现多样效果。这里通过权重控制事件触发频率:
<CodeHeader></CodeHeader>
```json
::: code-group
```json [事件配置]
"wiki:do_event": {
"randomize": [
{
"weight": 1,
"add": {
"component_groups": [
"wiki:my_event"
]
}
"weight": 1, //1/56概率触发稀有事件
"add": {"component_groups":["wiki:my_event"]}
},
{
"weight": 5,
"add": {
"component_groups": [
"wiki:my_more_frequent_event"
]
}
"weight": 5, //5/56概率触发常见事件
"add": {"component_groups":["wiki:my_more_frequent_event"]}
},
{
"weight": 50 //Fires nothing
"weight": 50 //50/56概率无事件触发
}
]
}
```
:::
### minecraft:environment_sensor
### minecraft:environment_sensor组件
Another component ([minecraft:environment_sensor](https://bedrock.dev/docs/stable/Entities#minecraft:environment_sensor)) which can be very useful for time-based events is `minecraft:environment_sensor`. Pairing this sensor with the `hourly_clock_time` or `clock_time` filters can be used to trigger events based off in-game time.
当结合`hourly_clock_time`或`clock_time`筛选器时,[minecraft:environment_sensor](https://bedrock.dev/docs/stable/Entities#minecraft:environment_sensor)可用于游戏内时间触发事件。
Here is an example which is used to fire an event 800 ticks after the start of the day (valid range is 0 to 24000):
示例(每日开始800tick后触发事件):
<CodeHeader></CodeHeader>
```json
::: code-group
```json [实体组件]
"minecraft:environment_sensor": {
"triggers": [
{
"filters": {
"test": "hourly_clock_time",
"operator": "=",
"value": 800
},
"event": "wiki:my_daily_event"
}
]
"triggers": [{
"filters": {
"test": "hourly_clock_time",
"operator": "=",
"value": 800
},
"event": "wiki:my_daily_event"
}]
}
```
:::
### minecraft:ageable
### minecraft:ageable组件
If this component ([minecraft:ageable](https://bedrock.dev/docs/stable/Entities#minecraft:ageable)) is not being used in the entity's behavior for a different purpose, it can be useful as an additional timer. It's important to note that it requires the `minecraft:is_baby` component to be defined in order to function.
当行为包未占用[miinecraft:ageable](https://bedrock.dev/docs/stable/Entities#minecraft:ageable)时,可配合`minecraft:is_baby`组件实现计时功能。
Here is an example which fires an event after four seconds:
示例4秒后触发事件
<CodeHeader></CodeHeader>
```json
::: code-group
```json [实体组件]
"minecraft:is_baby": {},
"minecraft:ageable": {
"duration": 4,
@@ -150,171 +132,138 @@ Here is an example which fires an event after four seconds:
}
}
```
:::
### Other dummy-timers:
### 其他计时组件思路
Taking a peak at the docs suggest there are other components which can also can be used for timing. Essentially, you are looking for any component with a "time down event" or a "duration".
查阅文档可发现更多具有"time_down_event""duration"参数的潜在组件,例如:
Non-exhaustive list of promising examples:
- `minecraft:angry`(需要攻击目标,时间限定整数)
- `minecraft.behavior.hide`
- `minecraft:behavior.celebrate`
- `minecraft:angry` (requires the entity to have a target, time must be an integer)
- `minecraft.behavior.hide`
- `minecraft:behavior.celebrate`
## 基于动画的计时器
## Animation-based timers
行为包动画是实现定时事件的强力工具。其优势在于理论上可创造无限计时器,但存在重载实体时重置的局限(玩家退出世界或区块卸载后重新加载会重置计时器)。
Behavior pack animations are an extremely powerful tool for triggering time-based events. They have the distinct advantage of providing an "infinite" amount of timers, but are restarted upon an entity being reloaded (leaving and rejoining the world or the chunk containing the entity unloading will cause the timer to restart when the entity reloads).
动画在行为包中的运作方式与资源包不同建议通过官方文档或wiki其他页面了解基本机制。
Animations function differently in behavior packs than in resource packs. If you are unfamiliar with how they operate, it is recommended to learn more about them by checking out the official documentation or the other pages on this wiki.
### 基础动画计时
### Simple timers
通过动画控制器或直接执行时间线指令,可以实现精确时序触发:
By triggering animations from an animation controller or directly from the scripts section, you can execute specific events, commands, or molang expressions in a timed-sequence, called a timeline.
You can set up timelines like this:
<CodeHeader></CodeHeader>
```json
::: code-group
```json [动画定义]
{
"format_version": "1.8.0",
"animations": {
"animation.command.example_timeline": {
"timeline": {
"0.0": "/say this will trigger instantly",
"3.0": "/say this will trigger after 3 seconds"
"0.0": "/say 立即触发",
"3.0": "/say 3秒后触发"
},
"animation_length": 3.1
},
"animation.command.example_timeline_2": {
"timeline": {
"100": "/say this will trigger after 100 seconds",
"100": "/say 100秒后触发",
"0.0": [
"/say you can trigger multiple events at once",
"/say by using timelines."
"/say 同时触发多个指令",
"/say 通过时间线组实现"
],
"55.55": "/say this will trigger after 55.55 seconds."
"55.55": "/say 55.55秒后触发"
},
"animation_length": 100.1
}
}
}
```
:::
### Random interval
### 随机间隔实现
A very useful feature of the timer component is its ability to define a random interval in which the event will be triggered. This functionality can be replicated using animations and a controller. Below is an example of an animation triggered by adding the `minecraft:is_sheared` component to an entity which randomly fires an event between 2 to 7 seconds after activation. Animation and controller version 1.10.0.
通过动画控制器模拟`minecraft:timer`的随机区间功能。示例实体被剪毛后2-7秒随机触发事件。
<CodeHeader></CodeHeader>
```json
::: code-group
```json [动画控制器]
"controller.animation.shanewolf.random_interval": {
"initial_state": "inactive",
"states": {
"inactive": {
"transitions": [
{
"active": "q.is_sheared"
}
]
"transitions": [{"active": "q.is_sheared"}]
},
"active": {
"on_entry": [
"v.random_interval = math.random(2, 7);",
"/say random interval started"
],
"animations": [
"wiki:animate_interval"
],
"transitions": [
{
"inactive": "q.anim_time >= v.random_interval"
}
"/say 随机计时开始"
],
"animations": ["wiki:animate_interval"],
"transitions": [{
"inactive": "q.anim_time >= v.random_interval"
}],
"on_exit": [
"@s wiki:stop_random_interval",
"/say random interval finished"
"/say 随机计时结束"
]
}
}
}
```
:::
<CodeHeader></CodeHeader>
```json
::: code-group
```json [动画定义]
"animation.shanewolf.random_interval": {
"animation_length": 100
}
```
:::
Explanation: Upon entry into the state beginning the animation, a variable is given a random value between 2 and 7. The animation finishes when the current animation time is greater than or equal to the value of this v.
实现逻辑进入激活状态时生成2-7秒随机数变量动画播放时间超过变量值时退出状态。
**Notes**:
- The animation length can be set to any value greater than the maximum end of the time range (100 is used as a general template)
- math.random(a, b) is used to trigger an event in the range [a, b]
- math.floor(math.random(a, b.99)) may be used to end the timer at integer values (0.99 must be added to b)
- Any events or commands to run when the animation is finished are put inside on_exit
注意事项:
- 动画总时长需大于最大可能值
- 使用`math.floor(math.random(a, b.99))`可生成整数结果
- 收尾指令写入on_exit事件
### Weighted random choice
### 权重选择实现
Another useful feature of the timer component is its ability to trigger events at a time determined by a weighted list of values. This functionality can also be replicated using animations and a controller. Below is an example of an animation triggered by adding the `minecraft:is_charged` component to an entity which randomly fires an event at either 2, 5, or 9 seconds with weights of 30, 60, and 10, respectively. Animation and controller version 1.10.0.
通过动画控制器模拟权重时间选择功能。示例带电实体30%概率2秒、60%概率5秒、10%概率9秒触发事件。
<CodeHeader></CodeHeader>
```json
::: code-group
```json [动画控制器]
"controller.animation.shanewolf.random_choices": {
"initial_state": "inactive",
"states": {
"inactive": {
"transitions": [
{
"active": "q.is_powered"
}
]
"transitions": [{"active": "q.is_powered"}]
},
"active": {
"on_entry": [
"v.random_choices = math.random(0, 100);",
"/say random interval started"
],
"animations": [
"wiki:animate_choices"
"/say 随机选择开始"
],
"animations": ["wiki:animate_choices"],
"transitions": [
{
"inactive": "q.anim_time >= 2.0 && v.random_choices < 30"
},
{
"inactive": "q.anim_time >= 5.0 && v.random_choices < 90"
},
{
"inactive": "q.anim_time >= 9.0 && v.random_choices <= 100"
}
{"inactive": "q.anim_time >= 2.0 && v.random_choices < 30"},
{"inactive": "q.anim_time >= 5.0 && v.random_choices < 90"},
{"inactive": "q.anim_time >= 9.0 && v.random_choices <= 100"}
],
"on_exit": [
"@s wiki:stop_random_choices",
"/say random choices finished"
"/say 随机选择结束"
]
}
}
}
```
:::
<CodeHeader></CodeHeader>
实现逻辑生成0-100随机数变量通过多个状态切换条件实现权重分段检查。
```json
"animation.shanewolf.random_choices": {
"animation_length": 100
}
```
注意事项:
- 时间段需从小到大排列
- 通过权重累加值划分概率区间
- 按规范处理收尾事件
Explanation: Upon entry into the state beginning the animation, a variable is given a random value between 0 and 100 (sum of the weights). The transitions are laid out with the list of values ordered from the smallest time to the largest time. This is done so multiple && operators are not required in the latter transitions to define the variable's range (the query for the smallest times return true first and have their weights checked before the others--flipping 2 and 5 would result in 2 mistakenly having a weight of 90 instead of 30). The animation finishes when the current animation time is greater than or equal to a time in the list and the value of the random variable falls within that time's defined weight range.
**Notes**:
- The animation length can be set to any value greater than the maximum end of the time range (100 is used as a general template)
- For this particular format to work, order the list of valid times from smallest to largest
- To assign a weight to a time in the list, add the weight to the value the randomized variable must be less than in the list's previous entry (e.g. 5 seconds has a weight of 90 - 30 = 60)
- Any events or commands to run when the animation is finished are put inside on_exit
Hopefully this spread some light on the subject of handling time in Minecraft Bedrock! As shown above, there are many possible ways it can be done, each with their own pros and cons. If you have any other useful methods for creating time-based events, please [contribute to the wiki](/contribute)!
希望本指南能帮助你更好地掌握基岩版的时间管理技巧!如果你有其他巧妙的时间事件实现方法,欢迎[参与wiki编辑](/contribute)