示例#1
0
    private void Start()
    {
        identity = GetComponent <MyNetworkIdentity>();
        if (!identity.HasControl)
        {
            enabled = false;
        }

        data = new PlayerData();
    }
示例#2
0
    private void Hook()
    {
        On("open", (connect) =>
        {
            Debug.Log("connected");
            connectedPlayers = new Dictionary <string, MyNetworkIdentity>();
        });

        On("playerJoin", (join) =>
        {
            GameObject playerLabel = hostingLobby.AddPlayer(join.data["name"].ToString().Replace("\"", string.Empty));
            if (isHost)
            {
                if (join.data["id"].ToString().Replace("\"", string.Empty) == id)
                {
                    playerLabel.GetComponentInChildren <Button>().onClick.AddListener(() => CloseLobby());
                }
                else
                {
                    playerLabel.GetComponentInChildren <Button>().onClick.AddListener(() => KickPlayer(join.data["id"].ToString().Replace("\"", string.Empty)));
                }
            }
            else
            {
                if (join.data["id"].ToString().Replace("\"", string.Empty) == id)
                {
                    playerLabel.GetComponentInChildren <Button>().onClick.AddListener(() => LeaveLobby());
                }
                else
                {
                    playerLabel.GetComponentInChildren <Button>().gameObject.SetActive(false);
                }
            }

            MyNetworkIdentity playerID = playerLabel.GetComponent <MyNetworkIdentity>();
            playerID.SetID(join.data["id"].ToString().Replace("\"", string.Empty));
            playerID.Socket = this;
            connectedPlayers.Add(join.data["id"].ToString().Replace("\"", string.Empty), playerID);
        });
        On("kicked", (kicked) =>
        {
            LeaveLobby();
        });
        On("onRegister", (register) =>
        {
            string a = register.data["id"].ToString().Replace("\"", string.Empty);
            Debug.Log(a);
            id = register.data["id"].ToString().Replace("\"", string.Empty);
            SimpleMessage nameData = new SimpleMessage();
            nameData.message       = playerName;
            Emit("setName", new JSONObject(JsonUtility.ToJson(nameData)));
            serverConnection = true;
            Emit("joinLobby");
        });

        /*
         * On("updateIn", (updateTransform) =>
         * {
         *  float x = updateTransform.data["position"]["x"].f;
         *  float y = updateTransform.data["position"]["y"].f;
         *  float z = updateTransform.data["position"]["z"].f;
         *  connectedPlayers[updateTransform.data["id"].ToString()].gameObject.transform.position = new Vector3(x, y, z);
         *  Debug.Log(updateTransform.data["id"].ToString());
         * });*/

        On("spawnObj", (spawnObject) => {
            GameObject spawneable = Instantiate(serverObj.GetObject(spawnObject.data["objectName"].ToString().Replace("\"", string.Empty)).prefav);
            float x = spawnObject.data["position"]["x"].f;
            float y = spawnObject.data["position"]["y"].f;
            float z = spawnObject.data["position"]["z"].f;
            spawneable.transform.position = new Vector3(x, y, z);
            SpawnedObjects.Add(spawnObject.data["id"].ToString().Replace("\"", string.Empty), new ServerObject());
        });
        On("unspawnObj", (unspawnObject) => {
            string objID = unspawnObject.data["id"].ToString().Replace("\"", string.Empty);
            GameObject objectToDestroy = SpawnedObjects[objID].prefav;
            Destroy(objectToDestroy);
            SpawnedObjects.Remove(objID);
        });
        On("updateObj", (updateObj) => {
            string objID = updateObj.data["id"].ToString().Replace("\"", string.Empty);
            GameObject objectToDestroy = SpawnedObjects[objID].prefav;
            Destroy(objectToDestroy);
            SpawnedObjects.Remove(objID);
        });

        On("disconnect", (disconection) =>
        {
            serverConnection           = false;
            GameObject objectToDestroy = connectedPlayers[id].gameObject;
            Destroy(objectToDestroy);
            connectedPlayers.Remove(id);
        });
        On("playerDisconnected", (disconection) =>
        {
            string playerID            = disconection.data["id"].ToString().Replace("\"", string.Empty);
            GameObject objectToDestroy = connectedPlayers[playerID].gameObject;
            Destroy(objectToDestroy);
            connectedPlayers.Remove(playerID);
        }
           );
        On("startGame", (start) =>
        {
            ChangeScene("GameScene");
        }
           );
        On("newTurn", (NewTurn) =>
        {
            currentGameDirector.NewTurn();
        }
           );
        On("endTurn", (EndTurn) =>
        {
            currentGameDirector.EndOfTurn();
        }
           );
        On("recieveActionData", (RecievedData) =>
        {
            CharacterActionData data = JsonUtility.FromJson <CharacterActionData>(RecievedData.data.ToString());
            currentGameDirector.ReceiveActionToReplicate(data);
        }
           );
        On("startActionTurn", (actions) =>
        {
            currentGameDirector.StartReplication();
        }
           );
    }