玩家输入
Player Input 是一个专为处理基于轮询的输入检测而设计的库。得益于玩家类型特定的 input 字段,该库能够在所有游戏模式、任何上下文(GUI 界面内除外)下,以 20Hz 的频率随时访问 forward(前进)、backward(后退)、left(左移)、right(右移)、jump(跳跃)、sneak(潜行)和 sprint(疾跑)这些按键绑定。
本库的目标有两方面:
- 优化对此类数据的可操作获取;许多整合包需要这些数据,但如果所有整合包同时轮询,会对性能造成负面影响。
- 创建简单且实用的开发体验(DX),用于跟踪和处理此类数据的变化。
用法
如果整合包仅在特定场景下需要为少数玩家提供此输入数据,应使用 capture/player 路径来开始跟踪数据。
如果整合包需要为大多数或所有玩家提供输入数据,应使用 capture/players 路径来开始跟踪数据,并在不再需要数据时调用 release/players(如果数据需要一直可用,请使用 #minecraft:unload)。
当接收到数据时,专用的 player_input.* /tag 标签将始终可用。
如果整合包需要每刻进行自定义跟踪,无论按键是否发生变化,可以使用 raw 入口点。如果只需检查部分玩家,请使用 #player_input:player/raw 函数标签(针对每个玩家调用),并使用你自己的 /tag 标签来过滤整合包正在处理的玩家,因为该函数标签将为库的所有用途被调用。
事件:
这些入口点会在匹配所需状态的玩家身上调用,并将上下文数据提供给各标签中的函数,从而实现基于事件的处理,而不是每刻检查状态。
要订阅其中任何一个事件,请调用相应的捕获函数,然后在用完后调用释放函数以进行清理。
与 raw 类似,请务必通过 /tag 标记你的整合包专门处理的玩家,因为该函数标签将为库的所有用途被调用。
key_update - 当玩家的输入堆栈从上刻发生更改时调用。
- 通过专用的
player_input.previous.* /tag 标签以及玩家身上的 player_input.current_tick/player_input.previous_tick 复合分数进行跟踪。
- 以下事件也可用此上下文。
key_down - 当玩家在上一刻未按住任何给定键但现在开始按住时调用。
- 通过专用的
player_input.down.* /tag 标签以及在执行期间的 keys_down player_input.current_tick 复合分数进行跟踪。
key_up - 当玩家在上一刻按住任何给定键但现在松开时调用。
- 通过专用的
player_input.up.* /tag 标签以及在执行期间的 keys_up player_input.current_tick 复合分数进行跟踪。
示例
jetpack:jetpack/equip
tag @s add jetpack.equipped
function player_input:capture/player/key_update
# Jetpack Things...
jetpack:input_received
execute if entity @s[tag=jetpack.equipped] run function jetpack:jetpack/control
jetpack:jetpack/control
# Handle changes to player input
jetpack:jetpack/unequipped
tag @s remove jetpack.equipped
function player_input:release/player/key_update
# Jetpack Things...
#player_input:player/key_update
{
"values": [
"jetpack:input_received"
],
"replace": false
}
计划
- 使用本库和 Player Motion 创建几个完整的概念验证生存整合包。
- 在 Sandstone 官方
player 库中重新实现。
- 创建一个原版使用测试工具并发布到本仓库。
- 归档当前手动编写的原版代码,自动发布生成的代码到此仓库。
- 将概念验证整合包移植到 Sandstone。
- 随 Minecraft 更新维护变更。
Player Input
Player Input is a library specifically designed for handling polling based input detection, made possible by the type specific input field on players that gives us 20hz access to the keybinds forward, backward, left, right, jump, sneak, sprint at all times, in all game modes, regardless of context, except within GUIs.
The goal of this library is two fold:
- Optimize actionable retrieval of this data; many packs need this data but its worse for performance for all of them to be polling at the same time.
- Create easy & useful DX for tracking and acting on changes in this data.
Usage
Packs that need access to this input data for few out of many players in specific scenarios should use the capture/player path to begin tracking of the data.
Packs that need access to input data for most/all of players should use the capture/players path to begin tracking of the data, and call the release/players when the data is no longer needed (use #minecraft:unload if the data is needed all of the time)
When data is being received, the exclusive player_input.* /tag labels are always made available.
Packs that need to do their own custom tracking every tick regardless of if the binds change can use the raw entrypoint. If only some players need to be checked use the #player_input:player/raw function tag (called as/at each player) and use your own /tag label to filter players your pack is operating with as the function tag will be called for all usage of the library.
Events:
These entrypoints are called as/at players that match the required state along with context data made available to functions in each tag, enabling event-based handling, instead of checking for state every tick.
To subscribe to any of these, call the appropriate capture function, then to clean up after usage, call the release function.
As with raw, make sure to /tag players that your pack is specifically handling, as the function tag will be called for all usage of the library.
key_update - Called as/at players whose input stack has changed from the previous tick.
- Tracked via the exclusive
player_input.previous.* /tag labels and the player_input.current_tick/player_input.previous_tick composite Scores on the player.
- This context is also available for the following events.
key_down - Called as/at players that were not holding down any given key(s) in the previous tick and started to.
- Tracked via the exclusive
player_input.down.* /tag labels and the keys_down player_input.current_tick composite Score during execution.
key_up - Called as/at players that were holding down any given key(s) in the previous tick and released them.
- Tracked via the exclusive
player_input.up.* /tag labels and the keys_up player_input.current_tick composite Score during execution.
Example
jetpack:jetpack/equip
tag @s add jetpack.equipped
function player_input:capture/player/key_update
# Jetpack Things...
jetpack:input_received
execute if entity @s[tag=jetpack.equipped] run function jetpack:jetpack/control
jetpack:jetpack/control
# Handle changes to player input
jetpack:jetpack/unequipped
tag @s remove jetpack.equipped
function player_input:release/player/key_update
# Jetpack Things...
#player_input:player/key_update
{
"values": [
"jetpack:input_received"
],
"replace": false
}
Plans
- Create a couple full proof-of-concept survival packs using this library and Player Motion.
- Reimplement in the Sandstone official
player library
- Create a vanilla usage harness and deploy it to this repo
- Archive the current manually written vanilla code, automatically publish generated code to this repository.
- Port proof-of-concept packs to Sandstone.
- Maintain for changes in minecraft updates.
评论 0
登录后参与评分
还没有评论,来抢沙发吧