Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. extends Actor
  2.  
  3. var direction = Vector2.ZERO
  4.  
  5. var hookOut = false
  6. var hookPosition = Vector2.ZERO
  7. var ropeLength = Vector2.ZERO
  8. var ropeAngleVelocity = 0
  9. var ropeAngle = 0
  10. var ropeEndPoint = Vector2.ZERO
  11.  
  12. signal animate
  13.  
  14.  
  15. func _physics_process(delta: float) -> void:
  16. # check for short jump
  17. var is_jump_interrupted = Input.is_action_just_released("ButtonA") and velocity.y < 0
  18. # get where the player wants to go.
  19. direction = get_direction()
  20. operate_hook_shot()
  21. if hookOut:
  22. hook_swing()
  23. velocity.y = gravity
  24. velocity = calculate_platforming_velocity(velocity, direction, speed, is_jump_interrupted)
  25. velocity = move_and_slide(velocity, FLOOR_NORMAL)
  26. animate(velocity)
  27.  
  28.  
  29.  
  30.  
  31. func get_direction() -> Vector2:
  32. return Vector2(
  33. Input.get_action_strength("Right") - Input.get_action_strength("Left"),
  34. -Input.get_action_strength("ButtonA") if can_jump() and Input.is_action_just_pressed("ButtonA") else 0.0
  35. )
  36.  
  37.  
  38. func can_jump() -> bool:
  39. # TODO checks if player can jump based on being on the floor, having coyote time or swinging
  40. return is_on_floor()
  41.  
  42.  
  43. func calculate_platforming_velocity(
  44. linear_velocity: Vector2,
  45. direction: Vector2,
  46. speed: Vector2,
  47. is_jump_interrupted: bool
  48. ) -> Vector2:
  49. # Horizontal Velocity
  50. var new_velocity: = linear_velocity
  51. new_velocity.x = speed.x * direction.x
  52. # Vertical Velocity
  53. if direction.y != 0.0:
  54. new_velocity.y = speed.y * direction.y
  55. if is_jump_interrupted:
  56. new_velocity.y = 0.0
  57. return new_velocity
  58.  
  59.  
  60. func animate(velocity):
  61. emit_signal("animate", velocity)
  62.  
  63.  
  64. func operate_hook_shot():
  65. if Input.is_action_just_released("ButtonB"):
  66. hookOut = false
  67. velocity.y -= 150
  68. elif Input.is_action_just_pressed("ButtonB"):
  69. hookPosition = get_global_mouse_position()
  70. ropeLength = get_position().distance_to(hookPosition)
  71. ropeAngleVelocity = 0
  72. ropeAngle = get_position().angle_to_point(hookPosition)
  73. hookOut = true
  74.  
  75.  
  76. func hook_swing():
  77. var playerDelta = hookPosition - get_position()
  78. var currentDistance = playerDelta.length()
  79. if currentDistance > ropeLength:
  80. var correctionAmount = currentDistance - ropeLength
  81. var hookDir = playerDelta / currentDistance
  82. var correctionVector = (hookDir * correctionAmount)
  83. print(correctionVector)
  84. move_and_collide(correctionVector)
  85. velocity += (correctionVector * 0.1) / get_physics_process_delta_time()
  86.  
  87.  
  88. #func hook_swing():
  89. # var left_pressed = Input.get_action_strength("Left")
  90. # var right_pressed = Input.get_action_strength("Right")
  91. # var _ropeAcceleration = sin(ropeAngle - deg2rad(90)) * -0.08 / ropeLength
  92. #
  93. # _ropeAcceleration -= (right_pressed - left_pressed) * 0.1 / ropeLength
  94. # ropeAngleVelocity += _ropeAcceleration
  95. # ropeAngle += ropeAngleVelocity
  96. # ropeAngleVelocity *= 0.99
  97. # ropeEndPoint = hookPosition + (Vector2(cos(ropeAngle),sin(ropeAngle)) * ropeLength)
  98. #
  99. # var swingMotion = (ropeEndPoint - get_position())
  100. ## swingMotion.x = clamp(swingMotion.x, -5,5)
  101. ## swingMotion.y = clamp(swingMotion.y, -5,5)
  102. ## if swingMotion.x < 0.2 and swingMotion.x > -0.2:
  103. ## swingMotion.x = 0
  104. ## ropeAngleVelocity = 0
  105. ## ropeAngle = get_position().angle_to_point(hookPosition)
  106. ## print(swingMotion.x)
  107. # var colision = move_and_collide(swingMotion)
  108. # if colision:
  109. # ropeAngle = get_position().angle_to_point(hookPosition)
  110. # ropeAngleVelocity *= -0.5
  111. # velocity = swingMotion / get_physics_process_delta_time()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement