diff --git a/vr-configurator/Assets/Scenes/SampleScene.unity b/vr-configurator/Assets/Scenes/SampleScene.unity index 0dd73de..0e1ffce 100644 --- a/vr-configurator/Assets/Scenes/SampleScene.unity +++ b/vr-configurator/Assets/Scenes/SampleScene.unity @@ -12934,6 +12934,7 @@ MonoBehaviour: colliderSurface: {fileID: 163506087} interactable: {fileID: 698127119} rayInteractable: {fileID: 698127110} + afterBirth: 0 PortSelector: {fileID: 1494770816} Index: -1 --- !u!1 &700047848 @@ -21960,6 +21961,7 @@ MonoBehaviour: colliderSurface: {fileID: 557957011} interactable: {fileID: 1272917968} rayInteractable: {fileID: 1272917969} + afterBirth: 0 PortSelector: {fileID: 1494770816} Index: -1 --- !u!114 &1272917968 diff --git a/vr-configurator/Assets/Scripts/Models/ModelBehaviour.cs b/vr-configurator/Assets/Scripts/Models/ModelBehaviour.cs index e387aa0..45f3774 100644 --- a/vr-configurator/Assets/Scripts/Models/ModelBehaviour.cs +++ b/vr-configurator/Assets/Scripts/Models/ModelBehaviour.cs @@ -34,6 +34,8 @@ public class ModelBehaviour : MonoBehaviour, IResettable bool lateInited = false; + public bool afterBirth = false; + public PortSelector PortSelector; public int Index = -1; @@ -133,6 +135,8 @@ public class ModelBehaviour : MonoBehaviour, IResettable Debug.Log($"Spawning {gameObject.name}'s ports"); SpawnChildPorts(newModel); } + + afterBirth = true; } void LateUpdateModel() diff --git a/vr-configurator/Assets/Scripts/Transform/Exploder.cs b/vr-configurator/Assets/Scripts/Transform/Exploder.cs index ea23bb5..f647724 100644 --- a/vr-configurator/Assets/Scripts/Transform/Exploder.cs +++ b/vr-configurator/Assets/Scripts/Transform/Exploder.cs @@ -62,47 +62,41 @@ public class Exploder : MonoBehaviour, IResettable, IPointerDownHandler void PopulateModelList() { - models.Clear(); + List newModels = new List(); foreach (Transform child in baseModel.GetComponentsInChildren()) { var cmb = child.GetComponent(); if (cmb == null) { - continue; //only for basemodel + continue; //skip basemodel } - Port port = cmb.GetParentPort(); - models.Add(new ExploderModel(port, cmb.ChildModel, cmb)); - } - } - - void UpdateModelList() - { - List modelss = new List(); - foreach (var exploModel in models) - { - if (exploModel.cmb == null) + bool skip = false; + foreach (var model in models) { - continue; //skip dead ends - } - if (exploModel.model != exploModel.cmb.ChildModel) //update - { - Debug.LogWarning($"Updating Model List from {exploModel.model.NameId} to {exploModel.cmb.ChildModel.NameId}"); - // add children + itself - foreach (Transform child in exploModel.cmb.GetComponentsInChildren()) + if (model.tf == child) { - var cmb = child.GetComponent(); - if (cmb == null) + if (model.model == cmb.ChildModel) //nothing changed { - continue; + newModels.Add(model); } - Port port = cmb.GetParentPort(); - modelss.Add(new ExploderModel(port, cmb.ChildModel, cmb)); + else // model is changed -> its unexploded + { + newModels.Add(new ExploderModel(model.port, cmb.ChildModel, model.cmb, model.tf, false)); + } + skip = true; + break; } + } + if (skip) + { continue; } - modelss.Add(exploModel); + //new model altogether -> its unexploded + Port port = cmb.GetParentPort(); + newModels.Add(new ExploderModel(port, cmb.ChildModel, cmb, child, false)); } - models = modelss; + models.Clear(); + models = newModels; } void CleanModelList() @@ -112,6 +106,7 @@ public class Exploder : MonoBehaviour, IResettable, IPointerDownHandler void ForceSetPos() { + Debug.Log($"ForceSetPos {explode}"); foreach (var exploModel in models) { exploModel.tf.localPosition = explode ? exploModel.goalPos : exploModel.initPos; @@ -135,8 +130,12 @@ public class Exploder : MonoBehaviour, IResettable, IPointerDownHandler public void HandleModelChange() { - UpdateModelList(); - ForceSetPos(); + //UpdateModelList(); + if (mm.BaseModelBehaviour.afterBirth) + { + PopulateModelList(); + ForceSetPos(); + } } public void ResetThis() diff --git a/vr-configurator/Assets/Scripts/Transform/ExploderModel.cs b/vr-configurator/Assets/Scripts/Transform/ExploderModel.cs index 22cde73..80cd285 100644 --- a/vr-configurator/Assets/Scripts/Transform/ExploderModel.cs +++ b/vr-configurator/Assets/Scripts/Transform/ExploderModel.cs @@ -12,18 +12,18 @@ public class ExploderModel public Vector3 initPos; public Vector3 goalPos; - public ExploderModel(Port port, ChildModel model, ChildModelBehaviour cmb) + public ExploderModel(Port port, ChildModel model, ChildModelBehaviour cmb, Transform tf, bool exploded) { this.port = port; this.model = model; this.cmb = cmb; - CalculatePos(); + this.tf = tf; + CalculatePos(exploded); } - public void CalculatePos() + public void CalculatePos(bool exploded = false) { - tf = cmb.transform; - initPos = tf.localPosition; - goalPos = tf.localPosition + port.ExplodeDirection; + initPos = exploded ? tf.localPosition - port.ExplodeDirection : tf.localPosition; + goalPos = exploded ? tf.localPosition : tf.localPosition + port.ExplodeDirection; } }