Exemplo n.º 1
0
    // Place the key
    void PlaceKey(GameObject key, GameObject chest)
    {
        // Distance from key to chest
        float dist = Vector3.Distance(key.transform.position, chest.transform.position);

        // Distance from player to key
        float distToPlayer = Vector3.Distance(key.transform.position, GameObject.FindGameObjectWithTag(Tags.playerTag).transform.position);

        // Has the key been placed
        bool keyPlaced = false;

        // While the key has not been placeed
        while (!keyPlaced)
        {
            // If the distance from key to chest is inbetween the max and min distance place the key
            if (dist < questManager.maxKeyToChestDist && distToPlayer > questManager.minKeyToPlayerDist)
            {
                // Place the key
                keyPlaced = true;
                key.transform.position = new Vector3((int)key.transform.position.x, key.transform.position.y, (int)key.transform.position.z);
                key.GetComponent <Key>().Index(GameDataManager.instance.RecordKeyPosition(key.transform.position));
            }

            // Else find a new location for the key
            else
            {
                // Choose a new random key position
                map.RandomPosition(key);
                dist = Vector3.Distance(key.transform.position, chest.transform.position);
            }
        }

        // Open the grid cube for the key and set it as empty
        CubeData cubeData = map.GridCubeArray()[(int)key.transform.position.x, (int)key.transform.position.z].GetComponent <CubeData>();

        // Open the grid cube - empty cube
        cubeData.OpenGridCube();
    }
Exemplo n.º 2
0
    // Position a gameobject in place of a rock cube
    public void PositionChestInRock(GameObject go)
    {
        // Set eth gameobject as not spawned
        bool spawned = false;

        // While the gameobject has not been spawned in
        while (!spawned)
        {
            // Counter for the open grid cubes
            int allCubesClosed = 0;

            // Choose a random grid x and y coord
            Vector3 randomPos = RandomPosition();

            // Assign the cubedata
            CubeData cubeData = gridCubeArray[(int)randomPos.x, (int)randomPos.z].GetComponent <CubeData>();

            // If the cube type is rock
            if (cubeData.GetCubeType() == CUBETYPE.ROCK)
            {
                // Loop through the adjacent grid cubes - z axis
                for (int z = -1; z < 2; z++)
                {
                    // Loop through the adjacent grid cubes - x axis
                    for (int x = -1; x < 2; x++)
                    {
                        // Assign the cubedata
                        CubeData cubeDataToCheck = gridCubeArray[(int)randomPos.x + x, (int)randomPos.z + z].GetComponent <CubeData>();

                        // If the grid cube at the random coord is not open
                        if (!cubeDataToCheck.GetCubeOpen())
                        {
                            // Count the adjacent closed cube (plus 1)
                            allCubesClosed++;
                        }
                    }
                }

                // If all the adjacent grid cubes are closed
                if (allCubesClosed == 9)
                {
                    // Open the grid cube and set as chest cube
                    cubeData.OpenGridCube();
                    cubeData.SetAsChestCube();

                    // Rotate gameobject to face out from walls
                    if (randomPos.x <= gridWidth / 2 && randomPos.z <= gridHeight / 2)
                    {
                        go.transform.Rotate(Vector3.up, 90.0f, Space.Self);
                    }

                    if (randomPos.x <= gridWidth / 2 && randomPos.z > gridHeight / 2)
                    {
                        go.transform.Rotate(Vector3.up, 180.0f, Space.Self);
                    }

                    if (randomPos.x > gridWidth / 2 && randomPos.z > gridHeight / 2)
                    {
                        go.transform.Rotate(Vector3.up, -90.0f, Space.Self);
                    }

                    // Set the gamobjects position
                    go.transform.position = new Vector3(randomPos.x, go.transform.position.y, randomPos.z);

                    // Break out of the loop and spawn the gameobject
                    spawned = true;
                }
            }
        }
    }