markman4897

joystick.gd

Jul 19th, 2020
75
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node2D
  2.  
  3. # TODO: make this invisible if not on phone
  4.  
  5. onready var back: Sprite = get_node("Back")
  6. onready var front: Sprite = get_node("Front")
  7.  
  8. onready var back_size = back.region_rect.size
  9. onready var limits = {
  10. "x": [back.global_position.x - back_size.x/2, back.global_position.x + back_size.x/2],
  11. "y": [back.global_position.y - back_size.y/2, back.global_position.y + back_size.y/2]
  12. }
  13.  
  14. export var ease_value: float = 0.25
  15. onready var deadzone = back_size.x*0.2
  16.  
  17. var button_pressed = {
  18. "up": false,
  19. "down": false,
  20. "left": false,
  21. "right": false
  22. }
  23.  
  24. # HACK: because keys dont get depressed when you kill this scene!!!
  25. func _ready():
  26. Input.action_release("ui_right")
  27. Input.action_release("ui_left")
  28. Input.action_release("ui_up")
  29. Input.action_release("ui_down")
  30.  
  31. func _process(delta: float) -> void:
  32. if Input.is_mouse_button_pressed(1):
  33. front.global_position = lerp(front.global_position, get_global_mouse_position(), ease_value)
  34. front.global_position.x = clamp (front.global_position.x, limits.x[0], limits.x[1])
  35. front.global_position.y = clamp (front.global_position.y, limits.y[0], limits.y[1])
  36.  
  37. else:
  38. front.global_position = lerp(front.global_position, back.global_position, ease_value)
  39.  
  40. commit_buttons(pressed_buttons())
  41.  
  42. func pressed_buttons():
  43. var direction = Vector2(front.global_position.x-back.global_position.x, front.global_position.y-back.global_position.y)
  44. var out = Vector2(0,0)
  45. if abs(direction.x) > deadzone:
  46. out.x = 1 if direction.x > 0 else -1
  47. if abs(direction.y) > deadzone:
  48. out.y = 1 if direction.y > 0 else -1
  49. return out
  50.  
  51. func commit_buttons(pressed_buttons):
  52. if pressed_buttons.x != 0:
  53. if pressed_buttons.x == 1:
  54. if !button_pressed.right:
  55. Input.action_press("ui_right")
  56. button_pressed.right = true
  57.  
  58. else:
  59. if !button_pressed.left:
  60. Input.action_press("ui_left")
  61. button_pressed.left = true
  62. else:
  63. if button_pressed.right:
  64. Input.action_release("ui_right")
  65. button_pressed.right = false
  66.  
  67. if button_pressed.left:
  68. Input.action_release("ui_left")
  69. button_pressed.left = false
  70.  
  71. if pressed_buttons.y != 0:
  72. if pressed_buttons.y == 1:
  73. if !button_pressed.down:
  74. Input.action_press("ui_down")
  75. button_pressed.down = true
  76.  
  77. else:
  78. if !button_pressed.up:
  79. Input.action_press("ui_up")
  80. button_pressed.up = true
  81. else:
  82. if button_pressed.down:
  83. Input.action_release("ui_down")
  84. button_pressed.down = false
  85.  
  86. if button_pressed.up:
  87. Input.action_release("ui_up")
  88. button_pressed.up = false
RAW Paste Data Copied