1 using Godot; 2 3 public class CubeMath : Spatial 4 { 5 private static PackedScene cubePointScene = ResourceLoader.Load<PackedScene>("res://assets/cube/cube_point.tscn"); 6 7 private bool _isParentReady = false; 8 private Node2D _parent; 9 private Spatial[] _cubePointsMath = new Spatial[27]; // The math node of each 2.5D cube point 10 private Spatial[] _cubeMathSpatials = new Spatial[27]; // The CubeMath children that find position. 11 _Ready()12 public override void _Ready() 13 { 14 _parent = GetParent<Node2D>(); 15 16 // Initialize the cube 17 for (int i = 0; i < 27; i++) 18 { 19 int a = (i / 9) - 1; 20 int b = (i / 3) % 3 - 1; 21 int c = (i % 3) - 1; 22 Vector3 spatialPosition = 5 * (a * Vector3.Right + b * Vector3.Up + c * Vector3.Back); 23 24 _cubeMathSpatials[i] = new Spatial(); 25 _cubeMathSpatials[i].Translation = spatialPosition; 26 _cubeMathSpatials[i].Name = "CubeMath #" + i + ", " + a + " " + b + " " + c; 27 AddChild(_cubeMathSpatials[i]); 28 } 29 } 30 _Process(float delta)31 public override void _Process(float delta) 32 { 33 if (Input.IsActionPressed("exit")) 34 { 35 GetTree().Quit(); 36 } 37 38 if (Input.IsActionJustPressed("view_cube_demo")) 39 { 40 GetTree().ChangeScene("res://assets/demo_scene.tscn"); 41 return; 42 } 43 44 if (_isParentReady) 45 { 46 RotateX(delta * (Input.GetActionStrength("move_back") - Input.GetActionStrength("move_forward"))); 47 RotateY(delta * (Input.GetActionStrength("move_right") - Input.GetActionStrength("move_left"))); 48 RotateZ(delta * (Input.GetActionStrength("move_counterclockwise") - Input.GetActionStrength("move_clockwise"))); 49 if (Input.IsActionJustPressed("reset_position")) 50 { 51 Transform = Transform.Identity; 52 } 53 for (int i = 0; i < 27; i++) 54 { 55 _cubePointsMath[i].GlobalTransform = _cubeMathSpatials[i].GlobalTransform; 56 } 57 } 58 else 59 { 60 // This code block will be run only once. It's not in _Ready() because the parent isn't set up there. 61 for (int i = 0; i < 27; i++) 62 { 63 PackedScene myCubePointScene = cubePointScene.Duplicate(true) as PackedScene; 64 Node25D cubePoint = myCubePointScene.Instance() as Node25D; 65 cubePoint.Name = "CubePoint #" + i; 66 _cubePointsMath[i] = cubePoint.GetChild<Spatial>(0); 67 _parent.AddChild(cubePoint); 68 } 69 _isParentReady = true; 70 } 71 } 72 } 73