Пример #1
0
    public override void OnPhotonPlayerConnected(PhotonPlayer newPlayer)
    {
        if (PhotonNetwork.isMasterClient)
        {
            Debug.Log("主客户端发请求给加入的客户端更新食物");
            //GameObject[] foodList = GameObject.FindGameObjectsWithTag("food");
            GameObject[] foodList       = foodInstances;
            float[]      foodInfomation = new float[7 * foodList.Length];
            for (int i = 0; i < foodList.Length; i++)
            {
                foodInfomation[i * 7 + 0] = foodList[i].transform.position.x;
                foodInfomation[i * 7 + 1] = foodList[i].transform.position.y;
                foodInfomation[i * 7 + 2] = foodList[i].transform.position.z;
                foodInfomation[i * 7 + 3] = foodList[i].GetComponent <FoodOverrideController>().translation.x;
                foodInfomation[i * 7 + 4] = foodList[i].GetComponent <FoodOverrideController>().translation.y;
                foodInfomation[i * 7 + 5] = foodList[i].GetComponent <FoodOverrideController>().translation.z;
                foodInfomation[i * 7 + 6] = foodList[i].GetComponent <FoodOverrideController>().ID;
            }
            RaiseEventOptions options = new RaiseEventOptions();
            options.TargetActors = new int[] { newPlayer.ID };
            PhotonNetwork.RaiseEvent(2, foodInfomation, true, options);


            //主客户端发请求给加入的客户端更新食物AI
            float[] foodAIInfo = FoodAISyncInfo.Serialize(foodAIInstances);
            PhotonNetwork.RaiseEvent(5, foodAIInfo, true, options);

            //主客户端发请求给加入的客户端更新毒物AI
            foodAIInfo = FoodAISyncInfo.Serialize(poisonInstances);
            PhotonNetwork.RaiseEvent(8, foodAIInfo, true, options);
        }
    }
Пример #2
0
    private void OnTriggerStay(Collider other)
    {
        if (other.gameObject.tag == "player" || other.gameObject.tag == "playerCopy")
        {
            if (other.gameObject.GetComponent <Player>().photonView.isMine)
            {
                Debug.Log("毒物AI:删除");

                //重置主客户端食物AI位置
                GameObject parent = this.gameObject.GetComponentInParent <SyncTranform>().gameObject;

                //隐藏父物体
                parent.SetActive(false);

                //随机生成transform
                parent.transform.position = GetRandomVector3();
                parent.transform.rotation = GetRandomQuaternion();

                //序列化数据
                GameObject[] foodAIInstances = new GameObject[1];
                foodAIInstances[0] = parent;
                float[] foodAIInfo = FoodAISyncInfo.Serialize(foodAIInstances);

                //发送事件
                RaiseEventOptions options = new RaiseEventOptions();
                options.Receivers     = ReceiverGroup.All;
                options.CachingOption = EventCaching.DoNotCache;
                PhotonNetwork.RaiseEvent(10, foodAIInfo, true, options);

                //触发玩家吃到毒物事件
                other.gameObject.GetComponent <Player>().photonView.RPC("EatPoison", PhotonTargets.AllViaServer);
            }
        }
    }
Пример #3
0
    //定时同步食物AI位置及旋转
    public IEnumerator SyncFoodAITranform()
    {
        while (true)
        {
            yield return(new WaitForSeconds(0.05f));

            Debug.Log("主客户端周期发起更新食物AI");
            RaiseEventOptions options = new RaiseEventOptions();
            options.Receivers     = ReceiverGroup.Others;
            options.CachingOption = EventCaching.DoNotCache;
            //主客户端定时更新其他客户端的食物AI位置及旋转
            if (FoodAICount > 0 && foodAIInstances[0] != null)
            {
                float[] foodAIInfo = FoodAISyncInfo.Serialize(foodAIInstances);
                PhotonNetwork.RaiseEvent(6, foodAIInfo, true, options);
            }

            if (poisonCountAll > 0 && poisonInstances[0] != null)
            {
                float[] foodAIInfo = FoodAISyncInfo.Serialize(poisonInstances);
                PhotonNetwork.RaiseEvent(9, foodAIInfo, true, options);
            }
        }
    }