Beispiel #1
0
        private CanvasInformation cacheHeightMapForChunk(TileChunk chunk)
        {
            var md = chunk;
            var posj1 = new Point(0, 0);
            var canv = CanvasInformation.Create(128, 128, false);
            var ctx = canv.Context;
            engine.Clear(canv);
            for (var _y = 0; _y < 8; _y++)
            {
                for (var _x = 0; _x < 8; _x++)
                {
                    var tp = md.TilePieces[_x][_y];
                    var solid = (int)(SonicLevel.CurHeightMap ? tp.Solid1 : tp.Solid2);

                    var hd = SonicLevel.CurHeightMap ? tp.GetLayer1HeightMaps() : tp.GetLayer2HeightMaps();

                    var __x = _x;
                    var __y = _y;
                    var vangle = 0;
                    var posm = new Point(posj1.X + (__x * 16), posj1.Y + (__y * 16));

                    if (hd.Falsey()) continue;
                    if (hd.Full == false)
                    {
                    }
                    else if (hd.Full == true)
                    {
                        if (solid > 0)
                        {
                            ctx.FillStyle = HeightMap.colors[solid];
                            ctx.FillRect(posj1.X + (__x * 16),
                                posj1.Y + (__y * 16),
                                16,
                                16);
                        }
                    }
                    else
                    {
                        vangle = SonicLevel.CurHeightMap ? tp.GetLayer1Angles() : tp.GetLayer2Angles();
                        hd.Draw(ctx, posm, tp.XFlip, tp.YFlip, solid, vangle);
                        /*   posm.x += 16 * scale.x / 2;
                                        posm.y += 16 * scale.y / 2;
                                        ctx.strokeStyle = "#DDD";
                                        ctx.font = "18pt courier ";
                                        ctx.shadowColor = "";
                                        ctx.shadowBlur = 0;
                                        ctx.lineWidth = 1;
                                        ctx.strokeText(vangle.toString(16), posm.x - 12, posm.y + 7);*/
                    }
                }
            }
            return SpriteCache.HeightMapChunks[(SonicLevel.CurHeightMap ? 1 : 2) + " " + md.Index]
                    = canv;
        }
        public void Load(SLData sonicLevel)
        {
            Loading = true;
            Status = "Decoding";
            Status = "Determining Level Information";

            SonicLevel = new SonicLevel();
            for (var n = 0; n < sonicLevel.Rings.Length; n++)
            {
                SonicLevel.Rings[n] = new Ring(true);

                SonicLevel.Rings[n].X = sonicLevel.Rings[n].X;
                SonicLevel.Rings[n].Y = sonicLevel.Rings[n].Y;
            }

            SonicLevel.LevelWidth = sonicLevel.ForegroundWidth;
            SonicLevel.LevelHeight = sonicLevel.ForegroundHeight;
            SonicLevel.ChunkMap = sonicLevel.Foreground;
            SonicLevel.BGChunkMap = sonicLevel.Background;

            for (int l = 0; l < sonicLevel.Objects.Length; l++)
            {
                SonicLevel.Objects[l] = new LevelObjectInfo(sonicLevel.Objects[l]);
                SonicLevel.Objects[l].Index = l;
            }

            var objectKeys = new List<string>();

            foreach (LevelObjectInfo t in SonicLevel.Objects)
            {
                var o = t.Key;
                if (objectKeys.All(p => p != o)) objectKeys.Add(o);
            }
            loadObjects(objectKeys);

            for (int j = 0; j < sonicLevel.Tiles.Length; j++)
            {
                var fc = sonicLevel.Tiles[j];
                var tiles = fc;
                List<int> mj = new List<int>();

                for (int i = 0; i < tiles.Length; i++)
                {
                    var value = sonicLevel.Tiles[j][i];
                    mj.Add((value >> 4));
                    mj.Add((value & 0xF));
                }
                var mfc = new int[8][];
                for (int o = 0; o < 8; o++)
                {
                    mfc[o] = new int[8];
                }
                for (int n = 0; n < mj.Count; n++)
                {
                    mfc[n % 8][n / 8] = mj[n];
                }

                SonicLevel.Tiles[j] = new Tile(mfc);
                SonicLevel.Tiles[j].Index = j;
            }
            var acs = SonicLevel.AnimatedChunks = new List<TileChunk>();

            if (sonicLevel.AnimatedFiles.Truthy())
            {
                SonicLevel.AnimatedTileFiles = new Tile[sonicLevel.AnimatedFiles.Length][];
                for (var animatedFileIndex = 0; animatedFileIndex < sonicLevel.AnimatedFiles.Length; animatedFileIndex++)
                {
                    var animatedFile = sonicLevel.AnimatedFiles[animatedFileIndex];
                    SonicLevel.AnimatedTileFiles[animatedFileIndex] = new Tile[animatedFile.Length];

                    for (int filePiece = 0; filePiece < animatedFile.Length; filePiece++)
                    {
                        var c = animatedFile[filePiece];
                        var tiles = c;
                        List<int> mjc = new List<int>();

                        for (int l = 0; l < tiles.Length; l++)
                        {
                            var value = animatedFile[filePiece][l];
                            mjc.Add((value >> 4));
                            mjc.Add((value & 0xF));
                        }
                        var mfc = new int[8][];
                        for (int o = 0; o < 8; o++)
                        {
                            mfc[o] = new int[8];
                        }
                        for (int n = 0; n < mjc.Count; n++)
                        {
                            mfc[n % 8][n / 8] = mjc[n];
                        }
                        Tile tile = new Tile(mfc);
                        tile.IsTileAnimated = true;

                        tile.Index = filePiece * 10000 + animatedFileIndex;
                        SonicLevel.AnimatedTileFiles[animatedFileIndex][filePiece] = tile;
                    }
                }
            }

            for (int j = 0; j < sonicLevel.Blocks.Length; j++)
            {
                var fc = sonicLevel.Blocks[j];
                var mj = new TilePiece();
                mj.Index = j;
                mj.Tiles = new List<TileInfo>();

                for (int p = 0; p < fc.Length; p++)
                {
                    mj.Tiles.Add(new TileInfo()
                    {
                        _Tile = fc[p].Tile,
                        Index = p,
                        Palette = fc[p].Palette,
                        Priority = fc[p].Priority,
                        XFlip = fc[p].XFlip,
                        YFlip = fc[p].YFlip,
                    });
                }
                mj.Init();
                SonicLevel.TilePieces[j] = mj;
            }

            SonicLevel.Angles = sonicLevel.Angles;
            SonicLevel.TileAnimations =
                    new List<TileAnimationData>(
                            sonicLevel.Animations.Map(
                                    a =>
                                    new TileAnimationData()
                                    {
                                        AnimationTileFile = a.AnimationFile,
                                        AnimationTileIndex = a.AnimationTileIndex,
                                        AutomatedTiming = a.AutomatedTiming,
                                        NumberOfTiles = a.NumberOfTiles,
                                        DataFrames =
                                                (TileAnimationDataFrame[])
                                                a.Frames.Map(
                                                        b =>
                                                        new TileAnimationDataFrame() { Ticks = b.Ticks, StartingTileIndex = b.StartingTileIndex }).Slice(0)
                                    }));
            SonicLevel.CollisionIndexes1 = sonicLevel.CollisionIndexes1;
            SonicLevel.CollisionIndexes2 = sonicLevel.CollisionIndexes2;

            for (int i = 0; i < sonicLevel.HeightMaps.Length; i++)
            {
                var b1 = true;
                var b2 = true;
                for (int m = 0; m < sonicLevel.HeightMaps[i].Length; m++)
                {
                    if (b1 && sonicLevel.HeightMaps[i][m] != 0)
                        b1 = false;

                    if (b2 && sonicLevel.HeightMaps[i][m] != 16)
                        b2 = false;
                }
                if (b1)
                    SonicLevel.HeightMaps[i] = new HeightMap(false);
                else if (b2)
                    SonicLevel.HeightMaps[i] = new HeightMap(true);
                else
                    SonicLevel.HeightMaps[i] = new HeightMap(sonicLevel.HeightMaps[i], i);
            }

            for (int j = 0; j < sonicLevel.Chunks.Length; j++)
            {
                var fc = sonicLevel.Chunks[j];
                var mj = new TileChunk();
                mj.Index = j;
                mj.TilePieces = new TilePieceInfo[8][];
                for (int i = 0; i < 8; i++)
                {
                    mj.TilePieces[i] = new TilePieceInfo[8];
                }
                for (int p = 0; p < fc.Length; p++)
                {
                    mj.TilePieces[p % 8][p / 8] = new TilePieceInfo()
                    {
                        Index = p,
                        Block = fc[p].Block,
                        Solid1 = fc[p].Solid1,
                        Solid2 = fc[p].Solid2,
                        XFlip = fc[p].XFlip,
                        YFlip = fc[p].YFlip
                    };
                }

                SonicLevel.TileChunks[j] = mj;
                mj.TileAnimations = new JsDictionary<int, TileAnimationData>();
                for (int tpX = 0; tpX < mj.TilePieces.Length; tpX++)
                {
                    for (int tpY = 0; tpY < mj.TilePieces[tpX].Length; tpY++)
                    {
                        var pm = mj.TilePieces[tpX][tpY].GetTilePiece();
                        if (pm != null)
                        {
                            foreach (var mjc in pm.Tiles)
                            {
                                var fa = containsAnimatedTile(mjc._Tile, SonicLevel);
                                if (fa.Truthy())
                                {
                                    mj.TileAnimations[tpY * 8 + tpX] = fa;
                                    acs[j] = mj;
                                }
                            }
                        }
                    }
                }
            }

            SonicLevel.Palette = sonicLevel.Palette.Map(a => a.Map(b => "#"+b));
            SonicLevel.StartPositions = sonicLevel.StartPositions.Map(a => new Point(a.X, a.Y)).Array();

            SonicLevel.AnimatedPalettes = new List<PaletteItem>();
            if (sonicLevel.PaletteItems.Length > 0)
            {

                for (int k = 0; k < sonicLevel.PaletteItems[0].Length; k++)
                {
                    AnimatedPaletteItem pal = sonicLevel.PaletteItems[0][k];
                    SonicLevel.AnimatedPalettes.Add(new PaletteItem()
                    {
                        Palette = ((string[])Script.Eval(pal.Palette)).Map(b => "#" + b),
                        SkipIndex = pal.SkipIndex,
                        TotalLength = pal.TotalLength,
                        Pieces =
                                pal.Pieces.Map(a => new PaletteItemPieces()
                                {
                                    PaletteIndex = a.PaletteIndex,
                                    PaletteMultiply = a.PaletteMultiply,
                                    PaletteOffset = a.PaletteOffset
                                })
                    });
                }
            }

            foreach (var tilePiece in SonicLevel.TilePieces)
            {
                tilePiece.AnimatedPaletteIndexes = new List<int>();
                tilePiece.AnimatedTileIndexes = new List<int>();
                if (SonicLevel.AnimatedPalettes.Count > 0)
                {
                    foreach (var mj in tilePiece.Tiles)
                    {
                        Tile tile = mj.GetTile();

                        if (tile.Truthy())
                        {
                            tile.AnimatedPaletteIndexes = new List<int>();
                            var pl = tile.GetAllPaletteIndexes();
                            tile.PaletteIndexesToBeAnimated = new JsDictionary<int, List<int>>();
                            tile.AnimatedTileIndexes = new List<int>();


                            for (int tileAnimationIndex = 0; tileAnimationIndex < SonicLevel.TileAnimations.Count; tileAnimationIndex++)
                            {
                                var tileAnimationData = SonicLevel.TileAnimations[tileAnimationIndex];

                                var anin = tileAnimationData.AnimationTileIndex;
                                var num = tileAnimationData.NumberOfTiles;
                                if (tile.Index >= anin && tile.Index < anin + num)
                                {
                                    tilePiece.AnimatedTileIndexes.Add(tileAnimationIndex);
                                    tile.AnimatedTileIndexes.Add(tileAnimationIndex);
                                }
                            }


                            for (int animatedPaletteIndex = 0; animatedPaletteIndex < SonicLevel.AnimatedPalettes.Count; animatedPaletteIndex++)
                            {
                                var pal = SonicLevel.AnimatedPalettes[animatedPaletteIndex];
                                tile.PaletteIndexesToBeAnimated[animatedPaletteIndex] = new List<int>();

                                foreach (var mjce in pal.Pieces)
                                {
                                    PaletteItemPieces mje1 = mjce;
                                    if (mj.Palette == mje1.PaletteIndex)
                                    {
                                        if (pl.Any(j => j == mje1.PaletteOffset / 2 || j == mje1.PaletteOffset / 2 + 1))
                                        {
                                            tilePiece.AnimatedPaletteIndexes.Add(animatedPaletteIndex);
                                            tile.AnimatedPaletteIndexes.Add(animatedPaletteIndex);
                                            foreach (var pIndex in pl)
                                            {
                                                if (pIndex == mje1.PaletteOffset / 2 || pIndex == mje1.PaletteOffset / 2 + 1)
                                                {
                                                    tile.PaletteIndexesToBeAnimated[animatedPaletteIndex].Add(pIndex);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }


            var finished = new Action(() => { Loading = false; });
            PreloadSprites(() =>
            {
                finished();
                ForceResize();
            },
                           (s) => { });
            ForceResize();
            OnLevelLoad(SonicLevel);

            /* 

               

        var finished = function () {
            sonicManager.uiManager.levelManagerArea.visible = true;
            sonicManager.loading = false;
            sonicManager.uiManager.modifyTC.tileChunk = sonicManager.SonicLevel.TileChunks[0];
            sonicManager.uiManager.modifyTilePieceArea.tilePiece = sonicManager.uiManager.modifyTP.tilePiece = sonicManager.SonicLevel.TilePieces[0];

        };

        //        var inds = sonicManager.inds = { r:0,t: 0, tp: 0, tc: 0, total: (sonicManager.SonicLevel.TileChunks.length * 2 + sonicManager.SonicLevel.TilePieces.length * 5 + sonicManager.SonicLevel.Tiles.length), done: false };

        sonicManager.CACHING = true;
        sonicManager.preLoadSprites(scale, function () {
            //          inds.r = 1;
            sonicManager.CACHING = false;
            finished();

            sonicManager.uiManager.updateTitle("Level Loaded");
            sonicManager.forceResize();


            var dl = _H.getQueryString();
            if (dl["run"]) {
                setTimeout(sonicManager.uiManager.runSonic, 1000);
            }

        }, sonicManager.uiManager.updateTitle);

 

*/
        }
Beispiel #3
0
 public void SetChunkAt(int x, int y, TileChunk tileChunk)
 {
     ChunkMap[x][y] = tileChunk.Index;
 }
        public void BuildChunk(TileChunk chunk, bool isLayerOne)
        {
            if (isLayerOne)
            {
                if (chunk.HeightBlocks1.Truthy())
                    return;
                var hb1 = chunk.HeightBlocks1 = new Solidity[128][];
                var ab1 = chunk.AngleMap1 = new int[8][];
                for (var _1 = 0; _1 < 128; _1++)
                {
                    hb1[_1] = new Solidity[128];
                }
                for (var _1 = 0; _1 < 8; _1++)
                {
                    ab1[_1] = new int[8];
                }

                for (var _y = 0; _y < 8; _y++)
                {
                    for (var _x = 0; _x < 8; _x++)
                    {
                        var tp = chunk.TilePieces[_x][_y];

                        ab1[_x][_y] = tp.GetLayer1Angles();

                        if (!(ab1[_x][_y] == 0 || ab1[_x][_y] == 255 || ab1[_x][_y] == 1))
                        {
                            if (tp.XFlip)
                            {
                                if (tp.YFlip)
                                {
                                    ab1[_x][_y] = 192 - ab1[_x][_y] + 192;

                                    ab1[_x][_y] = 128 - ab1[_x][_y] + 128;
                                }
                                else
                                    ab1[_x][_y] = 128 - ab1[_x][_y] + 128;
                            }
                            else
                            {
                                if (tp.YFlip)
                                    ab1[_x][_y] = 192 - ab1[_x][_y] + 192;
                                else
                                    ab1[_x][_y] = (ab1[_x][_y]);
                            }
                        }

                        var __x = 0;
                        var __y = 0;

                        HeightMap heightMask = tp.GetLayer1HeightMaps();
                        int[] heightMaskItems = null;
                        if (heightMask == null) continue;
                        Solidity mj;
                        if (heightMask.Full != null)
                        {
                            mj = !heightMask.Full.Value ? 0 : tp.Solid1;
                            for (__y = 0; __y < 16; __y++)
                            {
                                for (__x = 0; __x < 16; __x++)
                                {
                                    hb1[(_x * 16 + __x)][(_y * 16 + __y)] = mj;
                                }
                            }
                        }
                        else
                            heightMaskItems = heightMask.Items;

                        for (__y = 0; __y < 16; __y++)
                        {
                            for (__x = 0; __x < 16; __x++)
                            {
                                var jx = 0;
                                var jy = 0;
                                if (tp.XFlip)
                                {
                                    if (tp.YFlip)
                                    {
                                        jx = 15 - __x;
                                        jy = 15 - __y;
                                    }
                                    else
                                    {
                                        jx = 15 - __x;
                                        jy = __y;
                                    }
                                }
                                else
                                {
                                    if (tp.YFlip)
                                    {
                                        jx = __x;
                                        jy = 15 - __y;
                                    }
                                    else
                                    {
                                        jx = __x;
                                        jy = __y;
                                    }
                                }

                                if (heightMask.Full == null)
                                {
                                    switch (tp.Solid1)
                                    {
                                        case 0:
                                            hb1[(_x * 16 + jx)][(_y * 16 + jy)] = 0;
                                            break;
                                        case (Solidity)1:
                                        case (Solidity)2:
                                        case (Solidity)3:
                                            hb1[(_x * 16 + jx)][(_y * 16 + jy)] = HeightMap.ItemsGood(heightMaskItems, __x, __y) ? tp.Solid1 : 0;
                                            break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                if (chunk.HeightBlocks2.Truthy())
                    return;

                var hb2 = chunk.HeightBlocks2 = new Solidity[128][];
                var ab2 = chunk.AngleMap2 = new int[8][];
                for (var _1 = 0; _1 < 128; _1++)
                {
                    hb2[_1] = new Solidity[128];
                }
                for (var _1 = 0; _1 < 8; _1++)
                {
                    ab2[_1] = new int[8];
                }

                for (var _y = 0; _y < 8; _y++)
                {
                    for (var _x = 0; _x < 8; _x++)
                    {
                        var tp = chunk.TilePieces[_x][_y];

                        ab2[_x][_y] = tp.GetLayer2Angles();

                        if (!(ab2[_x][_y] == 0 || ab2[_x][_y] == 255 || ab2[_x][_y] == 1))
                        {
                            if (tp.XFlip)
                            {
                                if (tp.YFlip)
                                {
                                    ab2[_x][_y] = 192 - ab2[_x][_y] + 192;

                                    ab2[_x][_y] = 128 - ab2[_x][_y] + 128;
                                }
                                else
                                    ab2[_x][_y] = 128 - ab2[_x][_y] + 128;
                            }
                            else
                            {
                                if (tp.YFlip)
                                    ab2[_x][_y] = 192 - ab2[_x][_y] + 192;
                                else
                                    ab2[_x][_y] = (ab2[_x][_y]);
                            }
                        }

                        int __x;
                        int __y;
                        var hd2 = tp.GetLayer2HeightMaps();
                        if (hd2 == null) continue;
                        Solidity mj;

                        int[] hd2Items = null;

                        if (hd2.Full != null)
                        {
                            mj = hd2.Full == false ? 0 : tp.Solid2;
                            for (__y = 0; __y < 16; __y++)
                            {
                                for (__x = 0; __x < 16; __x++)
                                {
                                    hb2[(_x * 16 + __x)][(_y * 16 + __y)] = mj;
                                }
                            }
                        }
                        else
                            hd2Items = hd2.Items;

                        for (__y = 0; __y < 16; __y++)
                        {
                            for (__x = 0; __x < 16; __x++)
                            {
                                var jx = 0;
                                var jy = 0;
                                if (tp.XFlip)
                                {
                                    if (tp.YFlip)
                                    {
                                        jx = 15 - __x;
                                        jy = 15 - __y;
                                    }
                                    else
                                    {
                                        jx = 15 - __x;
                                        jy = __y;
                                    }
                                }
                                else
                                {
                                    if (tp.YFlip)
                                    {
                                        jx = __x;
                                        jy = 15 - __y;
                                    }
                                    else
                                    {
                                        jx = __x;
                                        jy = __y;
                                    }
                                }

                                if (hd2.Full == null)
                                {
                                    switch (tp.Solid2)
                                    {
                                        case (Solidity)0:
                                            hb2[(_x * 16 + jx)][(_y * 16 + jy)] = Solidity.NotSolid;
                                            break;
                                        case (Solidity)1:
                                        case (Solidity)2:
                                        case (Solidity)3:
                                            hb2[(_x * 16 + jx)][(_y * 16 + jy)] = HeightMap.ItemsGood(hd2Items, __x, __y) ? tp.Solid2 : 0;
                                            break;
                                    }
                                }

                                //imap[(x * 128 + _x * 16 + __x) + (y * 128 + _y * 16 + __y) * (SonicManager.Instance.SonicLevel.LevelWidth)] = tp.heightMask.angle;
                            }
                        }
                    }
                }
            }
        }