block.gd 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. class_name Block
  2. extends AnimatableBody2D
  3. ## One arc-shaped segment of a ring around the core.
  4. var indestructible := false
  5. var color := Params.COLOR_BLUE
  6. var _polygon := PackedVector2Array()
  7. func _init(inner: float, outer: float, begin: float, end: float, is_indestructible: bool) -> void:
  8. sync_to_physics = false
  9. collision_layer = Params.LAYER_BLOCKS
  10. collision_mask = 0
  11. indestructible = is_indestructible
  12. # Leave a ~1px gap between neighbouring segments.
  13. begin += 1.0 / outer
  14. end -= 1.0 / outer
  15. var steps := maxi(2, int((end - begin) * sqrt(outer) / 1.5))
  16. var outer_arc := _arc(outer, begin, end, steps)
  17. # The drawn shape stops 2px short of the inner radius to leave a gap between rings.
  18. _polygon = outer_arc + _arc(inner + 2.0, end, begin, steps)
  19. var collision := CollisionPolygon2D.new()
  20. collision.polygon = outer_arc + _arc(inner, end, begin, steps)
  21. add_child(collision)
  22. if indestructible:
  23. color = Params.COLOR_ORANGE
  24. else:
  25. color = Color8(
  26. int(102 * (randf() / 5.0 + 0.5)),
  27. int(217 * (randf() / 5.0 + 0.5)),
  28. int(239 * (randf() / 5.0 + 0.5)))
  29. func destroy() -> void:
  30. collision_layer = 0
  31. queue_free()
  32. func _draw() -> void:
  33. draw_colored_polygon(_polygon, color)
  34. static func _arc(radius: float, from: float, to: float, steps: int) -> PackedVector2Array:
  35. var points := PackedVector2Array()
  36. for i in steps + 1:
  37. var angle := lerpf(from, to, float(i) / steps)
  38. points.append(Vector2(sin(angle), cos(angle)) * radius)
  39. return points