﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Child3DMeter : MonoBehaviour
{
    public GameObject reactingObject;
    public Vector3 minPos;
    public Vector3 maxPos;
    public Vector3 minScale;
    public Vector3 maxScale;
    public float speed = 10;

    public void SetValue(float value)
    {
        var remappedPos = RemapVector(value, new Vector2(0, 1), minPos, maxPos, false);
        var remappedScale = RemapVector(value, new Vector2(0, 1), minScale, maxScale, false);
        
        reactingObject.transform.localPosition = Vector3.MoveTowards(reactingObject.transform.localPosition, remappedPos, Time.deltaTime * speed);
        reactingObject.transform.localScale = Vector3.MoveTowards(reactingObject.transform.localScale, remappedScale, Time.deltaTime * speed);
    }

    
    /// <summary>
    /// Linearly remaps a Vector3 from min to max, based on the float value and its expected range. 
    /// The returned value can be clamped between min and max
    /// </summary>
    /// <param name="value"></param>
    /// <param name="expectedRange"></param>
    /// <param name="min"></param>
    /// <param name="max"></param>
    /// <param name="clampValue"></param>
    /// <param name="logValues"></param>
    /// <returns></returns>
    public static Vector3 RemapVector(float value, Vector2 expectedRange, Vector3 min, Vector3 max, bool clampValue = false)
    {
        // find percentage
        var mappedValue = CsoundUnity.Remap(value, expectedRange.x, expectedRange.y, 0f, 1f);
        // clamp value if needed
        var clamped = clampValue ? Mathf.Clamp01(mappedValue) : mappedValue;
        // interpolate between the two points
        return Vector3.Lerp(min, max, clamped);
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
