using System.Collections; using System.Collections.Generic; using UnityEngine; using Games.LogicObj; using UnityEngine.UI; public class TombRadierMapHelper : MonoBehaviour { private static TombRadierMapHelper instance; public static TombRadierMapHelper Instance { get { return instance; } } public RectTransform safeRoom; public Vector2 safeRoomPos; public RectTransform safeRoom_Big; public Vector2 safeRoomPos_Big; private float bigXScale; // 相对于小地图的缩放比 private float bigYScale; // 相对于小地图的缩放比 public GameObject prefab; // 用于小地图 public GameObject bigPrefab; // 用于大地图 private RectTransform itemsParent; // 图标的父物体 private RectTransform itemsParent_Big; // 大地图图标的父物体 private RectTransform mapRT; // 用于获得小地图尺寸 private Dictionary allPointDic = new Dictionary(); private Dictionary items = new Dictionary(); private Stack useLessItems = new Stack(); private Dictionary itemsBig = new Dictionary(); private Stack useLessItemsBig = new Stack(); private bool isFirstOpenMap = true; // 是否第一次打开地图,如果是,需要同步一次当前所有的图标 private void Awake() { if(instance == null) { instance = this; Init(); Games.Events.EventDispatcher.Instance.Add(Games.Events.EventId.SceneIdUpdate, OnSceneIDUpdate); Games.Events.EventDispatcher.Instance.Add(Games.Events.EventId.OpenMap, OnOpenMap); } } private void Start() { if (null == SceneMapFix.Instance()) { ResourceManager.InstantiateResource("Prefab/Logic/SceneMapFix", "SceneMapFix"); } } private void OnSceneIDUpdate(object args) { Clear(); } private void OnDestroy() { Games.Events.EventDispatcher.Instance.Remove(Games.Events.EventId.SceneIdUpdate, OnSceneIDUpdate); Games.Events.EventDispatcher.Instance.Remove(Games.Events.EventId.OpenMap, OnOpenMap); instance = null; } private void Init() { if (MiniMapLogic.Instance() != null) { GameObject newTrans = new GameObject("TombRaiderMapItems"); itemsParent = newTrans.AddComponent(); mapRT = MiniMapLogic.Instance().mapPointsParent as RectTransform; itemsParent.SetParent(MiniMapLogic.Instance().mapPointsParent); itemsParent.localScale = Vector3.one; itemsParent.anchoredPosition = Vector3.zero; safeRoom.gameObject.SetActive(true); safeRoom.SetParent(MiniMapLogic.Instance().mapPointsParent); safeRoom.localScale = Vector3.one; safeRoom.anchoredPosition = safeRoomPos; } } public void RecieveInfo(RespGetTombRaiderCollectPointList packet) { for (int i = 0; i < packet.CollectPointList.Count; ++i) { if (packet.CollectPointList[i].OpType == 0) { if (allPointDic.ContainsKey(packet.CollectPointList[i].ObjId)) { allPointDic.Remove(packet.CollectPointList[i].ObjId); } } else { allPointDic[packet.CollectPointList[i].ObjId] = packet.CollectPointList[i]; } } if (packet.CollectPointList.Count <= 0) { return; } for (int i = 0; i < packet.CollectPointList.Count; ++i) { if (packet.CollectPointList[i].OpType == 1) { TombRaiderMapItem newOne; if (useLessItems.Count > 0) { newOne = useLessItems.Pop(); } else { GameObject newOneObj = Instantiate(prefab, itemsParent); newOne = newOneObj.GetComponent(); } items[packet.CollectPointList[i].ObjId] = newOne; newOne.gameObject.SetActive(true); newOne.transform.SetParent(itemsParent); Vector2 newPos = GetPos(packet.CollectPointList[i].ObjId, new Vector2(packet.CollectPointList[i].x, packet.CollectPointList[i].z), mapRT); newOne.Show(packet.CollectPointList[i].ObjId, packet.CollectPointList[i].ItemId, newPos); ShowBigItem(true, packet.CollectPointList[i].ObjId, packet.CollectPointList[i].ItemId, newPos); } else { TombRaiderMapItem item; if (items.TryGetValue(packet.CollectPointList[i].ObjId, out item)) { useLessItems.Push(item); items.Remove(packet.CollectPointList[i].ObjId); item.gameObject.SetActive(false); } ShowBigItem(false, packet.CollectPointList[i].ObjId); } } } public void Clear() { items.Clear(); useLessItems.Clear(); if(itemsParent != null) { Destroy(itemsParent.gameObject); } itemsBig.Clear(); useLessItems.Clear(); if (safeRoom != null) { Destroy(safeRoom.gameObject); } if (itemsParent_Big != null) { Destroy(itemsParent_Big.gameObject); } } private Vector2 GetPos(int objID, Vector2 pos, RectTransform parentRt) { //Obj_Character obj = Singleton.Instance.FindObjCharacterInScene(objID); //if (obj != null) //{ // yDev = obj.transform.position.y * deviation; //} //Vector2 result = new Vector2(pos.x * scaleX, pos.y * scaleY); //result.y += yDev; Vector3 targetPos = new Vector3(); targetPos.x = pos.x / 100.0f; targetPos.z = pos.y / 100.0f; targetPos.y = 99999.0f; Ray r = new Ray(targetPos, Vector3.down); var temp = Games.Scene.ActiveScene.GetTerrainPosition(r); Vector3 result = temp == null ? Vector3.zero : temp.Value; var newPos = SceneMapFix.Instance().ScenePosToMapPosVec3(result, parentRt); return newPos; } // 打开小地图事件 private void OnOpenMap(object args) { RectTransform bigMap = SceneMapLogicUI.Instance.m_SceneMapImage.rectTransform; bigXScale = bigMap.rect.width / 1000.0f; bigYScale = bigMap.rect.height / 500.0f; if (isFirstOpenMap) { isFirstOpenMap = false; if(SceneMapLogicUI.Instance != null) { GameObject bigMapParent = new GameObject("TombRaiderMapItems"); itemsParent_Big = bigMapParent.AddComponent(); itemsParent_Big.SetParent(bigMap); itemsParent_Big.localScale = Vector3.one; itemsParent_Big.localPosition = Vector3.zero; } } safeRoom_Big.SetParent(itemsParent_Big); safeRoom_Big.gameObject.SetActive(true); safeRoom_Big.localPosition = safeRoomPos_Big; foreach (var kv in itemsBig) { useLessItemsBig.Push(kv.Value); kv.Value.gameObject.SetActive(false); } itemsBig.Clear(); foreach(var kv in items) { ShowBigItem(true, kv.Value.ObjID, kv.Value.ItemID, kv.Value.rt.anchoredPosition); } } private void ShowBigItem(bool isIncrease, int objID = -1, string itemID = null, Vector2? pos = null) { if(SceneMapLogicUI.Instance == null || !SceneMapLogicUI.Instance.gameObject.activeInHierarchy) { return; } if (isIncrease) { TombRaiderMapItem newOne; if (useLessItemsBig.Count > 0) { newOne = useLessItemsBig.Pop(); } else { GameObject newBigMapItem = Instantiate(bigPrefab, itemsParent_Big); newOne = newBigMapItem.GetComponent(); } newOne.transform.SetParent(itemsParent_Big); itemsBig[objID] = newOne; newOne.gameObject.SetActive(true); //Vector2 newPos = GetPos(packet.CollectPointList[i].ObjId, new Vector2(packet.CollectPointList[i].x, packet.CollectPointList[i].z), itemsParent); newOne.Show(objID, itemID, new Vector2(pos.Value.x * bigXScale, pos.Value.y * bigYScale)); } else { itemsBig[objID].gameObject.SetActive(false); useLessItemsBig.Push(itemsBig[objID]); } } public void ReFresh() { if (itemsParent == null || mapRT == null) { Init(); TombRaiderMapItem newOne; foreach (var item in allPointDic.Values) { if (useLessItems.Count > 0) { newOne = useLessItems.Pop(); } else { GameObject newOneObj = Instantiate(prefab, itemsParent); newOne = newOneObj.GetComponent(); } items[item.ObjId] = newOne; newOne.gameObject.SetActive(true); newOne.transform.SetParent(itemsParent); Vector2 newPos = GetPos(item.ObjId, new Vector2(item.x, item.z), mapRT); newOne.Show(item.ObjId, item.ItemId, newPos); ShowBigItem(true, item.ObjId, item.ItemId, newPos); } } } }