Adding WASD control in tandem with arrow keys

Started by b0undarybreaker, July 29, 2017, 07:56:54 AM

Previous topic - Next topic
I'm working on making a game with Solarus and I want the WASD keys to be usable for moving the character alongside the arrow keys, kinda like how you can use the demo mouse_control script and the arrow keys at the same time. Is this possible to do? I've tried making a script off of the mouse_control one but it isn't seeming to work. It doesn't help that d is a pause key, apparently. If anyone can point out where I messed up, I'd love to know. Thanks!

Code (lua) Select

require("scripts/multi_events")


local function initialize_wasd_control_features(game)

  local wasd_control = {}

  local is_key_pushed = false
  print("wasd control startup")

  -- Movement of the hero.
  local directions_pressed = {
      right = false,
      up = false,
      left = false,
      down = false
  }

  function wasd_control:on_key_pressed(key)
    is_key_pushed = true
   
  end

  function wasd_control:on_mouse_released(keu)

    is_key_pushed = false
    pressed_key = key
  end

  function wasd_control:on_update()

    local map = game:get_map()
    if map == nil then
      return
    end

    if is_key_pushed then

      pressed_right = sol.input.is_key_pressed("d")
      pressed_up = sol.input.is_key_pressed("w")
      pressed_left = sol.input.is_key_pressed("a")
      pressed_down = sol.input.is_key_pressed("s")

      self:update_direction("right", pressed_right)
      self:update_direction("up", pressed_up)
      self:update_direction("left", pressed_left)
      self:update_direction("down", pressed_down)
    else

      for direction, _ in pairs(directions_pressed) do
        self:stop_direction(direction)
      end
    end
    function wasd_control:start_direction(direction)

      if not directions_pressed[direction] then
        directions_pressed[direction] = true
        game:simulate_command_pressed(direction)
      end
    end

    function wasd_control:stop_direction(direction)

     if directions_pressed[direction] then
        directions_pressed[direction] = false
        game:simulate_command_released(direction)
      end
    end
  end
end
local game_meta = sol.main.get_metatable("game")
game_meta:register_event("on_started", initialize_wasd_control_features)
return true

It should be simpler by using game:on_key_pressed/released and calling game:simulate_command_pressed/released() from there.

To avoid the conflict with the D key, just assign the pause command to another key than D.


Quote from: Christopho on July 29, 2017, 01:02:43 PM
It should be simpler by using game:on_key_pressed/released and calling game:simulate_command_pressed/released() from there.

To avoid the conflict with the D key, just assign the pause command to another key than D.
Hey is there an exact way to reassign D? because I tried assigning P to pause and D to move right but D pauses AND moves right

Normally if you assign P to pause then the previous key (D) is removed.