using UnityEngine;

public class GameObjectPlacementButton : UnitPlacementButton
{
    [SerializeField]
    private GameObject _prefab;
    protected override void Place()
    {
        var instance = Instantiate(_prefab);

        foreach (var comp in instance.transform.GetAllComponents<Rigidbody2D>()) Destroy(comp);
        foreach (var comp in instance.transform.GetAllComponents<Collider2D>()) Destroy(comp);
        foreach (var comp in instance.transform.GetAllComponents<MonoBehaviour>()) Destroy(comp);

        var placeholder = instance.AddComponent<ObjectPlaceholder>();
        placeholder.Prefab = _prefab;

        var outline = new GameObject("Outline");
        outline.transform.SetParent(instance.transform);
        outline.transform.localPosition = Vector2.zero;

        var rend = outline.AddComponent<SpriteRenderer>();
        rend.sortingLayerName = "Character";
        rend.sortingOrder = 1;
        rend.sprite = Sprite.Create(new Texture2D(100, 100), new Rect(0, 0, 100, 100), Vector2.one * 0.5f);

        placeholder.Outline = outline.transform;
    }
}