Esempio n. 1
0
        /// <summary>
        /// Create the tile image layer as a child entity
        /// </summary>
        /// <param name="tmxImageLayer">The tmx image layer.</param>
        /// <param name="layerIndex">The layer index</param>
        /// <param name="previousEntities">previousEntities</param>
        private void CreateChildTileImageLayer(TmxImageLayer tmxImageLayer, int layerIndex, IList <Entity> previousEntities)
        {
            var tmxLayerName = tmxImageLayer.Name;

            var tiledMapImageLayer = new TiledMapImageLayer(tmxImageLayer, this);

            Entity layerEntity = null;

            if (previousEntities != null)
            {
                layerEntity = previousEntities.FirstOrDefault(e => e.Tag.StartsWith(TileImageTag) && e.Name == tmxLayerName);
                previousEntities.Remove(layerEntity);
            }

            var tileLayerOffset = new Vector2((float)tmxImageLayer.OffsetX, (float)tmxImageLayer.OffsetY);

            if (layerEntity != null)
            {
                var tileMapTransform = layerEntity.FindComponent <Transform2D>();
                var sprite           = layerEntity.FindComponent <Sprite>();

                if (tileMapTransform != null &&
                    sprite != null)
                {
                    tileMapTransform.LocalPosition = tileLayerOffset;
                    sprite.TexturePath             = tiledMapImageLayer.ImagePath;
                    layerEntity.Name = tmxLayerName;
                }
                else
                {
                    this.Owner.RemoveChild(layerEntity.Name);
                    layerEntity = null;
                }
            }

            if (layerEntity == null)
            {
                layerEntity = new Entity(tmxLayerName)
                {
                    Tag = TileImageTag
                }
                .AddComponent(new Sprite(tiledMapImageLayer.ImagePath))
                .AddComponent(new Transform2D()
                {
                    LocalPosition = tileLayerOffset,
                    Origin        = this.transform.Origin,
                    Opacity       = (float)tmxImageLayer.Opacity
                })
                .AddComponent(new SpriteRenderer());
            }

            this.Owner.AddChild(layerEntity);
            this.imageLayers.Add(tmxLayerName, tiledMapImageLayer);
        }
Esempio n. 2
0
        /// <summary>
        /// Create the tile image layer as a child entity
        /// </summary>
        /// <param name="tmxImageLayer">The tmx image layer.</param>
        /// <param name="layerIndex">The layer index</param>
        private void CreateChildTileImageLayer(TmxImageLayer tmxImageLayer, int layerIndex)
        {
            var tag          = "TileImageLayer_" + layerIndex;
            var tmxLayerName = tmxImageLayer.Name;

            var tiledMapImageLayer = new TiledMapImageLayer(tmxImageLayer, this);

            Entity layerEntity = null;

            layerEntity = this.Owner.FindChildrenByTag(tag).FirstOrDefault();
            var tileLayerOffset = new Vector2((float)tmxImageLayer.OffsetX, (float)tmxImageLayer.OffsetY);

            if (layerEntity != null)
            {
                var tileMapTransform = layerEntity.FindComponent <Transform2D>();
                var sprite           = layerEntity.FindComponent <Sprite>();

                if (tileMapTransform != null &&
                    sprite != null)
                {
                    tileMapTransform.LocalPosition = tileLayerOffset;
                    sprite.TexturePath             = tiledMapImageLayer.ImagePath;
                    layerEntity.Name = tmxLayerName;
                }
                else
                {
                    this.Owner.RemoveChild(layerEntity.Name);
                    layerEntity = null;
                }
            }

            if (layerEntity == null)
            {
                layerEntity = new Entity(tmxLayerName)
                {
                    Tag = tag
                }
                .AddComponent(new Sprite(tiledMapImageLayer.ImagePath))
                .AddComponent(new Transform2D()
                {
                    LocalPosition = tileLayerOffset,
                    Origin        = this.transform.Origin,
                    Opacity       = (float)tmxImageLayer.Opacity
                })
                .AddComponent(new SpriteRenderer());
                this.Owner.AddChild(layerEntity);
            }

            this.imageLayers.Add(tmxLayerName, tiledMapImageLayer);
        }