using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LengthMap : MonoBehaviour
{
    [SerializeField] private Vector3 _startPos;
    [SerializeField] private Vector3 _endPos;

    private float _length;

    [SerializeField] private Transform player1;
    [SerializeField] private Transform player2;

    private float player1Map;
    private float player2Map;

    [SerializeField] private RectTransform player1Icon;
    [SerializeField] private RectTransform player2Icon;

    [SerializeField] private Vector2 player1MapPositions;
    [SerializeField] private Vector2 player2MapPositions;


    // Start is called before the first frame update
    void Start()
    {
        _length = Vector3.Distance(_startPos, _endPos);
    }

    // Update is called once per frame
    void Update()
    {
        player1Map = map(player1.transform.position.z, _startPos.z, _endPos.z, player1MapPositions.x, player1MapPositions.y);
        player2Map = map(player2.transform.position.z, _startPos.z, _endPos.z, player2MapPositions.x, player2MapPositions.y);

        player1Icon.localPosition = new Vector3(0, player1Map, 0);
        player2Icon.localPosition = new Vector3(0, player2Map, 0);

        if(player2Map > player1Map)
        {
            player1Icon.SetAsFirstSibling();
        } else
        {
            player2Icon.SetAsFirstSibling();
        }
    }

    float map(float s, float a1, float a2, float b1, float b2)
    {
        return b1 + (s - a1) * (b2 - b1) / (a2 - a1);
    }
}
