Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. #refs
  4. onready var sprite: AnimatedSprite = get_node("Sprite")
  5. # Constants
  6. const DEFAULT_SNAP = Vector2(0, 32)
  7. const SPEEDY = 250
  8. const GRAVITY = 900
  9. const JUMP_FORCE = 500
  10. const slope_slide_threshold = 50.0
  11.  
  12. # State
  13. var velocity = Vector2(0, 0)
  14. var snap = false
  15. var friction = 0.5
  16. var animation = "idle_1"
  17. var flip = false
  18. # Input
  19. var action_jump = false
  20. var jump_release = false;
  21. var direction_x = 0
  22.  
  23.  
  24. func _ready():
  25. set_process_input(true)
  26. pass
  27.  
  28.  
  29. func update_animation(velocity: Vector2):
  30.  
  31. var walking = abs(velocity.x) > 10
  32.  
  33. if walking and is_on_floor():
  34. animation = "run"
  35. elif is_on_floor():
  36. animation = "idle_1"
  37. elif not is_on_floor():
  38. animation = "jump" if velocity.y < 0 else "fall";
  39.  
  40. if walking:
  41. sprite.flip_h = velocity.x < 0
  42.  
  43. if sprite.animation != animation:
  44. sprite.play(animation)
  45.  
  46.  
  47. ################################################################################################
  48. # Handle Physics
  49. #
  50. ################################################################################################
  51. func _physics_process(delta):
  52. # Handle left and right
  53. direction_x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
  54. velocity.x = direction_x * SPEEDY
  55.  
  56. # Handle Jump
  57. if is_on_floor() and snap:
  58. if Input.is_action_just_pressed("ui_jump"):
  59. velocity.y = -JUMP_FORCE
  60. snap = false
  61.  
  62. # Normalizations
  63. velocity.y += GRAVITY * delta;
  64. velocity.x = lerp(velocity.x, 0, friction)
  65.  
  66. var snap_vector = DEFAULT_SNAP if snap else Vector2()
  67.  
  68. # Physics
  69. velocity = move_and_slide(velocity, Vector2.UP, true)
  70.  
  71. var just_landed = is_on_floor() and not snap
  72.  
  73. if just_landed:
  74. snap = true
  75.  
  76. update_animation(velocity)
  77.  
  78. action_jump = false
  79. jump_release = false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement