private Tilemap CreateNewTilemap(TilemapKey key)
        {
            string     name = key.GetNameFormat(Layer.Type);
            GameObject tilemapGameObject = LayerGameObject.CreateChildGameObject(name);

            /*if (Importer.DeparentInRuntime)
             * {
             *  tilemapGameObject.AddComponent<LDtkDetachChildren>();
             * }*/

            Tilemap tilemap = tilemapGameObject.AddComponent <Tilemap>();


            if (Importer.IntGridValueColorsVisible)
            {
                TilemapRenderer renderer = tilemapGameObject.AddComponent <TilemapRenderer>();
                renderer.sortingOrder = SortingOrder.SortingOrderValue;
            }

            TilemapCollider2D collider = tilemapGameObject.AddComponent <TilemapCollider2D>();

            if (Importer.UseCompositeCollider)
            {
                Rigidbody2D rb = tilemapGameObject.AddComponent <Rigidbody2D>();
                rb.bodyType = RigidbodyType2D.Static;

                CompositeCollider2D composite = tilemapGameObject.AddComponent <CompositeCollider2D>();
                collider.usedByComposite = true;
            }

            return(tilemap);
        }
        public void BuildIntGridValues()
        {
            RoundTilemapPos();

            SortingOrder.Next();

            GameObject tilemapGameObject = LayerGameObject.CreateChildGameObject(Layer.Type);

            /*if (Importer.DeparentInRuntime)
             * {
             *  tilemapGameObject.AddComponent<LDtkDetachChildren>();
             * }*/

            Tilemap = tilemapGameObject.AddComponent <Tilemap>();


            if (Importer.IntGridValueColorsVisible)
            {
                TilemapRenderer renderer = tilemapGameObject.AddComponent <TilemapRenderer>();
                renderer.sortingOrder = SortingOrder.SortingOrderValue;
            }

            TilemapCollider2D collider = tilemapGameObject.AddComponent <TilemapCollider2D>();

            int[] intGridValues = Layer.IntGridCsv.Select(p => (int)p).ToArray();

            for (int i = 0; i < intGridValues.Length; i++)
            {
                int intGridValue = intGridValues[i];

                //all empty intgrid values are 0
                if (intGridValue == 0)
                {
                    continue;
                }

                LayerDefinition        intGridDef      = Layer.Definition;
                IntGridValueDefinition intGridValueDef = intGridDef.IntGridValues[intGridValue - 1];

                string          intGridValueKey = LDtkKeyFormatUtil.IntGridValueFormat(intGridDef, intGridValueDef);
                LDtkIntGridTile intGridTile     = Importer.GetIntGridValueTile(intGridValueKey);

                if (intGridTile == null)
                {
                    intGridTile = LDtkResourcesLoader.LoadDefaultTile();
                }

                BuildIntGridValue(intGridValueDef, i, intGridTile);
            }

            Tilemap.SetOpacity(Layer);
        }
        private Tilemap ConstructNewTilemap()
        {
            string     objName    = $"{GetLayerName(Layer)}_{_layerCount}";
            GameObject tilemapObj = LayerGameObject.CreateChildGameObject(objName);
            Tilemap    tilemap    = tilemapObj.AddComponent <Tilemap>();

            TilemapRenderer renderer = tilemapObj.AddComponent <TilemapRenderer>();

            renderer.sortingOrder = SortingOrder.SortingOrderValue;

            _layerCount++;

            return(tilemap);
        }
Example #4
0
        public void BuildEntityLayerInstances()
        {
            if (Importer.DeparentInRuntime)
            {
                LayerGameObject.AddComponent <LDtkDetachChildren>();
            }

            SortingOrder.Next();

            LDtkFieldParser.CacheRecentBuilder(this);

            foreach (EntityInstance entityData in Layer.EntityInstances)
            {
                GameObject entityPrefab = Importer.GetEntity(entityData.Identifier);
                BuildEntityInstance(entityData, entityPrefab);
            }
        }
Example #5
0
        void BuildGameObject()
        {
            LayerGameObject.transform.parent        = Parent;
            LayerGameObject.transform.localPosition = new Vector3(Position.x, Position.y, this.LayerDepth);

            LayerGameObject.isStatic = true;
            LayerGameObject.SetActive(Visible);

            SpriteRenderer tileRenderer = LayerGameObject.AddComponent <SpriteRenderer>();

            tileRenderer.sprite       = Sprite.Create(Texture, new Rect(0, 0, Texture.width, Texture.height), Vector2.up, TileWidth);
            tileRenderer.sprite.name  = Texture.name;
            tileRenderer.sortingOrder = SortingOrder;
            // Use Layer's name as Sorting Layer
            tileRenderer.sortingLayerName = this.Name;
            //tileRenderer.material = new Material(baseMaterial);
            //tileRenderer.material.mainTexture = Texture;
        }
Example #6
0
        public void BuildEntityLayerInstances()
        {
            if (Importer.DeparentInRuntime)
            {
                LayerGameObject.AddComponent <LDtkDetachChildren>();
            }

            SortingOrder.Next();

            LDtkParsedPoint.InformOfRecentLayerVerticalCellCount(Layer.UnityWorldPosition, (int)Layer.CHei);

            foreach (EntityInstance entityData in Layer.EntityInstances)
            {
                GameObject entityPrefab = Importer.GetEntity(entityData.Identifier);
                if (entityPrefab == null)
                {
                    continue;
                }

                BuildEntityInstance(entityData, entityPrefab);
            }
        }
Example #7
0
        // Renders the tile vertices.
        // Basically, it reads the tiles and creates its 4 vertexes (forming a rectangle or square according to settings) or sprite
        private void GenerateLayer()
        {
            int startX     = Width - 1;
            int endX       = -1;
            int startY     = Height - 1;
            int endY       = -1;
            int directionX = -1;
            int directionY = -1;

            //float zOffset = 0.001f;
            // To create the tiles, we must follow the order dictated by map.MapRenderOrder
            // Not unique tiles have inverted bottom-top top-bottom render order :X
            switch (BaseMap.MapRenderOrder)
            {
            case RenderOrder.Right_Down:
                if (MakeUniqueTiles)
                {
                    startY     = Height - 1;
                    endY       = -1;
                    directionY = -1;
                }
                else
                {
                    startX     = 0;
                    endX       = Width;
                    directionX = 1;
                    startY     = 0;
                    endY       = Height;
                    directionY = 1;
                }
                break;

            case RenderOrder.Right_Up:
                if (MakeUniqueTiles)
                {
                    startY     = 0;
                    endY       = Height;
                    directionY = 1;
                }
                else
                {
                    startX     = 0;
                    endX       = Width;
                    directionX = 1;
                    startY     = Height - 1;
                    endY       = -1;
                    directionY = -1;
                }
                break;

            case RenderOrder.Left_Up:
                if (MakeUniqueTiles)
                {
                    startX     = 0;
                    endX       = Width;
                    directionX = 1;
                    startY     = 0;
                    endY       = Height;
                    directionY = 1;
                }
                else
                {
                    startY     = Height - 1;
                    endY       = -1;
                    directionY = -1;
                }
                break;

            case RenderOrder.Left_Down:
                if (MakeUniqueTiles)
                {
                    startX     = 0;
                    endX       = Width;
                    directionX = 1;
                }
                else
                {
                    startY     = 0;
                    endY       = Height;
                    directionY = 1;
                }
                break;
            }

            // If we are not going to generate unique tiles, then we need a mesh, a mesh filter and a mesh renderer
            if (!MakeUniqueTiles)
            {
                CreateLayerMesh(startX, endX, startY, endY, directionX, directionY);
            }
            else
            {
                CreateUniqueTiles(startX, endX, startY, endY, directionX, directionY);
            }

            LayerGameObject.transform.parent        = BaseMap.MapObject.transform;
            LayerGameObject.transform.localPosition = new Vector3(0, 0, this.LayerDepth);
            LayerGameObject.isStatic = true;

            LayerGameObject.SetActive(Visible);
        }
Example #8
0
        /// <summary>
        /// Creates a Map Object Layer from node
        /// </summary>
        /// <param name="node">XML node to parse</param>
        /// <param name="tiledMap">MapObjectLayer parent Map</param>
        /// <param name="layerDepth">This Layer's zDepth</param>
        /// <param name="materials">List of Materials containing the TileSet textures</param>
        public MapObjectLayer(NanoXMLNode node, Map tiledMap, int layerDepth, List <Material> materials)
            : base(node)
        {
            if (node.GetAttribute("color") != null)
            {
                // get the color string, removing the leading #
                string color = node.GetAttribute("color").Value.Substring(1);

                // get the RGB individually
                string r = color.Substring(0, 2);
                string g = color.Substring(2, 2);
                string b = color.Substring(4, 2);

                // convert to the color
                Color = new Color(
                    (byte)int.Parse(r, NumberStyles.AllowHexSpecifier),
                    (byte)int.Parse(g, NumberStyles.AllowHexSpecifier),
                    (byte)int.Parse(b, NumberStyles.AllowHexSpecifier));
            }

            LayerGameObject.transform.parent        = tiledMap.MapObject.transform;
            LayerGameObject.transform.localPosition = new Vector3(0, 0, this.LayerDepth);
            LayerGameObject.isStatic = true;
            LayerGameObject.SetActive(Visible);

            Objects = new List <MapObject>();

            foreach (NanoXMLNode objectNode in node.SubNodes)
            {
                if (!objectNode.Name.Equals("object"))
                {
                    continue;
                }

                MapObject mapObjectContent = new MapObject(objectNode, this);

                mapObjectContent.ScaleObject(tiledMap.TileWidth, tiledMap.TileHeight, tiledMap.Orientation);
                mapObjectContent.Name = this.Name + "_" + mapObjectContent.Name;
                // Object names need to be unique for our lookup system, but Tiled
                // doesn't require unique names.
                string objectName     = mapObjectContent.Name;
                int    duplicateCount = 2;

                // if a object already has the same name...
                if (Objects.Find(o => o.Name.Equals(objectName)) != null)
                {
                    // figure out a object name that does work
                    do
                    {
                        objectName = string.Format("{0}{1}", mapObjectContent.Name, duplicateCount);
                        duplicateCount++;
                    } while (Objects.Find(o => o.Name.Equals(objectName)) != null);

                    // log a warning for the user to see
                    //Debug.LogWarning("Renaming object \"" + mapObjectContent.Name + "\" to \"" + objectName + "\" in layer \"" + Name + "\" to make a unique name.");

                    // save that name
                    mapObjectContent.Name = objectName;
                }
                mapObjectContent.CreateTileObject(tiledMap, Name, layerDepth, materials);

                AddObject(mapObjectContent);
            }
        }