using UnityEngine;
using System.Collections;

public class CharacterControllerPush: MonoBehaviour
{
    [SerializeField]
    private float impulsePerRelativeVelocity = 0.0f;

    [SerializeField]
    private float impulseBase = 0.0f;

    [SerializeField]
    private ForceMode impulseMode = ForceMode.VelocityChange;

    [SerializeField]
    private float forcePerRelativeVelocity = 0.0f;

    [SerializeField]
    private float forceBase = 10.0f;

    [SerializeField]
    private ForceMode forceMode = ForceMode.Acceleration;

	void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if (hit.collider.GetComponent<Block>() == null)
        {
            return;
        }

        Vector3 relativeVelocity = hit.controller.velocity - hit.collider.attachedRigidbody.velocity;

        if (Mathf.Abs(hit.moveDirection.x) > Mathf.Abs(hit.moveDirection.y))
        {
            // X
            float impulse = impulseBase * Mathf.Sign(hit.moveDirection.x) + relativeVelocity.x * impulsePerRelativeVelocity;

            hit.rigidbody.AddForce(new Vector3(impulse, 0.0f, 0.0f), impulseMode);

            float force = forceBase * Mathf.Sign(hit.moveDirection.x) + relativeVelocity.x * impulsePerRelativeVelocity;

            hit.rigidbody.AddForce(new Vector3(force, 0.0f, 0.0f), forceMode);
        }
        else
        {
            // Y
            float impulse = impulseBase * Mathf.Sign(hit.moveDirection.y) + relativeVelocity.y * impulsePerRelativeVelocity;

            hit.rigidbody.AddForce(new Vector3(0.0f, impulse, 0.0f), impulseMode);

            float force = forceBase * Mathf.Sign(hit.moveDirection.y) + relativeVelocity.y * impulsePerRelativeVelocity;

            hit.rigidbody.AddForce(new Vector3(0.0f, force, 0.0f), forceMode);
        }
    }
}
