Пример #1
0
    public void SetupGameObject(GameObject go)
    {
        if (sprite != null)
        {
            SpriteRenderer spriteRenderer = go.GetComponent <SpriteRenderer>();
            if (spriteRenderer == null)
            {
                spriteRenderer = go.AddComponent <SpriteRenderer>();
            }

            spriteRenderer.sprite = sprite;

            if (FlagUtil.IsSet(tileFlags, ETileAttribute.FLIPX))
            {
                spriteRenderer.flipX = true;
            }
            if (FlagUtil.IsSet(tileFlags, ETileAttribute.FLIPY))
            {
                spriteRenderer.flipY = true;
            }

            if (FlagUtil.IsExactly(tileFlags, (ETileAttribute.ROTATE180 | ETileAttribute.ROTATE90)))
            {
                go.transform.Rotate(Vector3.back * 270);
            }
            else if (FlagUtil.IsExactly(tileFlags, ETileAttribute.ROTATE180))
            {
                go.transform.Rotate(Vector3.back * 180);
            }
            else if (FlagUtil.IsExactly(tileFlags, ETileAttribute.ROTATE90))
            {
                go.transform.Rotate(Vector3.back * 90);
            }
        }
    }
Пример #2
0
    private static void GetCorners(RectInt sourceRect, ETileAttribute eTileFlags, out Vector2Int vTL, out Vector2Int vTR, out Vector2Int vBR, out Vector2Int vBL)
    {
        // Unity textures start in the bottom left so TOP = y + height
        vTL = new Vector2Int(sourceRect.x, sourceRect.yMax - 1);
        vTR = new Vector2Int(sourceRect.xMax - 1, sourceRect.yMax - 1);
        vBR = new Vector2Int(sourceRect.xMax - 1, sourceRect.y);
        vBL = new Vector2Int(sourceRect.x, sourceRect.y);

        if (eTileFlags == ETileAttribute.NONE)
        {
            return;
        }

        if (FlagUtil.IsSet(eTileFlags, ETileAttribute.FLIPX))
        {
            Vector2Int vTLCopy = vTL;
            Vector2Int vTRCopy = vTR;
            Vector2Int vBRCopy = vBR;
            Vector2Int vBLCopy = vBL;

            vTL.Set(vTRCopy.x, vTRCopy.y);
            vTR.Set(vTLCopy.x, vTLCopy.y);
            vBR.Set(vBLCopy.x, vBLCopy.y);
            vBL.Set(vBRCopy.x, vBRCopy.y);
        }

        if (FlagUtil.IsSet(eTileFlags, ETileAttribute.FLIPY))
        {
            Vector2Int vTLCopy = vTL;
            Vector2Int vTRCopy = vTR;
            Vector2Int vBRCopy = vBR;
            Vector2Int vBLCopy = vBL;

            vTL.Set(vBLCopy.x, vBLCopy.y);
            vTR.Set(vBRCopy.x, vBRCopy.y);
            vBR.Set(vTRCopy.x, vTRCopy.y);
            vBL.Set(vTLCopy.x, vTLCopy.y);
        }

        if (FlagUtil.IsSet(eTileFlags, ETileAttribute.ROTATEANY))
        {
            Vector2Int[] vCorners = { vTL, vTR, vBR, vBL };

            ETileAttribute eRotate       = (eTileFlags & ETileAttribute.ROTATEANY);
            int            iRotateOffset = 3;
            if (eRotate == ETileAttribute.ROTATE90)
            {
                iRotateOffset = 3;
            }
            else if (eRotate == ETileAttribute.ROTATE180)
            {
                iRotateOffset = 2;
            }
            else if (eRotate == (ETileAttribute.ROTATE180 | ETileAttribute.ROTATE90))
            {
                iRotateOffset = 1;
            }

            int iTL = iRotateOffset % 4;
            int iTR = (iRotateOffset + 1) % 4;
            int iBR = (iRotateOffset + 2) % 4;
            int iBL = (iRotateOffset + 3) % 4;

            vTL.Set(vCorners[iTL].x, vCorners[iTL].y);
            vTR.Set(vCorners[iTR].x, vCorners[iTR].y);
            vBR.Set(vCorners[iBR].x, vCorners[iBR].y);
            vBL.Set(vCorners[iBL].x, vCorners[iBL].y);
        }
    }