Example #1
0
 private void ResetNodeColor(IConfigurableAstarNode node)
 {
     if (node == null)
     {
         return;
     }
     TintNode(node, NodeSharedData.Instance.GetColor(NodeSharedData.TintColor.NO_TINT));
 }
Example #2
0
 private void ConnectNodesBothWays(IConfigurableAstarNode node1, IConfigurableAstarNode node2)
 {
     if (node1.isWalkable && node2.isWalkable)
     {
         node1.AddNeighbour(node2);
         node2.AddNeighbour(node1);
     }
 }
Example #3
0
    private void TintNode(IConfigurableAstarNode node, Color color)
    {
        ITintable tintComponent = ((Node)node).GetComponent <ITintable>();

        if (tintComponent != null)
        {
            tintComponent.Tint(color);
        }
    }
Example #4
0
    public float CostTo(IAStarNode neighbour)
    {
        IConfigurableAstarNode node = (IConfigurableAstarNode)neighbour;

        if (node != null)
        {
            return(node.Cost);
        }
        else
        {
            throw new System.Exception("Can't cast IAstarNode");
        }
    }
Example #5
0
    public float EstimatedCostTo(IAStarNode target)
    {
        IConfigurableAstarNode node = (IConfigurableAstarNode)target;

        if (node != null)
        {
            Vector2 origin      = new Vector2(transform.position.x, transform.position.z);
            Vector2 destination = new Vector2(node.Transform.position.x, node.Transform.position.z);;
            return(Utility.PythagoreanDistance(origin, destination));
        }
        else
        {
            throw new System.Exception("Can't cast IAstarNode");
        }
    }
Example #6
0
    //This function creates the map and configures it according to the rows, columns and the loaded CSV file
    public IConfigurableAstarNode[,] CreateMap(Action <IObservable> OnClickedCallback)
    {
        var map = new IConfigurableAstarNode[rows, columns];

        for (int i = 0; i < map.GetLength(0); i++)
        {
            for (int j = 0; j < map.GetLength(1); j++)
            {
                GameObject element = Instantiate(nodePrefab);

                /*tile position and color configuration*/
                IConfigurableAstarNode ConfigurableComponent = element.GetComponent <IConfigurableAstarNode>();
                ConfigurableComponent.Initialize();

                //we displace the even rows a bit to match the hexagonal grid format
                float columnOffset = i % 2 == 0 ? tileColumnOffset * 0.5f : tileColumnOffset;

                Vector3 offsetDistance = new Vector3(i * tileRowOffset, 0, j * tileColumnOffset + columnOffset);

                //setting instantiated objects names for debugging purposes. Not necessary for implementation
                ConfigurableComponent.Transform.name = "(" + i + ")" + " " + "(" + j + ")";

                //setting element position
                ConfigurableComponent.Transform.position = transform.position + offsetDistance;

                //setting the data to the element
                ConfigurableComponent.Configure(_dataTable[(NodeSharedData.Type)parsedMapValues[i, j]], new Vector2(i, j));

                //setting tile interaction callbacks
                IObservable observableComponent = ConfigurableComponent.Transform.GetComponent <IObservable>();
                if (observableComponent != null)
                {
                    observableComponent.RegisterObserverCallback(OnClickedCallback);
                }


                //adding the element to the map
                map[i, j] = ConfigurableComponent;

                //making connections with previous nodes
                ConnectNodes(map, i, j);
            }
        }

        return(map);
    }
Example #7
0
    private void NotifyClickedTile(IObservable node)
    {
        if (_startNode != null && _endNode != null)
        {
            return;
        }

        var configurableNode = (IConfigurableAstarNode)node;

        if (!configurableNode.isWalkable)
        {
            UIManager.Instance.TriggerNonWalkableWarning();
            return; // we return because we mustn't process non walkable nodes (water in this case).
        }
        if (_startNode == null)
        {
            _startNode = configurableNode;
            TintNode(_startNode, NodeSharedData.Instance.GetColor(NodeSharedData.TintColor.EXTREMES));
        }
        else if (_endNode == null && node != _startNode)
        {
            _endNode = configurableNode;
            _path    = AStar.GetPath(_startNode, _endNode);
            if (_path.Count > 0)
            {
                for (int i = 0; i < _path.Count; i++)
                {
                    var tintableComponent = ((Node)_path[i]).transform.GetComponent <ITintable>();
                    if (tintableComponent != null)
                    {
                        var color = (i == 0 || i == _path.Count - 1) ? NodeSharedData.TintColor.EXTREMES : NodeSharedData.TintColor.ROUTE;

                        tintableComponent.Tint(NodeSharedData.Instance.GetColor(color));
                    }
                }
            }
            else
            {
                TintNode(_endNode, NodeSharedData.Instance.GetColor(NodeSharedData.TintColor.EXTREMES));
                print("no path found");
            }
        }
    }
Example #8
0
    public void ResetPathing()
    {
        if (_path != null)
        {
            for (int i = 0; i < _path.Count; i++)
            {
                ResetNodeColor((IConfigurableAstarNode)_path[i]);
            }
            _path = null;
        }

        if (_startNode != null)
        {
            ResetNodeColor(_startNode);
            _startNode = null;
        }

        if (_endNode != null)
        {
            ResetNodeColor(_endNode);
            _endNode = null;
        }
    }
Example #9
0
 /*------------------------------------------------------*/
 public void AddNeighbour(IConfigurableAstarNode n)
 {
     _neighbours.Add(n);
 }