CableSewer

 avatar
unknown
csharp
8 days ago
2.6 kB
10
Indexable
using Obi;
using Sirenix.OdinInspector;
using System.Collections;
using UnityEngine;

public class CableSewer : MonoBehaviour
{
    [SerializeField]
    private ObiRope rope;

    [ValueDropdown(nameof(ParticleNames), HideChildProperties = true)]
    public int targetGroupIndex;

    [SerializeField]
    private Transform transformToSewInto;

    private ObiParticleGroup _particleGroup;

    private ObiNativeIntList _solverIndiceIndex;
    
    private void OnEnable()
    {
        _solverIndiceIndex = new ObiNativeIntList();
        
        if (!rope.solver)
        {
            rope.OnSimulationStart += RopeOnOnSimulationStart;
        }
        
    }

    private void RopeOnOnSimulationStart(ObiActor actor, float simulatedtime, float substeptime)
    {
        foreach (var index in rope.blueprint.groups[targetGroupIndex].particleIndices)
        {
            if (CableUtils.IsObiListIndexValid(rope.solverIndices, index))//just list.Count() > index && index >= 0;
            {
                _solverIndiceIndex.Add(rope.solverIndices[index]);
            }
        }
        
        rope.OnSimulationStart -= RopeOnOnSimulationStart;
        rope.OnInterpolate += RopeOnOnInterpolate;
    }

    private void RopeOnOnInterpolate(ObiActor actor, float simulatedtime, float substeptime)
    {
        foreach (var indice in _solverIndiceIndex)
        {
            if (CableUtils.IsObiListIndexValid(actor.solver.renderablePositions, indice))
            {
                actor.solver.renderablePositions[indice] = rope.solver.transform.InverseTransformPoint(transformToSewInto.position);
            }
        }
    }

    private void OnDisable()
    {
        rope.solver.OnInterpolate -= SolverOnOnInterpolate;
    }

    private void SolverOnOnInterpolate(ObiSolver solver, float timetosimulate, float substeptime)
    {
        foreach (var indice in _solverIndiceIndex)
        {
            if (CableUtils.IsObiListIndexValid(solver.renderablePositions, indice))
            {
                solver.renderablePositions[indice] = rope.solver.transform.InverseTransformPoint(transformToSewInto.position);
            }
            
        }
    }

    private IEnumerable ParticleNames()
    {
        var output = new ValueDropdownList<int>();

        if (!rope)
            return output;

        for (int i = 0; i < rope.blueprint.groups.Count; i++)
        {
            output.Add(rope.blueprint.groups[i].name, i);
        }

        return output;
    }
}
Editor is loading...
Leave a Comment