Пример #1
0
        /// <summary>
        /// Handle scene entry
        /// </summary>
        public override void Enter()
        {
            base.Enter();

            RB.SpriteSheetSetup(0, "Demos/DemoReel/Sprites", new Vector2i(16, 16));
            RB.SpriteSheetSet(0);

            mMap = RB.MapLoadTMX("Demos/DemoReel/Tilemap");

            if (mMap != null)
            {
                mMapSize = mMap.size;

                RB.MapLoadTMXLayer(mMap, "Clouds", 0);
                RB.MapLoadTMXLayer(mMap, "Decoration", 1);
                RB.MapLoadTMXLayer(mMap, "Terrain", 2);
            }

            if (mStyle == RB.PixelStyle.Wide)
            {
                RB.DisplayModeSet(new Vector2i(640 / 2, 360), mStyle);
            }
            else
            {
                RB.DisplayModeSet(new Vector2i(640, 360 / 2), mStyle);
            }
        }
Пример #2
0
        /// <summary>
        /// Handle scene entry
        /// </summary>
        public override void Enter()
        {
            base.Enter();

            RB.SpriteSheetSetup(0, "Demos/DemoReel/Sprites", new Vector2i(16, 16));
            RB.SpriteSheetSet(0);

            RB.SpriteSheetSetup(1, "Demos/DemoReel/Ghost", new Vector2i(104, 106));

            mMap = RB.MapLoadTMX("Demos/DemoReel/Tilemap");

            if (mMap != null)
            {
                RB.MapLoadTMXLayer(mMap, "Decoration", 0);
                RB.MapLoadTMXLayer(mMap, "Terrain", 1);

                RB.MapLayerSpriteSheetSet(0, 0);
                RB.MapLayerSpriteSheetSet(1, 0);
            }

            var demo = (DemoReel)RB.Game;

            RB.ShaderSetup(0, "Demos/DemoReel/WavyMaskShader");

            RB.SpriteSheetSetup(2, RB.DisplaySize);
            RB.SpriteSheetSetup(3, RB.DisplaySize);

            mBouncePos = new Vector2(RB.DisplaySize.width * 0.5f, RB.DisplaySize.height * 0.55f);
            mVelocity  = mSpeed;
        }
Пример #3
0
        private void DrawTMX(int x, int y)
        {
            var demo = (DemoReel)RB.Game;

            Rect2i   clipRect    = new Rect2i(x + 16, y + 142, 310, 207);
            Vector2i cameraPos   = new Vector2i((int)(Mathf.Sin(RB.Ticks / 100.0f) * 450) + 200, (int)Mathf.Sin(RB.Ticks / 10.0f) * 16);
            int      spriteFrame = (RB.Ticks % 40) > 20 ? 1 : 0;

            if (mMap != null)
            {
                int scrollPos = -(int)RB.Ticks % (mMapSize.width * RB.SpriteSize().width);

                RB.ClipSet(clipRect);
                RB.DrawRectFill(clipRect, DemoUtil.IndexToRGB(22));

                RB.CameraSet(cameraPos);

                RB.DrawMapLayer(1, new Vector2i(scrollPos, 0));
                RB.DrawMapLayer(1, new Vector2i(scrollPos + (mMapSize.width * RB.SpriteSize().width), 0));
                RB.DrawMapLayer(2);
                RB.DrawMapLayer(3);
                RB.DrawSprite(0 + spriteFrame, new Vector2i(13 * 16, 16 * 16));
                RB.DrawSprite(RB.SpriteIndex(6, 10) + spriteFrame, new Vector2i(67 * 16, 14 * 16));
            }
            else
            {
                RB.CameraReset();
                RB.ClipReset();
                RB.Print(new Vector2i(x + 18, y + 144), DemoUtil.IndexToRGB(14), "Failed to load TMX map.\nPlease try re-importing the map Demos/DemoReel/Tilemap.tmx in Unity");
            }

            RB.CameraReset();
            RB.ClipReset();
            RB.DrawRect(clipRect, DemoUtil.IndexToRGB(21));

            mMap = RB.MapLoadTMX("Demos/DemoReel/Tilemap");

            mFormatStr.Set("@C// Load a map from a TMX file\n");
            mFormatStr.Append("@Kvar@N map = @[email protected](@S\"Demos/Demo/Tilemap.xml\"@N);\n");
            mFormatStr.Append("@[email protected](map, @S\"Clouds\"@N, @L1@N);\n");
            mFormatStr.Append("@[email protected](map, @S\"Decoration\"@N, @L2@N);\n");
            mFormatStr.Append("@[email protected](map, @S\"Terrain\"@N, @L3@N);\n");
            mFormatStr.Append("@[email protected](@Knew @MRect2i(@L").Append(clipRect.x).Append("@N, @L").Append(clipRect.y).Append("@N, @L").Append(clipRect.width).Append("@N, @L").Append(clipRect.height).Append("@N));\n");
            mFormatStr.Append("@[email protected](@Knew@N @MRect2i@N(@L").Append(clipRect.x).Append("@N, @L").Append(clipRect.y).Append("@N, @L").Append(clipRect.width).Append("@N, @L").Append(clipRect.height).Append("@N), @I22);\n");
            mFormatStr.Append("@[email protected](@Knew @MVector2i@N(@L").Append(cameraPos.x).Append("@N, @L").Append(cameraPos.y).Append("@N));\n");
            mFormatStr.Append("@Kint@N pos = @L").Append(-(int)RB.Ticks).Append("@N % (map.size.width * @[email protected]);\n");
            mFormatStr.Append("@[email protected](@L1@N, @Knew @MVector2i@N(pos, @L0@N));\n");
            mFormatStr.Append("@[email protected](@L1@N, @Knew @MVector2i@N(pos + (map.size.width * @[email protected])), @L0@N));\n");
            mFormatStr.Append("@[email protected](@L2@N);\n");
            mFormatStr.Append("@[email protected](@L3@N);\n");
            mFormatStr.Append("@[email protected](@L").Append(0 + spriteFrame).Append("@N, @Knew@N @MVector2i@N(@L").Append(13 * 16).Append("@N, @L").Append(16 * 16).Append("@N));\n");
            mFormatStr.Append("@[email protected](@L").Append(RB.SpriteIndex(6, 10) + spriteFrame).Append("@N, @Knew @MVector2i@N(@L").Append(67 * 16).Append("@N, @L").Append(14 * 16).Append("@N));\n");
            mFormatStr.Append("@[email protected]();\n");
            mFormatStr.Append("@[email protected](@Knew @MRect2i(@L").Append(clipRect.x).Append("@N, @L").Append(clipRect.y).Append("@N, @L").Append(clipRect.width).Append("@N, @L").Append(clipRect.height).Append("@N), @I21);\n");

            RB.Print(new Vector2i(x, y), DemoUtil.IndexToRGB(5), DemoUtil.HighlightCode(mFormatStr, mFinalStr));
        }
Пример #4
0
        private bool LoadMap()
        {
            mTitleMap = RB.MapLoadTMX("Demos/SuperFlagRun/TitleMap");
            mGameMap  = RB.MapLoadTMX("Demos/SuperFlagRun/GameMap");

            if (mTitleMap != null)
            {
                RB.MapLoadTMXLayer(mTitleMap, "Terrain", SuperFlagRun.MAP_LAYER_TITLE_TERRAIN);
                RB.MapLoadTMXLayer(mTitleMap, "Deco", SuperFlagRun.MAP_LAYER_TITLE_DECO);
                RB.MapLoadTMXLayer(mTitleMap, "Sky", SuperFlagRun.MAP_LAYER_TITLE_SKY);
            }

            if (mGameMap != null)
            {
                RB.MapLoadTMXLayer(mGameMap, "Sky", SuperFlagRun.MAP_LAYER_SKY);
                RB.MapLoadTMXLayer(mGameMap, "Clouds", SuperFlagRun.MAP_LAYER_CLOUDS);
                RB.MapLoadTMXLayer(mGameMap, "Terrain", SuperFlagRun.MAP_LAYER_TERRAIN);
                RB.MapLoadTMXLayer(mGameMap, "Background", SuperFlagRun.MAP_LAYER_BACKGROUND);
            }

            RB.MapLayerSpriteSheetSet(SuperFlagRun.MAP_LAYER_TITLE_TERRAIN, SPRITESHEET_TERRAIN);
            RB.MapLayerSpriteSheetSet(SuperFlagRun.MAP_LAYER_TITLE_DECO, SPRITESHEET_DECO);
            RB.MapLayerSpriteSheetSet(SuperFlagRun.MAP_LAYER_TITLE_SKY, SPRITESHEET_DECO);

            RB.MapLayerSpriteSheetSet(SuperFlagRun.MAP_LAYER_SKY, SPRITESHEET_DECO);
            RB.MapLayerSpriteSheetSet(SuperFlagRun.MAP_LAYER_CLOUDS, SPRITESHEET_DECO);
            RB.MapLayerSpriteSheetSet(SuperFlagRun.MAP_LAYER_TERRAIN, SPRITESHEET_TERRAIN);
            RB.MapLayerSpriteSheetSet(SuperFlagRun.MAP_LAYER_BACKGROUND, SPRITESHEET_DECO);

            RB.SpriteSheetSet(SuperFlagRun.SPRITESHEET_TERRAIN);

            if (mGameMap != null)
            {
                mGameMapSize = mGameMap.size;

                // Convert TMXProperties to simple ColliderInfo.ColliderType, for faster access
                for (int x = 0; x < mGameMapSize.width; x++)
                {
                    for (int y = 0; y < mGameMapSize.height; y++)
                    {
                        var tilePos   = new Vector2i(x, y);
                        var tileProps = RB.MapDataGet <TMXProperties>(MAP_LAYER_TERRAIN, tilePos);
                        if (tileProps != null)
                        {
                            RB.MapDataSet <ColliderInfo.ColliderType>(MAP_LAYER_TERRAIN, tilePos, (ColliderInfo.ColliderType)tileProps.GetInt("ColliderType"));
                        }
                        else
                        {
                            RB.MapDataSet <ColliderInfo.ColliderType>(MAP_LAYER_TERRAIN, tilePos, ColliderInfo.ColliderType.NONE);
                        }
                    }
                }
            }

            return(true);
        }
Пример #5
0
 public void LoadTileMaps(GraphicsDevice graphics)
 {
     foreach (Room r in rooms)
     {
         if (r.room_id <= 117)
         {
             TMXMap newmap = new TMXMap(graphics, "TileMap" + r.room_id.ToString());
             r.tileMap = newmap;
         }
     }
 }
Пример #6
0
        /// <summary>
        /// Handle scene entry
        /// </summary>
        public override void Enter()
        {
            base.Enter();

            RB.SpriteSheetSetup(0, "Demos/DemoReel/Sprites", new Vector2i(16, 16));
            RB.SpriteSheetSet(0);

            mMap = RB.MapLoadTMX("Demos/DemoReel/TilemapProps");

            if (mMap != null)
            {
                RB.MapLoadTMXLayer(mMap, "Clouds", 1);
                RB.MapLoadTMXLayer(mMap, "Decoration", 2);
                RB.MapLoadTMXLayer(mMap, "Terrain", 3);
            }
        }
Пример #7
0
        /// <summary>
        /// Handle scene entry
        /// </summary>
        public override void Enter()
        {
            base.Enter();

            RB.SpriteSheetSetup(0, "Demos/DemoReel/SpritesSmall", new Vector2i(8, 8));
            RB.SpriteSheetSetup(1, "Demos/DemoReel/WaterSmall", new Vector2i(8, 8));

            mCameraPos      = Vector2i.zero;
            mChunkCameraPos = Vector2i.zero;
            mTopLeftChunk   = new Vector2i(-100000, 100000);

            RB.MapClear();
            mMap = RB.MapLoadTMX("Demos/DemoReel/TilemapInfinite");

            mClipRect  = new Rect2i(16, 4 + 219, 400, (16 * 8) + 2);
            mCameraPos = Vector2i.zero;
        }
Пример #8
0
        /// <summary>
        /// Handle scene entry
        /// </summary>
        public override void Enter()
        {
            base.Enter();

            RB.SpriteSheetSetup(0, "Demos/DemoReel/Sprites", new Vector2i(16, 16));
            RB.SpriteSheetSet(0);

            RB.EffectReset();

            mMap = RB.MapLoadTMX("Demos/DemoReel/Tilemap");

            if (mMap != null)
            {
                RB.MapLoadTMXLayer(mMap, "Decoration", 0);
                RB.MapLoadTMXLayer(mMap, "Terrain", 1);
            }

            RB.ShaderSetup(0, "Demos/DemoReel/PresentRippleShader");
        }
Пример #9
0
        /// <summary>
        /// Handle scene entry
        /// </summary>
        public override void Enter()
        {
            base.Enter();

            RB.SpriteSheetSetup(0, "Demos/DemoReel/Sprites", new Vector2i(16, 16));
            RB.SpriteSheetSet(0);

            RB.EffectReset();

            mMap = RB.MapLoadTMX("Demos/DemoReel/Tilemap");

            if (mMap != null)
            {
                mMapSize = mMap.size;

                RB.MapLoadTMXLayer(mMap, "Clouds", 0);
                RB.MapLoadTMXLayer(mMap, "Decoration", 1);
                RB.MapLoadTMXLayer(mMap, "Terrain", 2);
            }

            mEffectNames[(int)RB.Effect.Scanlines]       = "Scanlines";
            mEffectNames[(int)RB.Effect.Noise]           = "Noise";
            mEffectNames[(int)RB.Effect.Desaturation]    = "Desaturation";
            mEffectNames[(int)RB.Effect.Curvature]       = "Curvature";
            mEffectNames[(int)RB.Effect.Slide]           = "Slide";
            mEffectNames[(int)RB.Effect.Wipe]            = "Wipe";
            mEffectNames[(int)RB.Effect.Shake]           = "Shake";
            mEffectNames[(int)RB.Effect.Zoom]            = "Zoom";
            mEffectNames[(int)RB.Effect.Rotation]        = "Rotation";
            mEffectNames[(int)RB.Effect.ColorFade]       = "ColorFade";
            mEffectNames[(int)RB.Effect.ColorTint]       = "ColorTint";
            mEffectNames[(int)RB.Effect.Negative]        = "Negative";
            mEffectNames[(int)RB.Effect.Pixelate]        = "Pixelate";
            mEffectNames[(int)RB.Effect.Pinhole]         = "Pinhole";
            mEffectNames[(int)RB.Effect.InvertedPinhole] = "InvertedPinhole";
            mEffectNames[(int)RB.Effect.Fizzle]          = "Fizzle";
        }
Пример #10
0
        /// <summary>
        /// Load a single layer chunk
        /// </summary>
        /// <param name="tmx">Map definition</param>
        /// <param name="tmxSourceLayer">Name of source layer</param>
        /// <param name="destinationLayer">RetroBlit destination layer</param>
        /// <param name="chunkOffset">Chunk offset</param>
        /// <param name="destPos">Destination position</param>
        /// <param name="packedSpriteLookup">Lookup table for translating TMX tile indexes to packed sprites</param>
        /// <returns>True if successful</returns>
        public bool LoadTMXLayerChunk(TMXMap tmx, string tmxSourceLayer, int destinationLayer, Vector2i chunkOffset, Vector2i destPos, PackedSpriteID[] packedSpriteLookup)
        {
            TMXMapDef map = null;

            if (!(tmx is RetroBlitInternal.RetroBlitTilemap.TMXMapDef))
            {
                Debug.LogError("Can't load TMX layer, invalid map object!");
                return(false);
            }

            map = (TMXMapDef)tmx;

            if (map == null || map.realPathName == null || map.realPathName.Length == 0 || map.layers == null)
            {
                Debug.LogError("Can't load TMX layer, invalid map, or map not open yet!");
                return(false);
            }

            if (!map.infinite)
            {
                Debug.LogError("TMX map is not infinite, use LoadTMXLayer() instead");
                return(false);
            }

            if (!map.layers.ContainsKey(tmxSourceLayer))
            {
                Debug.LogError("Layer " + tmxSourceLayer + " not found");
                return(false);
            }

            int chunkWidth  = map.chunkSize.x;
            int chunkHeight = map.chunkSize.y;

            var tmxLayer = (TMXLayerDef)map.layers[tmxSourceLayer];

            ulong part1  = (ulong)chunkOffset.x;
            ulong part2  = (ulong)chunkOffset.y;
            ulong offset = ((part1 << 32) & 0xFFFFFFFF00000000) | (part2 & 0xFFFFFFFF);

            int layerNameHash = mWorkStr.Set(tmxSourceLayer).ToLowerInvariant().GetHashCode();
            var tupleKey      = new RetroBlitTuple <int, ulong>(layerNameHash, offset);

            var decompressed = map.mChunkLRU.Get(tupleKey);

            if (decompressed == null)
            {
                var chunkTable = GetLayerIndexTable(map, layerNameHash);

                if (chunkTable == null)
                {
                    Debug.LogError("TMX could not load chunk index table for layer " + tmxSourceLayer);
                    return(false);
                }

                // If the chunk can't be found then fail silently and wipe the chunk area. This will also
                // release the chunk geometry on next draw because it will not have any vertices
                if (!chunkTable.ContainsKey(offset))
                {
                    for (int y = destPos.y; y < destPos.y + chunkHeight; y++)
                    {
                        for (int x = destPos.x; x < destPos.x + chunkWidth; x++)
                        {
                            mRetroBlitAPI.Tilemap.SpriteSet(destinationLayer, x, y, RB.SPRITE_EMPTY, Color.white, 0);

                            Tile[] tilesArr;
                            int    tileIndex;
                            if (GetTileRef(destinationLayer, x, y, out tilesArr, out tileIndex, true))
                            {
                                tilesArr[tileIndex].data = null;
                            }
                        }
                    }

                    return(true);
                }

                var chunkDef = chunkTable[offset];

                var chunkFileName = map.realPathName + "layer_" + layerNameHash.ToString("x") + "_seg_" + chunkDef.segmentIndex;

                var chunkFile = Resources.Load <TextAsset>(chunkFileName);

                if (chunkFile == null)
                {
                    Debug.LogError("Can't find TMX file when loading TMX layer!");
                    return(false);
                }

                var chunkBytes = chunkFile.bytes;

                decompressed = RetroBlitDeflate.Decompress(chunkBytes, chunkDef.segmentOffset, chunkDef.compressedLength);
                if (decompressed == null || decompressed.Length <= 0)
                {
                    Debug.LogError("Could not decompress tile data for layer " + tmxSourceLayer);
                    return(false);
                }

                map.mChunkLRU.Add(tupleKey, decompressed, decompressed.Length);
            }

            var tileDataReader = new BinaryReader(new MemoryStream(decompressed));

            if (tileDataReader == null)
            {
                Debug.LogError("Could not read tile data for layer " + tmxSourceLayer);
                return(false);
            }

            Color32 color = Color.white;

            int sx = 0;
            int sy = 0;

            int dx = destPos.x;
            int dy = destPos.y;

            while (tileDataReader.PeekChar() >= 0)
            {
                // Skip tsxIndex, don't need it for now
                tileDataReader.ReadByte();

                byte flags  = tileDataReader.ReadByte();
                int  tileId = tileDataReader.ReadInt32();

                if (packedSpriteLookup != null)
                {
                    if (packedSpriteLookup != null)
                    {
                        if (tileId < packedSpriteLookup.Length && tileId >= 0)
                        {
                            tileId = packedSpriteLookup[tileId].id;
                            flags |= RetroBlitInternal.RetroBlitTilemap.SPRITEPACK;
                        }
                    }
                }

                SpriteSet(destinationLayer, dx, dy, tileId, color, flags);
                dx++;
                sx++;

                if (sx >= chunkWidth)
                {
                    sx = 0;
                    dx = destPos.x;
                    sy++;
                    dy++;
                }

                if (sy >= chunkHeight)
                {
                    break;
                }
            }

            return(true);
        }
Пример #11
0
        /// <summary>
        /// Load a layer definition from an map definition
        /// </summary>
        /// <param name="tmx">Map definition</param>
        /// <param name="tmxSourceLayer">Name of the layer to load</param>
        /// <param name="destinationLayer">Destination RetroBlit layer</param>
        /// <param name="sourceRect">Source rectangle</param>
        /// <param name="destPos">Destination position</param>
        /// <param name="packedSpriteLookup">Lookup table for translating TMX tile indexes to packed sprites</param>
        /// <returns>True if successful</returns>
        public bool LoadTMXLayer(TMXMap tmx, string tmxSourceLayer, int destinationLayer, Rect2i sourceRect, Vector2i destPos, PackedSpriteID[] packedSpriteLookup)
        {
            TMXMapDef map = null;

            if (!(tmx is RetroBlitInternal.RetroBlitTilemap.TMXMapDef))
            {
                Debug.LogError("Can't load TMX layer, invalid map object!");
                return(false);
            }

            map = (TMXMapDef)tmx;

            if (map == null || map.realPathName == null || map.realPathName.Length == 0 || map.layers == null)
            {
                Debug.LogError("Can't load TMX layer, invalid map, or map not open yet!");
                return(false);
            }

            if (map.infinite)
            {
                Debug.LogError("TMX map is infinite, use MapLoadTMXLayerChunk() instead");
                return(false);
            }

            if (!map.layers.ContainsKey(tmxSourceLayer))
            {
                Debug.LogError("Layer " + tmxSourceLayer + " not found");
                return(false);
            }

            var tmxLayer = (TMXLayerDef)map.layers[tmxSourceLayer];

            var layerNameHash = mWorkStr.Set(tmxSourceLayer).ToLowerInvariant().GetHashCode().ToString("x");

            var tmxFileName = map.realPathName + "layer_" + layerNameHash;
            var tmxFile     = Resources.Load <TextAsset>(tmxFileName);

            if (tmxFile == null)
            {
                Debug.LogError("Can't find TMX file when loading TMX layer!");
                return(false);
            }

            var tmxBytes = tmxFile.bytes;

            var decompressed = RetroBlitDeflate.Decompress(tmxBytes, 0, tmxBytes.Length);

            if (decompressed == null || decompressed.Length <= 0)
            {
                Debug.LogError("Could not decompress tile data for layer " + tmxSourceLayer);
                return(false);
            }

            var tileDataReader = new BinaryReader(new MemoryStream(decompressed));

            if (tileDataReader == null)
            {
                Debug.LogError("Could not read tile data for layer " + tmxSourceLayer);
                return(false);
            }

            Color32 color = Color.white;

            int sx = 0;
            int sy = 0;

            int sx0 = sourceRect.x;
            int sx1 = sourceRect.x + sourceRect.width;

            int sy0 = sourceRect.y;
            int sy1 = sourceRect.y + sourceRect.height;

            int dx = destPos.x;
            int dy = destPos.y;

            while (tileDataReader.PeekChar() >= 0)
            {
                byte tsxIndex = tileDataReader.ReadByte();
                byte flags    = tileDataReader.ReadByte();
                int  tileId   = tileDataReader.ReadInt32();

                if (packedSpriteLookup != null)
                {
                    if (tileId < packedSpriteLookup.Length && tileId >= 0)
                    {
                        tileId = packedSpriteLookup[tileId].id;
                        flags |= RetroBlitInternal.RetroBlitTilemap.SPRITEPACK;
                    }
                }

                if (sx >= sx0 && sx <= sx1 && sy >= sy0 && sy <= sy1)
                {
                    SpriteSet(destinationLayer, dx, dy, tileId, color, flags);

                    // Set properties if available
                    if (tsxIndex >= 0 && tsxIndex < map.allTileProperties.Count)
                    {
                        var props = map.allTileProperties[tsxIndex];
                        if (props != null)
                        {
                            if (props.ContainsKey(tileId))
                            {
                                DataSet <TMXProperties>(destinationLayer, dx, dy, props[tileId]);
                            }
                        }
                    }

                    dx++;
                }

                sx++;
                if (sx >= tmxLayer.size.x)
                {
                    sx = 0;
                    dx = destPos.x;
                    sy++;
                    dy++;
                }

                if (sy >= tmxLayer.size.y || sy >= sourceRect.y + sourceRect.height)
                {
                    break;
                }
            }

            return(true);
        }