private void SetupEvents() { On("open", (E) => { Debug.Log("Connection mate to the 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().Replace("\"", string.Empty).Trim(); GameObject go = Instantiate(playerPrefab, networkContainer); go.name = string.Format("Payer({0})", id); NetworkIdentity networkIdentity = go.GetComponent <NetworkIdentity>(); networkIdentity.SetControllerID(id); networkIdentity.SetSocketReference(this); serverObjects.Add(id, networkIdentity); }); On("disconected", (E) => { string id = E.data["id"].ToString(); 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; //actualizo posicion del juador correspondiente NetworkIdentity networkIdentity = serverObjects[id]; networkIdentity.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; //actualizo la rotacion del jugador correspondiente NetworkIdentity networkIdentity = serverObjects[id]; networkIdentity.transform.localEulerAngles = new Vector3(0, 0, tankRotation); networkIdentity.GetComponent <PlayerManager>().SetBarrelRotation(barrelRotation); }); }
public void ForcePlayerToIdle() { NetworkIdentity ni = networkIdentities.Single(i => i.GetID() == ClientID); ni.GetComponent <NetworkAnimator>().ForceToIdle(); }
// 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); }); }); }
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); }); }
private void setupEvents() { On("open", (E) => { Debug.Log("Connection Made To The Server"); }); On("register", (E) => { ClientID = E.data["id"].ToString().Trim('"'); Debug.LogFormat("Our client's ID ({0})", ClientID); }); On("spawn", (E) => { //Handling all spawning all players //Passed Data string id = E.data["id"].ToString().Trim('"'); 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("disconected", (E) => { string id = E.data["id"].ToString().Trim('"'); GameObject go = serverObjects[id].gameObject; Destroy(go); //Remove from game serverObjects.Remove(id); //Remove from memory Debug.Log("disconected"); }); On("updatePosition", (E) => { string id = E.data["id"].ToString().Trim('"'); 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().Trim('"'); 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().Trim('"'); 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 spwanedObject = Instantiate(sod.Prefab, networkContainer); spwanedObject.transform.position = new Vector3(x, y, 0); var ni = spwanedObject.GetComponent <NetworkIdentity>(); ni.SetControllerID(id); ni.SetSocketReference(this); //If bullet applay direction as well if (name == "Bullet") { float directionX = E.data["direction"]["x"].f; float directionY = E.data["direction"]["y"].f; float rot = Mathf.Atan2(directionY, directionX) * Mathf.Rad2Deg; Vector3 currentRotation = new Vector3(0, 0, rot - 90); spwanedObject.transform.rotation = Quaternion.Euler(currentRotation); } serverObjects.Add(id, ni); } }); On("serverUnspawn", (E) => { string id = E.data["id"].ToString().Trim('"'); NetworkIdentity ni = serverObjects[id]; serverObjects.Remove(id); DestroyImmediate(ni.gameObject); }); }
private void SetupEvents() { On("open", (e) => { Debug.Log("Connected to server"); }); On("register", (e) => { ClientID = e.data["id"].ToString().RemoveQuotes(); Debug.LogFormat("My client 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.SetCotrollerID(id); ni.SetSocketReference(this); serverObjects.Add(id, ni); //Enabling Camera if (ni.IsControlling()) { go.transform.Find("Third Person Camera").gameObject.SetActive(true); } Debug.LogFormat("I have spawned"); }); On("disconnected", (e) => { string id = e.data["id"].ToString().RemoveQuotes(); GameObject go = serverObjects[id].gameObject; Destroy(go); serverObjects.Remove(id); Debug.LogFormat("Disconnected"); }); On("updatePosition", (e) => { string id = e.data["id"].ToString().RemoveQuotes(); float x = float.Parse(e.data["position"]["x"].str, CultureInfo.InvariantCulture.NumberFormat); float y = float.Parse(e.data["position"]["y"].str, CultureInfo.InvariantCulture.NumberFormat); float z = float.Parse(e.data["position"]["z"].str, CultureInfo.InvariantCulture.NumberFormat); NetworkIdentity ni = serverObjects[id]; ni.transform.position = new Vector3(x, y, z); }); On("updateRotation", (e) => { string id = e.data["id"].ToString().RemoveQuotes(); float rotation = float.Parse(e.data["rotation"].str, CultureInfo.InvariantCulture.NumberFormat); NetworkIdentity ni = serverObjects[id]; ni.transform.localEulerAngles = new Vector3(0, rotation, 0); }); On("updateAI", (e) => { string id = e.data["id"].ToString().RemoveQuotes(); float x = float.Parse(e.data["position"]["x"].str, CultureInfo.InvariantCulture.NumberFormat); float y = float.Parse(e.data["position"]["y"].str, CultureInfo.InvariantCulture.NumberFormat); float z = float.Parse(e.data["position"]["z"].str, CultureInfo.InvariantCulture.NumberFormat); float rotation = float.Parse(e.data["rotation"].str, CultureInfo.InvariantCulture.NumberFormat); NetworkIdentity ni = serverObjects[id]; if (ni.gameObject.activeInHierarchy) { StartCoroutine(AIPositionSmoothing(ni.transform, new Vector3(x, y, z))); ni.GetComponent <EnemyManager>().SetRotation(rotation); } }); On("serverSpawn", (e) => { string name = e.data["name"].str; string id = e.data["id"].ToString().RemoveQuotes(); float x = float.Parse(e.data["position"]["x"].ToString(), CultureInfo.InvariantCulture.NumberFormat); float y = float.Parse(e.data["position"]["y"].ToString(), CultureInfo.InvariantCulture.NumberFormat); float z = float.Parse(e.data["position"]["z"].ToString(), CultureInfo.InvariantCulture.NumberFormat); Debug.LogFormat("Server wants us to spawn a '{0}'", name); if (!serverObjects.ContainsKey(id)) { ServerObjectData sod = serverSpawnables.GetObjectByName(name); GameObject spawnedObject = Instantiate(sod.prefab, networkContainer); spawnedObject.transform.position = new Vector3(x, y, z); NetworkIdentity ni = spawnedObject.GetComponent <NetworkIdentity>(); ni.SetCotrollerID(id); ni.SetSocketReference(this); Debug.Log(spawnedObject.transform.position); //if bullet apply direction as well // if(name == "Bullet") // { // float directionX = float.Parse(e.data["direction"]["x"].str, CultureInfo.InvariantCulture.NumberFormat); // float directionY = float.Parse(e.data["direction"]["y"].str, CultureInfo.InvariantCulture.NumberFormat); // string activator = e.data["activator"].ToString().RemoveQuotes(); // float speed = float.Parse(e.data["speed"].str, CultureInfo.InvariantCulture.NumberFormat); // 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("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]; ni.gameObject.SetActive(false); }); 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); //I prefer use Invoke to make difference between Action and function }); }
private void SetUpEvents() { //open prints two times, but this is bug with the socket.io asset //the connection does not get duplicated so it does not matter much. On("open", (e) => { Debug.Log("Connection Made with Server"); }); On("register", (e) => { ClientID = e.data["id"].ToString().RemoveQuotes();//.RemoveQuotes(); Debug.LogFormat("Our Client's ID is ({0})", ClientID); }); On("spawn", (e) => { string id = e.data["id"].ToString().RemoveQuotes(); GameObject go = Instantiate(playerPrefab, networkContainer); go.name = string.Format("Player ({0})", id); //Network Identity: NI : ni 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 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; float rot = Mathf.Atan2(directionY, directionX) * Mathf.Rad2Deg; Vector3 currentRotation = new Vector3(0, 0, rot - 90); spawnedObject.transform.rotation = Quaternion.Euler(currentRotation); } serverObjects.Add(id, ni); } }); On("serverUnspawn", (e) => { string id = e.data["id"].ToString().RemoveQuotes(); NetworkIdentity ni = serverObjects[id]; serverObjects.Remove(id); DestroyImmediate(ni.gameObject); }); }