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

public class ColliderPosition : MonoBehaviour
{
    private GameObject head;
    private GameObject leftSide;
    private GameObject rightSide;
    private GameObject feet;

    private CapsuleCollider2D playerCollider = null;
    private void Start()
    {
        if (this.TryGetComponent(out CapsuleCollider2D col)) playerCollider = col;
        head = this.transform.Find("Head").gameObject;
        leftSide = this.transform.Find("LeftSide").gameObject;
        rightSide = this.transform.Find("RightSide").gameObject;
        feet = this.transform.Find("Feet").gameObject;
        RepositionColliders();
    }
    public void RepositionColliders()
    {
        if (playerCollider != null)
        {
            playerCollider.offset = Vector2.zero;
            playerCollider.size = new Vector2(.5f, 2.1f);
            if (leftSide.TryGetComponent(out BoxCollider2D bcLeft)) bcLeft.size = new Vector2(bcLeft.size.x, 1.7f);
            if (rightSide.TryGetComponent(out BoxCollider2D bcRight)) bcRight.size = new Vector2(bcRight.size.x, 1.7f);
            if (feet.TryGetComponent(out BoxCollider2D bcFeet)) bcFeet.size = new Vector2(0.8f, bcFeet.size.y);
            RelocateChildrenColliders();
        }
    }
    public void RepositionCollidersSlime()
    {
        if(playerCollider != null)
        {
            playerCollider.offset = new Vector2(0, -0.15f);
            playerCollider.size = new Vector2(0.43f, 0.75f);
            if (leftSide.TryGetComponent(out BoxCollider2D bcLeft)) bcLeft.size = new Vector2(bcLeft.size.x, .5f);
            if (rightSide.TryGetComponent(out BoxCollider2D bcRight)) bcRight.size = new Vector2(bcRight.size.x, .5f);
            if (feet.TryGetComponent(out BoxCollider2D bcFeet)) bcFeet.size = new Vector2(0.4f, bcFeet.size.y);
            RelocateChildrenColliders();
        }
    }

    private void RelocateChildrenColliders()
    {
        if (playerCollider != null)
        {
            leftSide.transform.localPosition = new Vector2(-playerCollider.bounds.extents.x, leftSide.transform.localPosition.y);
            rightSide.transform.localPosition = new Vector2(playerCollider.bounds.extents.x, rightSide.transform.localPosition.y);

            head.transform.localPosition = new Vector2(0, playerCollider.bounds.extents.y + playerCollider.offset.y);
            feet.transform.localPosition = new Vector2(0, -playerCollider.bounds.extents.y + playerCollider.offset.y);
        }
    }
}
