Exemplo n.º 1
0
 // Update is called once per frame
 void Update()
 {
     foreach (Tilemap tilemap in Tilemaps)
     {
         if (tilemap.name == "Ground")
         {
             Vector3 worldCenter =
                 tilemap.CellToWorld(
                     new Vector3Int(
                         Mathf.RoundToInt(tilemap.cellBounds.center.x),
                         Mathf.RoundToInt(tilemap.cellBounds.center.y), 0));
             Bounds bounds = new Bounds(worldCenter,
                                        tilemap.cellBounds.size);
             Bounds worldBounds = bounds;
             if (worldBounds.Contains(target.position))
             {
                 string roomName = tilemap.transform.parent.name;
                 if (CurrentGrid.Key != roomName)
                 {
                     if (!GridsDict.ContainsKey(roomName))
                     {
                         Debug.LogErrorFormat(
                             "Didn't find room with name: {0}", roomName);
                     }
                     else
                     {
                         CurrentGrid =
                             new KeyValuePair <string, PathFind.Grid>(
                                 roomName, BuildGrid(GridsDict[roomName]));
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
    public Vector3?GridPosToWorldPos(Vector2Int gridPos)
    {
        string roomName = CurrentGrid.Key;

        if (!GridsDict.ContainsKey(roomName))
        {
            Debug.LogErrorFormat(
                "Didn't find room with name: {0}", roomName);
            return(null);
        }
        Tilemap ground = GridsDict[roomName].transform.Find("Ground")
                         .GetComponent <Tilemap>();
        Vector3 worldPos = ground.CellToWorld(
            new Vector3Int(gridPos.x + ground.cellBounds.min.x,
                           gridPos.y + ground.cellBounds.min.y, 0));

        // Move position to cell center.
        worldPos += new Vector3(ground.cellSize.x / 2.0f, ground.cellSize.y / 2.0f, 0.0f);
        return(worldPos);
    }
Exemplo n.º 3
0
    public Vector2Int?WorldPosToGridPos(Vector3 worldPos)
    {
        string roomName = CurrentGrid.Key;

        if (!GridsDict.ContainsKey(roomName))
        {
            Debug.LogErrorFormat(
                "Didn't find room with name: {0}", roomName);
            return(null);
        }
        Tilemap ground = GridsDict[roomName].transform.Find("Ground")
                         .GetComponent <Tilemap>();
        Vector3Int cellPosInt = ground.WorldToCell(worldPos);

        if (!ground.cellBounds.Contains(cellPosInt))
        {
            Debug.LogErrorFormat("World position {0} is not in current grid " +
                                 "cell bounds {1}.", cellPosInt, ground.cellBounds);
            return(null);
        }
        // Add xy offset to align with 2D arrary start point.
        return(new Vector2Int(cellPosInt.x - ground.cellBounds.min.x,
                              cellPosInt.y - ground.cellBounds.min.y));
    }