123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- 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<string> onlePlayerID = new List<string>();
- public List<string> makeTypeOfWorkID = new List<string>();
- private GameObject TypeOfWork;
- public List<GameObject> playerTypeOfWorksList = new List<GameObject>();//玩家自身
- public List<GameObject> playerMakeTypeOfWorksList = new List<GameObject>();//玩家创造的单位
- public List<Data_PlayerAttributes> dataPlayerAttributesList = new List<Data_PlayerAttributes>();
- public List<Data_PlayerAttributesOne> dataPlayerAttributesListOne = new List<Data_PlayerAttributesOne>();
- 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<GameManager>();
- 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_PlayerAttributesOne>(data.ToJson());//没有创造单位
- dataPA = JsonMapper.ToObject<Data_PlayerAttributes>(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<MiniMapFit>().id = dataPA.data.message.dataEnemy[i].id;
- playerTypeOfWork.GetComponent<MiniMapFit>().action = dataPA.data.message.dataEnemy[i].action;
- playerTypeOfWork.GetComponent<MiniMapFit>().des = dataPA.data.message.dataEnemy[i].des;
- playerTypeOfWork.GetComponent<MiniMapFit>().pos = dataPA.data.message.dataEnemy[i].pos;
- playerTypeOfWork.GetComponent<MiniMapFit>().eul = dataPA.data.message.dataEnemy[i].eul;
- playerTypeOfWork.GetComponent<MiniMapFit>().GpsPlayerPosStr = dataPA.data.message.dataEnemy[i].gpsPos;
- playerTypeOfWork.GetComponent<MiniMapFit>().typeOfWork = dataPA.data.message.dataEnemy[i].typeOfWork;
- playerTypeOfWork.GetComponent<MiniMapFit>().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<MiniMapFit>().id = player_ID;
- thisPlayerTypeOfWork.GetComponent<MiniMapFit>().action = "ThisPlayerUser";
- thisPlayerTypeOfWork.GetComponent<MiniMapFit>().des = "MakeThisPlayerUser";
- thisPlayerTypeOfWork.GetComponent<MiniMapFit>().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<MiniMapFit>().pos = shadowPos.x + "_" + shadowPos.y + "_" + shadowPos.z;
- thisPlayerTypeOfWork.GetComponent<MiniMapFit>().eul = SvrManager.Instance.head.rotation.x + "_" + SvrManager.Instance.head.rotation.y + "_" + SvrManager.Instance.head.rotation.z;
- thisPlayerTypeOfWork.GetComponent<MiniMapFit>().GpsPlayerPos = shadowPos;
- // thisPlayerTypeOfWork.GetComponent<MiniMapFit>().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<GameObject> 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<MiniMapFit>().action;
- dataEnemys["id"] = games[i].GetComponent<MiniMapFit>().id;
- dataEnemys["des"] = games[i].GetComponent<MiniMapFit>().des;
- dataEnemys["pos"] = games[i].GetComponent<MiniMapFit>().pos;
- dataEnemys["eul"] = games[i].GetComponent<MiniMapFit>().eul;
- dataEnemys["gpsPos"] = games[i].GetComponent<MiniMapFit>().GpsPlayerPosStr;
- dataEnemys["typeOfWork"] = games[i].GetComponent<MiniMapFit>().typeOfWork;
- data["dataEnemy"].Add(dataEnemys);
- }
- }
- if (makeTypeOfWorkID.Count <= 0 && isMakeAIaircraft)
- {
- //造一个无人机
- // makeEneutralManager.MakeAIAircraftEneutra("AIAircraft", gameManager.gpsPosInitx, 100, gameManager.gpsPosInitz);
- isMakeAIaircraft = false;
- }
- WSHandler.SendMessage(message);
- }
- }
|