test_pyramid.gd 958 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. extends Test
  2. export(int, 1, 100) var height = 10
  3. export(Vector2) var box_size = Vector2(40.0, 40.0)
  4. export(Vector2) var box_spacing = Vector2(0.0, 0.0)
  5. func _ready():
  6. _create_pyramid()
  7. func _create_pyramid():
  8. var root_node = $Pyramid
  9. var template_body = create_rigidbody_box(box_size, true)
  10. var pos_y = -0.5 * box_size.y - box_spacing.y
  11. for level in height:
  12. var level_index = height - level - 1
  13. var num_boxes = 2 * level_index + 1
  14. var row_node = Node2D.new()
  15. row_node.position = Vector2(0.0, pos_y)
  16. row_node.name = "Row%02d" % (level + 1)
  17. root_node.add_child(row_node)
  18. var pos_x = -0.5 * (num_boxes - 1) * (box_size.x + box_spacing.x)
  19. for box_index in range(num_boxes):
  20. var box = template_body.duplicate()
  21. box.position = Vector2(pos_x, 0.0)
  22. box.name = "Box%02d" % (box_index + 1)
  23. row_node.add_child(box)
  24. pos_x += box_size.x + box_spacing.x
  25. pos_y -= box_size.y + box_spacing.y
  26. template_body.queue_free()