game.gd 6.0 KB

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