Thwomp.gd 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. class_name Thwomp
  2. extends Enemy
  3. enum States{IDLE, FALLING, LANDED, RISING}
  4. var current_state := States.IDLE
  5. @onready var starting_y := global_position.y
  6. var can_fall := true
  7. func _physics_process(delta: float) -> void:
  8. velocity.x = move_toward(velocity.x, 0, 20)
  9. match current_state:
  10. States.IDLE:
  11. handle_idle(delta)
  12. States.FALLING:
  13. handle_falling(delta)
  14. States.RISING:
  15. handle_rising(delta)
  16. _:
  17. pass
  18. move_and_slide()
  19. func handle_idle(delta: float) -> void:
  20. var target_player = get_tree().get_first_node_in_group("Players")
  21. var x_distance = abs(target_player.global_position.x - global_position.x)
  22. velocity = Vector2.ZERO
  23. if x_distance < 24 and can_fall:
  24. can_fall = false
  25. current_state = States.FALLING
  26. $TrackJoint.detach()
  27. elif x_distance < 48:
  28. %Sprite.play("Look")
  29. else:
  30. %Sprite.play("Idle")
  31. func handle_falling(delta: float) -> void:
  32. %Sprite.play("Fall")
  33. velocity.y += (15 / delta) * delta
  34. velocity.y = clamp(velocity.y, -INF, Global.entity_max_fall_speed)
  35. handle_block_breaking()
  36. if is_on_floor():
  37. land()
  38. func handle_block_breaking() -> void:
  39. for i in %BlockBreakingHitbox.get_overlapping_bodies():
  40. if i is Block and i.get("destructable") == true:
  41. i.destroy()
  42. func land() -> void:
  43. AudioManager.play_sfx("cannon", global_position)
  44. current_state = States.LANDED
  45. await get_tree().create_timer(1, false).timeout
  46. current_state = States.RISING
  47. func handle_rising(delta: float) -> void:
  48. velocity.y = -50
  49. %Sprite.play("Idle")
  50. if global_position.y <= starting_y:
  51. global_position.y = starting_y
  52. if global_position.y <= starting_y or is_on_ceiling():
  53. current_state = States.IDLE
  54. await get_tree().create_timer(0.5, false).timeout
  55. can_fall = true