예제 #1
0
        private void setupEvent()
        {
            //Eventio is a callback give by the socket
            //Whatever event we are generate over the server all will be revicered our here

            /*
             *  Here On("Event name",(E)=>{}) will get the response from server to client
             *  where as in Emit("event name",(e)=>{}) will send the response to the server from client
             *
             *  Here event name in server and the script should be same
             */
            On("open", (Eventio) =>
            {
                Debug.Log("Connection made to the server");
            });
            On("register", (Eventio) =>
            {
                string id = Eventio.data["id"].ToString().RemoveQuotes();
                Debug.LogFormat("Our Client's Id ({0})", id);

                ClientID = id; // setting the Client id of the player
                Cliednt  = ClientID;
            });

            //This event will handle when the player spwan
            On("spawn", (Eventio) =>
            {
                string id = Eventio.data["id"].ToString().RemoveQuotes();

                GameObject go;
                go      = Instantiate(Swapingobject, Vector3.one, Quaternion.identity);
                go.name = "Service Id" + id;
                print("-=-=-=->" + go.name);
                NetworkIdentity ni = go.GetComponent <NetworkIdentity>();
                ni.SetControllerID(id);
                ni.SetSocketReference(this);
                go.transform.SetParent(networkContainer);
                serverObject.Add(id, go.GetComponent <NetworkIdentity>());
            });

            // This event will update the position of the Player
            On("updatepostion", (Eventio) =>
            {
                string id = Eventio.data["id"].ToString().RemoveQuotes();
                float x   = Eventio.data["position"]["x"].f;
                float y   = Eventio.data["position"]["y"].f;
                float z   = Eventio.data["position"]["z"].f;

                NetworkIdentity ni    = serverObject[id];
                ni.transform.position = new Vector3(x, y, z);
            });

            // This event will update the rotation of the player
            On("updaterotation", (Eventio) =>
            {
                string id            = Eventio.data["id"].ToString().RemoveQuotes();
                float tankRotation   = Eventio.data["tankRotation"].f;
                float barrelRotation = Eventio.data["barrelRotation"].f;

                NetworkIdentity ni            = serverObject[id];
                ni.transform.localEulerAngles = new Vector3(0, 0, tankRotation);
                ni.transform.GetComponent <PlayerManager>().SetRotation(barrelRotation);
            });

            On("serverSpawn", (Eventio) =>
            {
                string name = Eventio.data["name"].str;
                string id   = Eventio.data["id"].ToString().RemoveQuotes();
                float x     = Eventio.data["position"]["x"].f;
                float y     = Eventio.data["position"]["y"].f;
                float z     = Eventio.data["position"]["z"].f;
                Debug.LogFormat("Server want us to spawn a '{0}'", name);

                if (!serverObject.ContainsKey(id))
                {
                    ServerObjectData Sod           = serverSpwanables.GetObjectByName(name);
                    var spawnobject                = Instantiate(Sod.Prefabs, networkContainer);
                    spawnobject.transform.position = new Vector3(x, y, 0);

                    var ni = spawnobject.GetComponent <NetworkIdentity>();
                    ni.SetControllerID(id);
                    ni.SetSocketReference(this);

                    serverObject.Add(id, ni);
                    // If Bullet applt direction as well
                    // print("-=-=Bullet-=-=->");
                    if (name == "Bullet")
                    {
                        float directionX = Eventio.data["Direction"]["x"].f;
                        float directionY = Eventio.data["Direction"]["y"].f;
                        float directionZ = Eventio.data["Direction"]["z"].f;
                        string activator = Eventio.data["activator"].str;
                        float speed      = Eventio.data["Speed"].f;

                        print("-=-=Bullet-=-=->");
                        float rot = Mathf.Atan2(directionY, directionX) * Mathf.Rad2Deg;
                        Vector3 currentRotation = new Vector3(0, 0, rot - 90);
                        print("currentRotation" + currentRotation + directionX);
                        spawnobject.transform.rotation = Quaternion.Euler(currentRotation);

                        WhoActivateMe whoActivateMe = spawnobject.transform.GetComponent <WhoActivateMe>();
                        whoActivateMe.SetActivator(activator);

                        Projectile projectile = spawnobject.GetComponent <Projectile>();
                        projectile.Direction  = new Vector3(directionX, directionY, directionZ);
                        projectile.Speed      = speed;
                    }
                }
            });

            On("serverUnSpawn", (Eventio) =>
            {
                string id          = Eventio.data["id"].ToString().RemoveQuotes();
                NetworkIdentity ni = serverObject[id];
                serverObject.Remove(id);
                DestroyImmediate(ni.gameObject);
            });

            On("playerDied", (Eventio) =>
            {
                print("-=-=PlayerDied-=-=->" + Eventio.data["id"].ToString());
                string id          = Eventio.data["id"].ToString().RemoveQuotes();
                NetworkIdentity ni = serverObject[id];

                ni.gameObject.SetActive(false);
            });

            On("playerRespawn", (Eventio) =>
            {
                string id          = Eventio.data["id"].ToString().RemoveQuotes();
                NetworkIdentity ni = serverObject[id];

                float x = Eventio.data["position"]["x"].f;
                float y = Eventio.data["position"]["y"].f;
                float z = Eventio.data["position"]["z"].f;

                ni.transform.position = new Vector3(x, y, z);
                ni.gameObject.SetActive(true);
            });

            On("disconnected", (Eventio) =>
            {
                print("-=-=-=-=->" + serverObject.Count);
                string id = Eventio.data["id"].ToString().RemoveQuotes();

                GameObject go = serverObject[id].gameObject;
                Destroy(go);
                serverObject.Remove(id);
                print("-=-=-=-=->" + serverObject.Count + " " + id);
            });
        }
예제 #2
0
        private void setupEvents()
        {
            On("open", (E) => {
                Debug.Log("Connected to Server");
            });

            On("register", (E) => {
                ClientID = E.data["id"].ToString().RemoveQuotes();
                Debug.LogFormat("Our Client's ID ({0})", ClientID);
            });

            On("spawn", (E) => {
                string id = E.data["id"].ToString().RemoveQuotes();

                GameObject go      = Instantiate(playerPrefab, networkContainer);
                go.name            = string.Format("Player: ({0})", id);
                NetworkIdentity ni = go.GetComponent <NetworkIdentity>();
                ni.SetControllerID(id);
                ni.SetSocketReference(this);
                serverObjects.Add(id, ni);
            });

            On("disconnected", (E) => {
                string id = E.data["id"].ToString().RemoveQuotes();

                GameObject go = serverObjects[id].gameObject;
                Destroy(go);
                serverObjects.Remove(id);
            });

            On("updatePosition", (E) => {
                string id = E.data["id"].ToString().RemoveQuotes();
                float x   = E.data["position"]["x"].f;
                float y   = E.data["position"]["y"].f;

                NetworkIdentity ni    = serverObjects[id];
                ni.transform.position = new Vector3(x, y, 0);
            });

            On("updateRotation", (E) => {
                string id            = E.data["id"].ToString().RemoveQuotes();
                float tankRotation   = E.data["tankRotation"].f;
                float barrelRotation = E.data["barrelRotation"].f;

                NetworkIdentity ni            = serverObjects[id];
                ni.transform.localEulerAngles = new Vector3(0, 0, tankRotation);
                ni.GetComponent <PlayerManager>().SetRotation(barrelRotation);
            });

            On("serverSpawn", (E) => {
                string name = E.data["name"].str;
                string id   = E.data["id"].ToString().RemoveQuotes();
                float x     = E.data["position"]["x"].f;
                float y     = E.data["position"]["y"].f;
                Debug.LogFormat("Server wants to spawn a '{0}'", name);

                if (!serverObjects.ContainsKey(id))
                {
                    ServerObjectData sod             = serverSpawnables.GetObjectByName(name);
                    var spawnedObject                = Instantiate(sod.Prefab, networkContainer);
                    spawnedObject.transform.position = new Vector3(x, y, 0);
                    var ni = spawnedObject.GetComponent <NetworkIdentity>();
                    ni.SetControllerID(id);
                    ni.SetSocketReference(this);

                    if (name == "Bullet")
                    {
                        float directionX = E.data["direction"]["x"].f;
                        float directionY = E.data["direction"]["y"].f;
                        string activator = E.data["activator"].ToString().RemoveQuotes();
                        float speed      = E.data["speed"].f;

                        float rot = Mathf.Atan2(directionY, directionX) * Mathf.Rad2Deg;
                        Vector3 currentRotation          = new Vector3(0, 0, rot - 90);
                        spawnedObject.transform.rotation = Quaternion.Euler(currentRotation);

                        WhoActivateMe whoActivateMe = spawnedObject.GetComponent <WhoActivateMe>();
                        whoActivateMe.SetActivator(activator);

                        Projectile projectile = spawnedObject.GetComponent <Projectile>();
                        projectile.Direction  = new Vector2(directionX, directionY);
                        projectile.Speed      = speed;
                    }

                    serverObjects.Add(id, ni);
                }
            });

            On("serverDespawn", (E) => {
                string id          = E.data["id"].ToString().RemoveQuotes();
                NetworkIdentity ni = serverObjects[id];
                serverObjects.Remove(id);
                DestroyImmediate(ni.gameObject);
            });

            On("playerDied", (E) => {
                string id          = E.data["id"].ToString().RemoveQuotes();
                NetworkIdentity ni = serverObjects[id];
                ni.gameObject.SetActive(false);
            });

            On("playerRespawn", (E) => {
                string id             = E.data["id"].ToString().RemoveQuotes();
                float x               = E.data["position"]["x"].f;
                float y               = E.data["position"]["y"].f;
                NetworkIdentity ni    = serverObjects[id];
                ni.transform.position = new Vector3(x, y, 0);
                ni.gameObject.SetActive(true);
            });

            On("loadGame", (E) => {
                Debug.Log("Switching to game");
                SceneManagementManager.Instance.LoadLevel(SceneList.LEVEL, (levelName) => {
                    SceneManagementManager.Instance.UnLoadLevel(SceneList.MAIN_MENU);
                });
            });

            On("lobbyUpdate", (E) => {
                OnGameStateChange.Invoke(E);
            });
        }