using System.Collections.Generic;
using UnityEngine;
using CurveArchitect;
using CurveArchitect.Objects;
using CurveArchitect.Utilities;
public class SimpleAnimationLinkExample : MonoBehaviour
{
public ArchitectCurveObject aco;
public Vector3 speed;
public float resetRange;
Vector3 startPos;
ArchitectCurve startAc;
void Start()
{
startPos = aco.localCurvePosition;
startAc = aco.acParent;
}
void Update()
{
Vector3 oldPosition = aco.localCurvePosition;
aco.localCurvePosition += speed;
if (aco.localCurvePosition.z > resetRange)
{
HelperArchitectCurveObject.ChangeArchitectCurveParent(aco, startAc);
aco.localCurvePosition = startPos;
}
HandleLinkCrossing(aco, oldPosition);
}
public void HandleLinkCrossing(ArchitectCurveObject aco, Vector3 oldPosition)
{
//Try get links
List<Link> links = HelperArchitectCurve.TryFindLinkCrossings(aco.acParent, aco.curvePosition, oldPosition, out Segment segment);
if (links.Count <= 0)
return;
//Get random link index
int randomIndex = Random.Range(0, links.Count);
Link l = links[randomIndex];
//Get new segment and ArchitectCurve.
ArchitectCurve newAc = HandleRegistry.GetArchitecCurve(l.architectCurveId);
Segment newAcSegment = HandleRegistry.GetSegment(l.segmentId);
//Not needed, but it will make the transition smoother.
float dif = aco.localCurvePosition.z - segment.zPosition;
//Set the new position on the new ArchitectCurve for the aco.
aco.localCurvePosition.z = newAcSegment.zPosition + dif;
//Cross over to the new ArchitecCurve.
HelperArchitectCurveObject.ChangeArchitectCurveParent(aco, newAc);
}
}