diff --git a/Assets/Control.cs b/Assets/Control.cs index e6f7dc2..003da5b 100644 --- a/Assets/Control.cs +++ b/Assets/Control.cs @@ -1,29 +1,32 @@ +using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements; public class Control : MonoBehaviour { - Button _sphere, _ring, _2ring, _cage, _2cube, _2sphere, _tetrahedron, _escherianKnot; Label _amount; void OnEnable() { var root = GetComponent().rootVisualElement; - - _sphere = root.Q("sphere") as Button; - _ring = root.Q("ring") as Button; - _2ring = root.Q("2ring") as Button; - _cage = root.Q("cage") as Button; - _2cube = root.Q("2cube") as Button; - _2sphere = root.Q("2sphere") as Button; - _tetrahedron = root.Q("tetrahedron") as Button; - _escherianKnot = root.Q("escherian-knot") as Button; + var universe = GetComponentInParent(); _amount = root.Q("Amount") as Label; - var universe = GetComponentInParent(); root.Q("add").RegisterCallback(evt => universe.AddStar(action: SetAmount)); root.Q("del").RegisterCallback(evt => universe.DelStar(action: SetAmount)); + string[] shapeNames = { + "sphere", + "ring", + "2ring", + "cage", + "2cube", + "2sphere", + "tetrahedron", + "escherian-knot", + }; + foreach (var name in shapeNames) + root.Q(name).RegisterCallback(evt => universe.SetShape(name)); } public void SetAmount(int amount) { diff --git a/Assets/Universe.cs b/Assets/Universe.cs index ff534ab..4f20d91 100644 --- a/Assets/Universe.cs +++ b/Assets/Universe.cs @@ -1,14 +1,17 @@ using System; using UnityEngine; -using UnityEngine.UIElements; using Random = UnityEngine.Random; public class Universe : MonoBehaviour { GameObject _prototype, _stars; Color _color = new(0, 0, 1); + Vector3[] _finalPositions; + Vector3[] _velocities; + float _transformShapeDelay = 0f; const ushort InitialAmount = 42; + const float TotalTransformShapeDelay = 1.25f; public int DegreePerSecond = 36; public Vector3Int RotationAxis = new(60, 80, 0); @@ -29,6 +32,19 @@ void Update() { foreach (Transform starTransform in _stars.transform) { starTransform.gameObject.GetComponent().material.color = _color; } + // Transform. + if (_transformShapeDelay > 0) { + if (_transformShapeDelay > Time.deltaTime) { + for (int i = 0; i < _stars.transform.childCount; i++) + _stars.transform.GetChild(i).localPosition += _velocities[i] * Time.deltaTime; + _transformShapeDelay -= Time.deltaTime; + } else { + for (int i = 0; i < _stars.transform.childCount; i++) + //_stars.transform.GetChild(i).localPosition += _velocities[i] * _transformShapeDelay; + _stars.transform.GetChild(i).localPosition = _finalPositions[i]; + _transformShapeDelay = 0f; + } + } } /* ******************** */ @@ -43,8 +59,8 @@ public void AddStar(Action action = null) { // Color. obj.GetComponent().material.color = _color; // Set random position. - obj.transform.localPosition = PointGenerators.Ring(); - //obj.transform.localPosition = PointGenerators.Sphere(); + //obj.transform.localPosition = PointGenerators.Ring(); + obj.transform.localPosition = PointGenerators.Sphere(); // Enable. obj.SetActive(true); @@ -82,6 +98,22 @@ void UpdateCurrentColor() { _color.g = 0; _color.b = x; _color.r = 1 - x; } } + + public void SetShape(string name) { + Func generator = name switch { + "sphere" => PointGenerators.Sphere, + _ => PointGenerators.Ring, + }; + + var N = _stars.transform.childCount; + _finalPositions = new Vector3[N]; + _velocities = new Vector3[N]; + _transformShapeDelay = TotalTransformShapeDelay; + for (int i = 0; i < N; i++) { + _finalPositions[i] = generator(); + _velocities[i] = (_finalPositions[i] - _stars.transform.GetChild(i).localPosition) / TotalTransformShapeDelay; + } + } } class PointGenerators {