Exemplo n.º 1
0
    void SpawnTile(GameObject ghostBlockToChange)
    {
        List <GameObject> connections = new List <GameObject>();

        foreach (GameObject tileObject in ghostBlockToChange.transform.GetComponent <GhostBlockScript>().refBlocks)
        {
            connections.Add(tileObject.GetComponent <TileScript>().topPoint.gameObject);
        }
        //Spawn in the block
        GameObject spawnedTile = Instantiate(BlockSelectorScript.instance.tilePrefab, GameObject.FindGameObjectWithTag("Board").transform);

        //Set the connections
        TileConnectionsScript spawnedTileConnections = spawnedTile.GetComponentInChildren <TileConnectionsScript>();

        spawnedTileConnections.connections = connections;

        //Set each connection to be connected to this one
        foreach (GameObject otherCons in connections)
        {
            otherCons.GetComponent <TileConnectionsScript>().connections.Add(spawnedTile.GetComponent <TileScript>().topPoint.gameObject);
        }

        //Set the position

        spawnedTile.transform.position = ghostBlockToChange.transform.position;

        //Set the name
        spawnedTile.transform.name = "Cube (" + spawnedTile.transform.position.x + "," + spawnedTile.transform.position.z + ")";

        StartCoroutine(ResetGhostGrid());
    }
Exemplo n.º 2
0
    private void Start()
    {
        tiles = new GameObject[xAmount, zAmount];

        //Creating the tile
        for (int x = 0; x < xAmount; x++)
        {
            for (int z = 0; z < zAmount; z++)
            {
                Vector3    boxPos = new Vector3(x, 0, z);
                GameObject tile   = Instantiate(boxPrefab, boxPos, Quaternion.identity, transform);
                tiles[x, z] = tile;
                tile.name   = "Cube (" + x + "," + z + ")";
            }
        }
        for (int x = 0; x < xAmount; x++)
        {
            for (int z = 0; z < zAmount; z++)
            {
                GameObject            tile = tiles[x, z];
                TileConnectionsScript topConnectionScript = tile.GetComponent <TileScript>().topPoint;
                for (int i = 0; i < 4; i++)
                {
                    int[] mod     = directionMap[(Direction)i];
                    int[] newTile = new int[] { x + mod[0], z + mod[1] };
                    if (((newTile[0] < 0 || newTile[0] >= xAmount) || (newTile[1] < 0 || newTile[1] >= zAmount)) == false)
                    {
                        topConnectionScript.connections.Add((tiles[newTile[0], newTile[1]]).GetComponent <TileScript>().topPoint.gameObject);
                    }
                }
            }
        }

        GameObject.FindGameObjectWithTag("Player").GetComponent <PlayerMovement>().targetTile = tiles[0, 0].GetComponent <TileScript>().topPoint.gameObject;
    }
Exemplo n.º 3
0
    void SpawnBaseLevel()
    {
        gridBeenBuilt = true;
        //Check if there is a board
        if (GameObject.FindGameObjectWithTag("Board"))
        {
            DestroyImmediate(GameObject.FindGameObjectWithTag("Board"));
        }
        //board = Instantiate(new GameObject(), null);
        board = new GameObject();
        board.tag = "Board";
        board.name = "Level";


        int xAmount = (int)gridSetup.x;
        int zAmount = (int)gridSetup.y;

        tiles = new GameObject[xAmount, zAmount];

        //Creating the tile
        for (int x = 0; x < xAmount; x++)
        {
            for (int z = 0; z < zAmount; z++)
            {
                Vector3 boxPos = new Vector3(x, 0, z);
                GameObject tile = Instantiate(tileprefab, boxPos, Quaternion.identity, board.transform);
                tiles[x, z] = tile;
                tile.name = "Cube (" + x + "," + z + ")";
            }
        }
        for (int x = 0; x < xAmount; x++)
        {
            for (int z = 0; z < zAmount; z++)
            {
                GameObject tile = tiles[x, z];
                TileConnectionsScript topConnectionScript = tile.GetComponent<TileScript>().topPoint;
                for (int i = 0; i < 4; i++)
                {
                    int[] mod = directionMap[(Direction)i];
                    int[] newTile = new int[] { x + mod[0], z + mod[1] };
                    if (((newTile[0] < 0 || newTile[0] >= xAmount) || (newTile[1] < 0 || newTile[1] >= zAmount)) == false)
                    {
                        topConnectionScript.connections.Add((tiles[newTile[0], newTile[1]]).GetComponent<TileScript>().topPoint.gameObject);
                    }

                }
            }
        }

        GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMovement>().targetTile = tiles[0, 0].GetComponent<TileScript>().topPoint.gameObject;

    }
Exemplo n.º 4
0
    public void RemoveBlock(GameObject blockToRemove)
    {
        TileConnectionsScript connectionsScript = blockToRemove.GetComponentInChildren <TileConnectionsScript>();

        //remove the connections going to this block
        foreach (GameObject connection in connectionsScript.connections)
        {
            GameObject topConnectionPoint = blockToRemove.GetComponent <TileScript>().topPoint.gameObject;

            //check if it is connected
            if (connection.GetComponentInChildren <TileConnectionsScript>().connections.Contains(topConnectionPoint))
            {
                connection.GetComponentInChildren <TileConnectionsScript>().connections.Remove(topConnectionPoint);
            }
        }

        //Destroy the tile clicked
        Destroy(blockToRemove);
    }
Exemplo n.º 5
0
    public void generateTileGrid(GameObject tile)
    {
        //Directions from the tile that are missing connections
        List <Direction> missingDirections = new List <Direction>()
        {
            Direction.East, Direction.West, Direction.North, Direction.South
        };
        Vector3 startingPos = tile.transform.position;

        //Check the connections find if any are missing
        TileConnectionsScript tileConnections = tile.GetComponentInChildren <TileConnectionsScript>();

        foreach (GameObject connection in tileConnections.connections)
        {
            Vector3 checkingPos = connection.transform.position;

            if (startingPos.x < checkingPos.x)
            {
                //Debug.Log("Have East");
                missingDirections.Remove(Direction.East);
            }
            else if (startingPos.x > checkingPos.x)
            {
                //Debug.Log("Have West");
                missingDirections.Remove(Direction.West);
            }
            else if (startingPos.z < checkingPos.z)
            {
                //Debug.Log("Have North");
                missingDirections.Remove(Direction.North);
            }
            else if (startingPos.z > checkingPos.z)
            {
                //Debug.Log("Have South");
                missingDirections.Remove(Direction.South);
            }
        }



        //iterate through the missing directions
        foreach (Direction dir in missingDirections)
        {
            bool    spawn    = true;
            Vector3 SpawnPos = tile.transform.position;
            switch (dir)
            {
            case Direction.North:
                //Debug.Log("Spawning North");
                if (CheckIfGhost(Direction.North, tile))
                {
                    spawn = false;
                }
                SpawnPos += new Vector3(0, 0, 1);
                break;

            case Direction.South:
                //Debug.Log("Spawning South");
                if (CheckIfGhost(Direction.South, tile))
                {
                    spawn = false;
                }
                SpawnPos += new Vector3(0, 0, -1);
                break;

            case Direction.East:
                //Debug.Log("Spawning East");
                if (CheckIfGhost(Direction.East, tile))
                {
                    spawn = false;
                }
                SpawnPos += new Vector3(1, 0, 0);
                break;

            case Direction.West:
                //Debug.Log("Spawning West");
                if (CheckIfGhost(Direction.West, tile))
                {
                    spawn = false;
                }
                SpawnPos += new Vector3(-1, 0, 0);
                break;
            }
            if (spawn)
            {
                GameObject temp = Instantiate(blockAddGridBlock, SpawnPos, Quaternion.identity, addGridParent);
                temp.GetComponent <GhostBlockScript>().refBlocks.Add(tile);
            }
        }
    }