Advertisement
scolain

Godot Fox Player Platformer

Sep 21st, 2022
1,996
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.32 KB | Source Code | 0 0
  1. # Atelier secondaire
  2. # Kidscancode.org KinematicBody2D
  3.  
  4. extends KinematicBody2D
  5.  
  6. export (int) var run_speed
  7. export (int) var jump_speed
  8. export (int) var gravity
  9.  
  10. enum {IDLE, RUN, JUMP}
  11. var velocity = Vector2()
  12. var state
  13. var anim
  14. var new_anim
  15.  
  16. func _ready():
  17.     change_state(IDLE)
  18.  
  19. func change_state(new_state):
  20.     state = new_state
  21.     match state:
  22.         IDLE:
  23.             new_anim = 'idle'
  24.         RUN:
  25.             new_anim = 'run'
  26.         JUMP:
  27.             new_anim = 'jump_up'
  28.  
  29. func get_input():
  30.     velocity.x = 0
  31.     var right = Input.is_action_pressed('ui_right')
  32.     var left = Input.is_action_pressed('ui_left')
  33.     var jump = Input.is_action_just_pressed('ui_select')
  34.  
  35.     if jump and is_on_floor():
  36.         change_state(JUMP)
  37.         velocity.y = jump_speed
  38.     if right:
  39.         change_state(RUN)
  40.         velocity.x += run_speed
  41.     if left:
  42.         change_state(RUN)
  43.         velocity.x -= run_speed
  44.     $Sprite.flip_h = velocity.x < 0
  45.     if !right and !left and state == RUN:
  46.         change_state(IDLE)
  47.  
  48. func _process(_delta):
  49.     get_input()
  50.     if new_anim != anim:
  51.         anim = new_anim
  52.         $AnimationPlayer.play(anim)
  53.  
  54. func _physics_process(delta):
  55.     velocity.y += gravity * delta
  56.     if state == JUMP:
  57.         if is_on_floor():
  58.             change_state(IDLE)
  59.     velocity = move_and_slide(velocity, Vector2(0, -1))
  60.     #move_and_slide(velocity, Vector2(0, -1))
  61.  
  62.     if position.y > 600:
  63.         var _x = get_tree().reload_current_scene()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement