public int CreateLevel(Vector3 myPosition, Vector3 blockPosition) { float i = blockPosition.x; float j = blockPosition.y; float k = blockPosition.z; Vector3 point = new Vector3(i + myPosition.x, j + myPosition.y, k + myPosition.z); float levelSeed = levelSeedGenerate.PerlinNoiseGenerate(point); if ((levelSeed > 0.55 && levelSeed < .65) || levelSeed < 0.2 || levelSeed > 0.9) { var newBlock = GameObject.Instantiate(levelBlock); newBlock.transform.position = new Vector3(i + myPosition.x, j + myPosition.y, k + myPosition.z); return(1); } // make horizontal platforms a bit bigger else if (levelSeed > 0.45 && levelSeed < .7) { Vector3[] points = new Vector3[] { new Vector3(i + myPosition.x + 1, j + myPosition.y, k + myPosition.z), new Vector3(i + myPosition.x - 1, j + myPosition.y, k + myPosition.z), new Vector3(i + myPosition.x, j + myPosition.y, k + myPosition.z + 1), new Vector3(i + myPosition.x, j + myPosition.y, k + myPosition.z - 1) }; for (int ctr = 0; ctr < points.Length; ctr++) { float levelSeedNextTo = levelSeedGenerate.PerlinNoiseGenerate(points[ctr]); if (levelSeedNextTo > 0.5 && levelSeed < 0.6) { GameObject newBlock = GameObject.Instantiate(levelBlock); newBlock.transform.position = new Vector3(i + myPosition.x, j + myPosition.y, k + myPosition.z); return(1); } } } //generate powerup else { point = new Vector3(point.x, point.y - 2, point.z); levelSeed = levelSeedGenerate.PerlinNoiseGenerate(point); if (levelSeed > 0.55 && levelSeed < .65) { powerupGenerate.seed = (int)(blockPosition.x + myPosition.x + 10 * (blockPosition.y + blockPosition.y) + 100 * (blockPosition.z + blockPosition.z) + seed); if (powerupGenerate.range(0, 50) == 10) { GameObject newPowerup = GameObject.Instantiate(powerup); point = new Vector3(point.x, point.y + 2, point.z); newPowerup.transform.position = point; } //Debug.Log(a); } } return(0); }
// Perlin Noise Function public float PerlinNoiseGenerate(float point1, float point2) { //setup variables (more boring stuff) float valReturn = 0; float interpolate1; float interpolate2; float interpolated; int res = resolution; RandomXORShift rand1 = new RandomXORShift(); RandomXORShift rand2 = new RandomXORShift(); RandomXORShift rand3 = new RandomXORShift(); RandomXORShift rand4 = new RandomXORShift(); // Loop to get the data from each octave for (int ctr = 1; ctr < detail; ctr++) { // make boxes smaller each octave res = res / 2; rand1.seed = seed + Mathf.RoundToInt((Mathf.FloorToInt(point1 / res) * -7) + (Mathf.FloorToInt(point2 / res) * 3)); rand2.seed = seed + Mathf.RoundToInt((Mathf.FloorToInt((point1 + res) / res) * -7) + (Mathf.FloorToInt(point2 / res) * 3)); rand3.seed = seed + Mathf.RoundToInt((Mathf.FloorToInt(point1 / res) * -7) + (Mathf.FloorToInt((point2 + res) / res) * 3)); rand4.seed = seed + Mathf.RoundToInt((Mathf.FloorToInt((point1 + res) / res) * -7) + (Mathf.FloorToInt((point2 + res) / res) * 3)); // interpolation interpolate1 = RandomXORShift.interpolateCosine(rand1.range(0, 2), rand2.range(0, 2), point1 - (Mathf.FloorToInt(point1 / res) * res), res); interpolate2 = RandomXORShift.interpolateCosine(rand3.range(0, 2), rand4.range(0, 2), point1 - (Mathf.FloorToInt(point1 / res) * res), res); interpolated = RandomXORShift.interpolateCosine(interpolate1, interpolate2, point2 - (Mathf.FloorToInt(point2 / res) * res), res); // add octave value to point; each octave has half as much influence as the one before it valReturn += interpolated * Mathf.Pow(2, -ctr); } return(valReturn); }