예제 #1
0
    virtual public void Update()
    {
        if (_clipNode != null && !_skipZero)
        {
            // get position of _clipNode relative to this
            Vector2 relPos = StageToLocal(_clipNode.GetPosition());

            float xMin = relPos.x - _clipWidth / 2 - _tileWidth * 0.75f;
            float yMin = relPos.y - _clipHeight / 2 - _tileHeight * 0.75f;

            // check if the _clipNode has moved enough to check tile positions
            if (Mathf.Round(xMin / _tileWidth) == _clipPos.x && Mathf.Round(yMin / _tileHeight) == _clipPos.y)
            {
                return;
            }
            _clipPos = new Vector2(Mathf.Round(xMin / _tileWidth), Mathf.Round(yMin / _tileHeight));

            float tileOffsetX = (Mathf.Floor(_clipWidth / _tileWidth) + 2) * _tileWidth;
            float tileOffsetY = (Mathf.Floor(_clipHeight / _tileHeight) + 2) * _tileHeight;

            float xMax = xMin + tileOffsetX;
            float yMax = yMin + tileOffsetY;

            foreach (FSprite tile in _tiles)
            {
                bool tileChangedX = false;
                bool tileChangedY = false;
                while (tile.x < xMin)
                {
                    tile.x      += tileOffsetX;
                    tileChangedX = true;
                }
                if (!tileChangedX)
                {
                    while (tile.x > xMax)
                    {
                        tile.x      -= tileOffsetX;
                        tileChangedX = true;
                    }
                }
                while (tile.y < yMin)
                {
                    tile.y      += tileOffsetY;
                    tileChangedY = true;
                }
                if (!tileChangedY)
                {
                    while (tile.y > yMax)
                    {
                        tile.y      -= tileOffsetY;
                        tileChangedY = true;
                    }
                }

                if (tileChangedX || tileChangedY)
                {
                    int tileX = Mathf.FloorToInt((tile.x - _tileWidth / 2) / _tileWidth);
                    int tileY = Mathf.FloorToInt((-tile.y - _tileHeight / 2) / _tileHeight);

                    if (repeatX)
                    {
                        while (tileX < 0)
                        {
                            tileX += _tilesWide;
                        }
                        while (tileX >= _tilesWide)
                        {
                            tileX -= _tilesWide;
                        }
                    }
                    else if (tileX < 0 || tileX >= _tilesWide)
                    { // display empty tile, outside of known data
                        tileX = -1;
                        tileY = -1;
                    }
                    if (repeatY)
                    {
                        while (tileY < 0)
                        {
                            tileY += _tilesHigh;
                        }
                        while (tileY >= _tilesHigh)
                        {
                            tileY -= _tilesHigh;
                        }
                    }
                    else if (tileY < 0 || tileY >= _tilesHigh)
                    { // display empty tile, outside of known data
                        tileX = -1;
                        tileY = -1;
                    }

                    int frame = tileX + tileY * _tilesWide;
                    if (frame >= 0 && frame < _tileArray.GetLength(0))
                    {
                        uint frameNum             = _tileArray[frame];
                        bool flipped_horizontally = FTmxMap.FlippedHorizontally(frameNum);
                        bool flipped_vertically   = FTmxMap.FlippedVertically(frameNum);
                        bool flipped_diagonally   = FTmxMap.FlippedDiagonally(frameNum);

                        tile.element = Futile.atlasManager.GetElementWithName(_baseName + "_" + FTmxMap.RemoveFrameFlags(frameNum));

                        tile.rotation = flipped_diagonally ? -90 : 0;
                        if (flipped_diagonally)
                        {
                            tile.scaleY = flipped_vertically ? 1 : -1;
                            tile.scaleX = flipped_horizontally ? -1 : 1;
                            if (!(flipped_horizontally || flipped_vertically))
                            {
                                tile.scaleX   = -1;
                                tile.scaleY   = 1;
                                tile.rotation = -90;
                            }
                            else if (flipped_vertically && flipped_horizontally)
                            {
                                tile.scaleX = 1;
                                tile.scaleY = -1;
                            }
                        }
                        else
                        {
                            tile.scaleY = flipped_vertically ? -1 : 1;
                            tile.scaleX = flipped_horizontally ? -1 : 1;
                        }
                        tile.isVisible = true;
                    }
                    else
                    {
                        tile.isVisible = false;
                    }
                }
            }
        }
    }
예제 #2
0
    protected void buildTiles()
    {
        // clear pos so next update fixes tiles
        _clipPos = Vector2.zero;

        // figure out needed size
        int clipTilesWide = Mathf.CeilToInt(_clipWidth / _tileWidth) + 2;

        if (_tilesWide < clipTilesWide || _clipWidth == 0 || _skipZero)
        {
            clipTilesWide = _tilesWide;
        }
        int clipTilesHigh = Mathf.CeilToInt(_clipHeight / _tileHeight) + 2;

        if (_tilesHigh < clipTilesHigh || _clipHeight == 0 || _skipZero)
        {
            clipTilesHigh = _tilesHigh;
        }

        // update count of tiles
        if (!_skipZero)
        {
            int totalNeeded = clipTilesWide * clipTilesHigh;

            // make sure we have the right amount of tiles for the current clip size
            if (_tiles.Count <= 0)
            {
                for (int i = 0; i < totalNeeded; i++)
                {
                    FSprite sprite = new FSprite(_baseName + "_1"); // set to 1
                    sprite.shader = _shader;

                    // add to this collection
                    _tiles.Add(sprite);
                    AddChild(sprite);
                }
            }
            else if (_tiles.Count < totalNeeded)
            {
                int start = _tiles.Count;
                for (int i = start; i < totalNeeded; i++)
                {
                    FSprite sprite = new FSprite(_baseName + "_1");
                    sprite.shader = _shader;

                    // add to this collection
                    _tiles.Add(sprite);
                    AddChild(sprite);
                }
            }
            else if (_tiles.Count > totalNeeded)
            {
                int removeThisMany = _tiles.Count - totalNeeded;
                for (int i = 0; i < removeThisMany; i++)
                {
                    // remove from the beginning of array
                    RemoveChild(_tiles[0]);
                    _tiles.RemoveAt(0);
                }
            }
        }

        // set clipWidth to the whole size if it doesn't exist
        if (_clipWidth == 0)
        {
            _clipWidth = _tilesWide * _tileWidth;
        }
        if (_clipHeight == 0)
        {
            _clipHeight = _tilesHigh * _tileHeight;
        }

        // make array of sprites
        for (int i = 0; i < clipTilesWide; i++)
        {
            for (int j = 0; j < clipTilesHigh; j++)
            {
                uint frameWithFlag = _tileArray[i + (j * _tilesWide)];

                bool flipped_horizontally = FTmxMap.FlippedHorizontally(frameWithFlag);
                bool flipped_vertically   = FTmxMap.FlippedVertically(frameWithFlag);
                bool flipped_diagonally   = FTmxMap.FlippedDiagonally(frameWithFlag);

                int frame = FTmxMap.RemoveFrameFlags(frameWithFlag);

                if (!_skipZero || frame > 0)
                {
                    FSprite sprite;
                    if (_skipZero)
                    {
                        sprite        = new FSprite(_baseName + "_" + frame);
                        sprite.shader = _shader;
                        AddChild(sprite);
                    }
                    else
                    {
                        sprite         = _tiles[i + (j * clipTilesWide)];
                        sprite.element = Futile.atlasManager.GetElementWithName(_baseName + "_" + frame);
                    }

                    sprite.rotation = flipped_diagonally ? -90 : 0;
                    if (flipped_diagonally)
                    {
                        sprite.scaleY = flipped_vertically ? 1 : -1;
                        sprite.scaleX = flipped_horizontally ? -1 : 1;
                        if (!(flipped_horizontally || flipped_vertically))
                        {
                            sprite.scaleX   = -1;
                            sprite.scaleY   = 1;
                            sprite.rotation = -90;
                        }
                        else if (flipped_horizontally && flipped_vertically)
                        {
                            sprite.scaleX = 1;
                            sprite.scaleY = -1;
                        }
                    }
                    else
                    {
                        sprite.scaleY = flipped_vertically ? -1 : 1;
                        sprite.scaleX = flipped_horizontally ? -1 : 1;
                    }

                    // offset sprite coordinates
                    sprite.x = i * _tileWidth + _tileWidth / 2;
                    sprite.y = -j * _tileHeight - _tileHeight / 2;

                    if (frame == 0)
                    {
                        sprite.isVisible = false;
                    }
                    else
                    {
                        sprite.isVisible = true;
                    }
                }
            }
        }
    }