/// <summary>
        /// Generates outline paths around the tilemap tiles.
        /// </summary>
        /// <returns></returns>
        public static Vector2[][] Generate(Tilemap tilemap, float detailLevel = 1f, bool holeDetection = true)
        {
            // detailScale is used to increase texture resolution and thus the level of details
            const int detailScale = 8;
            Texture2D texture     = TilemapToTexture(tilemap, detailScale, out Rect rect);

            Vector2[][] paths;
            GenerateOutline(texture, rect, detailLevel, alphaTolerance: 254, holeDetection, out paths);

            // The paths points' coords are in the texture pixel space relative to its center
            // so we need to transform them into the world space
            Vector2 offset = (Vector2Int)tilemap.cellBounds.min + rect.center / detailScale;

            for (int j = 0; j < paths.Length; j++)
            {
                Vector2[] path = paths[j];
                for (int i = 0; i < path.Length; i++)
                {
                    path[i] = tilemap.LocalToWorld(tilemap.CellToLocalInterpolated(path[i] / detailScale + offset));
                }
                // Looping the path
                Array.Resize(ref paths[j], path.Length + 1);
                path = paths[j];
                path[path.Length - 1] = path[0];
            }
            return(paths);
        }
Example #2
0
    void Awake()
    {
        ConfigScript config = (ConfigScript)Object.FindObjectOfType(typeof(ConfigScript));

        traffic        = GameObject.Find("Pedestrians Traffic").GetComponent <Tilemap>();
        walking        = GetComponent <PedestrianScript>();
        body           = GetComponent <Rigidbody2D>();
        pedestrianMask = LayerMask.GetMask("Pedestrians");

        Vector3 frontSide  = transform.position;
        Vector3 cellCenter = traffic.CellToLocalInterpolated(traffic.WorldToCell(frontSide) +
                                                             new Vector3(0.5f, 0.5f, 0));

        goal = GetNextCell(cellCenter);
    }
Example #3
0
    void Awake()
    {
        ConfigScript config = (ConfigScript)Object.FindObjectOfType(typeof(ConfigScript));

        traffic = GameObject.Find("Traffic").GetComponent <Tilemap>();
        driving = GetComponent <DrivingScript>();
        body    = GetComponent <Rigidbody2D>();
        carMask = LayerMask.GetMask("Cars");

        frontDist     = GetComponent <BoxCollider2D>().size.y / 2 + 0.1f;                // front of car
        frontAxleDist = driving.frontWheelOffset.localPosition.y / config.pixelsPerUnit; // front wheels
        Vector3 frontAxle  = transform.position + transform.up * frontAxleDist;
        Vector3 cellCenter = traffic.CellToLocalInterpolated(traffic.WorldToCell(frontAxle) +
                                                             new Vector3(0.5f, 0.5f, 0));

        goal = GetNextCell(cellCenter);
    }
Example #4
0
    IEnumerator DestroyTile(Vector3Int tilePos)
    {
        yield return(new WaitForSeconds(waitBreakDuration));

        if (!m_IsRefreshing)
        {
            m_Tilemap.SetColor(tilePos, new Color(1.0f, 1.0f, 1.0f, 0.0f));
            GameObject.Instantiate(destroyed, m_Tilemap.CellToLocalInterpolated(tilePos + new Vector3(0.5f, 0.5f)), Quaternion.identity);
            collapsesAudioPlayer.PlayRandomSound();
        }
        else
        {
            yield break;
        }

        yield return(new WaitForSeconds(0.5f));

        if (!m_IsRefreshing)
        {
            m_TileArray.RemoveAt(0);
            m_Tilemap.SetTile(tilePos, null);
        }
    }
 private Vector3 GetConstructionSpaceWorldCenter(ConstructionSpace space)
 {
     return(Map.LocalToWorld(Map.CellToLocalInterpolated(space.LocalOrigin + space.Data.RuinShape.cellBounds.center)));
 }
Example #6
0
        /// <summary>
        /// StartUp is called on the first frame of the running Scene.
        /// </summary>
        /// <param name="location">Position of the Tile on the Tilemap.</param>
        /// <param name="tilemap">The Tilemap the tile is present on.</param>
        /// <param name="instantiatedGameObject">The GameObject instantiated for the Tile.</param>
        /// <returns>Whether StartUp was successful</returns>
        public override bool StartUp(Vector3Int location, ITilemap tilemap, GameObject instantiatedGameObject)
        {
            if (instantiatedGameObject != null)
            {
                Tilemap   tmpMap       = tilemap.GetComponent <Tilemap>();
                Matrix4x4 orientMatrix = tmpMap.orientationMatrix;

                var        iden = Matrix4x4.identity;
                Vector3    gameObjectTranslation = new Vector3();
                Quaternion gameObjectRotation    = new Quaternion();
                Vector3    gameObjectScale       = new Vector3();

                bool ruleMatched = false;
                foreach (TilingRule rule in m_TilingRules)
                {
                    Matrix4x4 transform = iden;
                    if (RuleMatches(rule, location, tilemap, ref transform))
                    {
                        transform = orientMatrix * transform;

                        // Converts the tile's translation, rotation, & scale matrix to values to be used by the instantiated Game Object
                        gameObjectTranslation = new Vector3(transform.m03, transform.m13, transform.m23);
                        gameObjectRotation    = Quaternion.LookRotation(new Vector3(transform.m02, transform.m12, transform.m22), new Vector3(transform.m01, transform.m11, transform.m21));
                        gameObjectScale       = transform.lossyScale;

                        ruleMatched = true;
                        break;
                    }
                }
                if (!ruleMatched)
                {
                    // Fallback to just using the orientMatrix for the translation, rotation, & scale values.
                    gameObjectTranslation = new Vector3(orientMatrix.m03, orientMatrix.m13, orientMatrix.m23);
                    gameObjectRotation    = Quaternion.LookRotation(new Vector3(orientMatrix.m02, orientMatrix.m12, orientMatrix.m22), new Vector3(orientMatrix.m01, orientMatrix.m11, orientMatrix.m21));
                    gameObjectScale       = orientMatrix.lossyScale;
                }

                instantiatedGameObject.transform.localPosition = gameObjectTranslation + tmpMap.CellToLocalInterpolated(location + tmpMap.tileAnchor);
                instantiatedGameObject.transform.localRotation = gameObjectRotation;
                instantiatedGameObject.transform.localScale    = gameObjectScale;
            }

            return(true);
        }