using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Cinemachine;

public class CameraController : MonoBehaviourPunCallbacks {
   
	public CinemachineVirtualCamera cinacam;
	public KartController kController;

	// Start is called before the first frame update
    void Start () {
        
    }

    // Update is called once per frame
    void Update () {
		if (!kController) {
			SetTarget ();
		}
    }

	void SetTarget () {
		KartController[] karts = FindObjectsOfType<KartController> ();
		KartController me = null;
		if (karts.Length > 0) {
			foreach (KartController k in karts) {
				if (k.photonView && k.photonView.IsMine && PhotonNetwork.IsConnected) {
					me = k;
					break;
				} else if (!k.photonView || !PhotonNetwork.IsConnected) {
					me = k;
					break;
				}
			}
		}

		if (me) {
			kController = me;
			cinacam.Follow = me.transform.GetChild (0);
			cinacam.LookAt = me.transform;
			kController.boostVisual = transform.GetComponentInChildren<ParticleSystem> ();
		}
	}
}
