using UnityEngine;

public class MapPlayerIcon : MonoBehaviour
{
    public Vector3 RealMapBottomRight;
    public Vector3 RealMapTopLeft;

    RectTransform MapTransform;
    RectTransform PlayerIconTransform;

    void Start()
    {
        PlayerIconTransform = GetComponent<RectTransform>();
        MapTransform = PlayerIconTransform.parent.GetComponent<RectTransform>();
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 playerPos = GameManager.Instance.PlayerController.transform.position;

        float worldMinX = RealMapTopLeft.x;
        float worldMaxX = RealMapBottomRight.x;
        float worldMinZ = RealMapBottomRight.z;
        float worldMaxZ = RealMapTopLeft.z;

        // translate player position to percentage of map for x and y
        float normalizedX = Mathf.InverseLerp(worldMinX, worldMaxX, playerPos.x);
        float normalizedY = Mathf.InverseLerp(worldMinZ, worldMaxZ, playerPos.z);

        // anchor is 0, subtract 0.5 for accurate coords
        PlayerIconTransform.anchoredPosition = new Vector2((normalizedX - 0.5f) * MapTransform.rect.width, (normalizedY -0.5f) * MapTransform.rect.height);
    }
}
