Esempio n. 1
0
        public Theater(TileSet tileset)
        {
            this.tileset = tileset;

            var allocated = false;

            Func <Sheet> allocate = () =>
            {
                if (allocated)
                {
                    throw new SheetOverflowException("Terrain sheet overflow.Try increasing the tileset SheetSize parameter.");
                }
                allocated = true;
                return(new Sheet(SheetT.Indexed, new Size(tileset.SheetSize, tileset.SheetSize)));
            };

            sheetBuilder = new SheetBuilder(SheetT.Indexed, allocate);
            random       = new MersenneTwister();

            var frameCache = new FrameCache(WarGame.ModData.DefaultFileSystem, WarGame.ModData.SpriteLoaders);

            foreach (var t in tileset.Templates)
            {
                var variants = new List <Sprite[]>();

                foreach (var i in t.Value.Images)
                {
                    var allFrames  = frameCache[i];
                    var frameCount = tileset.EnableDepth ? allFrames.Length / 2 : allFrames.Length;
                    var indices    = t.Value.Frames != null ? t.Value.Frames : Enumerable.Range(0, frameCount);
                    variants.Add(indices.Select(j => {
                        var f    = allFrames[j];
                        var tile = t.Value.Contains(j)?t.Value[j]:null;

                        //The internal z axis is inverted from expectation (negative is closer)
                        var zOffset = tile != null ? -tile.ZOffset : 0;

                        var zRamp = tile != null ? tile.ZRamp : 1f;

                        var offset = new Vector3(f.Offset, zOffset);

                        var s = sheetBuilder.Allocate(f.Size, zRamp, offset);

                        Util.FastCopyIntoChannel(s, f.Data);

                        if (tileset.EnableDepth)
                        {
                            var ss = sheetBuilder.Allocate(f.Size, zRamp, offset);
                            Util.FastCopyIntoChannel(ss, allFrames[j + frameCount].Data);

                            //s and ss are guaranteed to use the same sheet because of the custom terrain sheet allocation.
                            s = new SpriteWithSecondaryData(s, ss.Bounds, ss.Channel);
                        }
                        return(s);
                    }).ToArray());
                }

                var allSprites = variants.SelectMany(s => s);

                if (tileset.IgnoreTileSpriteOffsets)
                {
                    allSprites = allSprites.Select(s => new Sprite(s.Sheet, s.Bounds, s.ZRamp, new Vector3(Vector2.Zero, s.Offset.Z), s.Channel, s.BlendMode));
                }

                templates.Add(t.Value.Id, new TheaterTemplate(allSprites.ToArray(), variants.First().Count(), t.Value.Images.Length));
            }

            missingTile = sheetBuilder.Add(new byte[1], new Size(1, 1));

            Sheet.ReleaseBuffer();
        }
Esempio n. 2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="fileSystem"></param>
 /// <param name="filename"></param>
 /// <param name="loaders"></param>
 /// <param name="sheetBuilder"></param>
 /// <returns></returns>
 public static Sprite[] GetSprites(IReadOnlyFileSystem fileSystem, string filename, ISpriteLoader[] loaders, SheetBuilder sheetBuilder)
 {
     return(GetFrames(fileSystem, filename, loaders).Select(a => sheetBuilder.Add(a)).ToArray());
 }