Esempio n. 1
0
        public void ResetToHighTiles()
        {
            TilemapLayer standingLayer = TilemapLayer.Standing;

            foreach (Position position in _positionsToReset)
            {
                _tileMatrixUpdater.RefreshTile(position, (int)standingLayer);
            }
            _positionsToReset.Clear();
        }
Esempio n. 2
0
 /// <summary>
 /// Returns a binary encoding of the tilemap. Size : 8 + W * H * 4
 /// </summary>
 /// <param name="layer"></param>
 /// <returns></returns>
 public static byte[] GenerateBinaries(TilemapLayer layer)
 {
     byte[] bytes = new byte[layer.height * layer.width * 4 + 8];
     ByteUtils.SetInteger(0, ref bytes, layer.width);
     ByteUtils.SetInteger(4, ref bytes, layer.height);
     for (int i = 0; i < layer.height; i++)
     {
         for (int j = 0; j < layer.width; j++)
         {
             int target = (i * layer.width + j) * 4 + 8;
             ByteUtils.SetInteger(target, ref bytes, layer[j, i]);
         }
     }
     return(bytes);
 }
Esempio n. 3
0
        public static TilemapLayer LoadLayerFromBinaries(byte[] bytes, int start = 0)
        {
            TilemapLayer layer = new TilemapLayer();
            int          w     = layer.width = ByteUtils.GetInteger(start + 0, bytes);
            int          h     = layer.height = ByteUtils.GetInteger(start + 4, bytes);

            layer.UpdateLayerSize();
            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    int target = start + (i * layer.width + j) * 4 + 8;
                    layer[j, i] = ByteUtils.GetInteger(target, bytes);
                }
            }
            return(layer);
        }
Esempio n. 4
0
        public override IEnumerator Recalculating()
        {
            SpreadInitialSeeds();

            yield return(new WaitForSeconds(0.1f));

            int iterations = _config.Iterations;

            for (var iteration = 0; iteration < iterations; iteration++)
            {
                SimulateOneGeneration(iteration < iterations - 2);

                yield return(new WaitForSeconds(0.1f));
            }

            for (var x = 1; x < Values.XSize - 1; x++)
            {
                for (var y = 1; y < Values.YSize - 1; y++)
                {
                    Plant plant = _plants[x, y];
                    if (plant != null)
                    {
                        if (plant.Tile == null)
                        {
                            throw new ArgumentNullException($"Missing tile for plant {plant.name}.");
                        }
                        TilemapLayer layer = plant.Tile.Layer;
                        byte         id    = plant.Tile.Id;
                        _tileMatricesByte[(int)layer].Set(x, y, id);

                        if (!plant.GrowsBelowOtherPlants) //not grass layer
                        {
                            Plant neighbourGrowingBelow = PositionUtilities.Neighbours8(new Position(x, y))
                                                          .Select(n => _plants[n.x, n.y])
                                                          .FirstOrDefault(p => p != null && p.GrowsBelowOtherPlants);
                            if (neighbourGrowingBelow != null)
                            {
                                _tileMatricesByte[(int)neighbourGrowingBelow.Tile.Layer]
                                .Set(x, y, neighbourGrowingBelow.Tile.Id);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 5
0
    // ------------------------------------------------------------------------
    // ------------------------------------------------------------------------
    private void createTilemapLayer(XMLNode node)
    {
        // parse TMX
        XMLNode csvData = new XMLNode();

        foreach (XMLNode child in node.children)
        {
            if (child.tagName == "data")
            {
                csvData = child;
            }
        }

        // make sure encoding is set to csv
        if (csvData.attributes["encoding"] != "csv")
        {
            Debug.Log("createTilemapLayer: Could not render layer data, encoding set to: " + csvData.attributes["encoding"]);
            return;
        }

        // new tilemap component for each layer
        TilemapLayer tilemapLayer = this.rootTilesGO.AddComponent <TilemapLayer>();

        tilemapLayer.loadLayer(csvData.value, this.rootTilesGO);
        tilemapLayer.layerName = node.attributes["name"];

        // was this the collision layer?  if so, setup pathfinding
        if (node.attributes["name"] == "Collision")
        {
            // set ref
            this.collisionLayer = tilemapLayer;
        }
        // background layer
        else if (node.attributes["name"] == "Background")
        {
            this.backgroundLayer = tilemapLayer;
        }
    }
Esempio n. 6
0
        private void GenerateImage()
        {
            if (image != null)
            {
                image.Dispose();
            }
            image = new Bitmap(RenderSize * this.Tilemap.Width, RenderSize * this.Tilemap.Height, PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(image);

            g.DrawImage(background, Point.Empty);
            for (int i = 0; i < Tilemap.Layers.Count; i++)
            {
                TilemapLayer l = Tilemap.Layers[i];
                if (i != ActiveLayer && ActiveLayer != -1)
                {
                    ColorMatrix matrix = new ColorMatrix();
                    if (i > ActiveLayer)
                    {
                        matrix.Matrix33 = 0.5F;
                    }
                    else
                    {
                        matrix.Matrix00 = 0.5F;
                        matrix.Matrix11 = 0.5F;
                        matrix.Matrix22 = 0.5F;
                    }
                    ImageAttributes attributes = new ImageAttributes();
                    attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                    g.DrawImage(l.LayerImage, new Rectangle(0, 0, l.LayerImage.Width, l.LayerImage.Height), 0, 0, l.LayerImage.Width, l.LayerImage.Height, GraphicsUnit.Pixel, attributes);
                }
                else
                {
                    g.DrawImage(l.LayerImage, 0, 0);
                }
            }
            g.Dispose();
        }
Esempio n. 7
0
 private void PrepareTileToSet(int x, int y, List <Vector3Int>[] batchPositionsLayers, List <TileBase>[] batchTilesLayers, TilemapLayer matrixLayer, OsnowaBaseTile baseTile)
 {
     if (matrixLayer != baseTile.Layer)
     {
         // todo: detect all occurences of situation below, but log once
         //Debug.LogWarning($"Layer mismatch on {x}, {y}: {tile.name} placed on layer {matrixLayer}, but tile layer is {tile.Layer}");
     }
     batchPositionsLayers[(int)matrixLayer].Add(new Vector3Int(x, y, 0));
     batchTilesLayers[(int)matrixLayer].Add(baseTile);
 }
Esempio n. 8
0
        public override void OnInspectorGUI()
        {
            int idFromEditor = EditorGUILayout.IntField("Unique ID", EditedTile.Id);

            if (idFromEditor != EditedTile.Id)
            {
                EditedTile.Id = (byte)idFromEditor;
                SaveTile();
            }

            TilemapLayer layerFromEditor = (TilemapLayer)EditorGUILayout.EnumPopup("Layer", EditedTile.Layer);

            if (layerFromEditor != EditedTile.Layer)
            {
                EditedTile.Layer = layerFromEditor;
                SaveTile();
            }

            WalkabilityModifier walkabilityFromEditor = (WalkabilityModifier)EditorGUILayout.EnumPopup("Walkability", EditedTile.Walkability);

            if (walkabilityFromEditor != EditedTile.Walkability)
            {
                EditedTile.Walkability = walkabilityFromEditor;
                SaveTile();
            }

            PassingLightModifier passingLightFromEditor = (PassingLightModifier)EditorGUILayout.EnumPopup("Passing light", EditedTile.IsPassingLight);

            if (passingLightFromEditor != EditedTile.IsPassingLight)
            {
                EditedTile.IsPassingLight = passingLightFromEditor;
                SaveTile();
            }

            EditorGUILayout.LabelField("Consistency class : when more than 0, the tile is treated by other OsnowaTiles as \"Neighbor.This\" for all other tiles with same consistency class.",
                                       new GUIStyle {
                wordWrap = true
            });
            int consistencyClassFromEditor = EditorGUILayout.IntField("Consistency class", EditedTile.ConsistencyClass);

            if (consistencyClassFromEditor != EditedTile.ConsistencyClass)
            {
                EditedTile.ConsistencyClass = consistencyClassFromEditor;
                SaveTile();
            }

            EditorGUILayout.LabelField("Drag another tile below if you want to use it as a shorter variant to display eg. when player is close to the tile:", new GUIStyle {
                wordWrap = true
            });
            OsnowaBaseTile shorterVariantTileFromEditor =
                EditorGUILayout.ObjectField("Shorter variant", EditedTile.ShorterVariant, typeof(OsnowaBaseTile), false) as OsnowaBaseTile;

            if (shorterVariantTileFromEditor != EditedTile.ShorterVariant)
            {
                EditedTile.ShorterVariant = shorterVariantTileFromEditor;
                SaveTile();
            }

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Rule generation. \r\n" +
                                       "1. Drag in a prepared multi-sprite, \r\n" +
                                       "2. generate the rules (<b>in order to see them you need to change Inspector focus to something else and back to this tile</b>),\r\n" +
                                       "3. <b>remember to save (ctrl+s) afterwards</b>.", new GUIStyle {
                wordWrap = true
            });
            EditedTile.GenerateFrom = (Sprite)EditorGUILayout.ObjectField("Multi-sprite source:", EditedTile.GenerateFrom, typeof(Sprite), false);

            if (GUILayout.Button("Remove all rules"))
            {
                EditedTile.m_TilingRules = null;
                SaveTile();
            }


            if (EditedTile.GenerateFrom != null && GUILayout.Button("Generate rules (box-like neighbourhood)"))
            {
                int[] sides =
                {
                    236,      41236,   412,  2,
                    89632, 78946123, 87412, 82,
                    896,      47896,   478,  8,
                    6,           46,     4,  5,
                };

                GenerateRules(sides, 5);
            }

            if (EditedTile.GenerateFrom != null && GUILayout.Button("Generate rules (8-sided neighbourhood)"))
            {
                // Performance tip if filling the tilemap is to slow:
                // Get occurence statistics of different rules (for sparsely- and densely appearing tiles)
                // and adjust the order of rule creation for them. That would help because refreshing of single tile
                // sequentially goes through all the rules untill it meets the satisfying one.
                int[] sides =
                {
                    236,        41236,   412,    2,      0,     42,   426,     26,
                    89632,   78946123, 87412,   82, 478263,    826,  4826,    842,
                    896,        47896,   478,    8, 896412,     86,   486,     48,
                    6,             46,     4,    5, 489632, 478962,  4236, 841236,
                    6987412,  4789632,  2146, 4782,   6982,  84269, 86247, 874126,

                    8741236,  8963214,  8632, 8964,   8746,   8214, 86214, 84236
                };

                GenerateRules(sides, 8);
            }

/* Doesn't work :( ctrl+s is needed anyway.
 *                      if (GUILayout.Button("Force save"))
 *                      {
 *                              SaveTile();
 *                      }*/

            Sprite defaultSpriteFromEditor = EditorGUILayout.ObjectField("Default Sprite", EditedTile.m_DefaultSprite, typeof(Sprite), false) as Sprite;

            if (defaultSpriteFromEditor != EditedTile.m_DefaultSprite)
            {
                EditedTile.m_DefaultSprite = defaultSpriteFromEditor;
                SaveTile();
            }

            var defaultColliderTypeFromEditor = (Tile.ColliderType)EditorGUILayout.EnumPopup("Default Collider", EditedTile.m_DefaultColliderType);

            if (defaultColliderTypeFromEditor != EditedTile.m_DefaultColliderType)
            {
                EditedTile.m_DefaultColliderType = defaultColliderTypeFromEditor;
                SaveTile();
            }

            EditorGUILayout.Space();

            if (m_ReorderableList != null && EditedTile.m_TilingRules != null)
            {
                m_ReorderableList.DoLayoutList();
            }
        }
Esempio n. 9
0
    static void Map2JSON()
    {
        AssetDatabase.Refresh();

        var nodesMapped     = MapToPNG.GetMappedNodes();
        var tilemapLayers   = new SortedDictionary <string, TilemapLayer>(Comparer <string> .Create(CompareSpriteLayer));
        var tempGameObjects = new List <GameObject>();

        for (int y = 0; y < nodesMapped.GetLength(0); y++)
        {
            for (int x = 0; x < nodesMapped.GetLength(1); x++)
            {
                var node = nodesMapped[y, x];

                if (node == null)
                {
                    continue;
                }

                var nodeRenderers = new List <SpriteRenderer>();

                var objectsToExport = node.GetTiles();
                node.GetItems().ForEach(behaviour => objectsToExport.Add(behaviour.gameObject));

                foreach (var tile in objectsToExport)
                {
                    var tileRenderers = tile.GetComponentsInChildren <SpriteRenderer>();
                    if (tileRenderers == null || tileRenderers.Length < 1)
                    {
                        continue;
                    }
                    var tileconnects = 0;
                    foreach (var renderer in tileRenderers)
                    {
                        if (thisRendererSucks(renderer) || renderer.sortingLayerID == 0)
                        {
                            continue;
                        }

                        TryMoveToSeparateLayer(renderer);

                        if (renderer.GetComponent <TileConnect>() || IsTileConnectWannabe(renderer))
                        {
                            tileconnects++;
                            if (tileconnects != 4)
                            {
                                continue;
                            }
                            if (tileconnects > 4)
                            {
                                Debug.LogWarningFormat("{0} — more than 4 tileconnects found!", renderer.name);
                            }
                            // grouping four tileconnect sprites into a single temporary thing
                            GameObject tcMergeGameObject = Instantiate(renderer.gameObject, tile.transform.position,
                                                                       Quaternion.identity, tile.transform);
                            tempGameObjects.Add(tcMergeGameObject);
                            var childClone = tcMergeGameObject.GetComponent <SpriteRenderer>();
                            var spriteName = childClone.sprite.name;

                            if (spriteName.Contains("_"))
                            {
                                childClone.name = TC + spriteName.Substring(0,
                                                                            spriteName.LastIndexOf("_", StringComparison.Ordinal));
                            }
                            nodeRenderers.Add(childClone);
                        }
                        else
                        {
                            renderer.name = renderer.sprite.name;
                            if (DuplicateFound(renderer, nodeRenderers))
                            {
                                Debug.LogFormat("Skipping {0}({1}) as duplicate", renderer.name, GetSortingLayerName(renderer));
                                continue;
                            }
                            var uniqueSortingOrder = GetUniqueSortingOrder(renderer, nodeRenderers);
                            if (!uniqueSortingOrder.Equals(renderer.sortingOrder))
                            {
                                renderer.sortingOrder = uniqueSortingOrder;
                            }
                            nodeRenderers.Add(renderer);
                        }
                    }
                }

                foreach (var renderer in nodeRenderers)
                {
                    var          currentLayerName = GetSortingLayerName(renderer);
                    TilemapLayer tilemapLayer;
                    if (tilemapLayers.ContainsKey(currentLayerName))
                    {
                        tilemapLayer = tilemapLayers[currentLayerName];
                    }
                    else
                    {
                        tilemapLayer = new TilemapLayer();
                        tilemapLayers[currentLayerName] = tilemapLayer;
                    }
                    if (tilemapLayer == null)
                    {
                        continue;
                    }
                    UniTileData tileDataInstance = CreateInstance <UniTileData>();
                    var         parentObject     = renderer.transform.parent.gameObject;
                    if (parentObject)
                    {
                        tileDataInstance.Name = parentObject.name;
                    }
                    var childtf  = renderer.transform;
                    var parenttf = renderer.transform.parent.gameObject.transform;
                    //don't apply any rotation for tileconnects
                    var isTC    = renderer.name.StartsWith(TC);
                    var zeroRot = Quaternion.Euler(0, 0, 0);

                    tileDataInstance.ChildTransform =
                        Matrix4x4.TRS(childtf.localPosition, isTC ? zeroRot : childtf.localRotation, childtf.localScale);

                    tileDataInstance.Transform =
                        Matrix4x4.TRS(parenttf.position, isTC ? zeroRot : parenttf.localRotation, parenttf.localScale);

                    tileDataInstance.OriginalSpriteName = renderer.sprite.name;
                    tileDataInstance.SpriteName         = renderer.name;
                    var assetPath = AssetDatabase.GetAssetPath(renderer.sprite.GetInstanceID());
                    tileDataInstance.IsLegacy = looksLikeLegacy(assetPath, tileDataInstance) && !isExcluded(assetPath);

                    string sheet = assetPath
                                   .Replace("Assets/Resources/", "")
                                   .Replace("Assets/textures/", "")
                                   .Replace("Resources/", "")
                                   .Replace(".png", "");
                    string overrideSheet;
                    tileDataInstance.SpriteSheet = IsTileConnectWannabe(renderer, out overrideSheet) ? overrideSheet : sheet;
                    tilemapLayer.Add(x, y, tileDataInstance);
                }
            }
        }

        foreach (var layer in tilemapLayers)
        {
            Debug.LogFormat("{0}: {1}", layer.Key, layer.Value);
        }

        fsData data;

        new fsSerializer().TrySerialize(tilemapLayers, out data);
        File.WriteAllText(Application.dataPath + "/Resources/metadata/" + SceneManager.GetActiveScene().name + ".json",
                          fsJsonPrinter.PrettyJson(data));

        //Cleanup
        foreach (var o in tempGameObjects)
        {
            DestroyImmediate(o);
        }

        Debug.Log("Export kinda finished");
        AssetDatabase.Refresh();
    }