blocks.gd 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. class_name Blocks
  2. extends Node2D
  3. ## The core plus rings of rotating blocks around it, generated from the level number.
  4. const BLAST_SCENE := preload("res://scenes/blast.tscn")
  5. const SEGMENTS_PER_RING := 10
  6. const TNT_RADIUS := 80.0
  7. const TNT_CHAIN_DELAY := 0.12 # seconds between one TNT going off and the next
  8. var core: Core
  9. var _rings: Array[Node2D] = []
  10. var _speeds: Array[float] = []
  11. var _frozen_time := 0.0
  12. ## Builds the level's rings. Rings are made thinner when needed so they all fit within
  13. ## [param max_radius]; the ring count, and so the difficulty, stays the same.
  14. func build(level: int, max_radius: float) -> void:
  15. var layout := _layout(level)
  16. var size := minf(layout.size, (max_radius - Params.CORE_RADIUS) / layout.rings)
  17. for index in range(layout.rings - 1, -1, -1):
  18. _add_ring(level, size, index, layout.indestructible_chance)
  19. core = Core.new()
  20. add_child(core)
  21. ## Stops the rings turning for a while.
  22. func freeze(seconds: float) -> void:
  23. _frozen_time = maxf(_frozen_time, seconds)
  24. func has_unbreakable() -> bool:
  25. for ring in _rings:
  26. for block: Block in ring.get_children():
  27. if not block.is_breakable() and block.collision_layer != 0:
  28. return true
  29. return false
  30. ## Damages every breakable segment within [param radius] of [param at] (global position).
  31. func explode(at: Vector2, radius: float) -> void:
  32. var blast: Node2D = BLAST_SCENE.instantiate()
  33. blast.radius = radius
  34. blast.position = to_local(at)
  35. add_child(blast)
  36. var query := PhysicsShapeQueryParameters2D.new()
  37. var circle := CircleShape2D.new()
  38. circle.radius = radius
  39. query.shape = circle
  40. query.transform = Transform2D(0.0, at)
  41. query.collision_mask = Params.LAYER_BLOCKS
  42. for hit in get_world_2d().direct_space_state.intersect_shape(query, 64):
  43. if hit.collider is Block:
  44. (hit.collider as Block).damage()
  45. func _physics_process(delta: float) -> void:
  46. if _frozen_time > 0.0:
  47. _frozen_time -= delta
  48. return
  49. for i in _rings.size():
  50. _rings[i].rotation_degrees += _speeds[i] * delta
  51. func _on_detonated(block: Block) -> void:
  52. var at := block.center()
  53. get_tree().create_timer(TNT_CHAIN_DELAY, false, true).timeout.connect(explode.bind(at, TNT_RADIUS))
  54. ## Ring count, ring thickness and indestructible-block chance for a level. All of them only
  55. ## ever grow with the level, so no level is easier than the one before it.
  56. static func _layout(level: int) -> Dictionary:
  57. return {
  58. rings = Params.ring_count(level),
  59. size = Params.RING_SIZE,
  60. indestructible_chance = Params.indestructible_chance(level),
  61. }
  62. ## Picks the kind of one segment. Special kinds unlock every 50 levels; their share of
  63. ## the segments grows with the level, and the newest kind is twice as likely as the others.
  64. static func _segment_kind(level: int, indestructible_chance: float) -> Block.Kind:
  65. if indestructible_chance >= randf():
  66. return Block.Kind.INDESTRUCTIBLE
  67. var unlocked: Array[Block.Kind] = []
  68. for kind: Block.Kind in Block.KINDS:
  69. var unlock: int = Block.KINDS[kind].unlock
  70. if unlock > 0 and unlock <= level:
  71. unlocked.append(kind)
  72. if unlocked.is_empty() or randf() >= Params.special_segment_share(level):
  73. return Block.Kind.NORMAL
  74. var weights: Array[float] = []
  75. for kind in unlocked:
  76. weights.append(2.0 if kind == unlocked[-1] else 1.0)
  77. var roll: float = randf() * weights.reduce(func(sum: float, w: float) -> float: return sum + w, 0.0)
  78. for i in unlocked.size():
  79. roll -= weights[i]
  80. if roll <= 0.0:
  81. return unlocked[i]
  82. return unlocked[-1]
  83. func _add_ring(level: int, size: float, index: int, indestructible_chance: float) -> void:
  84. var inner := index * size + Params.CORE_RADIUS
  85. var outer := (index + 1) * size + Params.CORE_RADIUS
  86. # Evenly spaced segment boundaries, each jittered so segments have uneven lengths.
  87. var step := TAU / SEGMENTS_PER_RING
  88. var bounds: Array[float] = []
  89. for i in SEGMENTS_PER_RING:
  90. bounds.append(step * i + randf_range(-1.0, 1.0) * 0.45 * step)
  91. bounds.append(bounds[0] + TAU)
  92. var ring := Node2D.new()
  93. for i in SEGMENTS_PER_RING:
  94. var block := Block.new(inner, outer, bounds[i], bounds[i + 1], _segment_kind(level, indestructible_chance))
  95. block.detonated.connect(_on_detonated)
  96. ring.add_child(block)
  97. add_child(ring)
  98. _rings.append(ring)
  99. var max_speed := Params.ring_speed(level)
  100. _speeds.append(randf_range(-max_speed, max_speed))