/*---------------------*/
        /*---Handler Methods---*/
        /*---------------------*/

        /** <summary>Performs additional functionality for any spawned bullet object.</summary>
         * <param name="pEvent">The socket event fired from the server</param>
         * <param name="pSpawnedObject">The bullet we have spawned</param>
         * */
        public void handleBulletSpawn(SocketIOEvent pEvent, GameObject pSpawnedObject)
        {
            //Extract Data from Event
            float  directionX = pEvent.data["direction"]["x"].f;
            float  directionY = pEvent.data["direction"]["y"].f;
            float  directionZ = pEvent.data["direction"]["z"].f;
            string activator  = pEvent.data["activator"].ToString().RemoveQuotes();
            float  speed      = pEvent.data["speed"].f;

            //Calculate and apply directional data.
            float   rot             = Mathf.Atan2(directionX, directionZ) * Mathf.Rad2Deg;
            Vector3 currentRotation = new Vector3(0, rot - 90, 0);

            pSpawnedObject.transform.rotation = Quaternion.Euler(currentRotation);

            //Set parameters so that this bullet does not hit its creator.
            WhoActivatedMe whoActivatedMe = pSpawnedObject.GetComponent <WhoActivatedMe>();

            whoActivatedMe.SetActivator(activator);

            //Set the direction and speed of the projectile.
            Projectile projectile = pSpawnedObject.GetComponent <Projectile>();

            projectile.Direction = new Vector3(directionX, directionY, directionZ);
            projectile.Speed     = speed;
        }
Пример #2
0
        public void fireBullet()
        {
            shootingCooldown.StartCooldown();

            //Define Bullet
            bulletData.activator  = NetworkClient.ClientID;
            bulletData.position.x = bulletSpawnPoint.position.x.TwoDecimals();
            bulletData.position.y = bulletSpawnPoint.position.y.TwoDecimals();
            bulletData.position.z = bulletSpawnPoint.position.z.TwoDecimals();

            bulletData.direction.x = bulletSpawnPoint.up.x;
            bulletData.direction.y = bulletSpawnPoint.up.y;
            bulletData.direction.z = bulletSpawnPoint.up.z;

            //Send Bullet

            //If we are connected to the server...
            if (mMaster.ourClientIsConnected)
            {
                mMaster.getNetworkIdentity().GetSocket().Emit("fireBullet", new JSONObject(JsonUtility.ToJson(bulletData)));
            }
            else
            {
                float speed = 0.5f;

                ServerObjectData sod = mMaster.OfflineSpawnables.GetObjectByName("Bullet");
                var spawnedObject    = Instantiate(sod.Prefab, mMaster.OfflineContainer);
                spawnedObject.transform.position = new Vector3(bulletData.position.x, bulletData.position.y, bulletData.position.z);

                //Set parameters so that this bullet does not hit its creator.
                WhoActivatedMe whoActivatedMe = spawnedObject.GetComponent <WhoActivatedMe>();
                whoActivatedMe.SetActivator(transform.name);

                //Set the direction and speed of the projectile.
                Projectile projectile = spawnedObject.GetComponent <Projectile>();
                projectile.Direction = transform.forward;
                projectile.Speed     = speed;
            }
        }
        // Update is called once per frame
        // public override void Update()
        // {
        //     base.Update();
        // }
        private void SetupEvents()
        {
            // Initial connection will connect just once, even if console displays TWO logs.
            io.On("open", (e) => {
                Debug.Log("connection made to server");
            });

            io.On("register", (e) => {
                ClientID = new JSONObject(e.data)["id"].str;
                Debug.LogFormat("Our Client's Id is ({0})", ClientID);
            });

            io.On("spawn", (e) => {
                // handling all spawned players
                string id = new JSONObject(e.data)["id"].str;

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

            io.On("disconnected", (e) => {
                string id = new JSONObject(e.data)["id"].str;
                Debug.Log("player disconnected");
                GameObject go = serverObjects[id].gameObject;
                Destroy(go); // remove GO from game
                serverObjects.Remove(id);
            });

            io.On("updateRotation", (e) => {
                JSONObject data    = new JSONObject(e.data);
                string id          = data["id"].ToString().RemoveQuotes();
                float weaponRot    = data["weaponRotation"].f;
                bool flipped       = data["playerFlipped"].b;
                NetworkIdentity ni = serverObjects[id];
                // Debug.Log("updating other rotations");
                ni.GetComponent <PlayerManager>().SetWeaponRotation(weaponRot);
            });

            io.On("updatePosition", (e) => {
                JSONObject data       = new JSONObject(e.data);
                string id             = data["id"].ToString().RemoveQuotes();
                float x               = data["position"]["x"].f;
                float y               = data["position"]["y"].f;
                NetworkIdentity ni    = serverObjects[id];
                ni.transform.position = new Vector3(x, y, 0);
            });

            io.On("serverSpawn", (e) => {
                JSONObject data = new JSONObject(e.data);
                string name     = data["name"].str;
                string id       = data["id"].ToString().RemoveQuotes();
                float x         = data["position"]["x"].f;
                float y         = data["position"]["y"].f;
                if (!serverObjects.ContainsKey(id))
                {
                    // Debug.LogFormat("Server wants to spawn '{0}'", name);
                    ServerObjectData sod           = serverSpawnables.GetObjectByName(name);
                    var spawnObject                = Instantiate(sod.Prefab, networkContainer);
                    spawnObject.transform.position = new Vector3(x, y, 0);
                    NetworkIdentity ni             = spawnObject.GetComponent <NetworkIdentity>();
                    ni.SetControllerID(id);
                    ni.SetSocketReference(io.socketIO);

                    // if projectile apply direction as well
                    if (name == "Arrow_Regular")
                    {
                        float directionX = data["direction"]["x"].f;
                        float directionY = data["direction"]["y"].f;
                        string activator = data["activator"].ToString().RemoveQuotes();
                        float speed      = data["speed"].f;

                        float rot = Mathf.Atan2(directionY, directionX) * Mathf.Rad2Deg;
                        Vector3 currentRotation        = new Vector3(0, 0, rot + 180);
                        spawnObject.transform.rotation = Quaternion.Euler(currentRotation);

                        WhoActivatedMe whoActivatedMe = spawnObject.GetComponent <WhoActivatedMe>();
                        whoActivatedMe.SetActivator(activator);

                        Projectile projectile = spawnObject.GetComponent <Projectile>();
                        projectile.Direction  = new Vector2(directionX, directionY);
                        projectile.Speed      = speed;
                    }
                    serverObjects.Add(id, ni);
                }
            });
            io.On("serverDespawn", (e) => {
                string id          = new JSONObject(e.data)["id"].str;
                NetworkIdentity ni = serverObjects[id];
                serverObjects.Remove(id);
                Destroy(ni.gameObject);
            });
            io.On("playerDied", (e) => {
                string id          = new JSONObject(e.data)["id"].str;
                NetworkIdentity ni = serverObjects[id];
                ni.gameObject.SetActive(false);
            });
            io.On("playerRespawn", (e) => {
                JSONObject data       = new JSONObject(e.data);
                string id             = data["id"].ToString().RemoveQuotes();
                float x               = data["position"]["x"].f;
                float y               = data["position"]["y"].f;
                NetworkIdentity ni    = serverObjects[id];
                ni.transform.position = new Vector3(x, y, 0);
                ni.gameObject.SetActive(true);
            });
            io.On("loadGame", (e) => {
                SceneManagementManager.Instance.LoadLevel(levelName: SceneList.LEVEL, onLevelLoaded: (levelName) => {
                    SceneManagementManager.Instance.UnLoadLevel(SceneList.MAIN_MENU);
                });
            });
        }
Пример #4
0
        private void setupEvents()
        {
            On("open", (E) => {
                Debug.Log("Connection made to the server");
            });

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

            On("spawn", (E) => {
                //Handling all spawning all players
                //Passed Data
                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);              //Remove from game
                serverObjects.Remove(id); //Remove from memory
            });

            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("updateAI", (E) => {
                string id            = E.data["id"].ToString().RemoveQuotes();
                float x              = E.data["position"]["x"].f;
                float y              = E.data["position"]["y"].f;
                float tankRotation   = E.data["tankRotation"].f;
                float barrelRotation = E.data["barrelRotation"].f;

                NetworkIdentity ni = serverObjects[id];
                if (ni.gameObject.activeInHierarchy)
                {
                    StartCoroutine(AIPositionSmoothing(ni.transform, new Vector3(x, y, 0)));
                    ni.GetComponent <AIManager>().SetTankRotation(tankRotation);
                    ni.GetComponent <AIManager>().SetBarrelRotation(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 us 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 bullet apply direction as well
                    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);

                        WhoActivatedMe whoActivatedMe = spawnedObject.GetComponent <WhoActivatedMe>();
                        whoActivatedMe.SetActivator(activator);

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

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

            On("serverUnspawn", (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];
                if (ni.GetComponent <AIManager>())
                {
                    ni.GetComponent <AIManager>().StopCoroutines();
                }

                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);
            });
        }
Пример #5
0
        private void SetupEvents()
        {
            On("open", (E) => {
                Debug.Log("Connection made to the server");
            });
            On("register", (E) => {
                ClientID = E.data["id"].ToString().RemoveQuotes();
                Debug.LogFormat("Our client's ID {0}", ClientID);
            });
            On("spawn", (E) => {
                //Spawn every players
                string id             = E.data["id"].ToString().RemoveQuotes();
                GameObject go         = Instantiate(playerPrefab, networkContainer);
                int spawnNum          = Random.Range(0, 4);
                go.transform.position = spawnPoint[spawnNum].position;
                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);              //Remove gameobject from game
                serverObjects.Remove(id); //Remove id from memory
            });
            On("updatePosition", (E) => {
                string id             = E.data["id"].ToString().RemoveQuotes();
                float x               = E.data["position"]["x"].JSONToFloat();
                float y               = E.data["position"]["y"].JSONToFloat();
                float z               = E.data["position"]["z"].JSONToFloat();
                NetworkIdentity ni    = serverObjects[id];
                ni.transform.rotation = Quaternion.Euler(0, float.Parse(E.data["rotation"].str), 0);
                ni.transform.position = new Vector3(x, y, z);
            });
            //Need to replace with a pool
            On("serverSpawn", (E) => {
                string name = E.data["name"].str;
                string id   = E.data["id"].ToString().RemoveQuotes();
                float x     = E.data["position"]["x"].JSONToFloat();
                float y     = E.data["position"]["y"].JSONToFloat();
                float z     = E.data["position"]["z"].JSONToFloat();
                Debug.LogFormat("Server wants us to spawn a '{0}' with id '{1}'", name, id);

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

                    if (name == "Bullet")
                    {
                        float directionX                 = E.data["direction"]["x"].JSONToFloat();
                        float directionY                 = E.data["direction"]["y"].JSONToFloat();
                        float directionZ                 = E.data["direction"]["z"].JSONToFloat();
                        string activator                 = E.data["activator"].ToString().RemoveQuotes();
                        float speed                      = E.data["speed"].JSONToFloat();
                        GameObject activatorGO           = serverObjects[activator].gameObject;
                        spawnedObject.transform.rotation = serverObjects[activator].gameObject.transform.rotation;
                        WhoActivatedMe whoActivatedMe    = spawnedObject.GetComponent <WhoActivatedMe>();
                        whoActivatedMe.WhoActivateMe     = activator;
                        activatorGO.GetComponent <PlayerManager>().ShootingEffects();
                        Projectile projectile = spawnedObject.GetComponent <Projectile>();
                        projectile.Direction  = new Vector3(directionX, directionY, directionZ);
                        projectile.Speed      = speed;
                    }

                    serverObjects.Add(id, ni);
                }
            });
            On("serverUnspawn", (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];
                if (id == ClientID)
                {
                    imageEffectScript.IsDead = true;
                }
                ni.gameObject.SetActive(false);
            });
            On("playerRespawn", (E) => {
                string id = E.data["id"].ToString().RemoveQuotes();
                if (id == ClientID)
                {
                    imageEffectScript.IsDead = false;
                }
                NetworkIdentity ni    = serverObjects[id];
                int spawnNum          = Random.Range(0, 4);
                ni.transform.position = spawnPoint[spawnNum].position;
                ni.gameObject.SetActive(true);
            });
        }