Improve Explode
This commit is contained in:
@@ -1,11 +1,7 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using TMPro;
|
using TMPro;
|
||||||
using Unity.VisualScripting;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.EventSystems;
|
using UnityEngine.EventSystems;
|
||||||
using UnityEngine.Serialization;
|
|
||||||
using UnityEngine.UI;
|
|
||||||
|
|
||||||
public class ExplodeModel : MonoBehaviour, IResettable, IPointerDownHandler
|
public class ExplodeModel : MonoBehaviour, IResettable, IPointerDownHandler
|
||||||
{
|
{
|
||||||
@@ -16,20 +12,19 @@ public class ExplodeModel : MonoBehaviour, IResettable, IPointerDownHandler
|
|||||||
|
|
||||||
List<Vector3> goalPos = new List<Vector3>();
|
List<Vector3> goalPos = new List<Vector3>();
|
||||||
List<Vector3> initialPos = new List<Vector3>();
|
List<Vector3> initialPos = new List<Vector3>();
|
||||||
|
List<Transform> transforms = new List<Transform>();
|
||||||
|
|
||||||
static readonly int finalTick = 60 * 2; // 60fps * 2sek
|
static readonly int finalTick = 60 * 2; // 60fps * 2sek
|
||||||
bool explode = false;
|
bool explode = false;
|
||||||
int tick = finalTick + 1;
|
int tick = finalTick + 1;
|
||||||
float lastClickTime = -1f;
|
|
||||||
const float debounceTime = 0.3f; // 300ms Sperre
|
|
||||||
|
|
||||||
public void OnPointerDown(PointerEventData eventData)
|
public void OnPointerDown(PointerEventData eventData)
|
||||||
{
|
{
|
||||||
if (Time.time - lastClickTime < debounceTime)
|
//ignore clicks inbetween
|
||||||
return; // Ignoriere doppeltes Event
|
if (tick > finalTick)
|
||||||
|
{
|
||||||
lastClickTime = Time.time;
|
Explode();
|
||||||
Explode();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update()
|
void Update()
|
||||||
@@ -37,16 +32,16 @@ public class ExplodeModel : MonoBehaviour, IResettable, IPointerDownHandler
|
|||||||
if (tick <= finalTick)
|
if (tick <= finalTick)
|
||||||
{
|
{
|
||||||
if(explode)
|
if(explode)
|
||||||
for (int i = 0; i < baseModel.transform.childCount; i++)
|
for (int i = 0; i < transforms.Count; i++)
|
||||||
{
|
{
|
||||||
baseModel.transform.GetChild(i).transform.localPosition =
|
transforms[i].localPosition =
|
||||||
MathUtil.EaseBoth(initialPos[i], goalPos[i],
|
MathUtil.EaseBoth(initialPos[i], goalPos[i],
|
||||||
(tick + 0f) / finalTick); //0f for floating point div
|
(tick + 0f) / finalTick); //0f for floating point div
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
for (int i = 0; i < baseModel.transform.childCount; i++)
|
for (int i = 0; i < transforms.Count; i++)
|
||||||
{
|
{
|
||||||
baseModel.transform.GetChild(i).transform.localPosition =
|
transforms[i].localPosition =
|
||||||
MathUtil.EaseBoth(goalPos[i], initialPos[i],
|
MathUtil.EaseBoth(goalPos[i], initialPos[i],
|
||||||
(tick + 0f) / finalTick); //0f for floating point div
|
(tick + 0f) / finalTick); //0f for floating point div
|
||||||
}
|
}
|
||||||
@@ -60,12 +55,19 @@ public class ExplodeModel : MonoBehaviour, IResettable, IPointerDownHandler
|
|||||||
{
|
{
|
||||||
initialPos.Clear();
|
initialPos.Clear();
|
||||||
goalPos.Clear();
|
goalPos.Clear();
|
||||||
foreach (Transform child in baseModel.transform)
|
transforms.Clear();
|
||||||
|
foreach (Transform child in baseModel.GetComponentsInChildren<Transform>())
|
||||||
{
|
{
|
||||||
ChildModel cm = child.GetComponent<ChildModelBehaviour>().ChildModel;
|
var cmb = child.GetComponent<ChildModelBehaviour>();
|
||||||
Port port = mm.GetPortForChildModel(cm);
|
if (cmb == null)
|
||||||
|
{
|
||||||
|
continue; //only for basemodel
|
||||||
|
}
|
||||||
|
Port port = cmb.GetParentPort();
|
||||||
initialPos.Add(child.localPosition);
|
initialPos.Add(child.localPosition);
|
||||||
goalPos.Add(child.localPosition + port.ExplodeDirection);
|
goalPos.Add(child.localPosition + port.ExplodeDirection);
|
||||||
|
transforms.Add(child);
|
||||||
|
Debug.LogWarning($"moving: {cmb.name} - {port.PortID} InitPos: {child.localPosition} -> goal {child.localPosition + port.ExplodeDirection}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
text.text = explode ? "Explode" : "Unexplode";
|
text.text = explode ? "Explode" : "Unexplode";
|
||||||
@@ -78,15 +80,14 @@ public class ExplodeModel : MonoBehaviour, IResettable, IPointerDownHandler
|
|||||||
{
|
{
|
||||||
if (explode)
|
if (explode)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < baseModel.transform.childCount; i++)
|
for (int i = 0; i < transforms.Count; i++)
|
||||||
{
|
{
|
||||||
var tf = baseModel.transform.GetChild(i);
|
if (transforms[i] == null)
|
||||||
if (tf == null)
|
|
||||||
{
|
{
|
||||||
Debug.LogWarning($"Skipping ChildModel {i}");
|
Debug.LogWarning($"Skipping ChildModel {i}");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
tf.transform.localPosition = initialPos[i]; //reset all
|
transforms[i].localPosition = initialPos[i]; //reset all
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
explode = false;
|
explode = false;
|
||||||
@@ -94,5 +95,6 @@ public class ExplodeModel : MonoBehaviour, IResettable, IPointerDownHandler
|
|||||||
tick = finalTick + 1;
|
tick = finalTick + 1;
|
||||||
initialPos.Clear();
|
initialPos.Clear();
|
||||||
goalPos.Clear();
|
goalPos.Clear();
|
||||||
|
transforms.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user