using LitJson; using System.Collections; using System.Collections.Generic; using UnityEngine; using BestHTTP.JSON; using System; using System.Linq.Expressions; public class PosCommunication : MonoBehaviour { // Start is called before the first frame update public Data_PlayerAttributes dataPA;//生成多个 public Data_PlayerAttributesOne dataPAOne; private int index; public string player_name_InputName; public string player_typeOfWork; public string player_ID; public string playerAction; public List onlePlayerID = new List(); public List makeTypeOfWorkID = new List(); private GameObject TypeOfWork; public List playerTypeOfWorksList = new List();//玩家自身 public List playerMakeTypeOfWorksList = new List();//玩家创造的单位 public List dataPlayerAttributesList = new List(); public List dataPlayerAttributesListOne = new List(); public MakeEnemyManager makeEnemyManager; public MakeEneutralManager makeEneutralManager; public bool isMakeTypeOfWork = false;//是否制造了单位 public EnemyMessagerPanelManager enemyMessagerPanelManager_Scenes5;//跟随视野旋转 左右上下 public EnemyMessagerPanelManager enemyMessagerPanelManager_2D;//固定位置界面 public EnemyMessagerPanelManager enemyMessagerPanelManager_3D;//跟随视野旋转,左右 不上下 private GameManager gameManager; float gpsX, gpsY, gpsZ; public static bool IsPlayerjoin = false;//判断玩家是否加入,false为未加入; private Vector3 thisGpsV3; private Vector3 thisSlamV3; public static float webMM = 0; void Start() { plugin = SvrPlugin.Instance; gpsNumType = 0; gameManager = this.GetComponent(); WSHandler.init(); WSHandler.User.OnLogin += AcceptMessage; TypeOfWork = Resources.Load("PlayerTypeOfWork") as GameObject; //player_name_InputName = "FriendlyForcesTypeOfWork1"+","+" "; //Debug.Log(makeEnemyManager.enemyList.Count); playerTypeOfWorksList.Clear(); MakeThisBady();//创造自己 } int iaaaa = 0; public static int currentMinMapFitIndex; private string strInitPoint; void AcceptMessage(JsonData data) { Debug.Log("wo zai jieshou"); // Debug.Log(data.ToJson()); dataPAOne = JsonMapper.ToObject(data.ToJson());//没有创造单位 dataPA = JsonMapper.ToObject(data.ToJson());//创造单位 if (dataPA.data.message.dataEnemy.Count <= 0) { IsPlayerjoin = true; Debug.Log("我在接收,没有人造单位"); if (!onlePlayerID.Contains(dataPAOne.data.message.data2.id)) { currentMinMapFitIndex += 1; Debug.Log((iaaaa += 1).ToString() + "yourenjinlaile"); onlePlayerID.Add(dataPAOne.data.message.data2.id);//ID 单个人 dataPlayerAttributesListOne.Add(dataPAOne); strInitPoint = dataPlayerAttributesListOne[0].data.message.data2.des; GameObject playerTypeOfWork = Instantiate(TypeOfWork, new Vector3(999, 999, 999), Quaternion.identity);//单个人 //playerTypeOfWork.transform.parent = GameObject.Find("SceneRoot").transform; playerTypeOfWorksList.Add(playerTypeOfWork); Debug.Log(playerTypeOfWorksList.Count + "nnnnnnnnnnnnnnnnnnnnnn"); } for (int i = 0; i < dataPlayerAttributesListOne.Count; i++) { if (dataPlayerAttributesListOne[i].data.message.data2.id.Contains(dataPAOne.data.message.data2.id)) { dataPlayerAttributesListOne[i] = dataPAOne; } // Debug.Log(dataPlayerAttributesList[i].data.message.data2.pos); } } else { // Debug.Log("有人创造了" + dataPA.data.message.dataEnemy.Count + "个造单位"); for (int i = 0; i < dataPA.data.message.dataEnemy.Count; i++) { if (!makeTypeOfWorkID.Contains(dataPA.data.message.dataEnemy[i].id)) { currentMinMapFitIndex = makeTypeOfWorkID.Count + 1; // Debug.Log("加入了一个创建的单位"); // Debug.Log("用户" + dataPA.data.message.data2.id + "生成了" + dataPA.data.message.dataEnemy.Count + "个单位"); makeTypeOfWorkID.Add(dataPA.data.message.dataEnemy[i].id); dataPlayerAttributesList.Add(dataPA); GameObject playerTypeOfWork = Instantiate(TypeOfWork, new Vector3(999, 999, 999), Quaternion.identity);//单个人 // playerTypeOfWork.transform.parent = GameObject.Find("SceneRoot").transform; playerTypeOfWork.name = "UserMakeTypeOfWork"; playerMakeTypeOfWorksList.Add(playerTypeOfWork); playerTypeOfWork.GetComponent().id = dataPA.data.message.dataEnemy[i].id; playerTypeOfWork.GetComponent().action = dataPA.data.message.dataEnemy[i].action; playerTypeOfWork.GetComponent().des = dataPA.data.message.dataEnemy[i].des; playerTypeOfWork.GetComponent().pos = dataPA.data.message.dataEnemy[i].pos; playerTypeOfWork.GetComponent().eul = dataPA.data.message.dataEnemy[i].eul; playerTypeOfWork.GetComponent().GpsPlayerPosStr = dataPA.data.message.dataEnemy[i].gpsPos; playerTypeOfWork.GetComponent().typeOfWork = dataPA.data.message.dataEnemy[i].typeOfWork; playerTypeOfWork.GetComponent().thisCurrentMinMapFitIndex = currentMinMapFitIndex+MakeEneutralManager.MakeEenmyIndex; MakeEneutralManager.ChangeEnemyIndex(dataPA.data.message.dataEnemy[i].des); webMM = Time.timeSinceLevelLoad; Debug.Log("webMM "+ webMM); if (gameManager.scenesName == "scenes3") { enemyMessagerPanelManager_2D.SetEnemyTypeOfWorkText(dataPA.data.message.dataEnemy[i].typeOfWork); enemyMessagerPanelManager_2D.SetEnemyPosText(dataPA.data.message.dataEnemy[i].pos); enemyMessagerPanelManager_2D.SetEnemyDisText(dataPA.data.message.dataEnemy[i].pos); enemyMessagerPanelManager_2D.ShowOrHeidThisPanel("Show"); } else if (gameManager.scenesName == "scenes4") { enemyMessagerPanelManager_3D.SetEnemyTypeOfWorkText(dataPA.data.message.dataEnemy[i].typeOfWork); enemyMessagerPanelManager_3D.SetEnemyPosText(dataPA.data.message.dataEnemy[i].pos); enemyMessagerPanelManager_3D.SetEnemyDisText(dataPA.data.message.dataEnemy[i].pos); enemyMessagerPanelManager_3D.ShowOrHeidThisPanel("Show"); } else if (gameManager.scenesName == "scenes5") { enemyMessagerPanelManager_Scenes5.SetEnemyTypeOfWorkText(dataPA.data.message.dataEnemy[i].typeOfWork); enemyMessagerPanelManager_Scenes5.SetEnemyPosText(dataPA.data.message.dataEnemy[i].pos); enemyMessagerPanelManager_Scenes5.SetEnemyDisText(dataPA.data.message.dataEnemy[i].pos); enemyMessagerPanelManager_Scenes5.ShowOrHeidThisPanel("Show"); } } } //for (int i = 0; i < dataPlayerAttributesList.Count; i++) //敌人位置同步 //{ // if (dataPlayerAttributesList[i].data.message.dataEnemy[i].id.Contains(dataPA.data.message.dataEnemy[i].id)) // { // dataPlayerAttributesList[i] = dataPA; // } //} } } private SvrPlugin plugin = null; float[] gness = new float[3]; double dt; //20米开外的值 //float[] dtnull = new float[3];//20米开外的值(测试); public TextMesh textM1, textM2, textM3; private string initPoint; private bool isB; public static Vector3 shadowPosStatic; public static double gpsNumType = 0;//0为正常数据。20为前方20米数据 public static Vector3 enemyGpsenemyPos;//敌人Gps数据 // public static Vector3 playerGpsPos; public LineRenderer lineX; public LineRenderer lineZ; IEnumerator Initialize() { yield return new WaitUntil(() => plugin.IsInitialized() == true); // plugin.getGnss(ref gpsNumType, gness); Debug.Log(gness + "Gps = {" + gness[0] + "," + gness[1] + "," + gness[2] + "," + "}"); gpsX = gness[0]; gpsY = 0; gpsZ = gness[1]; textM1.text = "gpsX:" + gness[0].ToString(); textM2.text = "gpsY:" + gness[1].ToString(); textM3.text = "gps高:" + gness[2].ToString(); shadowPosStatic = new Vector3(gpsX, gpsY, gpsZ); if (isB) { initPoint = "." + gpsX + "," + gpsY + "," + gpsZ; isB = false; strInitPoint = initPoint; } } private void ShowLine() { lineX.gameObject.SetActive(true); lineZ.gameObject.SetActive(true); lineX.SetPosition(0, API_SVR.GetHead().transform.position + new Vector3(-20f, -1, 0)); lineX.SetPosition(1, API_SVR.GetHead().transform.position + new Vector3(20f, -1, 0)); lineZ.SetPosition(0, API_SVR.GetHead().transform.position + new Vector3(0, -1, -20f)); lineZ.SetPosition(1, API_SVR.GetHead().transform.position + new Vector3(0, -1, 20f)); } private GameObject thisPlayerTypeOfWork; private void MakeThisBady()//创造自己 { Debug.Log("wo chan sheng le zi ji"); thisPlayerTypeOfWork = Instantiate(TypeOfWork, new Vector3(10, 10, 10), Quaternion.identity);//单个人 //thisPlayerTypeOfWork.transform.parent = GameObject.Find("SceneRoot").transform; thisPlayerTypeOfWork.GetComponent().id = player_ID; thisPlayerTypeOfWork.GetComponent().action = "ThisPlayerUser"; thisPlayerTypeOfWork.GetComponent().des = "MakeThisPlayerUser"; thisPlayerTypeOfWork.GetComponent().typeOfWork = player_typeOfWork; } public void SendMyMessage() { WSHandler.User.testLogin(id: player_name_InputName); Vector3 shadowPos = SvrManager.Instance.head.position; shadowPosStatic = shadowPos; //Debug.Log("shadowPos: " + shadowPos); textM1.text = "X:" + shadowPos.x.ToString(); textM2.text = "Z:" + shadowPos.z.ToString(); textM3.text = "y:" + shadowPos.y.ToString(); Vector3 shadowRot = new Vector3(SvrManager.Instance.head.rotation.x, SvrManager.Instance.head.rotation.y, SvrManager.Instance.head.rotation.z); thisPlayerTypeOfWork.GetComponent().pos = shadowPos.x + "_" + shadowPos.y + "_" + shadowPos.z; thisPlayerTypeOfWork.GetComponent().eul = SvrManager.Instance.head.rotation.x + "_" + SvrManager.Instance.head.rotation.y + "_" + SvrManager.Instance.head.rotation.z; thisPlayerTypeOfWork.GetComponent().GpsPlayerPos = shadowPos; // thisPlayerTypeOfWork.GetComponent().GpsPlayerPosStr = 11 + "_" +"0" + "_" + 12; //上一版本传输 传输 GPS坐标 UpdateAttitude(shadowPos, shadowRot, player_ID, player_name_InputName, player_typeOfWork, makeEneutralManager.typeOfEnutralOrenemyList, playerAction, initPoint); // UpdateAttitude(new Vector3(SvrManager.Instance.head.transform.position.x , SvrManager.Instance.head.transform.position.y , SvrManager.Instance.head.transform.position.z), shadowRot, player_ID, player_name_InputName, player_typeOfWork, makeEneutralManager.typeOfEnutralOrenemyList, playerAction, initPoint); } float time; private bool isMakeAIaircraft = true; void Update() { // ShowLine(); // StartCoroutine(Initialize()); time = time + Time.deltaTime; if (time > 0.3f) { Debug.Log("我在发送"); SendMyMessage(); time = 0; } } public void UpdateAttitude(Vector3 pos, Vector3 eul, string id, string describe, string TypeOfWork, List games, string PlayerAction, string initStrPoint) { JsonData message = new JsonData(); JsonData data = new JsonData(); JsonData data2 = new JsonData(); message["notification"] = true; message["method"] = "transmit"; message["data"] = data; data["data2"] = data2; data2["action"] = PlayerAction; data2["id"] = id; data2["des"] = describe; data2["pos"] = pos.x + "_" + pos.y + "_" + pos.z; data2["eul"] = eul.x + "_" + eul.y + "_" + eul.z; data2["typeOfWork"] = TypeOfWork; if (games.Count > 0) { JsonData dataEnemy = new JsonData(); data["dataEnemy"] = new JsonData(); for (int i = 0; i < games.Count; i++) { JsonData dataEnemys = new JsonData(); // data["dataEnemy" +i] = dataEnemys; dataEnemys["action"] = games[i].GetComponent().action; dataEnemys["id"] = games[i].GetComponent().id; dataEnemys["des"] = games[i].GetComponent().des; dataEnemys["pos"] = games[i].GetComponent().pos; dataEnemys["eul"] = games[i].GetComponent().eul; dataEnemys["gpsPos"] = games[i].GetComponent().GpsPlayerPosStr; dataEnemys["typeOfWork"] = games[i].GetComponent().typeOfWork; data["dataEnemy"].Add(dataEnemys); } } if (makeTypeOfWorkID.Count <= 0 && isMakeAIaircraft) { //造一个无人机 // makeEneutralManager.MakeAIAircraftEneutra("AIAircraft", gameManager.gpsPosInitx, 100, gameManager.gpsPosInitz); isMakeAIaircraft = false; } WSHandler.SendMessage(message); } }