cube_math.gd 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. extends Node3D
  2. @onready var _cube_point_scene: PackedScene = preload("res://assets/cube/cube_point.tscn")
  3. @onready var _parent = get_parent()
  4. var _is_parent_ready := false
  5. var _cube_points_math = []
  6. var _cube_math_spatials = []
  7. func _ready():
  8. _parent = get_parent()
  9. for i in range(27):
  10. # warning-ignore:integer_division
  11. var a: int = (i / 9) - 1
  12. # warning-ignore:integer_division
  13. var b: int = (i / 3) % 3 - 1
  14. var c: int = (i % 3) - 1
  15. var spatial_position: Vector3 = 5 * (a * Vector3.RIGHT + b * Vector3.UP + c * Vector3.BACK)
  16. _cube_math_spatials.append(Node3D.new())
  17. _cube_math_spatials[i].position = spatial_position
  18. _cube_math_spatials[i].name = "CubeMath #" + str(i) + ", " + str(a) + " " + str(b) + " " + str(c)
  19. add_child(_cube_math_spatials[i])
  20. func _process(delta):
  21. if Input.is_action_pressed(&"exit"):
  22. get_tree().quit()
  23. if Input.is_action_just_pressed(&"view_cube_demo"):
  24. get_tree().change_scene_to_file("res://assets/demo_scene.tscn")
  25. return
  26. if _is_parent_ready:
  27. if Input.is_action_just_pressed(&"reset_position"):
  28. transform = Transform3D.IDENTITY
  29. else:
  30. rotate_x(delta * (Input.get_axis(&"move_forward", &"move_back")))
  31. rotate_y(delta * (Input.get_axis(&"move_left", &"move_right")))
  32. rotate_z(delta * (Input.get_axis(&"move_clockwise", &"move_counterclockwise")))
  33. for i in range(27):
  34. _cube_points_math[i].global_transform = _cube_math_spatials[i].global_transform
  35. else:
  36. # This code block will be run only once. It's not in _ready() because the parent isn't set up there.
  37. for i in range(27):
  38. var my_cube_point_scene = _cube_point_scene.duplicate(true)
  39. var cube_point = my_cube_point_scene.instantiate()
  40. cube_point.name = "CubePoint #" + str(i)
  41. _cube_points_math.append(cube_point.get_child(0))
  42. _parent.add_child(cube_point)
  43. _is_parent_ready = true