test_pyramid.gd 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. extends Test
  2. @export_range(1, 100) var height := 10
  3. @export_range(1, 100) var width_max := 100
  4. @export_range(1, 100) var depth_max := 1
  5. @export var box_size := Vector3(1.0, 1.0, 1.0)
  6. @export var box_spacing := Vector3(0.0, 0.0, 0.0)
  7. func _ready() -> void:
  8. _create_pyramid()
  9. func _create_pyramid() -> void:
  10. var root_node: Node3D = $Pyramid
  11. var template_body := create_rigidbody_box(box_size, true)
  12. var pos_y := 0.5 * box_size.y + box_spacing.y
  13. for level in height:
  14. var level_index := height - level - 1
  15. var num_boxes := 2 * level_index + 1
  16. var num_boxes_width := mini(num_boxes, width_max)
  17. var num_boxes_depth := mini(num_boxes, depth_max)
  18. var row_node := Node3D.new()
  19. row_node.transform.origin = Vector3(0.0, pos_y, 0.0)
  20. row_node.name = "Row%02d" % (level + 1)
  21. root_node.add_child(row_node)
  22. var pos_x := -0.5 * (num_boxes_width - 1) * (box_size.x + box_spacing.x)
  23. for box_index_x in num_boxes_width:
  24. var pos_z := -0.5 * (num_boxes_depth - 1) * (box_size.z + box_spacing.z)
  25. for box_index_z in num_boxes_depth:
  26. var box_index := box_index_x * box_index_z
  27. var box := template_body.duplicate()
  28. box.transform.origin = Vector3(pos_x, 0.0, pos_z)
  29. box.name = "Box%02d" % (box_index + 1)
  30. row_node.add_child(box)
  31. pos_z += box_size.z + box_spacing.z
  32. pos_x += box_size.x + box_spacing.x
  33. pos_y += box_size.y + box_spacing.y
  34. template_body.queue_free()