private static void Update(string message) { if (message.Equals("")) { return; } //all the vars we'll need string[] data = message.Substring(1, message.Length - 2).Split('|'); // for splitting string int index = 0; // will be easier when mostRecentMessage gets larger //if it's a player, just update the position. if (gameObjects[int.Parse(data[index])].Equals(player)) { player.transform.position = DataParserAndFormatter.StringToVector3(data[index + 1]); return; } //get the object to update GameObject Object = gameObjects[int.Parse(data[index])]; //ignore if null if (Object != null) { Object.transform.position = DataParserAndFormatter.StringToVector3(data[index + 1]); Object.transform.rotation = DataParserAndFormatter.StringToQuaternion(data[index + 2]); //parse and update rotation if (debug == true) { Debug.Log("UNITY HANDLER: Updated Object of name: " + Object.name.ToString() + "to position " + data[index + 1] + " and rotation " + data[index + 2]); } } }
private static void Create(string message) { string[] data; // for splitting string int index = 0; // will be easier when mostRecentMessage gets larger data = message.Substring(1, message.Length - 2).Split('|'); //split each object into an array if (!data[index].Equals("playe")) //if this create message is for me then add a null object and move on { string resourcePath = data[index]; Debug.Log(resourcePath + " <- that's it"); Vector3 position = DataParserAndFormatter.StringToVector3(data[index + 1]); //parse and update position Quaternion orientation = DataParserAndFormatter.StringToQuaternion(data[index + 2].Substring(1, data[index + 2].Length - 1)); //parse and update rotation GameObject resource = (GameObject)Resources.Load(resourcePath); //get GameObject resource from resourcePath GameObject Object = GameObject.Instantiate(resource, position, orientation); //make the GameObject in the scene with the correct orientation and position //add this object to total list of gameObjects gameObjects.Add(Object); if (debug == true) { Debug.Log("UNITY HANDLER: Created Object with resource path " + data[index] + "to position " + data[index + 1] + " and rotation " + data[index + 2]); } } else { Debug.Log("made player"); gameObjects.Add(player); } }
//reads in server output and does what the server says public void HandleServerOutput() { List <string> serverOutput = udpListen.ReadMessages(); //get the whole output in one string, from oldest to newest messages string fullOutput = ""; foreach (string s in serverOutput) { fullOutput += s; } //turn the string into a nice list of messages List <Message> messages = DataParserAndFormatter.GetMessagesFromServerOutput(fullOutput); //handle each message (oldest to newest) foreach (Message message in messages) { UnityHandler.HandleMessage(message); if (debug == true) { // Debug.Log("CLIENT: Sent message to Unity Handler"); } } }
public void CreatePlayers() { List <string> messages = udpListen.ReadMessages(); foreach (string message in messages) { string ip = DataParserAndFormatter.GetIP(message); string classPath = DataParserAndFormatter.GetClassPath(message); int[] abilityIds = DataParserAndFormatter.GetAbilityIds(message); GameObject player = GameObject.Instantiate(Resources.Load(classPath) as GameObject, spawn, Quaternion.identity); player.GetComponent <PlayerScript>().SetAbilities(abilityIds); players.Add(new PlayerClient(ip, player, classPath)); Create(player, classPath, ip); } }
public void HandleClientInput() { List <string> messages = udpListen.ReadMessages(); foreach (string message in messages) { string ip = DataParserAndFormatter.GetIP(message); for (int i = 0; i < players.Count; i++) { if (players[i].ipAddr.Equals(ip)) { Quaternion[] rots = DataParserAndFormatter.GetRotationIn(message); players[i].playerGameObject.transform.rotation = rots[0]; players[i].playerGameObject.GetComponent <PlayerScript>().HandleInput(DataParserAndFormatter.GetKeysIn(message), rots[1], DataParserAndFormatter.GetPosIn(message), DataParserAndFormatter.GetMouseIn(message)); } } } }
public void SendPlayerData() { string keysPressed = ""; for (int i = 33; i <= 122; i++) { if (Input.GetKey(((char)i).ToString().ToLower() + "")) { string input = ((char)i).ToString().ToLower(); if (!keysPressed.Contains(input)) { keysPressed += input; } } } if (Input.GetKey(KeyCode.Space)) { keysPressed += " "; } string clientData = DataParserAndFormatter.GetClientInputFormatted(keysPressed, Input.GetMouseButtonDown(0), Input.GetMouseButtonDown(1), player.transform.rotation, camera.transform.rotation, camera.transform.position, UDP.GetLocalIPAddress()); udp.Send(clientData, serverIP); //send position and orientation and ipaddr of client to server for update }
public void SendClassData(String classPath, int[] abilityIds) { udp.Send(DataParserAndFormatter.GetClassPathAndAbilityIdsFormatted(classPath, abilityIds, UDP.GetLocalIPAddress()), serverIP); }