samples/05_mouse/02_mouse_move/app/main.rb
def input

    # If the "a" key or left key is pressed, the x position of the player decreases.
    # Otherwise, if the "d" key or right key is pressed, the x position of the player increases.
    if inputs.keyboard.key_held.a || inputs.keyboard.key_held.left
      state.player.x -= 5
    elsif inputs.keyboard.key_held.d || inputs.keyboard.key_held.right
      state.player.x += 5
    end

    # If the "w" or up key is pressed, the y position of the player increases.
    # Otherwise, if the "s" or down key is pressed, the y position of the player decreases.
    if inputs.keyboard.key_held.w || inputs.keyboard.key_held.up
      state.player.y += 5
    elsif inputs.keyboard.key_held.s || inputs.keyboard.key_held.down
      state.player.y -= 5
    end

    # Sets the attack angle so the player can move and attack in the precise direction it wants to go.
    # If the mouse is moved, the attack angle is changed (based on the player's position and mouse position).
    # Attack angle also contributes to the position of red square.
    if inputs.mouse.moved
      state.player.attack_angle = inputs.mouse.position.angle_from [state.player.x, state.player.y]
    end

    if inputs.mouse.click && state.player.dx < 0.5 && state.player.dy < 0.5
      state.player.attack_angle_on_click = inputs.mouse.position.angle_from [state.player.x, state.player.y]
      state.player.attack_angle = state.player.attack_angle_on_click # player's attack angle is set
      state.player.dx = state.player.attack_angle.vector_x(25) # change in player's position
      state.player.dy = state.player.attack_angle.vector_y(25)
    end
  end
# Uses input from the user to move the player around the screen.
Uses input from the user to move the player around the screen.
Uses input from the user to move the player around the screen .
def input
    if inputs.keyboard.key_held.a || inputs.keyboard.key_held.left
      state.player.x -= 5
    elsif inputs.keyboard.key_held.d || inputs.keyboard.key_held.right
      state.player.x += 5
    end
    if inputs.keyboard.key_held.w || inputs.keyboard.key_held.up
      state.player.y += 5
    elsif inputs.keyboard.key_held.s || inputs.keyboard.key_held.down
      state.player.y -= 5
    end
    if inputs.mouse.moved
      state.player.attack_angle = inputs.mouse.position.angle_from [state.player.x, state.player.y]
    end
    if inputs.mouse.click && state.player.dx < 0.5 && state.player.dy < 0.5
      state.player.attack_angle_on_click = inputs.mouse.position.angle_from [state.player.x, state.player.y]
      state.player.attack_angle = state.player.attack_angle_on_click 
      state.player.dx = state.player.attack_angle.vector_x(25) 
      state.player.dy = state.player.attack_angle.vector_y(25)
    end
  end
def input if inputs . keyboard . key_held . a || inputs . keyboard . key_held . left state . player . x -= 5 elsif inputs . keyboard . key_held . d || inputs . keyboard . key_held . right state . player . x += 5 end if inputs . keyboard . key_held . w || inputs . keyboard . key_held . up state . player . y += 5 elsif inputs . keyboard . key_held . s || inputs . keyboard . key_held . down state . player . y -= 5 end if inputs . mouse . moved state . player . attack_angle = inputs . mouse . position . angle_from [ state . player . x , state . player . y ] end if inputs . mouse . click && state . player . dx < 0.5 && state . player . dy < 0.5 state . player . attack_angle_on_click = inputs . mouse . position . angle_from [ state . player . x , state . player . y ] state . player . attack_angle = state . player . attack_angle_on_click state . player . dx = state . player . attack_angle . vector_x ( 25 ) state . player . dy = state . player . attack_angle . vector_y ( 25 ) end end
Uses input from the user to move the player around the screen.
Uses input from the user to move the player around the screen .
# If the "a" key or left key is pressed, the x position of the player decreases. # Otherwise, if the "d" key or right key is pressed, the x position of the player increases. # If the "w" or up key is pressed, the y position of the player increases. # Otherwise, if the "s" or down key is pressed, the y position of the player decreases. # Sets the attack angle so the player can move and attack in the precise direction it wants to go. # If the mouse is moved, the attack angle is changed (based on the player's position and mouse position). # Attack angle also contributes to the position of red square. # player's attack angle is set # change in player's position