Advertisement
MrRiptidePastes

Player.gd

Nov 23rd, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. const UP = Vector2(0, -1)
  4. const JUMP_FORCE = -550
  5. const GRAVITY = 20
  6. const MAX_FALL_SPEED = 1000
  7.  
  8. var motion = Vector2()
  9. var max_speed = 200
  10. var acceleration = 50
  11. var facing = ""
  12. var in_wall_counter = 0
  13.  
  14. onready var anim_player = $AnimationPlayer
  15. onready var sprite = $Sprite
  16.  
  17. func _physics_process(delta):
  18. if Input.is_action_pressed("sprint"):
  19. max_speed = 300
  20. acceleration = 75
  21. else:
  22. max_speed = 200
  23. acceleration = 50
  24. #motion.y += GRAVITY
  25. var friction = false
  26.  
  27. var move_dir = 0
  28. if Input.is_action_pressed("move_right"):
  29. motion.x = min(motion.x + acceleration, max_speed)
  30. facing = "right"
  31. if Input.is_action_pressed("move_left"):
  32. motion.x = max(motion.x - acceleration, -max_speed)
  33. facing = "left"
  34. if !Input.is_action_pressed("move_left") and !Input.is_action_pressed("move_right"):
  35. friction = true
  36. facing = ""
  37.  
  38. motion = move_and_slide(motion, UP)
  39.  
  40. var grounded = is_on_floor()
  41. var wall = is_on_wall()
  42.  
  43. if is_on_wall():
  44. in_wall_counter += 1
  45. else:
  46. in_wall_counter = 0
  47. if in_wall_counter >= 50:
  48. motion.y = 1000
  49.  
  50.  
  51. if wall and Input.is_action_just_pressed("jump"):
  52. motion.y = JUMP_FORCE
  53. if facing == "right":
  54. motion.x = -300
  55. elif facing == "left":
  56. motion.x = 300
  57.  
  58. if grounded:
  59. if friction:
  60. motion.x = lerp(motion.x, 0, 0.2)
  61. if Input.is_action_pressed("jump"):
  62. motion.y += JUMP_FORCE
  63. if move_dir == 0:
  64. play_anim("idle")
  65. else:
  66. play_anim("walk")
  67. else:
  68. if !friction:
  69. motion.x = lerp(motion.x, 0, 0.05)
  70. play_anim("jump")
  71. if motion.y > MAX_FALL_SPEED:
  72. motion.y = MAX_FALL_SPEED
  73. motion.x = 0
  74. motion.y = 0
  75. if Input.is_action_pressed("ui_up"):
  76. motion.y = -50
  77. if Input.is_action_pressed("ui_down"):
  78. motion.y = 50
  79. if Input.is_action_pressed("ui_right"):
  80. motion.x = 50
  81. if Input.is_action_pressed("ui_left"):
  82. motion.x = -50
  83. motion = move_and_slide(motion, UP)
  84.  
  85.  
  86. func flip():
  87. sprite.flip_h = !sprite.flip_h
  88.  
  89. func play_anim(anim_name):
  90. return
  91. if anim_player.is_playing() and anim_player.current_animation == anim_name:
  92. return
  93. anim_player.play(anim_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement