Пример #1
0
    void ColorChanges(ITilemap tilemap, TileBase tilebase, ref TileData tiledata, Vector3Int position)
    {
        for (int i = GridMin.x; i == GridMax.x; i++)
        {
            for (int j = GridMin.y; j == GridMax.y; j++)
            {
                for (int k = 0; i < InfectedTilesCoords.Count; k++)
                {
                    if (tilemap.GetTile(new Vector3Int(InfectedTilesCoords[k].x, InfectedTilesCoords[k].y, 0)) == tilemap.GetTile(new Vector3Int(ct.CurrentSelection.x, ct.CurrentSelection.y, 0)))
                    {
                        if (Input.GetKeyDown(KeyCode.Space))
                        {
                            tilebase.GetTileData(position, tilemap, ref tiledata);
                            tiledata.sprite = BrokenSelected;
                        }
                    }


                    if (tilemap.GetTile(new Vector3Int(i, j, 0)) == tilemap.GetTile(new Vector3Int(ct.CurrentSelection.x, ct.CurrentSelection.y, 0)))
                    {
                        tilebase.GetTileData(position, tilemap, ref tiledata);
                        tiledata.sprite = SelectedTile;
                    }
                }
            }
        }
    }
Пример #2
0
    public static TileData GetTileData(TileBase tile)
    {
        TileData data = new TileData();

        tile.GetTileData(new Vector3Int(0, 0, 0), null, ref data);
        return(data);
    }
Пример #3
0
    void InitializeUnityDefault()
    {
        tilemap2D = GetComponent <Tilemap>();

        if (tilemap2D == null)
        {
            return;
        }

        Grid grid = tilemap2D.layoutGrid;

        if (grid == null)
        {
            Debug.LogError("Lighting 2D Error: Lighting Tilemap Collider is missing Grid");
        }
        else
        {
            cellSize = grid.cellSize;
        }

        cellAnchor = tilemap2D.tileAnchor;

        int minPos = Mathf.Min(tilemap2D.cellBounds.xMin, tilemap2D.cellBounds.yMin);
        int maxPos = Mathf.Max(tilemap2D.cellBounds.size.x, tilemap2D.cellBounds.size.y);

        area = new BoundsInt(minPos, minPos, 0, maxPos + Mathf.Abs(minPos), maxPos + Mathf.Abs(minPos), 1);

        TileBase[] tileArray = tilemap2D.GetTilesBlock(area);

        map = new LightingTile[area.size.x + 1, area.size.y + 1];

        for (int index = 0; index < tileArray.Length; index++)
        {
            TileBase tile = tileArray[index];
            if (tile == null)
            {
                continue;
            }

            TileData tileData = new TileData();

            ITilemap tilemap = (ITilemap)FormatterServices.GetUninitializedObject(typeof(ITilemap));
            typeof(ITilemap).GetField("m_Tilemap", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(tilemap, tilemap2D);
            tile.GetTileData(new Vector3Int(0, 0, 0), tilemap, ref tileData);

            LightingTile lightingTile = new LightingTile();
            lightingTile.SetOriginalSprite(tileData.sprite);
            lightingTile.GetShapePolygons();


            map[(index % area.size.x), (index / area.size.x)] = lightingTile;
            //map[(index % area.size.x), (index / area.size.y)] = true;
        }
    }
Пример #4
0
        private void InitialiseSnakeHeadSprites()
        {
            SnakeHeadSprites = new Dictionary <string, Sprite>();
            AssemblySnakeFactory factory = new AssemblySnakeFactory();

            foreach (string name in factory.GetAllSnakeTypes())
            {
                string   path = String.Format("Assets\\IMG\\SnakeSprites\\Simple\\{0}\\{0}_1.asset", name);
                TileBase tile = (TileBase)AssetDatabase.LoadAssetAtPath(path, typeof(TileBase));
                TileData data = new TileData();
                tile.GetTileData(new Vector3Int(), null, ref data);
                SnakeHeadSprites.Add(name, data.sprite);
            }
        }
Пример #5
0
        private void InitializeGrid()
        {
            mapTiles.Clear();

            tilemap2D = properties.tilemap;
            ITilemap tilemap = GetITilemap(tilemap2D);

            foreach (Vector3Int position in tilemap2D.cellBounds.allPositionsWithin)
            {
                TileData tileData = new TileData();

                TileBase tilebase = tilemap2D.GetTile(position);

                if (tilebase != null)
                {
                    tilebase.GetTileData(position, tilemap, ref tileData);

                    LightTile lightTile = new LightTile();

                    lightTile.gridPosition = position;

                    if (shadowOptimization)
                    {
                        bool left  = GetTile(position + new Vector3Int(1, 0, 0));
                        bool up    = GetTile(position + new Vector3Int(0, 1, 0));
                        bool right = GetTile(position + new Vector3Int(-1, 0, 0));
                        bool down  = GetTile(position + new Vector3Int(0, -1, 0));

                        lightTile.occluded = left && right && up && down;
                    }

                    lightTile.rotation = tilemap2D.GetTransformMatrix(position).rotation.eulerAngles.z;

                    lightTile.SetOriginalSprite(tileData.sprite);
                    lightTile.GetPhysicsShapePolygons();

                    lightTile.colliderType = tileData.colliderType;

                    mapTiles.Add(lightTile);
                }
            }
        }
Пример #6
0
        public override void Initialize()
        {
            base.Initialize();

            if (UpdateProperties() == false)
            {
                return;
            }

            mapTiles.Clear();

            tilemap2D = properties.tilemap;

            ITilemap tilemap = (ITilemap)FormatterServices.GetUninitializedObject(typeof(ITilemap));

            typeof(ITilemap).GetField("m_Tilemap", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(tilemap, tilemap2D);

            foreach (Vector3Int position in tilemap2D.cellBounds.allPositionsWithin)
            {
                TileData tileData = new TileData();

                TileBase tilebase = tilemap2D.GetTile(position);

                if (tilebase != null)
                {
                    tilebase.GetTileData(position, tilemap, ref tileData);

                    LightTile lightTile = new LightTile();
                    lightTile.gridPosition = new Vector3Int(position.x, position.y, position.z);

                    lightTile.scale = tilemap.GetTransformMatrix(position).lossyScale;

                    lightTile.SetOriginalSprite(tileData.sprite);
                    lightTile.GetPhysicsShapePolygons();

                    mapTiles.Add(lightTile);
                }
            }

            chunkManager.Update(mapTiles, this);
        }
Пример #7
0
        public bool GetTile(Vector3Int position)
        {
            TileBase tilebase = tilemap2D.GetTile(position);

            if (tilebase == null)
            {
                return(false);
            }

            TileData tileData = new TileData();

            ITilemap tilemap = GetITilemap(tilemap2D);

            tilebase.GetTileData(position, tilemap, ref tileData);

            if (tileData.sprite == null)
            {
                return(false);
            }

            return(true);
        }
Пример #8
0
    public void SetupMap()
    {
        map = new RectangleMap();

        ITilemap tilemap = (ITilemap)FormatterServices.GetUninitializedObject(typeof(ITilemap));

        typeof(ITilemap).GetField("m_Tilemap", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(tilemap, tilemap2D);

        foreach (Vector3Int position in tilemap2D.cellBounds.allPositionsWithin)
        {
            TileData tileData = new TileData();

            TileBase tilebase = tilemap2D.GetTile(position);

            if (tilebase != null)
            {
                tilebase.GetTileData(position, tilemap, ref tileData);

                if (onlyColliders && tileData.colliderType == Tile.ColliderType.None)
                {
                    continue;
                }

                RectangleTile tile = new RectangleTile();
                tile.position.x = position.x;
                tile.position.y = position.y;

                map.mapTiles.Add(tile);
            }
        }

        map.width  = tilemap2D.cellBounds.size.x * 2;
        map.height = tilemap2D.cellBounds.size.y * 2;

        map.Init();
    }
    public void Initialize()
    {
        switch (mapType)
        {
        case MapType.UnityEngineTilemap:
            tilemap2D = GetComponent <Tilemap>();

            if (tilemap2D == null)
            {
                return;
            }

            Grid grid = tilemap2D.layoutGrid;

            if (grid == null)
            {
                Debug.LogError("Lighting 2D Error: Lighting Tilemap Collider is missing Grid");
            }
            else
            {
                cellSize = grid.cellSize;
            }

            cellAnchor = tilemap2D.tileAnchor;

            int minPos = Mathf.Min(tilemap2D.cellBounds.xMin, tilemap2D.cellBounds.yMin);
            int maxPos = Mathf.Max(tilemap2D.cellBounds.size.x, tilemap2D.cellBounds.size.y);

            area = new BoundsInt(minPos, minPos, 0, maxPos, maxPos, 1);

            TileBase[] tileArray = tilemap2D.GetTilesBlock(area);

            map = new LightingTile[area.size.x + 1, area.size.y + 1];

            for (int index = 0; index < tileArray.Length; index++)
            {
                TileBase tile = tileArray[index];
                if (tile == null)
                {
                    continue;
                }

                TileData tileData = new TileData();
                ITilemap tilemap  = (ITilemap)FormatterServices.GetUninitializedObject(typeof(ITilemap));
                typeof(ITilemap).GetField("m_Tilemap", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(tilemap, tilemap2D);
                tile.GetTileData(new Vector3Int(0, 0, 0), tilemap, ref tileData);

                LightingTile lightingTile = new LightingTile();
                lightingTile.sprite = tileData.sprite;
                lightingTile.GeneratePhysicsShape();


                map[(index % area.size.x), (index / area.size.x)] = lightingTile;
                //map[(index % area.size.x), (index / area.size.y)] = true;
            }

            break;

        case MapType.SuperTilemapEditor:
            /*
             *  CreativeSpore.SuperTilemapEditor.STETilemap tilemapSTE = GetComponent<CreativeSpore.SuperTilemapEditor.STETilemap>();
             *
             * map = new LightingTile[tilemapSTE.GridWidth + 2, tilemapSTE.GridHeight + 2];
             *
             * area.position = new Vector3Int((int)tilemapSTE.MapBounds.center.x, (int)tilemapSTE.MapBounds.center.y, 0);
             *
             * area.size = new Vector3Int((int)(tilemapSTE.MapBounds.extents.x + 1) * 2, (int)(tilemapSTE.MapBounds.extents.y + 1) * 2, 0);
             *
             * for(int x = 0; x <= tilemapSTE.GridWidth; x++) {
             *      for(int y = 0; y <= tilemapSTE.GridHeight; y++) {
             *              int tileX = x + area.position.x - area.size.x / 2;
             *              int tileY = y + area.position.y - area.size.y / 2;
             *              if(tilemapSTE.GetTile(tileX, tileY) == null) {
             *                      continue;
             *              }
             *              LightingTile lightingTile = new LightingTile();
             *              map[x, y] = lightingTile;
             *      }
             *  }
             */
            break;
        }
    }
Пример #10
0
    void CreateNodes()
    {
        int gridX = 0; //use these to work out the size and where each node should be in the 2d array we'll use to store our nodes so we can work out neighbours and get paths
        int gridY = 0;

        bool foundTileOnLastPass = false;

        //scan tiles and create nodes based on where they are
        for (int xCount = scanStartX; xCount < scanFinishX; xCount++)
        {
            float x = xCount * gridSizeX;

            for (int yCount = scanStartY; yCount < scanFinishY; yCount++)
            {
                float y = yCount * gridSizeY;

                //go through our world bounds in increments of 1
                TileBase tb = floor.GetTile(new Vector3Int(xCount, yCount, 0)); //check if we have a floor tile at that world coords

                if (tb == null)
                {
                }
                else
                {
                    //if we do we go through the obstacle layers and check if there is also a tile at those coords if so we set founObstacle to true
                    bool foundObstacle = false;
                    foreach (Tilemap t in obstacleLayers)
                    {
                        TileBase tb2 = t.GetTile(new Vector3Int(xCount, yCount, 0));

                        if (tb2 == null)
                        {
                        }
                        else
                        {
                            foundObstacle = true;
                        }

                        //if we want to add an unwalkable edge round our unwalkable nodes then we use this to get the neighbours and make them unwalkable
                        if (unwalkableNodeBorder > 0)
                        {
                            List <TileBase> neighbours = GetNeighbouringTiles(xCount, yCount, t);
                            foreach (TileBase tl in neighbours)
                            {
                                if (tl == null)
                                {
                                }
                                else
                                {
                                    foundObstacle = true;
                                }
                            }
                        }
                    }

                    if (foundObstacle == false)
                    {
                        //if we havent found an obstacle then we create a walkable node and assign its grid coords
                        GameObject node = (GameObject)Instantiate(nodePrefab, new Vector3(x + gridBase.transform.position.x + ((yCount % 2 != 0) ? .45f : 0), y + gridBase.transform.position.y, -.5f), Quaternion.Euler(0, 0, 0), walkableNodesParent);
                        WorldTile  wt   = node.GetComponent <WorldTile>();
                        wt.gridX            = gridX;
                        wt.gridY            = gridY;
                        foundTileOnLastPass = true; //say that we have found a tile so we know to increment the index counters
                        unsortedNodes.Add(node);

                        node.name = gridX.ToString() + " : " + gridY.ToString() + " - NODE";

                        Vector3Int pos      = new Vector3Int();
                        ITilemap   itilemap = null;
                        TileData   td       = new TileData();
                        tb.GetTileData(pos, itilemap, ref td);

                        wt.SetWorldTileType(td.sprite.name);
                    }
                    else
                    {
                        //if we have found an obstacle then we do the same but make the node unwalkable
                        GameObject node = (GameObject)Instantiate(nodePrefab, new Vector3(x + gridBase.transform.position.x + ((yCount % 2 != 0) ? .45f : 0), y + gridBase.transform.position.y, -.5f), Quaternion.Euler(0, 0, 0), nonWalkableNodesParent);
                        //we add the gridBase position to ensure that the nodes are ontop of the tile they relate too
                        //node.GetComponentInChildren<SpriteRenderer>().color = Color.red;
                        WorldTile wt = node.GetComponent <WorldTile>();
                        wt.gridX            = gridX;
                        wt.gridY            = gridY;
                        wt.walkable         = false;
                        foundTileOnLastPass = true;
                        unsortedNodes.Add(node);
                        node.name = gridX.ToString() + " : " + gridY.ToString() + " - UNWALKABLE NODE";
                    }
                    gridY++; //increment the y counter

                    if (gridX > gridBoundX)
                    { //if the current gridX/gridY is higher than the existing then replace it with the new value
                        gridBoundX = gridX;
                    }

                    if (gridY > gridBoundY)
                    {
                        gridBoundY = gridY;
                    }
                }
            }
            if (foundTileOnLastPass == true)
            {//since the grid is going from bottom to top on the Y axis on each iteration of the inside loop, if we have found tiles on this iteration we increment the gridX value and
             //reset the y value
                gridX++;
                gridY = 0;
                foundTileOnLastPass = false;
            }
        }

        //put nodes into 2d array based on the
        nodes = new GameObject[gridBoundX + 1, gridBoundY + 1]; //initialise the 2d array that will store our nodes in their position
        foreach (GameObject g in unsortedNodes)
        {                                                       //go through the unsorted list of nodes and put them into the 2d array in the correct position
            WorldTile wt = g.GetComponent <WorldTile>();
            //Debug.Log (wt.gridX + " " + wt.gridY);
            nodes[wt.gridX, wt.gridY] = g;
        }

        //assign neighbours to nodes
        for (int x = 0; x < gridBoundX; x++)
        { //go through the 2d array and assign the neighbours of each node
            for (int y = 0; y < gridBoundY; y++)
            {
                if (nodes[x, y] == null)
                { //check if the coords in the array contain a node
                }
                else
                {
                    WorldTile wt = nodes[x, y].GetComponent <WorldTile>(); //if they do then assign the neighbours
                    //if (wt.walkable == true) {
                    wt.neighbours = GetNeighbours(x, y, gridBoundX, gridBoundY);
                    //}
                }
            }
        }
        //after this we have our grid of nodes ready to be used by the astar algorigthm
    }