game.gd 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. extends Node2D
  2. ## One level: the core and its rings, the paddle, the balls and the falling power-ups.
  3. const BALL_SCENE := preload("res://scenes/ball.tscn")
  4. const POWER_UP_SCENE := preload("res://scenes/power_up.tscn")
  5. const SUN_BURST_SCENE := preload("res://scenes/sun_burst.tscn")
  6. var level := 0
  7. ## Set once the level is won or lost; stops power-up drops and collision handling.
  8. var over := false
  9. ## When a ball last touched anything (seconds). Long gaps make triple balls more likely.
  10. var last_touch := 0.0
  11. var screen := Vector2.ZERO
  12. @onready var blocks: Blocks = $Blocks
  13. @onready var paddle: Paddle = $Paddle
  14. @onready var balls: Node2D = $Balls
  15. @onready var power_ups: Node2D = $PowerUps
  16. @onready var alerts: Control = $HUD/Alerts
  17. @onready var end_menu: Control = $HUD/EndMenu
  18. @onready var end_message: Label = $HUD/EndMenu/Message
  19. @onready var end_button: Button = $HUD/EndMenu/Button
  20. func _ready() -> void:
  21. screen = get_viewport_rect().size
  22. level = Progress.current_level
  23. last_touch = _now()
  24. $HUD/Level.text = "Level %d" % level
  25. blocks.position = screen / 2.0
  26. blocks.build(level)
  27. paddle.position = Vector2(screen.x / 2.0, screen.y - Params.PADDLE_ALTITUDE - Params.PADDLE_HEIGHT / 2.0)
  28. end_button.pressed.connect(_restart)
  29. get_tree().create_timer(10.0).timeout.connect(_drop_level_save)
  30. _drop_triple_balls()
  31. _drop_paddle()
  32. _drop_superball()
  33. _add_and_throw_ball()
  34. func _on_ball_hit(ball: Ball, other: Node) -> void:
  35. last_touch = _now()
  36. if over:
  37. return
  38. if other is Block:
  39. var block := other as Block
  40. if not block.indestructible:
  41. $Sounds/Pipe.play()
  42. block.destroy()
  43. else:
  44. $Sounds/Metal.play()
  45. if ball.superball:
  46. block.destroy()
  47. ball.superball = false
  48. elif other is Core:
  49. _win()
  50. elif other.is_in_group("bottom"):
  51. _lose_ball(ball)
  52. func _on_power_up_collected(power_up: PowerUp) -> void:
  53. if over:
  54. return
  55. match power_up.kind:
  56. PowerUp.Kind.SAVE:
  57. Progress.save_level(level)
  58. _alert(Params.MSG_LEVEL_SAVED)
  59. PowerUp.Kind.TRIPLE_BALLS:
  60. _alert(Params.MSG_TRIPLE_BALLS)
  61. for i in 3 - _live_balls().size():
  62. _add_and_throw_ball()
  63. PowerUp.Kind.PADDLE:
  64. if randf() < 0.5:
  65. _alert(Params.MSG_DOUBLE_PADDLE)
  66. paddle.double_size()
  67. else:
  68. _alert(Params.MSG_HALF_PADDLE)
  69. paddle.half_size()
  70. PowerUp.Kind.SUPERBALL:
  71. _alert(Params.MSG_SUPER_BALL)
  72. var live := _live_balls()
  73. if not live.is_empty():
  74. live[0].superball = true
  75. func _lose_ball(ball: Ball) -> void:
  76. $Sounds/Bomb.play()
  77. _burst(ball.global_position)
  78. ball.queue_free()
  79. if _live_balls().is_empty():
  80. over = true
  81. _clear_play_field()
  82. end_message.text = Params.MSG_OVER
  83. end_message.show()
  84. end_button.text = Params.MSG_RESTART
  85. end_menu.show()
  86. func _win() -> void:
  87. over = true
  88. $Sounds/Win.play()
  89. _clear_play_field()
  90. await blocks.core.explode()
  91. level += 1
  92. end_message.hide()
  93. end_button.text = Params.MSG_NEXT_LEVEL
  94. end_menu.show()
  95. ## Restarts at the current level (the next one after a win).
  96. func _restart() -> void:
  97. Progress.current_level = level
  98. get_tree().reload_current_scene()
  99. func _clear_play_field() -> void:
  100. for node in balls.get_children() + power_ups.get_children():
  101. node.queue_free()
  102. paddle.queue_free()
  103. func _add_and_throw_ball() -> void:
  104. if paddle.is_queued_for_deletion():
  105. return
  106. var ball: Ball = BALL_SCENE.instantiate()
  107. ball.position = paddle.position + Vector2(0, -Params.PADDLE_HEIGHT / 2.0 - Params.BALL_RADIUS - 1.0)
  108. ball.hit.connect(_on_ball_hit)
  109. balls.add_child(ball)
  110. ball.throw()
  111. func _live_balls() -> Array:
  112. return balls.get_children().filter(func(ball: Node) -> bool: return not ball.is_queued_for_deletion())
  113. # Power-up drops. Each one rolls its chance, then reschedules itself.
  114. func _drop_level_save() -> void:
  115. if over:
  116. return
  117. if randf() < Params.DROP_LEVEL_SAVE:
  118. _spawn_power_up(PowerUp.Kind.SAVE)
  119. _schedule(_drop_level_save)
  120. func _drop_triple_balls() -> void:
  121. if over:
  122. return
  123. var idle := _now() - last_touch
  124. if randf() < idle * Params.DROP_TRIPLE_BALL:
  125. last_touch = _now()
  126. if _live_balls().size() < 3:
  127. _spawn_power_up(PowerUp.Kind.TRIPLE_BALLS)
  128. _schedule(_drop_triple_balls)
  129. func _drop_paddle() -> void:
  130. if over:
  131. return
  132. if randf() < Params.DROP_PADDLE_BALL:
  133. _spawn_power_up(PowerUp.Kind.PADDLE)
  134. _schedule(_drop_paddle)
  135. func _drop_superball() -> void:
  136. if over:
  137. return
  138. if randf() < Params.DROP_SUPER_BALL:
  139. _spawn_power_up(PowerUp.Kind.SUPERBALL)
  140. _schedule(_drop_superball)
  141. func _schedule(callback: Callable) -> void:
  142. var interval := Params.POWER_UP_INTERVAL
  143. var delay := interval + randf_range(-1.0, 1.0) * interval / 4.0
  144. get_tree().create_timer(delay).timeout.connect(callback)
  145. func _spawn_power_up(kind: PowerUp.Kind) -> void:
  146. var power_up: PowerUp = POWER_UP_SCENE.instantiate()
  147. power_up.kind = kind
  148. power_up.position = Vector2(randf_range(PowerUp.RADIUS, screen.x - PowerUp.RADIUS), PowerUp.RADIUS + 1.0)
  149. power_up.collected.connect(_on_power_up_collected)
  150. power_ups.add_child(power_up)
  151. func _alert(text: String) -> void:
  152. var label := Label.new()
  153. label.text = text
  154. label.add_theme_font_size_override("font_size", 40)
  155. label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
  156. label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
  157. label.mouse_filter = Control.MOUSE_FILTER_IGNORE
  158. alerts.add_child(label)
  159. label.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
  160. var tween := label.create_tween()
  161. tween.tween_property(label, "modulate:a", 0.0, 3.0)
  162. tween.tween_callback(label.queue_free)
  163. func _burst(at: Vector2) -> void:
  164. var burst: CPUParticles2D = SUN_BURST_SCENE.instantiate()
  165. burst.position = at
  166. add_child(burst)
  167. func _now() -> float:
  168. return Time.get_ticks_msec() / 1000.0