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

public class PhysicsProxy : MonoBehaviour {

    public System.Action<Collider> OnTriggerEnterA;
    public System.Action<Collider> OnTriggerExitA;
    public System.Action<Collision> OnCollisionEnterA;
    public System.Action<Collision> OnCollisionExitA;

    ICharacter _character;

    Transform transf;
    [SerializeField] Transform owner;
    [SerializeField] Y2DSorterData sorter;

    public ICharacter character {
        get {return _character;}
        set {
            if(_character == value) return;
            if(value == null) { _character = null; return;}

            _character = value;

            transf.SetParent(_character.transfPhys);
        }
    }

    private void Awake() {
        transf = transform;
    }

    Vector3 posBuf;
    Quaternion rotBuf;

    private void Update() {
        if(_character == null) return;

        posBuf = owner.position - _character.transfGFX.position; //offset
        rotBuf = owner.rotation * Quaternion.Inverse(_character.transfGFX.rotation);
        transf.localPosition = posBuf;
        transf.localRotation = rotBuf;
    }

    public void OnTriggerEnter(Collider col) {
        if(OnTriggerEnterA != null) OnTriggerEnterA.Invoke(col);
    }

    public void OnTriggerExit(Collider col) {
        if(OnTriggerExitA != null) OnTriggerExitA.Invoke(col);
    }

    public void OnCollisionEnter(Collision col) {
        if(OnCollisionEnterA != null) OnCollisionEnterA.Invoke(col);
    }

    public void OnCollisionExit(Collision col) {
        if(OnCollisionExitA != null) OnCollisionExitA.Invoke(col);
    }

}
