Advertisement
Jamiexxl5

ftb

May 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. const twodspace : Vector2 = Vector2(0, -1)
  4. #PlayerVars
  5. var velocity : Vector2 = Vector2()
  6. var ground_move_speed : int = 400
  7. var air_move_accel : int = 50
  8. var dash_speed : int = 500
  9. var air_max_speed : int = dash_speed
  10. var dash_var : int = 10
  11. var gravity : int = 1800
  12. var jump_velocity : int = 650
  13. var air_friction : float = 0.8
  14. var fastfall_speed : int = 500
  15. var multijump
  16. var available_jumps : int = 2
  17. var is_grounded
  18. #Preloading Raycasts
  19. onready var groundraycasts = $GroundRays
  20.  
  21. func _ready():
  22. pass
  23. #mainloop claiing functions
  24. func _physics_process(delta):
  25. velocity = move_and_slide_with_snap(velocity, twodspace)
  26. _get_input()
  27. velocity.y += gravity * delta
  28. is_grounded = _check_is_grounded()
  29. #input tree
  30. func _input(event):
  31. if event.is_action_pressed("move_jump") && is_grounded:
  32. velocity.y = 0
  33. velocity.y += -jump_velocity
  34.  
  35. elif event.is_action_pressed("move_jump") && !is_grounded && multijump > 0:
  36. velocity.y = 0
  37. multijump -= 1
  38. velocity.y += -jump_velocity
  39.  
  40. if event.is_action_pressed("move_dash") && is_grounded:
  41. var move_direction = -int(Input.is_action_pressed("move_left")) + int(Input.is_action_pressed("move_right"))
  42. velocity.x = lerp(velocity.x, dash_speed* dash_var * move_direction, _get_h_weight())
  43.  
  44. if is_grounded:
  45. multijump = available_jumps
  46.  
  47.  
  48. #movement tree
  49. func _get_input():
  50. var move_direction = -int(Input.is_action_pressed("move_left")) + int(Input.is_action_pressed("move_right"))
  51. if is_grounded:
  52. velocity.x = lerp(velocity.x, ground_move_speed * move_direction, _get_h_weight())
  53.  
  54. if !is_grounded:
  55. if move_direction > 0 and velocity.x < air_max_speed:
  56. velocity.x += air_move_accel* _get_h_weight()
  57. if move_direction < 0 and velocity.x > -air_max_speed:
  58. velocity.x -= air_move_accel* _get_h_weight()
  59. else:
  60. velocity.x = velocity.x
  61.  
  62. if !is_grounded and Input.is_action_just_pressed("move_down"):
  63. if velocity.y >= -(jump_velocity*4):
  64. velocity.y += fastfall_speed
  65.  
  66.  
  67. if move_direction > 0:
  68. get_node("Sprite").set_flip_h(true)
  69. elif move_direction < 0:
  70. get_node("Sprite").set_flip_h(false)
  71.  
  72. #horizontal friction
  73. func _get_h_weight():
  74. return 0.3 if is_grounded else 1
  75. #ground check
  76. func _check_is_grounded():
  77. for raycast in groundraycasts.get_children():
  78. if raycast.is_colliding():
  79. return true
  80.  
  81. #If loop completes but there were no raycasts detected
  82. return false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement