private void ProcessMapFile(IReadOnlyList <string> lines)
    {
        var startCoords = Vector2.zero;
        var endCoords   = Vector2.zero;
        var nodes       = transform.Find("/Nodes");

        numRows = lines.Count;
        numCols = lines[0].Length;
        for (var iRow = 0; iRow < numRows; iRow++)
        {
            for (var iCol = 0; iCol < numCols; iCol++)
            {
                var symbol = lines[iRow][iCol];
                if (symbol == '.')
                {
                    continue;
                }
                var raise      = "01".Contains(symbol) ? Vector3.up / 2 : Vector3.zero;
                var pos        = nodePrefab.position + Vector3.right * iCol + Vector3.forward * (lines.Count - iRow - 1) + raise;
                var nodeObject = Instantiate(nodePrefab, pos, nodePrefab.rotation);
                nodeObject.SetParent(nodes);
                if (symbol == '*')
                {
                    continue;
                }

                var material = nodeObject.GetComponent <Renderer>().material;
                if (symbol == '0')
                {
                    material.color = startColor;
                    startPosition  = pos;
                    startCoords    = new Vector2(iCol, iRow);
                }
                else if (symbol == '1')
                {
                    material.color = endColor;
                    endCoords      = new Vector2(iCol, iRow);
                }

                if (symbol == 'g')
                {
                    nodeObject.GetComponent <Node>().AddGun();
                }
            }
        }

        waypoints = WaypointFinder.CreateWaypoints(lines, startCoords, endCoords);
    }
Beispiel #2
0
    public static MapDescription CreateMapDescription(int level)
    {
        var startPosition = Vector3.zero;
        var startCoords   = Vector2.zero;
        var endCoords     = Vector2.zero;
        var nodePositions = new List <Vector3>();

        var mapLines = Resources.Load <TextAsset>($"Levels/{level}/map").text.Split('\n');
        var numRows  = mapLines.Length;
        var numCols  = mapLines[0].Length;

        for (var iRow = 0; iRow < numRows; iRow++)
        {
            for (var iCol = 0; iCol < numCols; iCol++)
            {
                var symbol = (MapSymbol)mapLines[iRow][iCol];
                if (symbol == MapSymbol.Path)
                {
                    continue;
                }

                bool    startOrEnd = symbol == MapSymbol.Start || symbol == MapSymbol.End;
                Vector3 nodeRaise  = startOrEnd ? Vector3.up * .75f : Vector3.zero;
                Vector3 nodePos    = Vector3.right * iCol + Vector3.forward * (numRows - iRow - 1) + nodeRaise;
                nodePositions.Add(nodePos);

                switch (symbol)
                {
                case MapSymbol.Start:
                    startPosition = nodePos;
                    startCoords   = new Vector2(iCol, iRow);
                    break;

                case MapSymbol.End:
                    endCoords = new Vector2(iCol, iRow);
                    break;
                }
            }
        }

        var waypoints  = WaypointFinder.CreateWaypoints(mapLines, startCoords, endCoords);
        var dimensions = new Vector2(numCols, numRows);

        return(new MapDescription(dimensions, startPosition, nodePositions, waypoints));
    }