Guest User

Scripts of the player from rungeon

a guest
Jan 8th, 2022
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. export var gravity = 1500
  4. export var acceleration = 2000
  5. export var deacceleration = 2000
  6. export var friction = 2000
  7. export var current_friction = 2000
  8. export var max_horizontal_speed = 400
  9. export var max_fall_speed = 1000
  10. export var jump_height = -700
  11. export var double_jump_height = -400
  12.  
  13. export var squash_speed = 0.1
  14.  
  15. var vSpeed = 0
  16. var hSpeed = 0
  17.  
  18. var touching_ground : bool = false # check if we're touching the ground
  19. var touching_wall : bool = false # check if we're touching wall
  20. var is_jumping : bool = false # check are we currently jumping
  21. var is_double_jumping : bool = false # check are we currently double jumping
  22. var air_jump_pressed : bool = false # check if we've pressed jump just before we land
  23. var coyote_time : bool = false #check if we can just JUST after we leave platform
  24. var can_double_jump : bool = false # check if we can double jump
  25.  
  26. var motion = Vector2.ZERO
  27. var UP : Vector2 = Vector2(0,-1)
  28.  
  29. onready var ani = $AnimatedSprite
  30. onready var ground_ray = $raycast_container/ray_ground
  31.  
  32. func _ready():
  33. pass # Replace with function body.
  34.  
  35.  
  36. func _physics_process(delta):
  37. #check if we're grounded
  38. check_ground_logic()
  39. #check if we're moving/jumping/sliding etc
  40. handle_input(delta)
  41. #apply the phsyics once we're done with the previous steps
  42. do_physics(delta)
  43. pass
  44.  
  45. func check_ground_logic():
  46. # check for coyote time ( have we just left platform?)
  47. if(touching_ground and !ground_ray.is_colliding()):
  48. touching_ground = false
  49. coyote_time = true
  50. yield(get_tree().create_timer(0.2),"timeout") # we pause here for 200 milliseoncds
  51. coyote_time = false
  52. #check the m oment we touch ground for first time
  53. if(!touching_ground and ground_ray.is_colliding()):
  54. ani.scale = Vector2(1.2,0.8)
  55. #Set if if we're touching ground or note
  56. touching_ground = ground_ray.is_colliding()
  57. if(touching_ground):
  58. is_jumping = false
  59. can_double_jump = true
  60. motion.y = 0
  61. vSpeed = 0
  62. pass
  63.  
  64. func handle_input(var delta):
  65. handle_movement(delta)
  66. handle_jumping(delta)
  67. pass
  68.  
  69. func handle_jumping(var delta):
  70. if(coyote_time and Input.is_action_just_pressed("jump")):
  71. vSpeed = jump_height
  72. is_jumping = true
  73. can_double_jump = true
  74.  
  75. if(touching_ground):
  76. if((Input.is_action_just_pressed("jump") or air_jump_pressed) and !is_jumping):
  77. vSpeed = jump_height
  78. is_jumping = true
  79. touching_ground = false
  80. ani.scale = Vector2(0.5,1.2)
  81. else:
  82. #Do variable jump logic
  83. if(vSpeed < 0 and !Input.is_action_pressed("jump") and !is_double_jumping):
  84. vSpeed = max(vSpeed,jump_height / 2)
  85. if(can_double_jump and Input.is_action_just_pressed("jump") and !coyote_time):
  86. vSpeed = double_jump_height
  87. ani.play("DOUBLEJUMP")
  88. is_double_jumping = true
  89. can_double_jump = false
  90. #Do some animation logic on the jump
  91. if(!is_double_jumping and vSpeed <0):
  92. ani.play("JUMPUP")
  93. elif(!is_double_jumping and vSpeed > 0):
  94. ani.play("JUMPDOWN")
  95. elif(is_double_jumping and ani.frame == 3):
  96. is_double_jumping = false
  97. #check if we're pressing jump just before we land on a platform
  98. if(Input.is_action_just_pressed("jump")):
  99. air_jump_pressed = true
  100. yield(get_tree().create_timer(0.2),"timeout")
  101. air_jump_pressed = false
  102. pass
  103.  
  104. func handle_movement(var delta):
  105. #controller right/keyboard right
  106. if(Input.get_joy_axis(0,0) > 0.3 or Input.is_action_pressed("ui_right")):
  107. if(hSpeed <-100):
  108. hSpeed += (deacceleration * delta)
  109. if(touching_ground):
  110. ani.play("TURN")
  111. elif(hSpeed < max_horizontal_speed):
  112. hSpeed += (acceleration * delta)
  113. ani.flip_h = false
  114. if(touching_ground):
  115. ani.play("RUN")
  116. else:
  117. if(touching_ground):
  118. ani.play("RUN")
  119. elif(Input.get_joy_axis(0,0) < -0.3 or Input.is_action_pressed("ui_left")):
  120. if(hSpeed > 100):
  121. hSpeed -= (deacceleration * delta)
  122. if(touching_ground):
  123. ani.play("TURN")
  124. elif(hSpeed > -max_horizontal_speed):
  125. hSpeed -= (acceleration * delta)
  126. ani.flip_h = true
  127. if(touching_ground):
  128. ani.play("RUN")
  129. else:
  130. if(touching_ground):
  131. ani.play("RUN")
  132. else:
  133. if(touching_ground):
  134. ani.play("IDLE")
  135. hSpeed -= min(abs(hSpeed),current_friction * delta) * sign(hSpeed)
  136. pass
  137.  
  138. func do_physics(var delta):
  139. if(is_on_ceiling()):
  140. motion.y = 10
  141. vSpeed = 10
  142.  
  143. vSpeed += (gravity * delta) # apply gravity downwards
  144.  
  145. vSpeed = min(vSpeed,max_fall_speed) # limit how fast we can fall
  146.  
  147. #update our motion vector
  148. motion.y = vSpeed
  149. motion.x = hSpeed
  150.  
  151. #apply our motion vectgor to move and slide
  152. motion = move_and_slide(motion,UP)
Advertisement
Add Comment
Please, Sign In to add comment