game.gd 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. const SHIELD_HEIGHT := 6.0 # px above the bottom edge
  7. var level := 0
  8. ## Set once the level is won or lost; stops power-up drops and collision handling.
  9. var over := false
  10. ## When a ball last touched anything (seconds). Long gaps make triple balls more likely.
  11. var last_touch := 0.0
  12. var screen := Vector2.ZERO
  13. ## Balls the shield will still save.
  14. var shield := 0:
  15. set(value):
  16. shield = value
  17. queue_redraw()
  18. var _started := 0.0
  19. @onready var blocks: Blocks = $Blocks
  20. @onready var paddle: Paddle = $Paddle
  21. @onready var balls: Node2D = $Balls
  22. @onready var power_ups: Node2D = $PowerUps
  23. @onready var alerts: Control = $HUD/Alerts
  24. @onready var end_menu: Control = $HUD/EndMenu
  25. @onready var end_message: Label = $HUD/EndMenu/Message
  26. @onready var end_button: Button = $HUD/EndMenu/Button
  27. func _ready() -> void:
  28. screen = get_viewport_rect().size
  29. level = Progress.current_level
  30. last_touch = _now()
  31. _started = _now()
  32. $HUD/Level.text = "Level %d" % level
  33. paddle.position = Vector2(screen.x / 2.0, screen.y - Params.PADDLE_ALTITUDE - Params.PADDLE_HEIGHT / 2.0)
  34. # The rings sit centred on screen; when there are many, the core moves up so they keep
  35. # their thickness and still leave PADDLE_CLEARANCE above the paddle.
  36. var lowest_ring_edge := paddle.position.y - Params.PADDLE_HEIGHT / 2.0 - Params.PADDLE_CLEARANCE
  37. var natural_radius := Params.CORE_RADIUS + Params.ring_count(level) * Params.RING_SIZE
  38. var core_y := clampf(lowest_ring_edge - natural_radius, screen.y * Params.HIGHEST_CORE, screen.y / 2.0)
  39. blocks.position = Vector2(screen.x / 2.0, core_y)
  40. blocks.build(level, lowest_ring_edge - core_y)
  41. paddle.released.connect(_release_caught_balls)
  42. end_button.pressed.connect(_restart)
  43. _drop_power_up()
  44. _drop_triple_balls()
  45. _add_and_throw_ball()
  46. func _on_ball_hit(ball: Ball, other: Node) -> void:
  47. last_touch = _now()
  48. if over:
  49. return
  50. if other is Block:
  51. var block := other as Block
  52. match block.strike(ball):
  53. Block.Hit.BROKE, Block.Hit.DAMAGED:
  54. $Sounds/Pipe.play()
  55. Block.Hit.BLOCKED:
  56. $Sounds/Metal.play()
  57. if ball.bomb:
  58. ball.bomb = false
  59. blocks.explode(ball.global_position, Params.BOMB_RADIUS)
  60. $Sounds/Bomb.play()
  61. elif other is Core:
  62. _win()
  63. elif other is Paddle:
  64. if paddle.catch_time > 0.0:
  65. ball.stick_to(paddle, Params.CATCH_HOLD_SECONDS)
  66. elif ball.velocity.y < 0.0:
  67. ball.aim_from(paddle)
  68. elif other.is_in_group("bottom"):
  69. if shield > 0:
  70. shield -= 1
  71. _alert(Params.MSG_SHIELD_USED)
  72. $Sounds/Metal.play()
  73. else:
  74. _lose_ball(ball)
  75. func _on_power_up_collected(power_up: PowerUp) -> void:
  76. if over:
  77. return
  78. var live := _live_balls()
  79. match power_up.kind:
  80. PowerUp.Kind.SAVE:
  81. Progress.save_level(level)
  82. _alert(Params.MSG_LEVEL_SAVED)
  83. PowerUp.Kind.TRIPLE_BALLS:
  84. _alert(Params.MSG_TRIPLE_BALLS)
  85. for i in 3 - live.size():
  86. _add_and_throw_ball()
  87. PowerUp.Kind.PADDLE:
  88. if randf() < 0.5:
  89. _alert(Params.MSG_DOUBLE_PADDLE)
  90. paddle.double_size()
  91. else:
  92. _alert(Params.MSG_HALF_PADDLE)
  93. paddle.half_size()
  94. PowerUp.Kind.SUPERBALL:
  95. if blocks.has_unbreakable() and not live.is_empty():
  96. _alert(Params.MSG_SUPER_BALL)
  97. live[0].superball = true
  98. PowerUp.Kind.SLOW:
  99. _alert(Params.MSG_SLOW)
  100. for ball: Ball in live:
  101. ball.set_time_scale(Params.SLOW_SCALE, Params.SLOW_SECONDS)
  102. PowerUp.Kind.RUSH:
  103. _alert(Params.MSG_RUSH)
  104. for ball: Ball in live:
  105. ball.set_time_scale(Params.RUSH_SCALE, Params.RUSH_SECONDS)
  106. PowerUp.Kind.CATCH:
  107. _alert(Params.MSG_CATCH)
  108. paddle.set_catch(Params.CATCH_SECONDS)
  109. PowerUp.Kind.SHIELD:
  110. _alert(Params.MSG_SHIELD)
  111. shield = Params.SHIELD_CHARGES
  112. PowerUp.Kind.BIG_BALL:
  113. _alert(Params.MSG_BIG_BALL)
  114. for ball: Ball in live:
  115. ball.set_big(Params.BIG_BALL_SECONDS)
  116. PowerUp.Kind.FIREBALL:
  117. _alert(Params.MSG_FIREBALL)
  118. for ball: Ball in live:
  119. ball.set_fire(Params.FIREBALL_SECONDS)
  120. PowerUp.Kind.LASER:
  121. _alert(Params.MSG_LASER)
  122. paddle.set_laser(Params.LASER_SECONDS)
  123. PowerUp.Kind.FREEZE:
  124. _alert(Params.MSG_FREEZE)
  125. blocks.freeze(Params.FREEZE_SECONDS)
  126. PowerUp.Kind.BOMB:
  127. _alert(Params.MSG_BOMB)
  128. if not live.is_empty():
  129. live[0].bomb = true
  130. PowerUp.Kind.HOMING:
  131. _alert(Params.MSG_HOMING)
  132. for ball: Ball in live:
  133. ball.set_homing(Params.HOMING_SECONDS)
  134. func _release_caught_balls() -> void:
  135. for ball: Ball in _live_balls():
  136. if ball.is_stuck_to(paddle):
  137. ball.release()
  138. func _lose_ball(ball: Ball) -> void:
  139. $Sounds/Bomb.play()
  140. _burst(ball.global_position)
  141. ball.queue_free()
  142. if _live_balls().is_empty():
  143. over = true
  144. _clear_play_field()
  145. end_message.text = Params.MSG_OVER
  146. end_message.show()
  147. end_button.text = Params.MSG_RESTART
  148. end_menu.show()
  149. func _win() -> void:
  150. over = true
  151. $Sounds/Win.play()
  152. _clear_play_field()
  153. await blocks.core.explode()
  154. level += 1
  155. end_message.hide()
  156. end_button.text = Params.MSG_NEXT_LEVEL
  157. end_menu.show()
  158. ## Restarts at the current level (the next one after a win).
  159. func _restart() -> void:
  160. Progress.current_level = level
  161. get_tree().reload_current_scene()
  162. func _clear_play_field() -> void:
  163. for node in balls.get_children() + power_ups.get_children():
  164. node.queue_free()
  165. paddle.queue_free()
  166. shield = 0
  167. func _add_and_throw_ball() -> void:
  168. if paddle.is_queued_for_deletion():
  169. return
  170. var ball: Ball = BALL_SCENE.instantiate()
  171. ball.position = paddle.position + Vector2(0, -Params.PADDLE_HEIGHT / 2.0 - Params.BALL_RADIUS - 1.0)
  172. ball.target = blocks.core
  173. ball.max_speed = Params.max_ball_speed(level)
  174. ball.hit.connect(_on_ball_hit)
  175. balls.add_child(ball)
  176. ball.throw(Params.launch_speed(level))
  177. func _live_balls() -> Array:
  178. return balls.get_children().filter(func(ball: Node) -> bool: return not ball.is_queued_for_deletion())
  179. # Power-up drops. Each one rolls its chance, then reschedules itself.
  180. func _drop_power_up() -> void:
  181. if over:
  182. return
  183. if randf() < Params.power_up_chance(level):
  184. var kind := _pick_power_up()
  185. if kind >= 0:
  186. _spawn_power_up(kind as PowerUp.Kind)
  187. _schedule(_drop_power_up)
  188. ## Weighted pick among the power-ups that can drop right now; -1 if none can.
  189. func _pick_power_up() -> int:
  190. var kinds: Array[int] = []
  191. var weights: Array[float] = []
  192. for kind: int in PowerUp.KINDS:
  193. var w := PowerUp.weight(kind as PowerUp.Kind, level)
  194. if kind == PowerUp.Kind.SAVE and _now() - _started < Params.SAVE_DELAY:
  195. w = 0.0
  196. # A super ball only helps while something unbreakable is left.
  197. if kind == PowerUp.Kind.SUPERBALL and not blocks.has_unbreakable():
  198. w = 0.0
  199. if w > 0.0:
  200. kinds.append(kind)
  201. weights.append(w)
  202. var roll: float = randf() * weights.reduce(func(sum: float, w: float) -> float: return sum + w, 0.0)
  203. for i in kinds.size():
  204. roll -= weights[i]
  205. if roll <= 0.0:
  206. return kinds[i]
  207. return -1
  208. func _drop_triple_balls() -> void:
  209. if over:
  210. return
  211. var idle := _now() - last_touch
  212. if randf() < idle * Params.DROP_TRIPLE_BALL:
  213. last_touch = _now()
  214. if _live_balls().size() < 3:
  215. _spawn_power_up(PowerUp.Kind.TRIPLE_BALLS)
  216. _schedule(_drop_triple_balls)
  217. func _schedule(callback: Callable) -> void:
  218. var interval := Params.POWER_UP_INTERVAL
  219. var delay := interval + randf_range(-1.0, 1.0) * interval / 4.0
  220. get_tree().create_timer(delay).timeout.connect(callback)
  221. func _spawn_power_up(kind: PowerUp.Kind) -> void:
  222. var power_up: PowerUp = POWER_UP_SCENE.instantiate()
  223. power_up.kind = kind
  224. power_up.position = Vector2(randf_range(PowerUp.RADIUS, screen.x - PowerUp.RADIUS), PowerUp.RADIUS + 1.0)
  225. power_up.collected.connect(_on_power_up_collected)
  226. power_ups.add_child(power_up)
  227. func _alert(text: String) -> void:
  228. var label := Label.new()
  229. label.text = text
  230. label.add_theme_font_size_override("font_size", 40)
  231. label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
  232. label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
  233. label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
  234. label.mouse_filter = Control.MOUSE_FILTER_IGNORE
  235. alerts.add_child(label)
  236. label.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
  237. var tween := label.create_tween()
  238. tween.tween_property(label, "modulate:a", 0.0, 3.0)
  239. tween.tween_callback(label.queue_free)
  240. func _burst(at: Vector2) -> void:
  241. var burst: CPUParticles2D = SUN_BURST_SCENE.instantiate()
  242. burst.position = at
  243. add_child(burst)
  244. func _draw() -> void:
  245. if shield > 0:
  246. var y := screen.y - SHIELD_HEIGHT
  247. draw_line(Vector2(0, y), Vector2(screen.x, y), Color("#f4f4f0"), 4.0, true)
  248. func _now() -> float:
  249. return Time.get_ticks_msec() / 1000.0