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

public class CameraHandler : MonoBehaviour
{
    public Transform trackTarget;
    public float positionAcceleration = 10f;
    public float positionVelocityDamping = 0.05f;
    public float rotationAcceleration = 10f;
    public float rotationVelocityDamping = 0.05f;

    private Vector2 velocity = Vector2.zero;
    private float angularVelocity = 0;

    // Update is called once per frame
    void FixedUpdate() {
        // 1) position tracking
        // Work with velocity!
        Vector2 diff = trackTarget.position - transform.position;
        velocity+=positionAcceleration*diff;

        // Apply damping
        velocity*=1-positionVelocityDamping;

        // Apply velocity
        transform.position = transform.position + (Vector3)velocity*Time.deltaTime;

        // 2) rotation tracking
        float targetAngle = Vector3.SignedAngle(Vector3.right, trackTarget.right, Vector3.forward);
        float myAngle = Vector3.SignedAngle(Vector3.right, transform.right, Vector3.forward);
        float diffAngle = ExtraMath.NormalizeAngle((targetAngle - myAngle)*Mathf.Deg2Rad)*Mathf.Rad2Deg;
        angularVelocity+=rotationAcceleration*diffAngle;

        // Apply damping
        angularVelocity*=1-rotationVelocityDamping;

        // Apply rotation
        myAngle = myAngle+angularVelocity*Time.deltaTime;
        transform.localRotation = Quaternion.AngleAxis(myAngle, Vector3.forward);
    }
}
