Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. var analog_stick_center
  4. var analog_stick_pos
  5.  
  6. func _ready():
  7. set_fixed_process(true)
  8. set_process_input(true)
  9.  
  10. func _input(event):
  11. if (event.type == InputEvent.SCREEN_TOUCH):
  12. if event.is_pressed():
  13. analog_stick_center = event.global_pos
  14. else:
  15. analog_stick_center = null
  16. analog_stick_pos = null
  17. elif (event.type == InputEvent.SCREEN_DRAG):
  18. analog_stick_pos = event.global_pos
  19.  
  20. func _fixed_process(delta):
  21. relative_directional_movement(delta)
  22.  
  23. func relative_directional_movement(delta):
  24. if analog_stick_pos != null && analog_stick_center != null:
  25. var pos_vector = analog_stick_pos - analog_stick_center
  26. if pos_vector.length() > MOUSE_WALK_THRESHOLD:
  27. var target_pos = get_global_pos() + pos_vector
  28. move_in_rect_line_towards(target_pos)
  29.  
  30. func move_in_rect_line_towards(pos):
  31. var angle = get_global_pos().angle_to_point(pos)
  32. var motion = move(Vector2(0, -speed).rotated(angle))
  33. if (is_colliding()):
  34. slide(motion)
  35.  
  36. func slide(motion, apply_friction=true):
  37. # You can check which tile was collision against with this
  38. # print(get_collider_metadata())
  39.  
  40. # Ran against something, is it the floor? Get normal
  41. var n = get_collision_normal()
  42.  
  43. # For every other case of motion, our motion was interrupted.
  44. # Try to complete the motion by "sliding" by the normal
  45.  
  46. var new_motion = n.slide(motion)
  47.  
  48.  
  49. if not apply_friction:
  50. # Ignore collision friction
  51. var current_angle = motion.angle()
  52. var slide_angle = new_motion.angle()
  53. new_motion = motion.rotated(slide_angle - current_angle)
  54.  
  55. move(new_motion)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement