Guest User

Movement Code

a guest
Aug 1st, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. extends KinematicBody2D
  2. class_name Player
  3.  
  4. signal dead
  5.  
  6. var current_dir: int
  7.  
  8. var gravity: float = 400 # 1100, 550, 400
  9. var max_jump_velocity: float = -200
  10. var min_jump_velocity: float = -50
  11. var move_speed: float = 75
  12. var velocity: Vector2 = Vector2.ZERO
  13. var current_state: int = states.idle
  14. var can_throw_rock: bool = false
  15. var can_coyote_jump: bool = false
  16.  
  17. onready var animation: AnimationPlayer = $AnimationPlayer
  18. onready var sprite: Sprite = $Sprite
  19. onready var rock_projectile: PackedScene = preload("res://objects/RockProjectile.tscn")
  20.  
  21. enum states {idle, running, jumping, falling, throwing, dead}
  22.  
  23. func gravity_check():
  24. if position.y > 88:
  25. gravity = 1100
  26. elif position.y > 40:
  27. gravity = 550
  28. else:
  29. gravity = 400
  30.  
  31. func enable_throw_rock() -> void:
  32. if not can_throw_rock:
  33. $PowerUp.play()
  34. can_throw_rock = true
  35.  
  36. func _input(event: InputEvent) -> void:
  37. if Input.is_action_just_pressed("jump") and (is_on_floor() or can_coyote_jump):
  38. velocity.y = max_jump_velocity if not can_coyote_jump else max_jump_velocity * 1.1
  39. current_state = states.jumping
  40. if not $Jump.playing:
  41. $Jump.play()
  42. if Input.is_action_just_released("jump") and current_state == states.jumping:
  43. velocity.y = 0
  44. current_state = states.falling
  45.  
  46. if Input.is_action_just_pressed("throw") and can_throw_rock and not GameMode.normal_mode:
  47. current_state = states.throwing
  48. var rock_instance = rock_projectile.instance()
  49. rock_instance.position = global_position
  50. if Input.is_action_pressed("down"):
  51. velocity.y = max_jump_velocity * 1.1
  52. rock_instance.linear_velocity = Vector2(0, 200)
  53. else:
  54. velocity.y = max_jump_velocity / 2
  55. velocity.x = 200 if $Sprite.flip_h else -200
  56. rock_instance.linear_velocity = Vector2(-200, 0) if $Sprite.flip_h else Vector2(200, 0)
  57. if not $Throw.playing:
  58. $Throw.play()
  59. get_parent().add_child(rock_instance)
  60. can_throw_rock = false
  61.  
  62. if GameMode.normal_mode and can_throw_rock and (Input.is_action_pressed("throw")
  63. or Input.is_action_pressed("throw_right") or Input.is_action_pressed("down")):
  64. current_state = states.throwing
  65. var rock_instance = rock_projectile.instance()
  66. rock_instance.position = global_position
  67. if Input.is_action_pressed("throw"):
  68. $Sprite.flip_h = true
  69. velocity.y = max_jump_velocity / 2
  70. velocity.x = 200
  71. rock_instance.linear_velocity = Vector2(-200, 0)
  72. if Input.is_action_pressed("throw_right"):
  73. $Sprite.flip_h = false
  74. velocity.y = max_jump_velocity / 2
  75. velocity.x = -200
  76. rock_instance.linear_velocity = Vector2(200, 0)
  77. if Input.is_action_pressed("down"):
  78. velocity.y = max_jump_velocity * 1.1
  79. rock_instance.linear_velocity = Vector2(0, 200)
  80. if not $Throw.playing:
  81. $Throw.play()
  82. get_parent().add_child(rock_instance)
  83. can_throw_rock = false
  84.  
  85. func idle_process(delta: float) -> void:
  86. if not animation.current_animation == "idle":
  87. animation.play("idle")
  88. if current_dir != 0:
  89. current_state = states.running
  90. if not is_on_floor():
  91. current_state = states.falling
  92. velocity.x = current_dir * move_speed
  93.  
  94. func running_process(delta: float) -> void:
  95. if not animation.current_animation == "run":
  96. animation.play("run")
  97. if current_dir == 0:
  98. current_state = states.idle
  99. if not is_on_floor():
  100. current_state = states.falling
  101. $CoyoteTime.start()
  102. can_coyote_jump = true
  103. velocity.x = current_dir * move_speed
  104.  
  105. func jumping_process(delta: float) -> void:
  106. if not animation.current_animation == "jump":
  107. animation.play("jump")
  108. if velocity.y > 0:
  109. current_state = states.falling
  110. velocity.x = current_dir * move_speed
  111.  
  112. func falling_process(delta: float) -> void:
  113. if not animation.current_animation == "fall":
  114. animation.play("fall")
  115. if is_on_floor():
  116. current_state = states.idle
  117. velocity.x = current_dir * move_speed
  118.  
  119. func throwing_process(delta: float) -> void:
  120. if not animation.current_animation == "throw":
  121. animation.play("throw")
  122. yield(get_tree().create_timer(0.2), "timeout")
  123. current_state = states.idle
  124.  
  125. func dead_process(delta: float) -> void:
  126. velocity = Vector2.ZERO
  127. gravity = 0
  128.  
  129. func _physics_process(delta: float) -> void:
  130. if current_state != states.dead:
  131. gravity_check()
  132. current_dir = Input.get_action_strength("right") - Input.get_action_strength("left")
  133. if current_dir == 1:
  134. sprite.flip_h = false
  135. elif current_dir == -1:
  136. sprite.flip_h = true
  137. velocity.y += gravity * delta
  138. velocity = move_and_slide(velocity, Vector2.UP)
  139.  
  140. match current_state:
  141. 0:
  142. idle_process(delta)
  143. 1:
  144. running_process(delta)
  145. 2:
  146. jumping_process(delta)
  147. 3:
  148. falling_process(delta)
  149. 4:
  150. throwing_process(delta)
  151. 5:
  152. dead_process(delta)
  153.  
  154.  
  155. func _on_SpikeHitBox_body_entered(body: Node) -> void:
  156. current_state = states.dead
  157. emit_signal("dead")
  158.  
  159.  
  160. func _on_SpikeHitBox_area_entered(area: Area2D) -> void:
  161. current_state = states.dead
  162. emit_signal("dead")
  163.  
  164.  
  165. func _on_CoyoteTime_timeout() -> void:
  166. can_coyote_jump = false
  167.  
Advertisement
Add Comment
Please, Sign In to add comment