Exemplo n.º 1
0
        /// <summary>
        /// 搜寻移动范围与攻击范围
        /// </summary>
        /// <param name="cls"></param>
        /// <param name="nAtk">是否包含攻击范围</param>
        /// <param name="moveCells"></param>
        /// <param name="atkCells"></param>
        /// <returns></returns>
        public bool SearchMoveRange(
            MapClass cls,
            bool nAtk,
            out IEnumerable <CellData> moveCells,
            out IEnumerable <CellData> atkCells)
        {
            moveCells = null;
            atkCells  = null;

            if (cls == null)
            {
                Debug.LogErrorFormat("MapGraph -> SearchMoveRange: `cls` is null.");
                return(false);
            }

            CellData cell = GetCellData(cls.cellPosition);

            if (cell == null)
            {
                Debug.LogErrorFormat("MapGraph -> SearchMoveRange: `cls.cellPosition` is out of range.");
                return(false);
            }

            // TODO 搜索移动范围,从MapClass中读取数据
            float           movePoint   = 0;
            MoveConsumption consumption = null;

            List <CellData> rangeCells = SearchMoveRange(cell, movePoint, consumption);

            if (rangeCells == null)
            {
                return(false);
            }

            moveCells = rangeCells.ToArray();

            if (nAtk /* TODO && 是否有武器 */)
            {
                // TODO 搜索攻击范围,从MapClass中读取数据
                Vector2Int atkRange = Vector2Int.one;

                HashSet <CellData> atkRangeCells = new HashSet <CellData>(cellPositionEqualityComparer);
                foreach (CellData moveCell in moveCells)
                {
                    rangeCells = SearchAttackRange(moveCell, atkRange.x, atkRange.y, true);

                    if (rangeCells == null)
                    {
                        return(false);
                    }

                    if (rangeCells.Count > 0)
                    {
                        atkRangeCells.UnionWith(rangeCells.Where(c => !c.hasCursor));
                    }
                }

                atkCells = atkRangeCells;
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 初始化加载地图时地图对象
        /// </summary>
        private void InitMapObjectsInMap()
        {
            if (mapObjectPool == null)
            {
                Debug.LogError("MapGraph -> MapObject Pool is null.");
                return;
            }

            MapObject[] mapObjects = mapObjectPool.gameObject.GetComponentsInChildren <MapObject>();
            if (mapObjects != null)
            {
                foreach (MapObject mapObject in mapObjects)
                {
                    // 我们的地图对象不应包含Cursor相关的物体
                    if (mapObject.mapObjectType == MapObjectType.MouseCursor ||
                        mapObject.mapObjectType == MapObjectType.Cursor)
                    {
                        GameObject.Destroy(mapObject.gameObject);
                        continue;
                    }

                    // 初始化
                    mapObject.InitMapObject(this);

                    // 更新坐标
                    Vector3    world        = mapObject.transform.position;
                    Vector3Int cellPosition = grid.WorldToCell(world);
                    mapObject.cellPosition = cellPosition;

                    // 设置CellData
                    CellData cellData = GetCellData(cellPosition);
                    if (cellData != null)
                    {
                        if (cellData.hasMapObject)
                        {
                            Debug.LogErrorFormat("MapObject in Cell {0} already exists.", cellPosition.ToString());
                            continue;
                        }
                        cellData.mapObject = mapObject;
                    }

                    // 如果是Class
                    // 可选项(可忽略):
                    //      请将Prefab加入到mapObjectPool的PrePrefabs中防止动态重复读取Prefab
                    //      为Prefab添加RuntimePrePoolObject组件:
                    //          如果此Prefab也用于动态读取,一定将组件enable设置为false;
                    //          将 Prefab Name 设置成对应的Prefab名称;
                    //          删除时会Despawn回池子。
                    //      这些不是必须的,一些需要这样干的情况:
                    //          绘制此Prefab的实例(比如某些杂兵)数量非常多(一般 > 20),
                    //          且在消灭之后会由于事件触发再次生成大量此Prefab的实例。
                    if (mapObject.mapObjectType == MapObjectType.Class)
                    {
                        RuntimePrePoolObject runtime = mapObject.GetComponent <RuntimePrePoolObject>();
                        if (runtime != null && !runtime.enabled)
                        {
                            runtime.m_PoolName = mapObjectPool.poolName;
                            runtime.enabled    = true;
                        }
                        MapClass cls = mapObject as MapClass;
                        cls.Load(0); // TODO Load Data
                        if (!m_Classes.Contains(cls))
                        {
                            m_Classes.Add(cls);
                        }
                    }

                    mapObject.gameObject.name += mapObject.cellPosition.ToString();
                }
            }
        }