eye-therapy-2 / Assets / Scripts / Interactables / Lever / LeverTwoWayAction.cs
LeverTwoWayAction.cs
Raw
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class LeverTwoWayAction : Lever
{
    #region Variables

    [SerializeField] [Range(0f, 1f)] protected float positiveReset = 0.15f;
    [SerializeField] [Range(0f, 1f)] protected float negativeReset = 0.15f;
    [SerializeField] protected bool linkResets = false;

    protected float posResetPrev = 0.15f;
    protected bool changeReady = true;

    #endregion

    #region Methods

    protected override void ProcessLever()
    {
        JointLimits limits = lever.limits;

        if (lever.angle > limits.min * negativeReset && lever.angle < limits.max * positiveReset)
            changeReady = true;

        if (changeReady && lever.angle > limits.max * positiveThreshold)
        {
            changeReady = false;
            PositiveAction();
        }
        else if (changeReady && lever.angle < limits.min * negativeThreshold)
        {
            changeReady = false;
            NegativeAction();
        }
    }

    protected abstract void PositiveAction();

    protected abstract void NegativeAction();

#if UNITY_EDITOR
    protected override void OnValidate()
    {
        base.OnValidate();

        if (linkResets)
        {
            if (positiveReset != posResetPrev)
                negativeReset = positiveReset;
            else
                positiveReset = negativeReset;
            posResetPrev = positiveReset;
        }
    }
#endif

    #endregion
}