public IEnumerator BroadcastAction(string actionID = null) { BroadcastPayload payloadData = m_curPlayer.GetComponent <Player>().GetInfo(); payloadData.action = actionID; string logJSON = "{ \n\"data\" : " + JsonUtility.ToJson(payloadData) + ", \n\"timestamp\" : " + Time.time.ToString() + "\n},"; string payloadJSON = "{ \"method\" : [\"action\"], \"data\" : " + JsonUtility.ToJson(payloadData) + "}"; m_socket.SendString(payloadJSON); yield return(0); }
public BroadcastPayload GetInfo() { BroadcastPayload curInfo = new BroadcastPayload { velocity = gameObject.GetComponent <Rigidbody2D>().velocity, position = gameObject.GetComponent <Rigidbody2D>().position, state = m_currentState, action = "null" }; return(curInfo); }
IEnumerator StartServerUpdator() { while (true) { if (m_curPlayer == null) { yield return(0); continue; } if ((Time.frameCount % Constants.UPDATE_FREQUENCY) == 0) { BroadcastPayload payloadData = m_curPlayer.GetComponent <Player>().GetInfo(); string payloadJSON = "{ \"method\" : [\"action\"], \"data\" : " + JsonUtility.ToJson(payloadData) + "}"; m_socket.SendString(payloadJSON); } yield return(0); } }
public void UpdateTransform(BroadcastPayload _data) { m_player.SetPosition(_data.position); gameObject.GetComponent <Rigidbody2D>().velocity = _data.velocity; }
public void Act(BroadcastPayload _data) { Correct(_data.position, _data.velocity); m_player.SendMessage(_data.action, 0); }
private void HandleMessage(string _msg) { var replyJSON = JSON.Parse(_msg); string method = replyJSON["method"]; if (method == "joininfo") { m_curPlayerSlot = replyJSON["slot"].AsInt; m_curPlayer = InstantiatePlayer(m_curPlayerSlot, Constants.PLAYER_TAG, SharedData.Username); var registeredPlayerSlots = replyJSON["players"].AsArray; for (int i = 0; i < registeredPlayerSlots.Count; i++) { var infoTuple = registeredPlayerSlots[i].AsArray; int index = (int)infoTuple[0]; string playerUsername = (string)infoTuple[1]; GameObject selectedPlayer = m_playerList[index]; if (selectedPlayer.activeSelf == false) { Debug.Log("Instantiating enemy in slot: " + playerUsername); InstantiatePlayer(index, Constants.ENEMY_TAG, playerUsername); } } } if (method == "newuser") { int playerSlot = replyJSON["slot"].AsInt; string playerUsername = replyJSON["username"].Value; if (playerUsername == "") { playerUsername = playerSlot.ToString(); } Debug.Log("Got newuser in " + playerSlot.ToString() + "with username: "******"action" && (replyJSON["slot"].AsInt != m_curPlayerSlot)) { int playerSlot = replyJSON["slot"].AsInt; BroadcastPayload data = new BroadcastPayload { position = new Vector2(replyJSON["data"]["position"]["x"].AsFloat, replyJSON["data"]["position"]["y"].AsFloat), velocity = new Vector2(replyJSON["data"]["velocity"]["x"].AsFloat, replyJSON["data"]["velocity"]["y"].AsFloat), state = (Player.State)replyJSON["data"]["state"].AsInt, action = replyJSON["data"]["action"] }; GameObject player = m_playerList[playerSlot]; if (data.action == "null") { player.GetComponent <WebController>().UpdateTransform(data); } else if (data.action == "player_death") { Debug.Log("Killing player: " + playerSlot); player.GetComponent <WebController>().KillPlayer(); m_infoPanel.KillUser(playerSlot); } else { player.GetComponent <WebController>().Act(data); } } if (method == "deaduser") { Debug.Log("Got deaduser request"); var info = replyJSON["deaduser"].AsArray; int playerSlot = (int)info[0]; string playerUsername = (string)info[1]; GameObject deadPlayer = m_playerList[playerSlot]; Destroy(deadPlayer); m_playerList[playerSlot] = (GameObject)Instantiate(Resources.Load("Character")); m_playerList[playerSlot].SetActive(false); m_infoPanel.DisableInfoSlot(playerSlot); } }