Advertisement
Guest User

Untitled

a guest
Jul 30th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDB 2.50 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. # Movement
  4. const UP = Vector2(0, -1)
  5.  
  6. var motion = Vector2()
  7. export var max_run_speed    = 300
  8. export var max_fall_speed   = 600
  9. export var run_acceleration = 10
  10. export var gravity          = 10
  11. export var jump_force       = -300
  12. export var friction         = 10
  13.  
  14. onready var anim = get_node("AnimatedSprite")
  15.        
  16. # What we should do when a certain animation finishes
  17. func _on_AnimatedSprite_animation_finished():
  18.     anim.set_speed_scale(2)
  19.     if not ["idle", "jump", "fall"].has(anim.animation):
  20.         motion.x = 0
  21.        
  22.     if is_on_floor():
  23.         anim.play("idle")
  24.     elif anim.animation == "jump":
  25.         if motion.y < 0:
  26.             anim.play("jump")
  27.             anim.frame = 2
  28.         else:
  29.             anim.play("fall")
  30.     elif ["fall"].has(anim.animation):
  31.         anim.play("fall")
  32.         anim.frame = 2
  33.     else:
  34.         anim.play("fall")  
  35.        
  36. func get_inputs():
  37.     # Perform movement calculations based on which button is pressed
  38.     if Input.is_action_pressed('ui_right') && motion.x < max_run_speed:
  39.         anim.flip_h = false
  40.        
  41.         if motion.x > max_run_speed / 8&& anim.animation == "idle":
  42.             anim.play("move")
  43.            
  44.         motion.x += run_acceleration
  45.     elif Input.is_action_pressed('ui_left') && motion.x > -max_run_speed:
  46.         anim.flip_h = true
  47.        
  48.         if motion.x < max_run_speed / -8 && anim.animation == "idle":
  49.             anim.play("move")
  50.            
  51.         motion.x -= run_acceleration
  52.     # If no movement keys were pressed, apply friction
  53.     else:
  54.         if motion.x > friction:
  55.             motion.x -= friction
  56.         elif motion.x < 0 && motion.x < -friction:
  57.             motion.x += friction
  58.         else:
  59.             motion.x = 0
  60.            
  61.            
  62. func _physics_process(delta):
  63.  
  64.    
  65.     # Stop movement for two frames on every movement cycle.
  66.     if anim.animation == "move" && anim.frame > 10 or anim.animation == "landing" && anim.frame < 2:
  67.         motion.x = 0
  68.         if Input.is_action_pressed('ui_right') || Input.is_action_pressed('ui_left'):
  69.             anim.play("move")
  70.             anim.frame = 1
  71.            
  72.     if motion.y < max_fall_speed - gravity:
  73.         motion.y += gravity
  74.     else:
  75.         motion.y == max_fall_speed
  76.    
  77.     get_inputs()
  78.     floor_check()
  79.     move_and_slide(motion, UP)
  80.  
  81. # Chech whether the character is on the floor or in air and play animations accordingly
  82. func floor_check():
  83.     if is_on_floor():
  84.         if not anim.is_playing():
  85.             anim.play("idle")
  86.         # If we are in the middle of an animation when on the floor, play the appropriate animation
  87.         if anim.is_playing():
  88.             if ["fall"].has(anim.animation):
  89.                 anim.play("landing")
  90.         if Input.is_action_just_pressed("ui_up"):
  91.             motion.y = jump_force
  92.             anim.play("jump")
  93.         else:
  94.             motion.y = 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement