/// <summary> /// 更新当前位置 /// </summary> /// <param name="centerPos"></param> public void SetCurPos(Vector3Int centerPos, int index) { /* * curPos = centerPos; * visiableZone.SetField(curPos, farSize, farSize); * nearZone.SetField(curPos, nearSize, nearSize); */ allCurPos[index] = centerPos; IntField visZone = new IntField(); visZone.SetField(centerPos, farSize, farSize); visiableZoneList[index] = visZone; IntField nearZone = new IntField(); nearZone.SetField(centerPos, nearSize, nearSize); nearZoneList[index] = nearZone; }
/// <summary> /// 初始化地图 /// </summary> /// <param name="miscList">小件列表</param> public void Init(GameObject[] miscList) { if (groundTilemap == null || miscList == null || miscList.Length < 1) { return; } // miscCache = new Dictionary<Vector3Int, List<GameObject>>(); activeCell = new HashSet <Vector3Int>(); /* * foreach (var go in miscList) * { * var localPos = groundTilemap.transform.InverseTransformPoint(go.transform.position); * var pos = groundTilemap.LocalToCell(localPos); * if (!miscCache.ContainsKey(pos)) * { * miscCache.Add(pos, new List<GameObject>()); * } * miscCache[pos].Add(go); * } */ miscBodyCache = new Dictionary <Vector3Int, List <GameObject> >(); miscDataDic = new Dictionary <GameObject, MiscData>(); foreach (var go in miscList) { var localPos = groundTilemap.transform.InverseTransformPoint(go.transform.position); var pos = groundTilemap.LocalToCell(localPos); /* * if (!miscBodyCache.ContainsKey(pos)) * { * miscBodyCache.Add(pos, new List<GameObject>()); * } * miscBodyCache[pos].Add(go); */ var spsBounds = go.GetComponent <SpriteRenderer>().bounds; var spsBoundsWidth = Mathf.CeilToInt(spsBounds.size.x); var spsBoundsHeight = Mathf.CeilToInt(spsBounds.size.y); var centerCellPos = groundTilemap.WorldToCell(spsBounds.center); /* * if (centerCellPos != pos) * { * if (!miscBodyCache.ContainsKey(centerCellPos)) * { * miscBodyCache.Add(centerCellPos, new List<GameObject>()); * } * miscBodyCache[centerCellPos].Add(go); * } */ IntField miscBody = new IntField(); miscBody.SetField(centerCellPos, spsBoundsWidth + 1, spsBoundsHeight * 2 + 1); int maxCount = 0; foreach (var p in miscBody.allPositionsWithin) { if (!groundTilemap.HasTile(p)) { continue; } if (spsBounds.Contains(groundTilemap.CellToWorld(p))) { if (!miscBodyCache.ContainsKey(p)) { miscBodyCache.Add(p, new List <GameObject>()); } miscBodyCache[p].Add(go); maxCount = maxCount + 1; } } miscDataDic.Add(go, new MiscData(go, maxCount, 0)); } }