Advertisement
MeeNov2

2D

Jun 29th, 2020
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody2D
  2.  
  3. const MOVE_SPEED = 500
  4. const JUMP_FORCE = 1000
  5. const GRAVITY = 50
  6. const MAX_FALL_SPEED = 1000
  7.  
  8. onready var anim_player = $AnimationPlayer
  9. onready var sprite = $Sprite
  10.  
  11. var y_velo = 0
  12. var facing_right = false
  13.  
  14. func _physics_process(delta):
  15.     var move_dir = 0
  16.     if Input.is_action_pressed("move_right"):
  17.         move_dir += 1
  18.     if Input.is_action_pressed("move_left"):
  19.         move_dir -= 1
  20.     move_and_slide(Vector2(move_dir * MOVE_SPEED, y_velo), Vector2(0, -1))
  21.    
  22.     var grounded = is_on_floor()
  23.     y_velo += GRAVITY
  24.     if grounded and Input.is_action_just_pressed("jump"):
  25.         y_velo = -JUMP_FORCE
  26.     if grounded and y_velo >= 5:
  27.         y_velo = 5
  28.     if y_velo > MAX_FALL_SPEED:
  29.         y_velo = MAX_FALL_SPEED
  30.    
  31.     if facing_right and move_dir < 0:
  32.         flip()
  33.     if !facing_right and move_dir > 0:
  34.         flip()
  35.    
  36.     if grounded:
  37.         if move_dir == 0:
  38.             play_anim("idle")
  39.         else:
  40.             play_anim("walk")
  41.     else:
  42.         play_anim("jump")
  43.  
  44. func flip():
  45.     facing_right = !facing_right
  46.     sprite.flip_h = !sprite.flip_h
  47.  
  48. func play_anim(anim_name):
  49.     if anim_player.is_playing() and anim_player.current_animation == anim_name:
  50.         return
  51.     anim_player.play(anim_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement