1.0.3 • Published 1 year ago

input-action v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

简介

这是一个简单的,不成熟的,类似于 ue4 输入系统的输入系统. 你可以使用Action来抽象输入, 而不是去监听具体的按键变化.

示例

比如, 如果你想在babylonjs中控制一个box前后左右移动, 你可以这么写

    // 配置输入
let input = new InputActionManage("jump", "moveY", "moveX")
let {jump, moveX, moveY} = input.actions
jump.addKey(" ").type("up") // click down up
moveY.addKey("w").scale(1)
moveY.addKey("s").scale(-1)
moveX.addKey("d").scale(1)
moveX.addKey("a").scale(-1)
input.attach(scene.getEngine().getInputElement())
let box = MeshBuilder.CreateBox("")

// 监听输入
scene.onBeforeRenderObservable.add(eventData => {
    let x = moveX.value
    let y = moveY.value
    box.position.addInPlace(new Vector3(x, 0, y).scale(scene.deltaTime * 0.001))
})
input.actions.jump.addListener(() => {
    console.log("jump")
})

这段代码定义了三个Action, 分别是跳跃, 前后移动和左右移动.

然后为jump绑定了空格键, .type的意思是什么时候触发 总共有三种, 分别是 按下时, 松开时和点击时.

为前后移动绑定了w和s按键, 其中的scale代表键按下的时候会应用的值. w为1 ,s为-1.

这样按下w时 moveY的值就是1, 按下s时 moveY的值就是-1. 两个同时按下的话, 值会是0.

类型支持

用typescript, 当然要支持自动补全啦. 定义的action名字, 可以在actions下面被补全出来.

img.png

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago