创建自定义键盘控件[Elm]

我正在尝试为4人游戏创建自定义键盘控件.现在,键是这样预先确定的:

type Orient = { x:Int, y:Int }
type GameInput = { space:Bool, delta:Time, so1:Orient, so2:Orient, so3:Orient, 
                   so4:Orient, amount:Int }


gameInput : Signal GameInput
gameInput = 
             let sampledInput = sampleOn delta 
                                <| GameInput <~ Keyboard.space
                                              ~ delta
                                              ~ Keyboard.arrows
                                              ~ Keyboard.wasd
                                              ~ Keyboard.directions (toCode 'O')
                                                                    (toCode 'L')
                                                                    (toCode 'K')
                                                                    (toCode 'M')
                                              ~ Keyboard.directions (toCode 'Y')
                                                                    (toCode 'H')
                                                                    (toCode 'G')
                                                                    (toCode 'J')
                                              ~ amountPlayers.signal
             in  lift (Debug.watch "input") sampledInput

我创建了(对于每个玩家)表单的输入:

type CustomKeys = { up:Char, down:Char, left:Char, right:Char }

customKeys2 : Signal CustomKeys
customKeys2 = CustomKeys <~ ck2up.signal 
                          ~ ck2down.signal 
                          ~ ck2left.signal 
                          ~ ck2right.signal

ck2up : Input Char
ck2up = input 'W'

ck2down : Input Char
ck2down = input 'S'

ck2left : Input Char
ck2left = input 'A'

ck2right : Input Char
ck2right = input 'D'

其他地方的处理程序将根据玩家的需要调整输入值.但是我很难弄清楚如何将这些值插入到Keyboard.directions的参数中.

我试过直接将参数提升到Keyboard.directions:

~ Keyboard.directions <~ ...

任何结果都将成为信号的信号,不能被提升到GameInput(正确).

我已经尝试将chars作为参数传递给gameInput:

gameInput2 : Signal GameInput
gameInput2 = gameInput <~ customKeys2

gameInput : CustomKeys -> Signal GameInput
gameInput { up,down,left,right } = GameInput <~ Keyboard.directions (toCode up)
                                                                    (toCode down)
                                                                    (toCode left)
                                                                    (toCode right)

现在,gameInput2将产生一个信号信号.

我的最后一个想法是组合信号,但这对我来说似乎不是一个选项,因为我希望一个信号依赖于另一个信号.

最佳答案 直接使用Keyboard.directions是行不通的.问题是,在游戏开始时,可​​以更改密钥,以便您有一些信号字符.而Keyboard.direction从“普通类型”变为“信号类型”.所以你不能解除它.

但您也可以访问当前按下的键,Keyboard.keysDown.我抬头查看了implementation of Keyboard.directions,我认为你可以在Elm中重新创建它,它采用Signal Int参数.

这是普通Keyboard.directions的Elm实现:

directions : Int -> Int -> Int -> Int -> Signal { x : Int, y : Int }
directions up down left right = 
  (\kd -> 
    List.filter (\ky -> List.member ky [up,down,left,right]) kd |>
      List.foldl (\ky st -> if | ky == up    -> { st | y <- st.y + 1 }
                               | ky == down  -> { st | y <- st.y - 1 }
                               | ky == left  -> { st | x <- st.x - 1 }
                               | ky == right -> { st | x <- st.x + 1 }
      ) {x=0,y=0}
  ) <~ Keyboard.keysDown

这是你想要使用的实现:

directions : Signal Int -> Signal Int -> Signal Int -> Signal Int -> Signal { x : Int, y : Int }
directions up down left right = 
  (\u d l r kd -> 
    List.filter (\ky -> List.member ky [u,d,l,r]) kd |>
      List.foldl (\ky st -> if | ky == u -> { st | y <- st.y + 1 }
                               | ky == d -> { st | y <- st.y - 1 }
                               | ky == l -> { st | x <- st.x - 1 }
                               | ky == r -> { st | x <- st.x + 1 }
      ) {x=0,y=0}
  ) <~ up ~ down ~ left ~ right ~ Keyboard.keysDown

随意重构,我只是快速地将这些代码一起攻击,因此它对于单个函数来说太大了.

点赞