Beispiel #1
0
    private void CheckNodeConnection(PathNode node, Vector2Int connectionIndex)
    {
        if (!_nodeMap.ContainsKey(connectionIndex))
        {
            return;
        }

        PathNode targetNode = _nodeMap[connectionIndex];

        // Check clear path
        Vector2 direction = targetNode.Position - node.Position;
        float   distance  = direction.magnitude;

        direction.Normalize();

        RaycastHit2D[]  results      = new RaycastHit2D[10];
        ContactFilter2D filter       = new ContactFilter2D();
        var             numberOfHits = Physics2D.CircleCast(node.Position, _requiredRadius, direction, filter, results, distance);

        if (numberOfHits > 0)
        {
            for (int i = 0; i < numberOfHits; ++i)
            {
                if (results[i].transform?.tag == "StaticWorld")
                {
                    return;
                }
            }
        }

        // Connect nodes
        targetNode.AddConnectedNodes(node);
        node.AddConnectedNodes(targetNode);
    }
Beispiel #2
0
    private void LoadNavmeshFromFile()
    {
        _nodeMap.Clear();

        TextAsset navmeshText = Resources.Load <TextAsset>("Navmesh/" + m_navmeshFile + ".path");
        List <PathNodeSerializable> serializableNodes = JsonConvert.DeserializeObject <List <PathNodeSerializable> >(navmeshText.text);

        foreach (var node in serializableNodes)
        {
            PathNode newNode = new PathNode(node.Position.Get(), node.Index.Get());
            newNode.ConnectedNodes.Capacity = node.ConnectedNodes.Count;
            _nodeMap.Add(node.Index.Get(), newNode);
        }


        foreach (var serializedNode in serializableNodes)
        {
            PathNode realNode = _nodeMap[serializedNode.Index.Get()];
            foreach (var connectedNode in serializedNode.ConnectedNodes)
            {
                if (_nodeMap.ContainsKey(connectedNode.Get()))
                {
                    realNode.AddConnectedNodes(_nodeMap[connectedNode.Get()]);
                }
            }
        }
    }