1
0

block.gd 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. # Polygons have no antialiasing of their own; a smooth outline softens the edge.
  35. var outline := _polygon.duplicate()
  36. outline.append(_polygon[0])
  37. draw_polyline(outline, color, 1.0, true)
  38. static func _arc(radius: float, from: float, to: float, steps: int) -> PackedVector2Array:
  39. var points := PackedVector2Array()
  40. for i in steps + 1:
  41. var angle := lerpf(from, to, float(i) / steps)
  42. points.append(Vector2(sin(angle), cos(angle)) * radius)
  43. return points