Ejemplo n.º 1
0
    public void HalfHighlight(HexUnit src)
    {
        if(!isReserved) {
            isHalfHighlight = true;
            halfHighlightSource = src;

            StopCoroutine("CheckHighlightSource");
            myRenderer.material.color = halfHighlightColor;
            StartCoroutine("CheckHighlightSource");
        }
    }
Ejemplo n.º 2
0
 public void FindPathTo(HexCell fromCell, HexCell toCell, HexUnit hexUnit, List <HexCell> results)
 {
     //StopAllCoroutines();
     //StartCoroutine(Search(fromCell,toCell,speed));
     Search(fromCell, toCell, hexUnit, results);
 }
Ejemplo n.º 3
0
    bool Search(HexCell fromCell, HexCell toCell, HexUnit unit)
    {
        int speed = unit.Speed;

        searchFrontierPhase += 2;
        if (searchFrontier == null)
        {
            searchFrontier = new HexCellPriorityQueue();
        }
        else
        {
            searchFrontier.Clear();
        }

        fromCell.SearchPhase = searchFrontierPhase;
        fromCell.Distance    = 0;
        searchFrontier.Enqueue(fromCell);
        while (searchFrontier.Count > 0)
        {
            HexCell current = searchFrontier.Dequeue();
            current.SearchPhase += 1;

            if (current == toCell)
            {
                return(true);
            }

            int currentTurn = (current.Distance - 1) / speed;

            for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
            {
                HexCell neighbor = current.GetNeighbor(d);
                if (
                    neighbor == null ||
                    neighbor.SearchPhase > searchFrontierPhase
                    )
                {
                    continue;
                }
                if (!unit.IsValidDestination(neighbor))
                {
                    continue;
                }
                int moveCost = unit.GetMoveCost(current, neighbor, d);
                if (moveCost < 0)
                {
                    continue;
                }

                int distance = current.Distance + moveCost;
                int turn     = (distance - 1) / speed;
                if (turn > currentTurn)
                {
                    distance = turn * speed + moveCost;
                }

                if (neighbor.SearchPhase < searchFrontierPhase)
                {
                    neighbor.SearchPhase     = searchFrontierPhase;
                    neighbor.Distance        = distance;
                    neighbor.PathFrom        = current;
                    neighbor.SearchHeuristic =
                        neighbor.coordinates.DistanceTo(toCell.coordinates);
                    searchFrontier.Enqueue(neighbor);
                }
                else if (distance < neighbor.Distance)
                {
                    int oldPriority = neighbor.SearchPriority;
                    neighbor.Distance = distance;
                    neighbor.PathFrom = current;
                    searchFrontier.Change(neighbor, oldPriority);
                }
            }
        }
        return(false);
    }
Ejemplo n.º 4
0
    /// <summary>
    /// Search this HexGrid.
    /// </summary>
    /// <param name="start"></param>
    /// <param name="end"></param>
    /// <param name="unit"></param>
    /// <returns></returns>
    private IEnumerable <HexEdge> AStarSearch(
        Hex start,
        Hex end,
        HexUnit unit,
        HexAdjacencyGraph graph
        )
    {
        IEnumerable <HexEdge> result;

//        AlgorithmExtensions.ShortestPathsAStar<Hex, HexEdge>(
//            graph,
//            GetPathfindingEdgeWeight,
//            GetPathfindingHeursitic,
//            start
//        ).Invoke(end, out result);

        return(null);

/*        int speed = unit.Speed;
 *
 *      _searchFrontierPhase += 2;
 *
 *      if (_searchFrontier == null)
 *      {
 *          _searchFrontier = new HexPriorityQueue();
 *      }
 *      else
 *      {
 *          _searchFrontier.Clear();
 *      }
 *
 * // Temporarily using a list instead of a priority queue.
 * // Should optimize this later.
 * //
 *      start.SearchPhase = _searchFrontierPhase;
 *      start.Distance = 0;
 *      _searchFrontier.Enqueue(start);
 *
 *      while (_searchFrontier.Count > 0)
 *      {
 *          Hex current = _searchFrontier.Dequeue();
 *          current.SearchPhase += 1;
 *
 *          if (current == end)
 *          {
 *              return true;
 *          }
 *
 *          int currentTurn = (current.Distance - 1) / speed;
 *
 *          for (HexDirection direction = HexDirection.Northeast; direction <= HexDirection.Northwest; direction++)
 *          {
 *              Hex neighbor = current.GetNeighbor(direction);
 *
 *              if
 *              (
 *                  neighbor == null ||
 *                  neighbor.SearchPhase > _searchFrontierPhase
 *              )
 *              {
 *                  continue;
 *              }
 *
 *              if (!unit.IsValidDestination(neighbor))
 *              {
 *                  continue;
 *              }
 *
 *              int moveCost = unit.GetMoveCost(current, neighbor, direction);
 *
 *              if (moveCost < 0)
 *              {
 *                  continue;
 *              }
 *
 * // Wasted movement points are factored into the cost of hexes outside
 * // the boundary of the first turn by adding the turn number multiplied
 * // by the speed plus the cost to move into the hex outside the boundary
 * // of the first turn. This method ensures that the the distances with
 * // which the algorithm is using to calculate the best path take into
 * // account wasted movement points.
 * //
 *              int distance = current.Distance + moveCost;
 *              int turn = (distance - 1) / speed;
 *
 *              if (turn > currentTurn) {
 *                  distance = turn * speed + moveCost;
 *              }
 *
 *              if (neighbor.SearchPhase < _searchFrontierPhase) {
 *                  neighbor.SearchPhase = _searchFrontierPhase;
 *                  neighbor.Distance = distance;
 *                  neighbor.PathFrom = current;
 *                  neighbor.SearchHeuristic =
 *                      neighbor.Coordinates.DistanceTo(end.Coordinates);
 *
 *                  _searchFrontier.Enqueue(neighbor);
 *              }
 *              else if (distance < neighbor.Distance) {
 *                  int oldPriority = neighbor.SearchPriority;
 *                  neighbor.Distance = distance;
 *                  neighbor.PathFrom = current;
 *                  _searchFrontier.Change(neighbor, oldPriority);
 *              }
 *          }
 *      }
 *
 *      return false;
 */
    }
Ejemplo n.º 5
0
 public void RemoveUnit(HexUnit unit)
 {
     _units.Remove(unit);
     unit.Die();
 }
Ejemplo n.º 6
0
    public bool RemoveUnit(HexUnit unit)
    {
        bool success = units.Remove(unit);

        return(success);
    }
Ejemplo n.º 7
0
 public void RemoveUnit(HexUnit unit)
 {
     units.Remove(unit);
 }
Ejemplo n.º 8
0
    /// <summary>
    /// 搜索
    /// </summary>
    /// <param name="fromCell"></param>
    /// <param name="toCell"></param>
    /// <param name="speed"></param>
    /// <returns></returns>
    bool Search(HexCell fromCell, HexCell toCell, HexUnit unit)
    {
        int speed = unit.Speed;

        searchFrontierPhase += 2;

        if (searchFrontier == null)
        {
            searchFrontier = new HexCellPriorityQueue();
        }
        else
        {
            searchFrontier.Clear();
        }

        fromCell.SearchPhase = searchFrontierPhase;
        fromCell.Distance    = 0;
        searchFrontier.Enqueue(fromCell);

        while (searchFrontier.Count > 0)
        {
            HexCell current = searchFrontier.Dequeue();
            current.SearchPhase += 1;

            if (current == toCell)
            {
                return(true);
            }

            int currentTurn = (current.Distance - 1) / speed;

            for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
            {
                HexCell neighbor = current.GetNeighbor(d);

                if (
                    neighbor == null ||
                    neighbor.SearchPhase > searchFrontierPhase
                    )
                {
                    continue;
                }
                //if (neighbor.IsUnderwater || neighbor.Unit)
                //{
                //    continue;
                //}

                //int moveCost;
                //HexEdgeType edgeType = current.GetEdgeType(neighbor);

                //if (edgeType == HexEdgeType.Cliff)
                //{
                //    continue;
                //}
                //if (current.HasRoadThroughEdge(d))
                //{
                //    moveCost = 1;
                //}
                //else if (current.Walled != neighbor.Walled)
                //{
                //    continue;
                //}
                //else
                //{
                //    moveCost = edgeType == HexEdgeType.Flat ? 5 : 10;
                //    moveCost += neighbor.UrbanLevel + neighbor.FarmLevel + neighbor.PlantLevel;
                //}

                if (!unit.IsValidDestination(neighbor))
                {
                    continue;
                }
                int moveCost = unit.GetMoveCost(current, neighbor, d);
                if (moveCost < 0)
                {
                    continue;
                }

                int distance = current.Distance + moveCost;
                int turn     = (distance - 1) / speed;
                if (turn > currentTurn)
                {
                    distance = turn * speed + moveCost;
                }

                if (neighbor.SearchPhase < searchFrontierPhase)
                {
                    neighbor.SearchPhase     = searchFrontierPhase;
                    neighbor.Distance        = distance;
                    neighbor.PathFrom        = current;
                    neighbor.SearchHeuristic = neighbor.coordinates.DistanceTo(toCell.coordinates);
                    searchFrontier.Enqueue(neighbor);
                }
                else if (distance < neighbor.Distance)
                {
                    int oldPriority = neighbor.SearchPriority;
                    neighbor.Distance = distance;
                    neighbor.PathFrom = current;
                    searchFrontier.Change(neighbor, oldPriority);
                }
            }
        }
        return(false);
    }
Ejemplo n.º 9
0
    /// <summary>
    ///
    /// </summary>
    /// <param name="roomId">房间id</param>
    /// <param name="ownerId">所属玩家id</param>
    /// <param name="actorId">自己的id</param>
    /// <param name="posX"></param>
    /// <param name="posZ"></param>
    /// <param name="orientation"></param>
    /// <param name="unitName"></param>
    /// <param name="cellIndex">该单位在地形块HexCell中的Index,因为根据PosX,PosZ可能得不到正确的cell,只能用这个数据确保正确</param>
    /// <param name="actorInfoId">兵种ID,在actor_info表中的id</param>
    /// <param name="name"></param>
    /// <param name="hp"></param>
    /// <param name="attackPower"></param>
    /// <param name="defencePower"></param>
    /// <param name="speed"></param>
    /// <param name="filedOfVision"></param>
    /// <param name="shootingRange"></param>
    /// <param name="attackDuration"></param>
    /// <param name="attackInterval"></param>
    /// <param name="ammoBase"></param>
    /// <param name="ammoBaseMax"></param>
    /// <returns></returns>
    public ActorVisualizer CreateUnit(long roomId, long ownerId, long actorId, int posX, int posZ, float orientation,
                                      string unitName, int cellIndex, int actorInfoId,
                                      string name, int hp, int hpMax, float attackPower, float defencePower, float speed, float filedOfVision, float shootingRange,
                                      float attackDuration, float attackInterval, int ammoBase, int ammoBaseMax)
    {
        HexCell cell = GetCell(cellIndex);

        if (!cell)
        {
            GameRoomManager.Instance.Log($"HexmapHelper CreateUnit :创建Actor失败!格子越界 - <{posX},{posZ}> - {unitName}");
            return(null);
        }

        if (cellIndex == 0)
        {
            Debug.LogError("HexmapHelper CreateUnit Error - CellIndex is lost!!!");
            return(null);
        }

        if (cell.Unit)
        {
            HexCell newCell = null;
            for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
            {
                HexCell neighbor = cell.GetNeighbor(d);
                if (neighbor && !neighbor.Unit)
                {
                    newCell = neighbor;
                    break;
                }
            }

            if (newCell)
            {
                GameRoomManager.Instance.Log($"HexmapHelper :创建Actor失败!物体位置重合了,重新放置! - 原坐标:<{posX},{posZ}> - 新坐标:<{newCell.coordinates.X},{newCell.coordinates.Z}> - {unitName}");
                cell = newCell;
            }
            else
            {
                GameRoomManager.Instance.Log($"HexmapHelper :创建Actor失败!原来这个格子没有物体,现在有了物体, 附近也没有空地! - <{posX},{posZ}> - {unitName}");
                return(null);
            }
        }

        // 区分颜色
        string newUnitName = unitName;

        if (ownerId != GameRoomManager.Instance.CurrentPlayer.TokenId)
        { // 如果不是自己,蓝色就换成红色(绿色换成黄色)
            if (unitName.IndexOf("BLUE") > 0)
            {
                newUnitName = unitName.Replace("BLUE", "RED");
            }
            else if (unitName.IndexOf("GREEN") > 0)
            {
                newUnitName = unitName.Replace("GREEN", "YELLOW");
            }
        }

        string unitPathName = $"Arts/BeffioPrefabs/Soldiers/{newUnitName}";
        var    go           = Resources.Load <HexUnit>(unitPathName);

        if (go != null)
        {
            HexUnit hu = Instantiate(go);
            if (hu != null)
            {
                hexGrid.AddUnit(hu, cell, orientation);
                var av = hu.GetComponent <ActorVisualizer>();
                if (av != null)
                {
                    av.RoomId      = roomId;
                    av.OwnerId     = ownerId;
                    av.ActorId     = actorId;
                    av.PosX        = posX;
                    av.PosZ        = posZ;
                    av.Orientation = orientation;
                    av.Species     = unitName;
                    av.CellIndex   = cellIndex;
                    av.ActorInfoId = actorInfoId;

                    av.Name          = name;
                    av.Hp            = hp;
                    av.HpMax         = hpMax;
                    av.AttackPower   = attackPower;
                    av.DefencePower  = defencePower;
                    av.Speed         = speed;
                    av.FieldOfVision = filedOfVision;
                    av.ShootingRange = shootingRange;

                    av.AttackDuration = attackDuration;
                    av.AttackInterval = attackInterval;
                    av.AmmoBase       = ammoBase;
                    av.AmmoBaseMax    = ammoBaseMax;
                }

                // 关闭预制件上没用的东西,看以后这东西能否用得上,如果没用,就完全干掉
                if (hu.GetComponentInChildren <ThirdPersonUserControl>())
                {
                    hu.GetComponentInChildren <ThirdPersonUserControl>().enabled = false;
                }
                if (hu.GetComponentInChildren <ThirdPersonCharacter>())
                {
                    hu.GetComponentInChildren <ThirdPersonCharacter>().enabled = false;
                }
                if (hu.GetComponentInChildren <CapsuleCollider>())
                {
                    hu.GetComponentInChildren <CapsuleCollider>().enabled = false;
                }
                EnableFollowCamera(av, false);

                if (!GameRoomManager.Instance.RoomLogic.ActorManager.AllActors.ContainsKey(actorId))
                {
                    ActorBehaviour ab = new ActorBehaviour()
                    {
                        RoomId      = roomId,
                        OwnerId     = ownerId,
                        ActorId     = actorId,
                        PosX        = posX,
                        PosZ        = posZ,
                        Orientation = orientation,
                        Species     = unitName,
                        CellIndex   = cellIndex,
                        ActorInfoId = actorInfoId,

                        Name          = name,
                        Hp            = hp,
                        HpMax         = hpMax,
                        AttackPower   = attackPower,
                        DefencePower  = defencePower,
                        Speed         = speed,
                        FieldOfVision = filedOfVision,
                        ShootingRange = shootingRange,

                        AttackDuration = attackDuration,
                        AttackInterval = attackInterval,
                        AmmoBase       = ammoBase,
                        AmmoBaseMax    = ammoBaseMax,
                    };
                    GameRoomManager.Instance.RoomLogic.ActorManager.AddActor(ab, hu);
                }
                GameRoomManager.Instance.Log($"MSG: CreateATroopReply - 创建了一个Actor - {unitName}");
                return(av);
            }
        }

        return(null);
    }
Ejemplo n.º 10
0
 void RefreshUnitVision(HexUnit unit)
 {
     unit.enabled = false;       // Disable the unit
     unit.enabled = true;        // Then enable to trigger on Enabled which triggers vision
 }
Ejemplo n.º 11
0
    private bool Search(HexCell fromCell, HexCell toCell, HexUnit unit)
    {
        int speed = unit.Speed;

        this.searchFrontierPhase += 2;

        if (this.searchFrontier == null)
        {
            this.searchFrontier = new HexCellPriorityQueue();
        }
        else
        {
            this.searchFrontier.Clear();
        }

        fromCell.EnableHighlight(Color.blue);
        fromCell.SearchPhase = this.searchFrontierPhase;
        fromCell.Distance    = 0;
        this.searchFrontier.Enqueue(fromCell);

        while (this.searchFrontier.Count > 0)
        {
            var current = this.searchFrontier.Dequeue();
            current.SearchPhase += 1;

            if (current == toCell)
            {
                return(true);
            }

            var currentTurn = (current.Distance - 1) / speed;

            for (var d = HexDirection.NE; d <= HexDirection.NW; d++)
            {
                var neighbor = current.GetNeighbor(d);

                if ((neighbor == null) || (neighbor.SearchPhase > this.searchFrontierPhase) || neighbor.Unit)
                {
                    continue;
                }

                if (!unit.IsValidDestination(neighbor))
                {
                    continue;
                }

                int moveCost = unit.GetMoveCost(current, neighbor, d);

                if (moveCost < 0)
                {
                    continue;
                }

                var distance = current.Distance + moveCost;
                var turn     = (distance - 1) / speed;

                if (turn > currentTurn)
                {
                    distance = turn * speed + moveCost;
                }

                if (neighbor.SearchPhase < this.searchFrontierPhase)
                {
                    neighbor.SearchPhase     = this.searchFrontierPhase;
                    neighbor.Distance        = distance;
                    neighbor.PathFrom        = current;
                    neighbor.SearchHeuristic = neighbor.coordinates.DistanceTo(toCell.coordinates);
                    this.searchFrontier.Enqueue(neighbor);
                }
                else if (distance < neighbor.Distance)
                {
                    var oldPriority = neighbor.SearchPriority;
                    neighbor.Distance = distance;
                    neighbor.PathFrom = current;
                    this.searchFrontier.Change(neighbor, oldPriority);
                }
            }
        }

        return(false);
    }
Ejemplo n.º 12
0
 public void RemoveUnit(HexUnit hexUnit)
 {
     hexUnits.Remove(hexUnit);
     hexUnits = hexUnits.OrderBy(u => u.HexUnitType).ToList();
     UpdateVision();
 }
Ejemplo n.º 13
0
 public void AddUnit(HexUnit hexUnit)
 {
     hexUnits.Add(hexUnit);
     hexUnits = hexUnits.OrderBy(u => u.HexUnitType).ToList();
     UpdateVision();
 }
Ejemplo n.º 14
0
 public Vector3 HexUnit2World(HexUnit u)
 {
     return(oneFloor.CellToWorld(mh.sqYXZHex2UnityYXZHex(mh.Z3Hex2sqYXZHex(u))));
 }
Ejemplo n.º 15
0
 public bool HasUnit(HexUnit unit)
 {
     return(units.Contains(unit));
 }
Ejemplo n.º 16
0
    void Search(HexCell fromCell, HexCell toCell, HexUnit hexUnit, List <HexCell> results)
    {
        m_currentPathFrom = fromCell;
        m_currentPathTo   = toCell;

        int speed = hexUnit.Speed;

        m_searchFrontierPhase = 2;
        ClearPath();
        ResetSearchPhase();

        for (int i = 0; i < m_cells.Count; i++)
        {
            m_cells[i].SetLabel(null);
            m_cells[i].DisableHighlight();
        }

        WaitForSeconds delay = new WaitForSeconds(1 / 60f);

        fromCell.Distance    = 0;
        fromCell.SearchPhase = m_searchFrontierPhase;
        m_searchQueue.Enqueue(fromCell);
        while (m_searchQueue.Count > 0)
        {
            //yield return delay;s
            HexCell current = m_searchQueue.Dequeue();
            current.SearchPhase += 1;

            if (current == toCell)
            {
                BuildFinalPath(fromCell, toCell);
                if (results != null && m_finalPath.Count > 0)
                {
                    results.AddRange(m_finalPath);
                }
                break;
            }

            int currentTurn = (current.Distance - 1) / speed;

            for (HexDirection dir = HexDirection.NE; dir <= HexDirection.NW; dir++)
            {
                HexCell neighbour = current.GetNeighbour(dir);

                if (neighbour == null || neighbour.SearchPhase > m_searchFrontierPhase)
                {
                    continue;
                }

                if (!hexUnit.IsValidDestination(neighbour))
                {
                    continue;
                }

                int moveCost = hexUnit.GetMoveCost(current, neighbour, dir);
                if (moveCost < 0)
                {
                    continue;
                }

                int distance = current.Distance;

                int newDistance = distance + moveCost;

                int turn = (newDistance - 1) / speed;
                if (turn > currentTurn)
                {
                    newDistance = (turn * speed) + moveCost;
                }


                if (neighbour.SearchPhase < m_searchFrontierPhase)
                {
                    neighbour.SearchPhase = m_searchFrontierPhase;
                    neighbour.Distance    = newDistance;
                    SetPathCurrentNext(current, neighbour);
                    neighbour.SearchHeuristic = neighbour.Coordinates.DistanceTo(toCell.Coordinates);
                    m_searchQueue.Enqueue(neighbour);
                }
                else if (newDistance < neighbour.Distance)
                {
                    neighbour.Distance = newDistance;
                    SetPathCurrentNext(current, neighbour);
                    m_searchQueue.Change(neighbour);
                }
            }
        }


        //for (int i = 0; i < m_cells.Count; i++)
        //{
        //    yield return delay;
        //    m_cells[i].Distance = cell.Coordinates.DistanceTo(m_cells[i].Coordinates);
        //}
    }
Ejemplo n.º 17
0
 public void RemoveUnit(HexUnit unit)
 {
     m_unitsList.Remove(unit);
     unit.Die();
 }
Ejemplo n.º 18
0
 public void UpdateTileByStep(HexUnit u)
 {
     UpdateTileByStepOverGroundHorizon(u);
     generatedCenter += u;
     SetTileOverGround(generatedCenter, sightRadius - 1);
 }
Ejemplo n.º 19
0
 private void DisablePOIInteraction(HexUnit unitMoved)
 {
     openPOIButton.SetActive(false);
 }
Ejemplo n.º 20
0
 private void CloseBoardingView(HexUnit unitMoved)
 {
     shipInspectPanel.SetActive(false);
 }
Ejemplo n.º 21
0
 public void DestroyUnit(HexUnit unit)
 {
     gameController.DestroyUnit(unit.GetComponent <Unit>());
 }
Ejemplo n.º 22
0
 public void CancelSelection()
 {
     selectedUnit = null;
 }