Exemple #1
0
 public void AddNeighbour(HexRoom room)
 {
     if (!neighbourRoom.Contains(room))
     {
         neighbourRoom.Add(room);
     }
 }
Exemple #2
0
 void OnRightClickCell(HexRoom room, HexCell cell)
 {
     if (cell.entity == null)
     {
         //判断是否可以移动到目标地点
         if (m_ControllerAction.IsCanMoveToDestination(cell))
         {
             m_ControllerAction.HideMoveAndAttackRange();
             m_ControllerAction.Move(cell, 0);
         }
     }
     else
     {
         //判断是否可以攻击目标
         if (m_ControllerAction.IsCanAttackTarget(cell.entity))
         {
             int targetDistance = GetEntity().centerCell.GetDistance(cell);
             int attackDistance = (int)m_PropertyComponent.attackRange;
             if (attackDistance >= targetDistance)
             {
                 m_ControllerAction.Attack(cell.entity);
             }
             else
             {
                 //先移动到附近
                 m_NextStepAttackEntity = cell.entity;
                 m_ControllerAction.Move(cell, attackDistance);
             }
         }
     }
 }
Exemple #3
0
    void GenerateHexMap()
    {
        List <HexCell> hexCellsList = new List <HexCell>();

        hexCellsList.Add(cells[0]);
        HexRoom hexRoom = new HexRoom(RegionType.NONE, hexRegions);

        rooms.Add(hexRoom);
        cells[0].initialize = true;
        cells[0].room       = hexRoom;
        hexRoom.AddCell(cells[0]);

        while (hexCellsList.Count > 0)
        {
            ProcessNextStep(hexCellsList);
        }

        List <int> roomIndexToRemove = null;

        if (removeSmallRegion)
        {
            roomIndexToRemove = new List <int> ();
            roomIndexToRemove = RemoveSmallRegions();
        }

        GenerateMeshForRooms(roomIndexToRemove);

        if (OnMapReady != null)
        {
            OnMapReady();
        }
    }
Exemple #4
0
 public void Triangulate(HexRoom room)
 {
     this.room = room;
     Clear();
     for (int i = 0; i < room.cells.Count; i++)
     {
         Triangulate(room.cells[i]);
     }
     Apply();
 }
Exemple #5
0
    /// <summary>
    /// Adds the cells of the given room to this room
    /// </summary>
    /// <param name="room">Room.</param>
    public void AddCellFromRoom(HexRoom room)
    {
        int totalCellsInRoom = room.cells.Count;

        for (int i = 0; i < totalCellsInRoom; i++)
        {
            HexCell cell = room.cells [i];
            AddCell(cell);
            cell.room = this;
        }
    }
Exemple #6
0
    public bool MoveTo(HexCell destination, int distance = 0)
    {
        if (isMoving)
        {
            Debug.LogError("当前正在移动,不可以发出新的移动指令");
            return(false);
        }

        List <HexCell> path = null;

        if (distance == 0)
        {
            HexRoom.HexCellPaths allPath = LevelManager.room.hexRoom.GetAvailableDestinations(entity.centerCell, m_PropertyComponent.movePoint);
            path = LevelManager.room.hexRoom.FindPath(allPath, entity.centerCell, destination);
            HexRoom.SortPathByDistance(path, destination);
        }
        else
        {
            //和目标保持一定的距离
            int targetDistance = entity.centerCell.GetDistance(destination);
            int moveDistance   = targetDistance - distance;
            moveDistance = moveDistance < 0 ? 0 : moveDistance;
            if (m_PropertyComponent.movePoint < moveDistance)
            {
                Debug.LogError("移动力不足 " + moveDistance);
                return(false);
            }

            HexRoom.HexCellPaths allPath = LevelManager.room.hexRoom.GetAvailableDestinations(entity.centerCell, targetDistance);
            path = LevelManager.room.hexRoom.FindPath(allPath, entity.centerCell, destination);
            HexRoom.SortPathByDistance(path, destination);
            for (int i = 0; i < distance; i++)
            {
                path.RemoveAt(path.Count - 1);
            }
        }

        m_MovingData = new MovingData(path, path[path.Count - 1], entity, m_PropertyComponent);
        if (m_MovingData.BeginMove())
        {
            onBeginMove.Invoke(destination);
            InvokeRepeating("OnMoving", 0f, Config.system.baseTimeDelta);
            return(true);
        }
        else
        {
            m_MovingData = null;
            return(false);
        }
    }
Exemple #7
0
 /// <summary>
 /// Refreshs the map.
 /// It will disable all the room except the room that you passed
 /// </summary>
 /// <param name="room">Room.</param>
 public void RefreshMap(HexRoom room)
 {
     foreach (HexRoom r in roomDictionary.Keys)
     {
         if (r == room)
         {
             roomDictionary [r].SetActive(true);
         }
         else
         {
             roomDictionary [r].SetActive(false);
         }
     }
 }
Exemple #8
0
    public void SetPassage(HexDirection direction, EdgeType type, HexRoom room)
    {
        edgeTypes[(int)direction] = type;

        if (!initialize)
        {
            initialize = true;
            if (room != null)
            {
                this.room = room;
            }
        }
        initializedEdgeCount++;
    }
Exemple #9
0
    /// <summary>
    /// Generates the mesh for each rooms.
    /// we ignore the rooms' which we merge
    /// </summary>
    /// <param name="roomIndexToIgnore">Room index to ignore.</param>
    void GenerateMeshForRooms(List <int> roomIndexToIgnore)
    {
        for (int i = 0; i < rooms.Count; i++)
        {
            if (roomIndexToIgnore != null && roomIndexToIgnore.Contains(i))
            {
                continue;
            }

            HexRoom room        = rooms [i];
            HexMesh hexRoomMesh = Instantiate(hexMeshPrefab, this.transform);
            hexRoomMesh.Triangulate(room);
            roomDictionary.Add(room, hexRoomMesh.gameObject);
        }
    }
Exemple #10
0
    /// <summary>
    /// 加载资源
    /// </summary>
    /// <param name="astarPath"></param>
    /// <returns></returns>
    IEnumerator LoadRoomModel()
    {
        if (m_Script != null && !m_Script.isInitialize)
        {
            //加载模型
            HexGeneratorcs generator     = new HexGeneratorcs();
            GameObject     movablePrefab = Resources.Load <GameObject>("HexTest/Hex0");
            GameObject[]   heightPrefab  = new GameObject[2];
            heightPrefab[0] = Resources.Load <GameObject>("HexTest/Hex1");
            heightPrefab[1] = Resources.Load <GameObject>("HexTest/Hex2");
            yield return(generator.GenerateGrid(15, 15, transform, movablePrefab, heightPrefab));

            hexRoom = new HexRoom(generator.result.Cells);
        }
    }
Exemple #11
0
 /// <summary>
 /// Adds the neighbour rooms to this room
 /// </summary>
 /// <param name="room">Room.</param>
 public void AddNeighbourRoomFrom(HexRoom room)
 {
     foreach (HexRoom r in room.neighbourRoom.ToArray())
     {
         if (!neighbourRoom.Contains(r) && r != this)
         {
             neighbourRoom.Add(r);
         }
         // make sure to remove the "room" from it's neighbour and add this to room r
         if (r.neighbourRoom.Contains(room))
         {
             r.neighbourRoom.RemoveAll(x => x == room);
             r.neighbourRoom.Add(this);
         }
     }
 }
Exemple #12
0
    public IEnumerator LoadContent(HexRoom model)
    {
        if (!isInitialize)
        {
            this.model = model;
            if (Config.system.loadContent)
            {
                //加载默认的Entity
                //预加载的资源
                yield return(LoadBatchContent(0, 0, 0, false));

                //载自定义的资源
                yield return(OnLoadContent());

                //完成
                yield return(1f);
            }
            else
            {
                yield return(1f);
            }
            isInitialize = true;
        }
    }
Exemple #13
0
 public static void FireOnUnselectCell(HexRoom room, HexCell cell)
 {
     onUnselectCell.Invoke(room, cell);
 }
Exemple #14
0
 void OnLeftClickCell(HexRoom room, HexCell cell)
 {
 }
Exemple #15
0
    List <int> RemoveSmallRegions()
    {
        /// we maintain a list of room which have combine so that we can actually ignore them from rendering
        List <int> indexRoomToIgnore = new List <int> ();

        foreach (HexRoom room in rooms)
        {
            if (indexRoomToIgnore.Contains((rooms.FindIndex(r => r == room))))
            {
                continue;
            }

            HexRoom currentRoom = room;
            /// add the neighbour rooms until the combination of room has minimum cell count
            while (currentRoom.cells.Count <= minCellInRoom)
            {
                /// find the neighbour with max cell so that we do not go in merge room after room
                HexRoom neighbourRoomWithMaxCell = currentRoom.GetNeighbourWIthMaxCells();
                /// add all the cell's neighbour rooms to current cell room
                neighbourRoomWithMaxCell.AddNeighbourRoomFrom(currentRoom);
                /// also add all of it's cell
                neighbourRoomWithMaxCell.AddCellFromRoom(currentRoom);

                indexRoomToIgnore.Add(rooms.FindIndex(r => r == currentRoom));
                /// we remove the index from the indexroomtoignore list if the neighbourRoomWithMaxCell room index is in that list
                if (indexRoomToIgnore.Contains(rooms.FindIndex(r => r == neighbourRoomWithMaxCell)))
                {
                    indexRoomToIgnore.Remove(rooms.FindIndex(r => r == neighbourRoomWithMaxCell));
                }
                currentRoom = neighbourRoomWithMaxCell;
            }

            /// we remove unwanted walls and door from the combine rooms
            foreach (HexCell cell in currentRoom.cells)
            {
                for (int i = 0; i < 6; i++)
                {
                    HexDirection direction = (HexDirection)i;
                    HexCell      neighbour = cell.GetNeighbor(direction);
                    if (neighbour != null)
                    {
                        if (neighbour.room.region.type == cell.room.region.type)
                        {
                            cell.SetPassage(direction, EdgeType.PASSAGE, cell.room);
                            neighbour.SetPassage(direction.Opposite(), EdgeType.PASSAGE, cell.room);
                        }
                        else
                        {
                            if (cell.edgeTypes [i] == EdgeType.PASSAGE)
                            {
                                cell.SetPassage(direction, EdgeType.WALL, cell.room);
                                neighbour.SetPassage(direction.Opposite(), EdgeType.WALL, cell.room);
                            }
                        }
                    }
                }
            }
        }

        return(indexRoomToIgnore);
    }
Exemple #16
0
    /// Maze Generation
    /// if we visit  cell for the first time  we will add it to our list and remove it form the last index (same as stack) once's it is fully initialize
    /// if it is visited first time we either create passage or door's
    /// if visited second time we check if it has same region type if so we create a passage also if both have different room we merge them
    /// else it has differnet region so we add wall
    /// if it doesn't have neighbour we simmple put walls
    ///
    void ProcessNextStep(List <HexCell> hexCellsList)
    {
        HexCell currentCell = hexCellsList[hexCellsList.Count - 1];

        if (currentCell.IsFullyInitialize == true)
        {
            hexCellsList.RemoveAt(hexCellsList.Count - 1);
            return;
        }
        /// we should avoid the direction which is already initialize with some type wall, door or passage so that
        /// we can make sure that all of it direction's are visited
        HexDirection randomDirection = currentCell.GetRandomUnVisitedDirection();
        HexCell      neighbour       = currentCell.GetNeighbor(randomDirection);

        if (neighbour != null)
        {
            if (!neighbour.initialize)
            {
                if (Random.value <= passagePercent)
                {
                    currentCell.SetPassage(randomDirection, EdgeType.PASSAGE, currentCell.room);
                    neighbour.SetPassage(randomDirection.Opposite(), EdgeType.PASSAGE, currentCell.room);
                    currentCell.room.AddCell(currentCell.GetNeighbor(randomDirection));
                }
                else
                {
                    HexRoom nextRoom = new HexRoom(currentCell.room.region.type, hexRegions);
                    currentCell.SetPassage(randomDirection, EdgeType.DOOR, currentCell.room);
                    neighbour.SetPassage(randomDirection.Opposite(), EdgeType.DOOR, nextRoom);

                    currentCell.room.AddNeighbour(nextRoom);
                    nextRoom.AddNeighbour(currentCell.room);

                    rooms.Add(nextRoom);
                    nextRoom.AddCell(neighbour);
                }
                hexCellsList.Add(neighbour);
            }
            else if (currentCell.room.region.type == neighbour.room.region.type)
            {
                currentCell.SetPassage(randomDirection, EdgeType.PASSAGE, currentCell.room);
                neighbour.SetPassage(randomDirection.Opposite(), EdgeType.PASSAGE, currentCell.room);
                if (currentCell.room != neighbour.room)
                {
                    rooms.Remove(neighbour.room);
                    /// add all the cell's neighbour rooms to current cell room
                    currentCell.room.AddNeighbourRoomFrom(neighbour.room);
                    /// also add all of it's cell
                    currentCell.room.AddCellFromRoom(neighbour.room);
                }
            }
            else
            {
                currentCell.SetPassage(randomDirection, EdgeType.WALL, null);
                neighbour.SetPassage(randomDirection.Opposite(), EdgeType.WALL, null);
            }
        }
        else
        {
            currentCell.SetPassage(randomDirection, EdgeType.WALL, null);
        }
    }
Exemple #17
0
 public static void FireOnRightClickCell(HexRoom room, HexCell cell)
 {
     onRightClickCell.Invoke(room, cell);
 }