Advertisement
Guest User

b

a guest
Mar 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. #Constants
  4. const GRAVITY_VEC = Vector2(0, 1500)
  5. const FLOOR_NORMAL = Vector2(0, -1)
  6. const SLOPE_SLIDE_STOP = 25.0
  7. const MIN_ONAIR_TIME = 0.01
  8. #Normal speed ( walking)
  9. const WALK_SPEED = 100
  10. #Running speed (dashing)
  11. const RUN_SPEED = 350
  12. const JUMP_SPEED = 500
  13. const FLOAT_HEIGHT = -200
  14.  
  15. var linear_vel = Vector2(0,0)
  16. var onair_time = 0
  17. var on_floor = false
  18. var lands
  19. #var inhale = ?
  20.  
  21. var Anims = ""
  22. #Run variables
  23. #Are we running? (Double tapping?)
  24. var running = false
  25. #time inbetween key taps
  26. var run_window = 0.7
  27. #keep track of double tap count
  28. var run_press_count = 0
  29. #Timer for run state to be true
  30. var run_time = 0
  31. #Last input
  32. var lastXInput = 0
  33. #Try step
  34. var try_step = false
  35.  
  36. #Cache sprite here for fast access
  37. onready var sprite = $Sprite
  38.  
  39. func _physics_process(delta):
  40. #increment counters
  41.  
  42. onair_time += delta
  43.  
  44. #Movement
  45.  
  46. #Apply gravity
  47. linear_vel = linear_vel + (delta * GRAVITY_VEC)
  48. #Move and slide
  49. linear_vel = move_and_slide(linear_vel, FLOOR_NORMAL)#, SLOPE_SLIDE_STOP)
  50. #Detect Floor
  51. if is_on_floor():
  52. onair_time = 0
  53.  
  54. on_floor = onair_time < MIN_ONAIR_TIME
  55.  
  56. ### Controls ###
  57. var target_speed = 0
  58.  
  59. #Walk
  60. if (Input.is_action_pressed("ui_left")):
  61. target_speed += -1
  62. target_speed *= WALK_SPEED
  63. if (Input.is_action_pressed("ui_right")):
  64. target_speed += 1
  65. target_speed *= WALK_SPEED
  66. #cancel out speed if both keys are pressed
  67. if ((Input.is_action_pressed("ui_right") and (Input.is_action_pressed("ui_left")))):
  68. target_speed = 0
  69. #reset timer if we stop pushing keys
  70.  
  71.  
  72. linear_vel.x = lerp(linear_vel.x, target_speed, 1)
  73.  
  74. #Run
  75. #Check for presses
  76. if (Input.is_action_just_pressed("ui_left")):
  77. run_press_count -= 1
  78. if (Input.is_action_just_pressed("ui_right")):
  79. run_press_count += 1
  80.  
  81. #if the direction variable is left or right(-1 left, 1, right) and the input window is appropriate, and the direction variable is the same as the last input..
  82. #if (run_press_count != 0 and run_time < run_window and run_press_count == lastXInput):
  83. if (run_press_count <= 2 * lastXInput and run_time < run_window and try_step == true):
  84. running = true
  85. if (run_press_count <= 2 * lastXInput and try_step == false):
  86. run_time = 0
  87. try_step = true
  88. #The last input: If negative, left. If positive, right.
  89. lastXInput = run_press_count
  90.  
  91. #We are inputting, and try_step is true, and then a timer starts
  92. if(try_step == true):
  93. run_time += delta
  94. if (run_time >= run_window):
  95. run_time = 0
  96. run_press_count = 0
  97. try_step = false
  98.  
  99.  
  100. if (running):
  101. target_speed *= RUN_SPEED
  102.  
  103. if (not(Input.is_action_pressed("ui_right") and (Input.is_action_pressed("ui_left")))):
  104. running = false
  105.  
  106. print(running)
  107. print(run_press_count)
  108.  
  109. #Jumping
  110. if ((on_floor) and (Input.is_action_just_pressed("ui_shift"))):
  111. linear_vel.y = -JUMP_SPEED
  112. if(not(on_floor) and (Input.is_action_just_pressed("ui_shift"))):
  113. linear_vel.y = -JUMP_SPEED
  114.  
  115. ### Animation ###
  116.  
  117. var new_Anims = "Idle"
  118.  
  119. #Scale modifier is 2, because our sprite is scaled.
  120. if (on_floor):
  121. #Walking
  122. if (Input.is_action_pressed("ui_left")):
  123. sprite.scale.x = -2
  124. new_Anims = "Walk"
  125.  
  126. if (Input.is_action_pressed("ui_right")):
  127. sprite.scale.x = 2
  128. new_Anims = "Walk"
  129.  
  130. #Running
  131. if (Input.is_action_pressed("ui_left") and (running)):
  132. sprite.scale.x = -2
  133. new_Anims = "Run"
  134. if (Input.is_action_pressed("ui_right") and (running)):
  135. sprite.scale.x = 2
  136. new_Anims = "Run"
  137.  
  138. else:
  139. #Kirby can quickly turn around regardless of running
  140. #or not whether in the air or not
  141. if (Input.is_action_pressed("ui_left") and not (Input.is_action_pressed("ui_right"))):
  142. sprite.scale.x = -2
  143. if (Input.is_action_pressed("ui_right") and not (Input.is_action_pressed("ui_left"))):
  144. sprite.scale.x = 2
  145.  
  146. # In the air
  147. if linear_vel.y < 0:
  148. new_Anims = "Jump"
  149. else:
  150. new_Anims = "Fall"
  151.  
  152. #If the animations aren't playing, play them
  153. if new_Anims != Anims:
  154. Anims = new_Anims
  155. $Anims.play(Anims)
  156.  
  157. ### Sounds ###
  158. #Landing sound
  159. if (is_on_floor() and not(lands)):
  160. $Sounds/s_land.play()
  161. lands = is_on_floor()
  162.  
  163. if (is_on_floor()):
  164. lands = true
  165. elif (not(is_on_floor())):
  166. lands = false
  167.  
  168. #Jump sound
  169. if ((on_floor) and (Input.is_action_just_pressed("ui_shift"))):
  170. $Sounds/s_jump.play()
  171. if(not(on_floor) and (Input.is_action_just_pressed("ui_shift"))):
  172. $Sounds/s_jump.play()
  173.  
  174. #Walk sound
  175. #Use a call function in the animationPlayer
  176. func Step():
  177. get_node("Sounds/s_walk").play()
  178.  
  179. """
  180. #Temporary:
  181. func _process(delta):
  182. motion.y += GRAVITY
  183. var friction = false
  184.  
  185. if (Input.is_action_pressed("ui_right")):
  186. motion.x = SPEED
  187. $AnimatedSprite.play("Walk")
  188. $AnimatedSprite.flip_h = false
  189. elif (Input.is_action_pressed("ui_left")):
  190. motion.x = -SPEED
  191. $AnimatedSprite.play("Walk")
  192. $AnimatedSprite.flip_h = true
  193. else:
  194. motion.x = 0
  195. friction = true
  196. $AnimatedSprite.play("Idle")
  197.  
  198. if (is_on_floor()):
  199. if (Input.is_action_just_pressed("ui_shift")):
  200. motion.y = JUMP_HEIGHT
  201. if (friction == true):
  202. motion.x = lerp(motion.x, 0 , 0.2)
  203. if (not(is_on_floor())):
  204. if (Input.is_action_just_pressed("ui_shift")):
  205. motion.y = FLOATH
  206.  
  207. else:
  208. if motion.y < 0 :
  209. $AnimatedSprite.play("Jump")
  210. elif motion.y > 0:
  211. $AnimatedSprite.play("Fall")
  212. if friction == true:
  213. motion.x = lerp(motion.x, 0, 0.5)
  214.  
  215. motion = move_and_slide(motion, UP)
  216.  
  217. print(motion)
  218. pass
  219. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement