Пример #1
0
    private void OnLoadingLevel(Scene arg0, LoadSceneMode arg1)
    {
        if (arg0.buildIndex == 2)
        {
            menu.CloseMenu();
            currentBirdIndex = 0;
            //find all relevant game objects
            Bricks = new List <GameObject>(GameObject.FindGameObjectsWithTag("Brick"));
            Birds  = new List <GameObject>(GameObject.FindGameObjectsWithTag("Bird"));
            foreach (GameObject go in Birds)
            {
                Bird bird = go.GetComponent <Bird>();
                if (bird == null)
                {
                    Debug.LogError(go.name + "is incorrectly tagged");
                    continue;
                }
                bird.OnHittingSurface += TurnEnded;
            }
            Pig = GameObject.FindGameObjectWithTag("Pig");
            //unsubscribe and resubscribe from the event
            //this ensures that we subscribe only once
            slingshot.BirdThrown -= Slingshot_BirdThrown; slingshot.BirdThrown += Slingshot_BirdThrown;

            CurrentGameState = GameState.Start;
            playerStates.FetchNextTurn(OnReceiveTaskRecommendation);
        }
    }
    //somebody joined (including me)
    public void OnPlayerJoin(SocketIOEvent e)
    {
        //read the data (just the id)
        AvatarData data = JsonUtility.FromJson<AvatarData>(e.data.ToString());
        Debug.Log("[SocketIO] Player Connected: " + data.id + " " + data.nickName);

        //add a player object to my dictionary
        Player p = new Player();
        p.id = data.id;
        p.nickName = data.nickName;
        p.DNA = data.DNA;

        //is it me?
        if (data.id == socket.SocketID)
        {

            //close the menu, change the camera
            if(gameMenu != null)
            {
                gameMenu.CloseMenu();
            }

            if (MyAvatarPrefabName != "")
            {
                print("I officially join the game as " + data.nickName);
                
                Net.playing = true;
                
                //do I have to create an avatar?
                Net.myId = socket.SocketID;

                //just in case
                if (myAvatar != null)
                    Destroy(myAvatar);

                //instantiate on the scene
                myAvatar = Instantiate(Resources.Load(MyAvatarPrefabName) as GameObject);
                myAvatar.name = socket.SocketID;


                //fetch or add the netcomponent object
                NetObject netObj = myAvatar.GetComponent<NetObject>();

                if (netObj == null)
                    netObj = myAvatar.AddComponent<NetObject>();

                Net.objects[socket.SocketID] = netObj;
                netObj.owner = socket.SocketID;
                netObj.type = Net.TEMPORARY;
                netObj.prefabName = MyAvatarPrefabName;

                //other client should instantiate a different avatar without controls
                //and camera but if it's not provided that's fine too
                if (OtherAvatarPrefabName == "")
                    OtherAvatarPrefabName = MyAvatarPrefabName;

                //tell the server to tell the other clients to make a puppet avatar
                InstantiationData iData = new InstantiationData();
                iData.prefabName = OtherAvatarPrefabName;
                iData.uniqueId = socket.SocketID;
                iData.type = Net.TEMPORARY;

                //instantiate all avatars around 0,0 by default
                //this is game logic and can be changed here on the server or on the netObject upon instantiation 
                Vector2 spawnRange = UnityEngine.Random.insideUnitCircle * spawnRadius;
                myAvatar.transform.position = iData.position = new Vector3(spawnPoint.x + spawnRange.x, spawnPoint.y, spawnPoint.z + spawnRange.y);
                myAvatar.transform.localScale = iData.localScale = Vector3.one;
                myAvatar.transform.rotation = iData.rotation = Quaternion.identity;

                print("Instantiation " + myAvatar.transform.position);
                socket.Emit("instantiateAvatar", JsonUtility.ToJson(iData));

            }//end avatar creation

            //find all the static netObjects - they are the ones in the Unity scene with a netObject attached
            //their uniqueId is their gameObject name 
            NetObject[] sceneObjects = FindObjectsOfType(typeof(NetObject)) as NetObject[];

            foreach (NetObject item in sceneObjects)
            {
                if (item.owner == "")
                {
                    Debug.Log(item.gameObject.name + " is a netObject without an owner in the scene, make sure the server knows about it");

                    //if the object is orphan assign ownership to me and make sure the server knows about it
                    //this should happen only to the first user logging after the server starts
                    InstantiationData iData = new InstantiationData();
                    iData.prefabName = item.gameObject.name;
                    iData.uniqueId = item.gameObject.name;
                    iData.position = item.transform.position;
                    iData.localScale = item.transform.localScale;
                    iData.rotation = item.transform.rotation;
                    iData.netVariables = item.netVariables;
                    iData.type = Net.PERSISTENT;
                    //item.OnVariableInit();
                    //if the server knows about it there will be no followup
                    socket.Emit("registerObject", JsonUtility.ToJson(iData));

                }
            }


        }//is it me?

        //add the player object to the players dictionary
        Net.players[p.id] = p;

    }