Exemplo n.º 1
0
    public GameObject Spawn(tk.JsonTcpClient client)
    {
        if (carPrefab == null)
        {
            Debug.LogError("No carPrefab set in CarSpawner!");
            return(null);
        }

        //Create a car object, and also hook up all the connections
        //to various places in game that need to hook into the car.
        GameObject go = GameObject.Instantiate(carPrefab) as GameObject;

        if (go == null)
        {
            Debug.LogError("CarSpawner failed to instantiate prefab!");
            return(null);
        }

        cars.Add(go);

        Vector3 startPos = GetCarStartPos(cars.Count - 1, true);

        go.transform.rotation = startTm.rotation;
        go.transform.position = startPos;
        go.GetComponent <Car>().SavePosRot();

        GameObject TcpClientObj = getChildGameObject(go, "TCPClient");

        Camera cam = Camera.main;

        bool bRaceActive = false;

        RaceManager raceMan = GameObject.FindObjectOfType <RaceManager>();

        if (raceMan)
        {
            bRaceActive = raceMan.bRaceActive;
        }

        GenPathFromDriving pathGen = GameObject.FindObjectOfType <GenPathFromDriving>();

        if (pathGen)
        {
            pathGen.Init(go);
        }

        //Detect that we have the second car. Doesn't really handle more than 2 right now.
        if (cars.Count > 1 && !bRaceActive)
        {
            cam = ActivateSplitScreen();
        }

        // CarTextFacecamera(go, cam.transform);

        if (TcpClientObj != null)
        {
            //without this it will not connect.
            TcpClientObj.SetActive(true);

            //now set the connection settings.
            TcpCarHandler carHandler = TcpClientObj.GetComponent <TcpCarHandler>();

            if (carHandler != null)
            {
                carHandler.Init(client);
            }
        }

        if (OnNewCarCB != null)
        {
            OnNewCarCB.Invoke(go);
        }

        ///////////////////////////////////////////////
        //Search scene to find these.
        CameraFollow cameraFollow = null;

        if (cam != null)
        {
            cameraFollow = cam.transform.GetComponent <CameraFollow>();
        }
        MenuHandler menuHandler = GameObject.FindObjectOfType <MenuHandler>();
        Canvas      canvas      = GameObject.FindObjectOfType <Canvas>();
        GameObject  panelMenu   = getChildGameObject(canvas.gameObject, "Panel Menu");
        PID_UI      pid_ui      = null;
        GameObject  pidPanel    = getChildGameObject(canvas.gameObject, "PIDPanel");

        ///////////////////////////////////////////////

        if (pidPanel)
        {
            pid_ui = pidPanel.GetComponent <PID_UI>();
        }

        //set camera target follow tm
        if (cameraFollow != null)
        {
            cameraFollow.target = getChildGameObject(go, "CameraFollowTm").transform;
        }

        //Set menu handler hooks
        if (menuHandler != null)
        {
            menuHandler.PIDContoller           = getChildGameObject(go, "PIDController");
            menuHandler.Logger                 = getChildGameObject(go, "Logger");
            menuHandler.NetworkSteering        = getChildGameObject(go, "TCPClient");
            menuHandler.carJSControl           = getChildGameObject(go, "JoyStickCarContoller");
            menuHandler.trainingManager        = getChildGameObject(go, "TrainingManager").GetComponent <TrainingManager>();
            menuHandler.trainingManager.carObj = go;

            if (EnableTrainingManager)
            {
                menuHandler.trainingManager.gameObject.SetActive(true);

                getChildGameObject(go, "OverheadViewSphere").SetActive(true);
            }

            if (GlobalState.bAutoHideSceneMenu && panelMenu != null)
            {
                panelMenu.SetActive(false);
            }
        }

        //Set the PID ui hooks
        if (pid_ui != null)
        {
            pid_ui.pid    = getChildGameObject(go, "PIDController").GetComponent <PIDController>();
            pid_ui.logger = getChildGameObject(go, "Logger").GetComponent <Logger>();
        }
        else
        {
            Debug.LogError("failed to find PID_UI");
        }

        //Add race status, if possible.
        if (raceMan != null)
        {
            GameObject to = getChildGameObject(go, "LapTimer");

            if (to != null)
            {
                raceMan.AddLapTimer(to.GetComponent <LapTimer>(), client);
            }
        }

        return(go);
    }