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

public class CameraMove : MonoBehaviour
{
    private GameObject player = null;
    private Rigidbody2D playerRigid = null;

    private Rigidbody2D rigid = null;
    private Vector3 LimiteMinimo, LimiteMaximo;
    private Vector2 DirectionRigid = new Vector2();
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        if (this.TryGetComponent(out Rigidbody2D rg)) this.rigid = rg;
        if(player != null) if (player.TryGetComponent(out Rigidbody2D rgPlayer)) this.playerRigid = rgPlayer;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        LimiteMinimo = Camera.main.ScreenToWorldPoint((Vector3)new Vector2(Camera.main.scaledPixelWidth / 4 * 1.5f, Camera.main.scaledPixelHeight / 3 * 1f));
        LimiteMaximo = Camera.main.ScreenToWorldPoint((Vector3)new Vector2(Camera.main.scaledPixelWidth / 4 * 2.5f, Camera.main.scaledPixelHeight / 3 * 1.5f));

        if (LimiteMinimo.x > player.transform.position.x)
        {
            DirectionRigid.x = this.playerRigid.velocity.x >= 0 ? -this.playerRigid.velocity.x : this.playerRigid.velocity.x;
            DirectionRigid.x = DirectionRigid.x == 0 ? -2 : DirectionRigid.x;
        }
        else if (LimiteMaximo.x < player.transform.position.x)
        {
            DirectionRigid.x = this.playerRigid.velocity.x >= 0 ? this.playerRigid.velocity.x : -this.playerRigid.velocity.x;
            DirectionRigid.x = DirectionRigid.x == 0 ? 2 : DirectionRigid.x;
        }
        else DirectionRigid.x = 0;

        if (LimiteMinimo.y > player.transform.position.y)
        {
            DirectionRigid.y = this.playerRigid.velocity.y >= 0 ? -this.playerRigid.velocity.y : this.playerRigid.velocity.y;
            DirectionRigid.y = DirectionRigid.y == 0 ? -2 : DirectionRigid.y;
        }
        else if (LimiteMaximo.y < player.transform.position.y)
        {
            DirectionRigid.y = this.playerRigid.velocity.y >= 0 ? this.playerRigid.velocity.y : -this.playerRigid.velocity.y;
            DirectionRigid.y = DirectionRigid.y == 0 ? 2 : DirectionRigid.y;
        }
        else DirectionRigid.y = 0;

        this.rigid.velocity = DirectionRigid;
    }
}
