Ejemplo n.º 1
0
        /// <summary>
        /// Load a tile map from a file.
        /// </summary>
        /// <param name="path">Path to JSON level map file.</param>
        /// <param name="textureLibrary">ResourceLibrary for storing textures.</param>
        public TileMap(string path, TextureLibrary textureLibrary)
        {
            _TileMap _tileMap = JsonHelper <_TileMap> .Load(path);

            _tileMap.Solidify(this, Path.GetDirectoryName(path), textureLibrary);
            //TODO: Will _tileMap be automatically garbage collected?
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Convert JSON-serializable _TileMap format to the more usable TileMap format.
        /// </summary>
        /// <param name="tileMap">Pre-existing TileMap object to use.</param>
        /// <param name="path">Path that contains the tile map and tilesheet (texture) files.</param>
        public TileMap Solidify(string path, TextureLibrary textureLibrary)
        {
            TileMap tileMap = new TileMap();

            Solidify(tileMap, path, textureLibrary);
            return(tileMap);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Load a sprite from a file.
        /// </summary>
        /// <param name="path">Path to JSON sprite file.</param>
        /// <param name="textureLibrary">ResourceLibrary for storing textures.</param>
        public Sprite(GameServices gs, string path, TextureLibrary textureLibrary)
        {
            this.gs     = gs;
            spriteBatch = gs.DisplayManager.SpriteBatch;
            _Sprite _sprite = JsonHelper <_Sprite> .Load(path);

            _sprite.Finalize(Path.GetDirectoryName(path), textureLibrary);
            _sprite.Solidify(this);
            //TODO: Will _sprite be automatically garbage collected?
            DetectCollisionShape();
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Convert JSON-serializable _TileMap format to the more usable TileMap format.
 /// </summary>
 /// <param name="tileMap">Pre-existing TileMap object to use.</param>
 /// <param name="path">Path that contains the tile map and tilesheet (texture) files.</param>
 /// <param name="textureLibrary">ResourceLibrary for storing textures.</param>
 public void Solidify(TileMap tileMap, string path, TextureLibrary textureLibrary)
 {
     textureLibrary.Load(Path.Combine(path, TileMapImage), out Texture2D item);
     tileMap.TileMapImage = item;
     tileMap.TileWidth    = TileSize[0];
     tileMap.TileHeight   = TileSize[1];
     Tile[] tiles = new Tile[Tiles.Count];
     for (int i = 0; i < Tiles.Count; i++)
     {
         tiles[i]             = Tiles[i];
         tiles[i].Position    = new int[2];
         tiles[i].Position[0] = Tiles[i].Index[0] * TileSize[0];
         tiles[i].Position[1] = Tiles[i].Index[1] * TileSize[1];
     }
     tileMap.Tiles = tiles;
     tileMap.MakeLookup();
 }
Ejemplo n.º 5
0
 public SpriteLibrary(GameServices gs) : base(gs)
 {
     // For storing referenced image files.
     this.textureLibrary = new TextureLibrary(gs);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Prepare _Sprite format for conversion to Sprite format.
        /// </summary>
        /// <param name="path">Path that contains the sprite and spritesheet (texture) files.</param>
        /// <param name="textureLibrary">ResourceLibrary for storing textures.</param>
        public void Finalize(string path, TextureLibrary textureLibrary)
        {
            if (CollisionBox == null)
            {
                CollisionBox = new CollisionBox();
            }
            if (DefaultFrame == null)
            {
                DefaultFrame = new _FrameRange();
            }
            if (DefaultFrame.Spritesheet != null)
            {
                textureLibrary.Load(Path.Combine(path, DefaultFrame.Spritesheet), out Texture2D item);
                DefaultFrame.Texture = item;
            }
            if (DefaultFrame.X == null)
            {
                DefaultFrame.X = 0;
            }
            if (DefaultFrame.Y == null)
            {
                DefaultFrame.Y = 0;
            }
            if (DefaultFrame.Texture != null)
            {
                if (DefaultFrame.Width == null)
                {
                    DefaultFrame.Width = DefaultFrame.Texture.Width;
                }
                if (DefaultFrame.Height == null)
                {
                    DefaultFrame.Height = DefaultFrame.Texture.Height;
                }
            }
            else
            {
                DefaultFrame.Width  = 0;
                DefaultFrame.Height = 0;
            }
            if (DefaultFrame.AnchorX == null)
            {
                DefaultFrame.AnchorX = 0f;
            }
            if (DefaultFrame.AnchorY == null)
            {
                DefaultFrame.AnchorY = 0f;
            }
            if (DefaultFrame.Count == null)
            {
                DefaultFrame.Count = 1;
            }
            if (DefaultFrame.Frametime == null)
            {
                DefaultFrame.Frametime = 1f;
            }
            if (DefaultFrame.FlipX == null)
            {
                DefaultFrame.FlipX = false;
            }
            if (DefaultFrame.FlipY == null)
            {
                DefaultFrame.FlipY = false;
            }

            if (Animations == null)
            {
                Animations = new List <_Animation>(new _Animation[] { new _Animation() });
            }
            foreach (_Animation animation in Animations)
            {
                if (animation.Name == null)
                {
                    animation.Name = "default";
                }
                if (animation.Speed == null)
                {
                    animation.Speed = 1f;
                }
                if (animation.FrameRanges == null)
                {
                    animation.FrameRanges = new List <_FrameRange>();
                    animation.FrameRanges.Add(DefaultFrame);
                }
                else
                {
                    foreach (_FrameRange frame in animation.FrameRanges)
                    {
                        if (frame.Spritesheet == null)
                        {
                            frame.Spritesheet = DefaultFrame.Spritesheet;
                            frame.Texture     = DefaultFrame.Texture;
                        }
                        else
                        {
                            textureLibrary.Load(Path.Combine(path, frame.Spritesheet), out Texture2D item);
                            frame.Texture = item;
                        }
                        if (frame.X == null)
                        {
                            frame.X = DefaultFrame.X;
                        }
                        if (frame.Y == null)
                        {
                            frame.Y = DefaultFrame.Y;
                        }
                        if (frame.Width == null)
                        {
                            frame.Width = DefaultFrame.Width;
                        }
                        if (frame.Height == null)
                        {
                            frame.Height = DefaultFrame.Height;
                        }
                        if (frame.AnchorX == null)
                        {
                            frame.AnchorX = DefaultFrame.AnchorX;
                        }
                        if (frame.AnchorY == null)
                        {
                            frame.AnchorY = DefaultFrame.AnchorY;
                        }
                        if (frame.Count == null)
                        {
                            frame.Count = DefaultFrame.Count;
                        }
                        if (frame.Frametime == null)
                        {
                            frame.Frametime = 1f;                          // Do not inherit this from DefaultFrame.
                        }
                        if (frame.FlipX == null)
                        {
                            frame.FlipX = false;                      // Do not inherit this from DefaultFrame.
                        }
                        if (frame.FlipY == null)
                        {
                            frame.FlipY = false;                      // Do not inherit this from DefaultFrame.
                        }
                    }
                }

                if (DefaultAnimation == null)
                {
                    DefaultAnimation = animation.Name;
                }
            }
        }