Exemplo n.º 1
0
    public TileEdge GetEdge()
    {
        if (tileSprite == null)
        {
            return(null);
        }

        return(tileSprite.GetEdge(joinEdge.Opposite()));
    }
Exemplo n.º 2
0
    public List <TileSpriteNeighbor> GetNeighborTileSprites()
    {
        List <TileSpriteNeighbor> tileSprites = new List <TileSpriteNeighbor>();

        for (int tileEdge = 0; tileEdge < (int)ETileEdge.COUNT; ++tileEdge)
        {
            ETileEdge eTileEdge     = (ETileEdge)tileEdge;
            ETileEdge eOppositeEdge = eTileEdge.Opposite();

            Tile neighbor = GetNeighbor(eTileEdge);
            if (neighbor != null && neighbor.GetTileSprite() != null)
            {
                tileSprites.Add(new TileSpriteNeighbor(neighbor.GetTileSprite(), eTileEdge));
            }
        }

        return(tileSprites);
    }
Exemplo n.º 3
0
    public List <TileEdge> GetTileNeighborEdges()
    {
        List <TileEdge> tileEdges = new List <TileEdge>();

        for (int tileEdge = 0; tileEdge < (int)ETileEdge.COUNT; ++tileEdge)
        {
            ETileEdge eTileEdge     = (ETileEdge)tileEdge;
            ETileEdge eOppositeEdge = eTileEdge.Opposite();

            Tile neighbor = GetNeighbor(eTileEdge);
            if (neighbor != null)
            {
                TileEdge neighborEdge = neighbor.GetTileEdge(eOppositeEdge);
                if (neighborEdge != null)
                {
                    tileEdges.Add(neighborEdge);
                }
            }
        }

        return(tileEdges);
    }
Exemplo n.º 4
0
    public List <TileSprite> FindValidTiles(List <TileSpriteNeighbor> neighborTiles)
    {
        // Find a list of tiles from the edgeLookup
        // This finds tiles based on a sample of the tile edge
        // This allows us to quickly narrow down a possible set of tiles to pull from
        // We then compare all the edges to do a full comparison of tile edges
        // This gives us a list of all suitable tiles for a space

        bool bHasConstraints            = false;
        List <TileSprite> candidateList = null;

        foreach (TileSpriteNeighbor neighbor in neighborTiles)
        {
            if (neighbor == null)
            {
                continue;
            }

            TileEdge neighborEdge = neighbor.GetEdge();
            if (neighborEdge == null)
            {
                continue;
            }

            TileSprite neighborTileSprite = neighbor.GetTileSprite();
            string     neighborEdgeHash   = neighborEdge.GetHash();
            ETileEdge  eNeighborEdge      = neighborEdge.GetETileEdge();
            ETileEdge  eCandidateEdge     = eNeighborEdge.Opposite();

            if (candidateList == null)
            {
                bHasConstraints = true;

                EdgeLookupType edgeLookup = GetDictionayFromEdge(eCandidateEdge);
                if (edgeLookup.ContainsKey(neighborEdgeHash))
                {
                    candidateList = new List <TileSprite>(edgeLookup[neighborEdgeHash]);
                }
            }
            else
            {
                // Remove candidates based on edge hash
                for (int tileIndex = candidateList.Count - 1; tileIndex >= 0; --tileIndex)
                {
                    TileSprite candidateTileSprite   = candidateList[tileIndex];
                    TileEdge   candidateTileEdge     = candidateTileSprite.GetEdge(eCandidateEdge);
                    string     candidateTileEdgeHash = candidateTileEdge.GetHash();

                    if (!candidateTileEdgeHash.Equals(neighborEdgeHash))
                    {
                        candidateList.RemoveAt(tileIndex);
                    }
                }
            }

            // Remove candidates based on full edge comparison
            for (int tileIndex = candidateList.Count - 1; tileIndex >= 0; --tileIndex)
            {
                TileSprite candidateTileSprite = candidateList[tileIndex];

                if (!candidateTileSprite.CompareEdge(neighborTileSprite, eCandidateEdge))
                {
                    candidateList.RemoveAt(tileIndex);
                }
            }
        }

        if (candidateList == null)
        {
            // We can use any tile
            if (!bHasConstraints)
            {
                candidateList = new List <TileSprite>();
                foreach (List <TileSprite> lookupList in edgeTopLookup.Values)
                {
                    foreach (TileSprite tileSprite in lookupList)
                    {
                        candidateList.Add(tileSprite);
                    }
                }
            }
        }

        return(candidateList);
    }
Exemplo n.º 5
0
    public static bool CompareEdges(TileSprite tileSprite, TileSprite neighborTileSprite, ETileEdge tileEdge)
    {
        int pixelThreshold = 2;

        Sprite         sprite         = tileSprite.GetSprite();
        Sprite         neighborSprite = neighborTileSprite.GetSprite();
        ETileAttribute spriteFlags    = tileSprite.GetFlags();
        ETileAttribute neighborFlags  = neighborTileSprite.GetFlags();

        Vector2Int vTileEdgeStart, vTileEdgeEnd, vNeighborEdgeStart, vNeighborEdgeEnd;

        GetEdgePoints(sprite, tileEdge, spriteFlags, out vTileEdgeStart, out vTileEdgeEnd);
        GetEdgePoints(neighborSprite, tileEdge.Opposite(), neighborFlags, out vNeighborEdgeStart, out vNeighborEdgeEnd);

        int tileWidth      = vTileEdgeEnd.x - vTileEdgeStart.x;
        int tileHeight     = vTileEdgeEnd.y - vTileEdgeStart.y;
        int neighborWidth  = vNeighborEdgeEnd.x - vNeighborEdgeStart.x;
        int neighborHeight = vNeighborEdgeEnd.y - vNeighborEdgeStart.y;

        int absWidth  = Math.Abs(tileWidth);
        int absHeight = Math.Abs(tileHeight);

        int neighborAbsWidth  = Math.Abs(neighborWidth);
        int neighborAbsHeight = Math.Abs(neighborHeight);

        int tileOffsetY = 0;

        if (absHeight > absWidth)
        {
            tileOffsetY = tileHeight > 0 ? 1 : -1;
        }

        int tileOffsetX = 0;

        if (absWidth >= absHeight)
        {
            tileOffsetX = tileWidth > 0 ? 1 : -1;
        }

        int neighborOffsetY = 0;

        if (neighborAbsHeight > neighborAbsWidth)
        {
            neighborOffsetY = neighborHeight > 0 ? 1 : -1;
        }

        int neighborOffsetX = 0;

        if (neighborAbsWidth >= neighborAbsHeight)
        {
            neighborOffsetX = neighborWidth > 0 ? 1 : -1;
        }

        int pixelCount = absWidth > absHeight ? absWidth : absHeight;

        int totalPixelDiff  = 0;
        int totalPixelCount = 0;

        for (int pixel = 0; pixel < pixelCount; ++pixel)
        {
            int tileX     = vTileEdgeStart.x + pixel * tileOffsetX;
            int neighborX = vNeighborEdgeStart.x + pixel * neighborOffsetX;
            int tileY     = vTileEdgeStart.y + pixel * tileOffsetY;
            int neighborY = vNeighborEdgeStart.y + pixel * neighborOffsetY;

            Color32 spriteCol   = sprite.GetColor(tileX, tileY);
            Color32 neighborCol = neighborSprite.GetColor(neighborX, neighborY);
            int     pixelDiff   = ComparePixel(spriteCol, neighborCol);
            totalPixelDiff += pixelDiff;
            ++totalPixelCount;
        }

        int averageDiff = Mathf.CeilToInt(totalPixelDiff / totalPixelCount);

        if (averageDiff <= pixelThreshold)
        {
            return(true);
        }

        return(false);
    }