Esempio n. 1
0
    private GridSpline MakeBranch(List <Vector2> points)
    {
        GameObject newBranch = Instantiate(branch, gameObject.transform.position, Quaternion.identity);

        newBranch.transform.parent = gameObject.transform;
        GridSpline gridSpline = (GridSpline)newBranch.GetComponent <GridSpline> ();

        gridSpline.gridSpacing = gridSpacing;

        MeshRenderer renderer = newBranch.GetComponent <MeshRenderer> ();

        renderer.material = colorMaterials [(int)color];

        gridSpline.Reshape(points);

        float baseDepth = 0;
        int   baseX     = (int)points [1].x;
        int   baseY     = (int)points [1].y;

        if (IsValidCoord(baseX, baseY) && pointsToBranches [baseX, baseY] != null)
        {
            GridSpline baseBranch = pointsToBranches [baseX, baseY];
            int        i          = gridSpline.splinePositions.Count - 1;
            while (i > 0 && !ApproxContains(baseBranch.splinePositions, gridSpline.splinePositions[i]))
            {
                i--;
            }
            Vector2 intersectionPoint = gridSpline.splinePositions [i];
            float   offseted          = 0;
            if (i == 0)
            {
                i        = 1;
                offseted = gridSpline.turnRadius;
                Vector2 offset = (points [1] - points [0]) * gridSpline.turnRadius;
                intersectionPoint = gridSpline.splinePositions [1] - offset;
                if (!ApproxContains(baseBranch.splinePositions, intersectionPoint))
                {
                    offseted          = -1 * gridSpline.turnRadius;
                    intersectionPoint = gridSpline.splinePositions [1] + offset;
                }
            }
            int   j     = ApproxIndexOf(baseBranch.splinePositions, intersectionPoint);
            float depth = baseBranch.cumulativeLengths [j] - gridSpline.cumulativeLengths [i] + offseted;
            baseDepth = depth + branchesToDepths [baseBranch];
        }
        branchesToDepths [gridSpline] = baseDepth;

        foreach (Vector2 point in points)
        {
            int x = (int)point.x;
            int y = (int)point.y;
            if (IsValidCoord(x, y) && pointsToBranches [x, y] == null)
            {
                pointsToBranches [x, y] = gridSpline;
            }
        }

        return(gridSpline);
    }
Esempio n. 2
0
    private void AddToBranch(GridSpline branch, Vector2 point)
    {
        int x = (int)point.x;
        int y = (int)point.y;

        branch.currentPoints.Add(point);
        branch.Reshape(branch.currentPoints);
        pointsToBranches [x, y] = branch;
    }
Esempio n. 3
0
    public GridSpline AddPoint(Vector2 point)
    {
        int x = (int)point.x;
        int y = (int)point.y;

        GridSpline leftBranch   = (x > 0) ? pointsToBranches [x - 1, y] : null;
        GridSpline rightBranch  = (x < gridSize - 1) ? pointsToBranches [x + 1, y] : null;
        GridSpline bottomBranch = (y > 0) ? pointsToBranches [x, y - 1] : null;

        if (leftBranch != null)
        {
            if (IsLastPoint(leftBranch, x - 1, y))
            {
                AddToBranch(leftBranch, point);
                return(leftBranch);
            }
            else
            {
                if (IsValidCoord(x - 1, y - 1) && leftBranch.currentPoints.Contains(new Vector2(x - 1, y - 1)))
                {
                    List <Vector2> gridArray = new List <Vector2>();
                    gridArray.Add(new Vector2(x - 1, y - 1));
                    gridArray.Add(new Vector2(x - 1, y));
                    gridArray.Add(new Vector2(x, y));
                    return(MakeBranch(gridArray));
                }
                else
                {
                    List <Vector2> gridArray = new List <Vector2> ();
                    gridArray.Add(new Vector2(x - 2, y));
                    gridArray.Add(new Vector2(x - 1, y));
                    gridArray.Add(new Vector2(x, y));
                    return(MakeBranch(gridArray));
                }
            }
        }
        else if (rightBranch != null)
        {
            if (IsLastPoint(rightBranch, x + 1, y))
            {
                AddToBranch(rightBranch, point);
                return(rightBranch);
            }
            else
            {
                if (IsValidCoord(x + 1, y - 1) && rightBranch.currentPoints.Contains(new Vector2(x + 1, y - 1)))
                {
                    List <Vector2> gridArray = new List <Vector2> ();
                    gridArray.Add(new Vector2(x + 1, y - 1));
                    gridArray.Add(new Vector2(x + 1, y));
                    gridArray.Add(new Vector2(x, y));
                    return(MakeBranch(gridArray));
                }
                else
                {
                    List <Vector2> gridArray = new List <Vector2> ();
                    gridArray.Add(new Vector2(x + 2, y));
                    gridArray.Add(new Vector2(x + 1, y));
                    gridArray.Add(new Vector2(x, y));
                    return(MakeBranch(gridArray));
                }
            }
        }
        else if (bottomBranch != null)
        {
            if (IsLastPoint(bottomBranch, x, y - 1))
            {
                AddToBranch(bottomBranch, point);
                return(bottomBranch);
            }
            else
            {
                if (IsValidCoord(x - 1, y - 1) && bottomBranch.currentPoints.Contains(new Vector2(x - 1, y - 1)) &&
                    IsValidCoord(x + 1, y - 1) && bottomBranch.currentPoints.Contains(new Vector2(x + 1, y - 1)))
                {
                    int   ind = bottomBranch.currentPoints.IndexOf(new Vector2(x, y - 1));
                    float dir = 1;
                    if (bottomBranch.currentPoints[ind + 1] == new Vector2(x + 1, y - 1))
                    {
                        dir = -1;
                    }
                    List <Vector2> gridArray = new List <Vector2> ();
                    gridArray.Add(new Vector2(x + dir, y - 1));
                    gridArray.Add(new Vector2(x, y - 1));
                    gridArray.Add(new Vector2(x, y));
                    return(MakeBranch(gridArray));
                }
                else
                {
                    List <Vector2> gridArray = new List <Vector2> ();
                    gridArray.Add(new Vector2(x, y - 2));
                    gridArray.Add(new Vector2(x, y - 1));
                    gridArray.Add(new Vector2(x, y));
                    return(MakeBranch(gridArray));
                }
            }
        }
        return(null);
    }
Esempio n. 4
0
    private bool IsLastPoint(GridSpline branch, int x, int y)
    {
        Vector2 lastPoint = branch.currentPoints [branch.currentPoints.Count - 1];

        return(x == lastPoint.x && y == lastPoint.y);
    }
Esempio n. 5
0
 private float GetDepth(GridSpline branch)
 {
     return(branch.cumulativeLengths [branch.cumulativeLengths.Count - 1] + branchesToDepths [branch]);
 }