Advertisement
otorp2

grid kinematic2d

Aug 5th, 2015
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. # Our accumulated motion on the X axis
  4. var xaccum
  5.  
  6. # Track if we're dragging a sprite
  7. var mouse_down
  8.  
  9. # These are the width and height of the sprite
  10. var twidth
  11. var theight
  12.  
  13. # A default fall speed (like gravity, but velocity instead of acceleration)
  14. const STARTING_SPEED = 100.0
  15.  
  16. # A velocity vector that we'll use for calculations below
  17. var velocity = Vector2()
  18.  
  19. func _ready():
  20. # This object will use input and fixed-timestep physics
  21. set_process_input(true)
  22. set_fixed_process(true)
  23.  
  24. # Initialize our variables
  25. xaccum = 0
  26. twidth = get_node("Sprite").get_texture().get_width()
  27. theight = get_node("Sprite").get_texture().get_height()
  28. mouse_down = false
  29. velocity.y = STARTING_SPEED
  30.  
  31. func _fixed_process(delta):
  32. # The object will fall until it hits the bottom of the world or another object
  33. var motion = velocity * delta
  34.  
  35. # Test if we've accumulated enough movement to "jump" one grid square,
  36. # If we have, then we'll add that much movement to our motion vector.
  37. if abs(xaccum) > twidth:
  38. motion.x = twidth * sign(xaccum)
  39. xaccum -= twidth * sign(xaccum)
  40. else:
  41. motion.x = 0
  42.  
  43. # Move the object as much as possible
  44. motion = move(motion)
  45.  
  46. # If we're colliding (with the wall or another object),
  47. # then we need to modify our motion vector.
  48. # See the Godot wiki for how and why this works:
  49. # https://github.com/okamstudio/godot/wiki/tutorial_kinematic_char#problem
  50. if is_colliding():
  51. var n = get_collision_normal()
  52. motion = n.slide(motion)
  53. move(motion)
  54.  
  55. # If the mouse button has been released,
  56. # we can stop worrying about motion on the X axis
  57. if not mouse_down:
  58. xaccum = 0
  59.  
  60. func _input(event):
  61. # Create a rectangle covering the entire sprite area
  62. var gp = get_global_pos()
  63. gp.x -= twidth/2
  64. gp.y -= theight/2
  65. var gr = Rect2(gp, Vector2(twidth, theight))
  66.  
  67. # If the left mouse button is pressed while over the object,
  68. # all we do is set our state variable. If it's released anywhere,
  69. # we clear that same variable.
  70. if event.type == InputEvent.MOUSE_BUTTON and event.button_index == 1:
  71. if gr.has_point(event.pos):
  72. mouse_down = event.pressed
  73. get_tree().set_input_as_handled()
  74. elif mouse_down and not event.pressed:
  75. mouse_down = false
  76.  
  77. # If the user drags while holding the left mouse button,
  78. # that's our signal to start accumulating motion.
  79. if event.type == InputEvent.MOUSE_MOTION and mouse_down:
  80. xaccum += event.relative_x
  81. get_tree().set_input_as_handled()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement