Exemplo n.º 1
0
    // Use this for initialization
    public void generate()
    {
        Vector3Int rng_range = rng.getBounds();

        bounds_x = new Vector2Int(rng_range.x, rng_range.x + rng_range.z);
        bounds_y = new Vector2Int(rng_range.y, rng_range.y + rng_range.z);


        bool randomizedGoal = false;
        bool randomizedKey  = false;

        generatePlayer();

        while (!randomizedGoal)
        {
            Vector3Int randomized = new Vector3Int(Random.Range(bounds_x.x, bounds_x.y), Random.Range(bounds_y.x, bounds_y.y), 0);
            if (withinDistance(new Vector2Int(randomized.x, randomized.y), gen_player_location))
            {
                if (rng.openSpace(randomized))
                {
                    gen_goal_location = new Vector2Int(randomized.x, randomized.y);
                    Vector3 location = walkable.CellToWorld(randomized);

                    GameObject goal = Instantiate(goalPrefab, location, Quaternion.identity) as GameObject;
                    goal.transform.localScale       = transform.localScale;
                    goal.GetComponent <Exit> ().rng = this.rng;
                    randomizedGoal = true;
                }
            }
        }

        while (!randomizedKey)
        {
            Vector3Int randomized = new Vector3Int(Random.Range(bounds_x.x, bounds_x.y), Random.Range(bounds_y.x, bounds_y.y), 0);

            if (withinDistance(new Vector2Int(randomized.x, randomized.y), gen_goal_location))
            {
                if (rng.openSpace(randomized))
                {
                    Vector3 location = walkable.CellToWorld(randomized);

                    GameObject key = Instantiate(keyPrefab, location, Quaternion.identity) as GameObject;
                    key.transform.localScale = transform.localScale;
                    randomizedKey            = true;
                }
            }
        }
    }
Exemplo n.º 2
0
    // Use this for initialization
    public void generate()
    {
        Vector3Int rng_range = rng.getBounds();

        //Debug.Log (rng_range);
        bounds_x = new Vector2Int(rng_range.x, rng_range.x + rng_range.z);
        bounds_y = new Vector2Int(rng_range.y, rng_range.y + rng_range.z);
//
//		if (prefabs.Length != amount.Length) {
//			if (prefabs.Length > amount.Length) {
//				int[] temp = amount;
//
//				amount = new int[prefabs.Length];
//
//				for (int i = 0; i < temp.Length; i++) {
//					amount [i] = temp [i];
//				}
//
//				for (int i = temp.Length; i < amount.Length; i++) {
//					amount [i] = temp [temp.Length - 1];
//				}
//
//
//			} else if (prefabs.Length < amount.Length) {
//				int[] temp = amount;
//
//				amount = new int[prefabs.Length];
//
//				for (int i = 0; i < amount.Length; i++) {
//					amount [i] = temp [i];
//				}
//			}
//		}
//

        for (int i = 0; i < prefabs.Length; i++)
        {
            for (int j = 0; j < amount[i]; j++)
            {
                bool spawned = false;


                int attempts = 0;

                while (!spawned)
                {
                    Vector3Int randomized = new Vector3Int(Random.Range(bounds_x.x, bounds_x.y), Random.Range(bounds_y.x, bounds_y.y), 0);


                    if (rng.openSpace(randomized))
                    {
                        Vector3 location = walkable.CellToWorld(randomized);

                        GameObject clone = Instantiate(prefabs [i], location, Quaternion.identity) as GameObject;
                        clone.transform.localScale = transform.localScale;
                        spawned = true;
                    }
                    else
                    {
                        attempts++;
                        if (attempts >= max_attempts)
                        {
                            spawned = true;
                            Debug.Log(prefabs[i] + " failure, " + attempts);
                        }
                    }
                }
            }
        }
    }