Exemplo n.º 1
0
    public bool RemoveTile(Vector3 pos)
    {
        MultiLayerTile multiTile = FetchTile(pos);

        if (multiTile != null)
        {
            GameObject tileToDestroy = multiTile.RemoveTop();
            if (tileToDestroy)
            {
                Tile t = tileToDestroy.GetComponent <Tile>();
                DestroyTile(ref t);

                // Recalculate the walkable value for tiles occupied
                Vector2 tileIndex  = FetchTileIndex(pos);
                float   scaleRatio = t.ScaleRatio;
                for (int row = (int)tileIndex.x; row < tileIndex.x + scaleRatio; ++row)
                {
                    for (int col = (int)tileIndex.y; col < tileIndex.y + scaleRatio; ++col)
                    {
                        MultiLayerTile tile = FetchTile(row, col);
                        if (tile != null)
                        {
                            tile.Walkable = tile.CalculateWalkable();
                        }
                    }
                }

                return(true);
            }
        }
        return(false);
    }
Exemplo n.º 2
0
    private void updateMap()
    {
        // Map
        tileSize             = calculateTileSize(TotalSize);
        tileMapDistToTopLeft = generateDistToTopLeft(rowCount, colCount);
        Vector3 startPos;

        for (int rowIndex = 0; rowIndex < map.Count; ++rowIndex)
        {
            Row row = map[rowIndex];
            for (int colIndex = 0; colIndex < row.column.Count; ++colIndex)
            {
                MultiLayerTile multiTile = row.column[colIndex];
                startPos = generateStartPos(rowCount, colCount, rowIndex, colIndex);
                for (int tileIndex = 0; tileIndex < multiTile.multiLayerTile.Count; ++tileIndex)
                {
                    GameObject tile = multiTile.multiLayerTile[tileIndex];
                    startPos.z = tile.transform.position.z;
                    float   scaleRatio = tile.GetComponent <Tile>().ScaleRatio;
                    Vector3 size       = new Vector3(tileSize * scaleRatio, tileSize * scaleRatio);
                    tile.transform.position   = startPos + new Vector3((scaleRatio - 1) * tileSize * 0.5f, -((scaleRatio - 1) * tileSize * 0.5f));
                    tile.transform.localScale = size;
                }
            }
        }

        // Grid lines
        updateGridLines();
    }
Exemplo n.º 3
0
    public bool AddTile(Vector3 mousePos, GameObject blueprint)
    {
        MultiLayerTile multiTile = FetchTile(mousePos);
        Vector2        tileIndex = FetchTileIndex(mousePos);

        if (multiTile != null && multiTile.IsWalkable())
        {
            // Check if same tile exists
            foreach (GameObject tempGO in multiTile.multiLayerTile)
            {
                if (tempGO.GetComponent <Tile>().Type == blueprint.GetComponent <Tile>().Type)
                {
                    return(false);
                }
            }

            float scaleRatio = blueprint.GetComponent <Tile>().ScaleRatio;
            List <MultiLayerTile> tempList = new List <MultiLayerTile>();

            // Set other tiles walkable to false if scale ratio bigger than 0
            for (int row = (int)tileIndex.x; row < tileIndex.x + scaleRatio; ++row)
            {
                for (int col = (int)tileIndex.y; col < tileIndex.y + scaleRatio; ++col)
                {
                    MultiLayerTile tile = FetchTile(row, col);
                    if (tile != null && tile.IsWalkable())
                    {
                        tile.Walkable = blueprint.GetComponent <Tile>().IsWalkable();
                        tempList.Add(tile);
                    }
                    else
                    {
                        foreach (MultiLayerTile t in tempList)
                        {
                            t.Walkable = t.CalculateWalkable();
                        }
                        return(false);
                    }
                }
            }

            // Adding the tile
            //GameObject newTile = Instantiate(blueprint);
            Tile.TILE_TYPE type    = blueprint.GetComponent <Tile>().Type;
            Vector3        pos     = generateStartPos(RowCount, ColCount, (int)tileIndex.x, (int)tileIndex.y);// + new Vector3((scaleRatio - 1) * tileSize * 0.5f, -((scaleRatio - 1) * tileSize * 0.5f));
            Vector3        size    = new Vector3(tileSize, tileSize);
            GameObject     newTile = createTile(type, pos, size);

            /*newTile.transform.position = pos + ;
             * newTile.transform.localScale = size;
             * newTile.transform.parent = transform;*/
            tiles.Add(newTile.GetComponent <Tile>());
            multiTile.AddFront(newTile);
            return(true);
        }
        return(false);
    }
Exemplo n.º 4
0
    public MultiLayerTile FetchTile(int rowIndex, int colIndex)
    {
        if (rowIndex < 0 || rowIndex >= map.Count || colIndex < 0 || colIndex >= map[0].column.Count)
        {
            return(null);
        }
        Row            row   = map[rowIndex];
        MultiLayerTile tiles = row.column[colIndex];

        return(tiles);
    }
Exemplo n.º 5
0
 private void ActivateTilesByIndex(int startRow, int endRow, int startCol, int endCol, bool activation)
 {
     for (int rowIndex = startRow; rowIndex < endRow; ++rowIndex)
     {
         for (int colIndex = startCol; colIndex < endCol; ++colIndex)
         {
             MultiLayerTile mTile = FetchTile(rowIndex, colIndex);
             if (mTile != null)
             {
                 for (int tileIndex = 0; tileIndex < mTile.multiLayerTile.Count; ++tileIndex)
                 {
                     GameObject tile = mTile.multiLayerTile[tileIndex];
                     tile.SetActive(activation);
                 }
             }
         }
     }
 }
Exemplo n.º 6
0
    protected virtual void ActivateTiles(int rowIndexMin, int rowIndexMax, int colIndexMin, int colIndexMax, bool mode = true)
    {
        //if (!isActivated()) // Check if first update is required
        if (minRowIndex != rowIndexMin || maxRowIndex != rowIndexMax || minColIndex != colIndexMin || maxColIndex != colIndexMax)
        {
            // First tile update is required (Full activation)
            IEnumerable <Tile> activeTiles = from tile in tiles where tile.gameObject.activeSelf select tile;
            foreach (Tile singleTile in activeTiles)
            {
                if (!singleTile.IgnoreActive)
                {
                    singleTile.gameObject.SetActive(false);
                }
            }

            for (int rowIndex = rowIndexMin; rowIndex <= rowIndexMax; ++rowIndex)
            {
                for (int colIndex = colIndexMin; colIndex <= colIndexMax; ++colIndex)
                {
                    MultiLayerTile multiTiles = FetchTile(rowIndex, colIndex);
                    if (multiTiles != null)
                    {
                        for (int multiIndex = 0; multiIndex < multiTiles.multiLayerTile.Count; ++multiIndex)
                        {
                            GameObject goTile = multiTiles.multiLayerTile[multiIndex];
                            if (!goTile.GetComponent <Tile>().IgnoreActive)
                            {
                                goTile.SetActive(mode);
                            }
                        }
                    }
                }
            }
            minRowIndex = rowIndexMin;
            maxRowIndex = rowIndexMax;
            minColIndex = colIndexMin;
            maxColIndex = colIndexMax;
        }

        /*else
         * {
         *  runtimeActivateTiles(rowIndexMin, rowIndexMax, colIndexMin, colIndexMax);
         * }*/
    }
Exemplo n.º 7
0
    private bool generateMap(ArrayList sMap, int numRow, int numCol)
    {
        tileSize             = calculateTileSize(TotalSize);
        tileMapDistToTopLeft = generateDistToTopLeft(numRow, numCol);
        rowCount             = numRow;
        colCount             = numCol;

        // Calculate data needed
        Vector3 startPos = generateStartPos(numRow, numCol);   // Calculate start position
        Vector3 size     = new Vector3(tileSize, tileSize, 1); // Size of tile (Scale)

        // Generate map
        for (int rowIndex = 0; rowIndex < numRow;) // Loop for rows
        {
            // Data
            string   line   = (string)sMap[rowIndex];
            string[] tokens = line.Split(TILE_SPLIT);             // Split each tile with ','

            //ArrayList rowOfData = new ArrayList(); // One row of tile
            Row rowOfData = new Row();

            for (int colIndex = 0; colIndex < numCol;)             // Loop for cols
            {
                GameObject tile;
                if (colIndex < tokens.Length)                 // Use data from file
                {
                    // Split different layer tiles
                    string   combinedLayerTiles = tokens[colIndex];
                    string[] layers             = combinedLayerTiles.Split(TILE_MULTIPLE_LAYER_SPLIT);

                    //ArrayList multiLayerTile = new ArrayList();
                    MultiLayerTile multiLayerTile = new MultiLayerTile();

                    for (int layerIndex = 0; layerIndex < layers.Length; ++layerIndex)
                    {
                        int   tileType   = Int32.Parse(layers[layerIndex]);
                        float scaleRatio = tileBlueprints[tileType].GetComponent <Tile>().ScaleRatio;
                        tile = createTile((Tile.TILE_TYPE)tileType, startPos, size);

                        if (tile)
                        {
                            // Add to multi-layer
                            multiLayerTile.AddTile(tile);

                            // Add to tile list
                            tiles.Add(tile.GetComponent <Tile>());
                        }
                    }

                    // Add to row of data
                    rowOfData.column.Add(multiLayerTile);
                }
                else                 // Data not within file, empty tile
                {
                    MultiLayerTile multiLayerTile = new MultiLayerTile();
                    tile = createTile(DefaultTile, startPos, size);

                    if (tile)
                    {
                        // Add to multi-layer
                        multiLayerTile.AddTile(tile);

                        // Add to tile list
                        tiles.Add(tile.GetComponent <Tile>());
                    }

                    // Add to row of data
                    rowOfData.column.Add(multiLayerTile);
                }

                // Next col startPos
                startPos = generateStartPos(numRow, numCol, rowIndex, ++colIndex);
            }

            // Add row of data into map
            map.Add(rowOfData);

            // Next row startPos
            startPos = generateStartPos(numRow, numCol, ++rowIndex, 0);
        }
        return(true);
    }
Exemplo n.º 8
0
    private bool generateMap(ArrayList sMap, int numRow, int numCol)
    {
        tileSize = calculateTileSize(TotalSize);
        tileMapDistToTopLeft = generateDistToTopLeft(numRow, numCol);
        rowCount = numRow;
        colCount = numCol;

        // Calculate data needed
        Vector3 startPos = generateStartPos(numRow, numCol); // Calculate start position
        Vector3 size = new Vector3(tileSize, tileSize, 1); // Size of tile (Scale)

        // Generate map
        for (int rowIndex = 0; rowIndex < numRow;) // Loop for rows
        {
            // Data
            string line = (string)sMap[rowIndex];
            string[] tokens = line.Split(TILE_SPLIT); // Split each tile with ','

            //ArrayList rowOfData = new ArrayList(); // One row of tile
            Row rowOfData = new Row();

            for (int colIndex = 0; colIndex < numCol;) // Loop for cols
            {
                GameObject tile;
                if (colIndex < tokens.Length) // Use data from file
                {
                    // Split different layer tiles
                    string combinedLayerTiles = tokens[colIndex];
                    string[] layers = combinedLayerTiles.Split(TILE_MULTIPLE_LAYER_SPLIT);

                    //ArrayList multiLayerTile = new ArrayList();
                    MultiLayerTile multiLayerTile = new MultiLayerTile();

                    for (int layerIndex = 0; layerIndex < layers.Length; ++layerIndex)
                    {
                        int tileType = Int32.Parse(layers[layerIndex]);
                        float scaleRatio = tileBlueprints[tileType].GetComponent<Tile>().ScaleRatio;
                        tile = createTile((Tile.TILE_TYPE)tileType, startPos, size);

                        if (tile)
                        {
                            // Add to multi-layer
                            multiLayerTile.AddTile(tile);

                            // Add to tile list
                            tiles.Add(tile.GetComponent<Tile>());
                        }
                    }

                    // Add to row of data
                    rowOfData.column.Add(multiLayerTile);
                }
                else // Data not within file, empty tile
                {
                    MultiLayerTile multiLayerTile = new MultiLayerTile();
                    tile = createTile(DefaultTile, startPos, size);

                    if (tile)
                    {
                        // Add to multi-layer
                        multiLayerTile.AddTile(tile);

                        // Add to tile list
                        tiles.Add(tile.GetComponent<Tile>());
                    }

                    // Add to row of data
                    rowOfData.column.Add(multiLayerTile);
                }

                // Next col startPos
                startPos = generateStartPos(numRow, numCol, rowIndex, ++colIndex);
            }

            // Add row of data into map
            map.Add(rowOfData);

            // Next row startPos
            startPos = generateStartPos(numRow, numCol, ++rowIndex, 0);
        }
        return true;
    }