Example #1
0
 public static int CheckMoveDistance(GridElement element)
 {
     if (element.PathParentField != null)
     {
         return(Mathf.Abs(element.ElementIndex.x - element.PathParentField.ElementIndex.x) +
                Mathf.Abs(element.ElementIndex.y - element.PathParentField.ElementIndex.y) +
                Mathf.Abs(element.ElementIndex.z - element.PathParentField.ElementIndex.z));
     }
     else
     {
         return(0);
     }
 }
Example #2
0
        private static bool FragmentsContaisPosition(String direction, GridElement leftUp, GridElement rightDown, Vector3 position)
        {
            switch (direction)
            {
            case "ru":
                if (position.x >= leftUp.transform.position.x && position.x <= rightDown.transform.position.x &&
                    position.y >= rightDown.transform.position.y && position.y <= leftUp.transform.position.y)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }

            case "rd":
                if (position.x >= leftUp.transform.position.x && position.x <= rightDown.transform.position.x &&
                    position.y <= leftUp.transform.position.y && position.y >= rightDown.transform.position.y)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }

            case "lu":
                if (position.x <= rightDown.transform.position.x && position.x >= leftUp.transform.position.x &&
                    position.y >= rightDown.transform.position.y && position.y <= leftUp.transform.position.y)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }

            case "ld":
                if (position.x <= rightDown.transform.position.x && position.x >= leftUp.transform.position.x &&
                    position.y <= leftUp.transform.position.y && position.y >= rightDown.transform.position.y)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(false);
        }
Example #3
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            GridElement objAsGridElement = obj as GridElement;

            if (objAsGridElement == null)
            {
                return(false);
            }
            return(Equals(objAsGridElement));
        }
Example #4
0
 private static void CheckNeighbourHelper(Grid grid, IntVector3 index)
 {
     if (IsIndexInGrid(grid, index))
     {
         GridElement element = grid.Elements[index.x, index.y, index.z];
         if (element.Walkable && !_closedElements.Contains(element))
         {
             if (!_openElements.Contains(element))
             {
                 element.PathParentField = _currentElement;
                 _openElements.Add(element);
             }
             else
             {
                 element.CheckNewParent(_currentElement);
             }
             element.CalculateHeuristic(_targetElement);
         }
     }
 }
Example #5
0
        private static GridElement FindClosestElement(AStarAgent agent, Vector3 position)
        {
            try
            {
                //GridElement closestElement = null;
                //int xMax, xMin, yMax,yMin/*, zMax, zMin*/;
                //xMax = agent.MyGrid.GridSize.y;
                //yMax = agent.MyGrid.GridSize.x;
                ////zMax = agent.MyGrid.GridSize.z;


                //xMin =  yMin =/* zMin =*/ 0;

                switch (agent.MyGrid.GridType)
                {
                case GridType.TwoDimensional:
                {
                    GridElement closest = null;
                    float       dist    = float.MaxValue;
                    foreach (GridElement ele in agent.MyGrid.Elements)
                    {
                        float currentDist = Vector3.Distance(position, ele.transform.position);
                        if (currentDist < dist)
                        {
                            dist    = currentDist;
                            closest = ele;
                        }
                    }
                    return(closest);

                    //List<GridElement> leftPoints = new List<GridElement>();
                    //List<GridElement> middlePoints = new List<GridElement>();
                    //List<GridElement> rightPoints = new List<GridElement>();

                    //leftPoints.Add(agent.MyGrid.Elements[xMin, yMin, 0]);
                    //leftPoints.Add(agent.MyGrid.Elements[xMin, yMax / 2, 0]);
                    //leftPoints.Add(agent.MyGrid.Elements[xMin, yMax - 1, 0]);

                    //middlePoints.Add(agent.MyGrid.Elements[xMax / 2, yMin, 0]);
                    //middlePoints.Add(agent.MyGrid.Elements[xMax / 2, yMax / 2, 0]);
                    //middlePoints.Add(agent.MyGrid.Elements[xMax / 2, yMax - 1, 0]);

                    //rightPoints.Add(agent.MyGrid.Elements[xMax - 1, 0, 0]);
                    //rightPoints.Add(agent.MyGrid.Elements[xMax - 1, yMax / 2, 0]);
                    //rightPoints.Add(agent.MyGrid.Elements[xMax - 1, yMax - 1, 0]);

                    //bool flag = true;

                    //int iterations = 0;
                    //while (flag)
                    //{
                    //    ++iterations;
                    //    if (Mathf.Abs(leftPoints[0].ElementIndex.x - rightPoints[0].ElementIndex.x) > 1 || Mathf.Abs(leftPoints[0].ElementIndex.y - rightPoints[2].ElementIndex.y) > 1)
                    //        flag = true;
                    //    else
                    //        flag = false;

                    //    bool contains = false;

                    //    if (FragmentsContaisPosition(agent.GridDirection, leftPoints[0], middlePoints[1], position))
                    //    {
                    //        xMax = middlePoints[1].ElementIndex.x;
                    //        yMax = middlePoints[1].ElementIndex.y;
                    //        contains = true;
                    //    }
                    //    else if (FragmentsContaisPosition(agent.GridDirection, middlePoints[0], rightPoints[1], position))
                    //    {
                    //        xMin = middlePoints[1].ElementIndex.x;
                    //        yMax = middlePoints[1].ElementIndex.y;
                    //        contains = true;
                    //    }
                    //    else if (FragmentsContaisPosition(agent.GridDirection, leftPoints[1], middlePoints[2], position))
                    //    {
                    //        xMax = middlePoints[1].ElementIndex.x;
                    //        yMin = middlePoints[1].ElementIndex.y;
                    //        contains = true;
                    //    }
                    //    else if (FragmentsContaisPosition(agent.GridDirection, middlePoints[1], rightPoints[2], position))
                    //    {
                    //        xMin = middlePoints[1].ElementIndex.x;
                    //        yMin = middlePoints[1].ElementIndex.y;
                    //        contains = true;
                    //    }
                    //    if (!contains || iterations > agent.MyGrid.Elements.Length)
                    //        break;

                    //    leftPoints.Clear();
                    //    middlePoints.Clear();
                    //    rightPoints.Clear();

                    //    leftPoints.Add(agent.MyGrid.Elements[xMin, yMin, 0]);
                    //    leftPoints.Add(agent.MyGrid.Elements[xMin, yMax - Mathf.CeilToInt((yMax - yMin) / 2f), 0]);
                    //    leftPoints.Add(agent.MyGrid.Elements[xMin, yMax - 1, 0]);

                    //    middlePoints.Add(agent.MyGrid.Elements[xMax - Mathf.CeilToInt((xMax - xMin) / 2f), yMin, 0]);
                    //    middlePoints.Add(agent.MyGrid.Elements[xMax - Mathf.CeilToInt((xMax - xMin) / 2f), yMax - Mathf.CeilToInt((yMax - yMin) / 2f), 0]);
                    //    middlePoints.Add(agent.MyGrid.Elements[xMax - Mathf.CeilToInt((xMax - xMin) / 2f), yMax - 1, 0]);

                    //    rightPoints.Add(agent.MyGrid.Elements[xMax - 1, 0, 0]);
                    //    rightPoints.Add(agent.MyGrid.Elements[xMax - 1, yMax - Mathf.CeilToInt((yMax - yMin) / 2f), 0]);
                    //    rightPoints.Add(agent.MyGrid.Elements[xMax - 1, yMax - 1, 0]);
                    //}

                    //float minDist = Vector3.Distance(leftPoints[0].transform.position, position);
                    //closestElement = leftPoints[0];
                    //float curDist = Vector3.Distance(rightPoints[0].transform.position, position);
                    //if (minDist > curDist)
                    //{
                    //    minDist = curDist;
                    //    closestElement = rightPoints[0];
                    //}
                    //curDist = Vector3.Distance(leftPoints[2].transform.position, position);
                    //if (minDist > curDist)
                    //{
                    //    minDist = curDist;
                    //    closestElement = leftPoints[2];
                    //}
                    //curDist = Vector3.Distance(rightPoints[2].transform.position, position);
                    //if (minDist > curDist)
                    //{
                    //    closestElement = rightPoints[2];
                    //}
                    //return closestElement;
                }

                case GridType.ThreeDimensional:
                {
                    Debug.LogError("A* 3D not implemented");
                    if (agent.MyGrid.GridType == GridType.ThreeDimensional)
                    {
                        throw new NotImplementedException();
                    }
                }
                break;
                }
            }
            catch (NotImplementedException)
            {
                return(null);
            }
            return(null);
        }
Example #6
0
 public static void GetTargetElement(AStarAgent agent, Vector3 position)
 {
     _targetElement = FindClosestElement(agent, position);
 }
Example #7
0
 public static void GetStaringElement(AStarAgent agent, Vector3 position)
 {
     _startElement = FindClosestElement(agent, position);
 }
Example #8
0
        public virtual void GenerateGrid()
        {
            _elementMoveCostDirection = new Vector3((_gridElementSize.x + _gridElementSize.y) / 2f, Mathf.Sqrt(Mathf.Pow(_gridElementSize.x, 2f) + Mathf.Pow(_gridElementSize.y, 2f)),
                                                    Mathf.Sqrt(Mathf.Pow(_gridElementSize.x, 2f) + Mathf.Pow(_gridElementSize.y, 2f) + Mathf.Pow(_gridElementSize.z, 2f)));

            switch (_gridType)
            {
            case GridType.TwoDimensional:
            {
                int x = _gridSize.x;
                int y = _gridSize.y;
                _elements = new GridElement[x, y, 1];

                for (int i = 0; i < x; ++i)
                {
                    for (int j = 0; j < y; ++j)
                    {
                        GameObject element;
                        if (_gridElementObject == null)
                        {
                            element = new GameObject();
                        }
                        else
                        {
                            element =
                                Instantiate(_gridElementObject, Vector3.zero, Quaternion.identity) as GameObject;
                        }
                        element.name = string.Format("GridElement[{0}][{1}]", j, i);

                        if (_useMyPositionAsStart)
                        {
                            Vector3 startPosition = gameObject.transform.position;
                            element.transform.position =
                                new Vector3(startPosition.x + (_gridElementSize.x * _generateDirection.x * j),
                                            startPosition.y + (_gridElementSize.y * _generateDirection.y * i),
                                            startPosition.z);
                        }
                        else
                        {
                            element.transform.position =
                                new Vector3(_startPosition.x + (_gridElementSize.x * _generateDirection.x * j),
                                            _startPosition.y + (_gridElementSize.y * _generateDirection.y * i),
                                            _startPosition.z);
                        }
                        element.transform.parent = gameObject.transform;

                        GridElement gridElement = element.GetComponent <GridElement>();
                        if (gridElement == null)
                        {
                            gridElement = element.AddComponent <GridElement>();
                        }

                        gridElement.ElementIndex             = new IntVector3(j, i, 0);
                        gridElement.ElementMoveCostDirection = _elementMoveCostDirection;
                        _elements[j, i, 0] = gridElement;
                    }
                }
            }
            break;

            case GridType.ThreeDimensional:
            {
                int x = _gridSize.x;
                int y = _gridSize.y;
                int z = _gridSize.z;
                _elements = new GridElement[x, y, z];

                for (int i = 0; i < x; ++i)
                {
                    for (int j = 0; j < y; ++j)
                    {
                        for (int k = 0; k < z; ++k)
                        {
                            GameObject element;
                            if (_gridElementObject == null)
                            {
                                element = new GameObject();
                            }
                            else
                            {
                                element =
                                    Instantiate(_gridElementObject, Vector3.zero, Quaternion.identity) as GameObject;
                            }
                            element.name = string.Format("GridElement[{0}][{1}][{2}]", j, i, k);

                            if (_useMyPositionAsStart)
                            {
                                Vector3 startPosition = gameObject.transform.position;
                                element.transform.position =
                                    new Vector3(startPosition.x + (_gridElementSize.x * _generateDirection.x * j),
                                                startPosition.y + (_gridElementSize.y * _generateDirection.y * i),
                                                startPosition.z + (_gridElementSize.z * _generateDirection.z * k));
                            }
                            else
                            {
                                element.transform.position =
                                    new Vector3(_startPosition.x + (_gridElementSize.x * _generateDirection.x * j),
                                                _startPosition.y + (_gridElementSize.y * _generateDirection.y * i),
                                                _startPosition.z + (_gridElementSize.z * _generateDirection.z * k));
                            }
                            element.transform.parent = gameObject.transform;

                            GridElement gridElement = element.GetComponent <GridElement>();

                            if (gridElement == null)
                            {
                                _elements[j, i, k] = element.AddComponent <GridElement>();
                            }
                            else
                            {
                                _elements[j, i, k] = gridElement;
                            }
                            gridElement.ElementIndex = new IntVector3(j, i, k);
                        }
                    }
                }
            }
            break;
            }
            if (_checkWalkability)
            {
                CheckGridWalkability();
            }
        }
Example #9
0
 public static int CheckMoveDistance(GridElement element, GridElement newParent)
 {
     return(Mathf.Abs(element.ElementIndex.x - newParent.ElementIndex.x) +
            Mathf.Abs(element.ElementIndex.y - newParent.ElementIndex.y) +
            Mathf.Abs(element.ElementIndex.z - newParent.ElementIndex.z));
 }
Example #10
0
 public virtual void CalculateHeuristic(GridElement element)
 {
     _heuristicCost = Mathf.CeilToInt(Vector3.Distance(gameObject.transform.position, element.transform.position));
 }