Ejemplo n.º 1
0
    public void BuildLevel(TextAsset levelData, GFGrid levelGrid)
    {
        //abort if there are no prefabs to instantiate
        if(!red || !green || !blue)
            return;

        //loop though the list of old blocks and destroy all of them, we don't want the new level on top of the old one
        foreach(GameObject go in blocks){
            if(go)
                Destroy(go);
        }
        //destroying the blocks doesn't remove the reference to them in the list, so clear the list
        blocks.Clear();

        //setup the reader, a variable for storing the read line and keep track of the number of the row we just read
        reader = new StringReader(levelData.text);
        string line;
        int row = 0;

        //read the text file line by line as long as there are lines
        while((line = reader.ReadLine()) != null){
            //read each line character by character
            for(int i = 0; i < line.Length; i++){
                //first set the target position based on where in the text file we are, then place a block there
                Vector3 targetPosition = levelGrid.GridToWorld(new Vector3(i + shift, -row - shift, 0)); //offset by 0.5
                CreateBlock(line[i], targetPosition);
            }
            //we read a row, now it's time to read the next one; increment the counter
            row++;
        }
    }
Ejemplo n.º 2
0
    // makes the object snap to the bottom of the grid, respecting the grid's rotation
    Vector3 CalculateOffsetY()
    {
        //first store the objects position in grid coordinates
        Vector3 gridPosition = grid.WorldToGrid(cachedTransform.position);

        //then change only the Y coordinate
        gridPosition.y = 0.5f * cachedTransform.lossyScale.y;

        //convert the result back to world coordinates
        return(grid.GridToWorld(gridPosition));
    }
Ejemplo n.º 3
0
    // is a grid  point in the castable area for a certain player?
    public bool inCastableArea(Vector2 gridPos, bool playersTurn = true)
    {
        List <GridBox> gbs;

        if (playersTurn)
        {
            gbs = playerCastArea;
        }
        else
        {
            //gbs = playerCastArea;
            return(false);            // you cant cast when its not your turn
        }

        foreach (GridBox gb in gbs)
        {
            if (gb.isIn(grid.GridToWorld(gridPos)))
            {
                return(true);
            }
        }

        return(false);
    }
Ejemplo n.º 4
0
    public void BuildLevel(TextAsset levelData, GFGrid levelGrid)
    {
        //abort if there are no prefabs to instantiate
        if (!red || !green || !blue)
        {
            return;
        }

        //loop though the list of old blocks and destroy all of them, we don't want the new level on top of the old one
        foreach (GameObject go in blocks)
        {
            if (go)
            {
                Destroy(go);
            }
        }
        //destroying the blocks doesn't remove the reference to them in the list, so clear the list
        blocks.Clear();

        //setup the reader, a variable for storing the read line and keep track of the number of the row we just read
        reader = new StringReader(levelData.text);
        string line;
        int    row = 0;

        //read the text file line by line as long as there are lines
        while ((line = reader.ReadLine()) != null)
        {
            //read each line character by character
            for (int i = 0; i < line.Length; i++)
            {
                //first set the target position based on where in the text file we are, then place a block there (add 1 for polar grids because we don't want the origin)
                Vector3 targetPosition = levelGrid.GridToWorld(new Vector3(i + shift + (levelGrid.GetType() == typeof(GFPolarGrid) ? 1 : 0), -row - shift, 0));                 //offset by 0.5
                CreateBlock(line[i], targetPosition);
            }
            //we read a row, now it's time to read the next one; increment the counter
            row++;
        }
    }
Ejemplo n.º 5
0
    /// <summary>Spawns blocks based on a text file and a grid.</summary>
    public void BuildLevel(TextAsset levelData, GFGrid levelGrid)
    {
        // abort if there are no prefabs to instantiate
        if (!red || !green || !blue)
        {
            return;
        }

        // loop though the list of old blocks and destroy all of them, we don't want the new level on top of the old one
        foreach (GameObject go in blocks)
        {
            if (go)
            {
                Destroy(go);
            }
        }

        // destroying the blocks doesn't remove the reference to them in the list, so clear the list
        blocks.Clear();

        //setup the reader, a variable for storing the read line and keep track of the number of the row we just read
        reader = new StringReader(levelData.text);
        string line;
        int    row = 0;

        //read the text file line by line as long as there are lines
        while ((line = reader.ReadLine()) != null)
        {
            //read each line character by character
            for (int column = 0; column < line.Length; column++)
            {
                Vector3 targetPosition = levelGrid.GridToWorld(new Vector3(column, -row, 0) + offset);
                CreateBlock(line[column], targetPosition);
            }
            //we read a row, now it's time to read the next one; increment the counter
            row++;
        }
    }
Ejemplo n.º 6
0
    Vector3 FindNextFace()
    {
        //we will be operating in grid space, so convert the position
        Vector3 newPosition = grid.WorldToGrid(cachedTransform.position);

        //first let's pick a random number for one of the four possible directions
        int n = Random.Range(0, 4);

        //now add one grid unit onto position in the picked direction
        if (n == 0)
        {
            newPosition = newPosition + new Vector3(1, 0, 0);
        }
        else if (n == 1)
        {
            newPosition = newPosition + new Vector3(-1, 0, 0);
        }
        else if (n == 2)
        {
            newPosition = newPosition + new Vector3(0, 1, 0);
        }
        else if (n == 3)
        {
            newPosition = newPosition + new Vector3(0, -1, 0);
        }
        //if we would wander off beyond the size of the grid turn the other way around
        for (int j = 0; j < 2; j++)
        {
            if (Mathf.Abs(newPosition[j]) > grid.size[j])
            {
                newPosition[j] -= Mathf.Sign(newPosition[j]) * 2.0f;
            }
        }

        //return the position in world space
        return(grid.GridToWorld(newPosition));
    }