public void Clear() { if (m_guardAreaView != null) { UnityEngine.Object.Destroy(m_guardAreaView.gameObject); m_guardAreaView = null; } UpdateDelayObject();//flush delay objects for (int i = 0; i < (int)OwnerType.Count; i++) { foreach (var tileEntity in m_entities[i]) { tileEntity.Destroy(); } } Init(); }
/// <summary> /// 初始化战斗中的防御区域 /// </summary> public void InitGuardAreaMap() { // 1、初始化空 m_guardAreaMap = new GuardAreaValue[Constants.EDGE_WIDTH, Constants.EDGE_HEIGHT]; for (int x = 0; x < Constants.EDGE_WIDTH; x++) { for (int y = 0; y < Constants.EDGE_HEIGHT; y++) { m_guardAreaMap[x, y] = GuardAreaValue.NIL; } } // 2、根据建筑初始化防御区域,这里获取Defender方建筑。 // REMARK:扩展范围宽度 这个值可以调整 int extendWidth = 2; foreach (var entity in GetAllEntitiesByOwner(OwnerType.Defender)) { if (!EntityTypeUtil.IsAnyBuilding(entity.entityType)) { continue; } int bgnX = Mathf.Max(entity.GetTilePos().x - extendWidth, 0); int endX = Mathf.Min(entity.GetTilePos().x + entity.width + extendWidth, Constants.EDGE_WIDTH - 1); int bgnY = Mathf.Max(entity.GetTilePos().y - extendWidth, 0); int endY = Mathf.Min(entity.GetTilePos().y + entity.height + extendWidth, Constants.EDGE_HEIGHT - 1); for (int x = bgnX; x <= endX; x++) { for (int y = bgnY; y <= endY; y++) { m_guardAreaMap[x, y] = GuardAreaValue.Uninitialized; } } } // 3、计算每个防御区域格子贴图方向 for (int x = 0; x < Constants.EDGE_WIDTH; x++) { for (int y = 0; y < Constants.EDGE_HEIGHT; y++) { if (m_guardAreaMap[x, y] == GuardAreaValue.NIL || m_guardAreaMap[x, y] != GuardAreaValue.Uninitialized) { continue; } // 计算防御区域方向(默认为zero无方向) GuardAreaValue dir = GuardAreaValue.Zero; { // 上 if (y + 1 < Constants.EDGE_HEIGHT && m_guardAreaMap[x, y + 1] == GuardAreaValue.NIL) { dir |= GuardAreaValue.Top; } // 下 if (y - 1 >= 0 && m_guardAreaMap[x, y - 1] == GuardAreaValue.NIL) { dir |= GuardAreaValue.Bottom; } // 左 if (x - 1 >= 0 && m_guardAreaMap[x - 1, y] == GuardAreaValue.NIL) { dir |= GuardAreaValue.Left; } // 右 if (x + 1 < Constants.EDGE_WIDTH && m_guardAreaMap[x + 1, y] == GuardAreaValue.NIL) { dir |= GuardAreaValue.Right; } } // 设置新的方向 m_guardAreaMap[x, y] = dir; } } if (m_guardAreaView == null) { var go = new GameObject(); go.name = "GuardAreaView"; m_guardAreaView = go.AddComponent <GuardAreaView>(); go.SetActive(false); } m_guardAreaView.Init(m_guardAreaMap); }