1
0

game.gd 5.7 KB

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