NoteBlock.gd 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. class_name NoteBlock
  2. extends Block
  3. var bodies: Array[CharacterBody2D] = []
  4. signal bounced
  5. var animating := false
  6. @export var play_sfx := true
  7. func bounce_up() -> void:
  8. if bouncing or animating:
  9. return
  10. bounced.emit()
  11. bouncing = true
  12. animating = true
  13. %Animations.play("BounceUp")
  14. dispense_item(-1)
  15. await %Animations.animation_finished
  16. bouncing = false
  17. animating = false
  18. func _physics_process(_delta: float) -> void:
  19. for i in %Area.get_overlapping_areas():
  20. if i.owner is CharacterBody2D:
  21. bounce_down(i.owner)
  22. func bounce_down(body: PhysicsBody2D) -> void:
  23. if bouncing or animating:
  24. return
  25. animating = true
  26. bounced.emit()
  27. if play_sfx:
  28. AudioManager.play_sfx("note_block", global_position)
  29. bodies.append(body)
  30. if body is Player:
  31. body.normal_state.jump_queued = false
  32. body.spring_bouncing = true
  33. %Animations.play("BounceDown")
  34. dispense_item(1)
  35. await %Animations.animation_finished
  36. animating = false
  37. bouncing = false
  38. func bounce_bodies() -> void:
  39. for i in bodies:
  40. if i is Player:
  41. i.spring_bouncing = false
  42. if Global.player_action_pressed("jump", i.player_id):
  43. i.jump_cancelled = false
  44. i.has_jumped = true
  45. i.velocity.y = -350
  46. i.gravity = i.JUMP_GRAVITY
  47. else:
  48. i.velocity.y = -300
  49. i.gravity = i.FALL_GRAVITY
  50. else:
  51. i.velocity.y = -200
  52. if i is Thwomp:
  53. i.velocity = Vector2.ZERO
  54. bodies.clear()
  55. func dispense_item(direction := -1) -> void:
  56. if item == null or item_amount <= 0:
  57. return
  58. item_amount -= 1
  59. var node = item.instantiate()
  60. node.global_position = global_position + Vector2(0, 8 * direction)
  61. node.set("velocity", Vector2(0, (100 if direction == 1 else -150)))
  62. add_sibling(node)
  63. AudioManager.play_sfx("item_appear", global_position)