Guest User

Untitled

a guest
Feb 25th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. enum ORIENTATION {
  4. UP,
  5. DOWN,
  6. LEFT,
  7. RIGHT
  8. }
  9.  
  10. enum STATES {
  11. WALK,
  12. RUN,
  13. IDLE
  14. }
  15.  
  16. var current_state = null
  17. var orientation = DOWN
  18.  
  19. var input_direction = Vector2()
  20. var last_direction = Vector2(1, 0)
  21.  
  22. const MAX_WALK_SPEED = 400
  23. const MAX_RUN_SPEED = 700
  24.  
  25. var speed = 0.0
  26. var max_speed = 0.0
  27.  
  28. var velocity = Vector2()
  29.  
  30. func change_direction(direction):
  31. if direction.y == -1:
  32. orientation = UP
  33. if direction.y == 1:
  34. orientation = DOWN
  35. if direction.x == 1:
  36. orientation = RIGHT
  37. if direction.x == -1:
  38. orientation = LEFT
  39.  
  40. change_state(current_state)
  41.  
  42. func change_state(state):
  43. current_state = state
  44. match current_state:
  45. IDLE:
  46. match orientation:
  47. UP:
  48. $AnimationPlayer.play('idle up')
  49. DOWN:
  50. $AnimationPlayer.play('idle down')
  51. LEFT:
  52. $AnimationPlayer.play("idle left")
  53. RIGHT:
  54. $AnimationPlayer.play("idle right")
  55. WALK:
  56. match orientation:
  57. UP:
  58. $AnimationPlayer.play('walk up')
  59. DOWN:
  60. $AnimationPlayer.play('walk down')
  61. LEFT:
  62. $AnimationPlayer.play("walk left")
  63. RIGHT:
  64. $AnimationPlayer.play("walk right")
  65. RUN:
  66. match orientation:
  67. UP:
  68. $AnimationPlayer.play('walk up')
  69. DOWN:
  70. $AnimationPlayer.play('walk down')
  71. LEFT:
  72. $AnimationPlayer.play("walk left")
  73. RIGHT:
  74. $AnimationPlayer.play("walk right")
  75.  
  76. func _physics_process(delta):
  77. update_direction()
  78. move(delta)
  79.  
  80. func update_direction():
  81. if input_direction and last_direction != input_direction:
  82. last_direction = input_direction
  83. change_direction(input_direction)
  84.  
  85. func move(delta):
  86. if input_direction:
  87. if speed != max_speed:
  88. speed = max_speed
  89. change_state(WALK)
  90. $Label.text = "WALK"
  91. else:
  92. speed = 0
  93. change_state(IDLE)
  94. $Label.text = "IDLE"
  95.  
  96. velocity = input_direction.normalized() * speed
  97. move_and_slide(velocity)
Add Comment
Please, Sign In to add comment