Advertisement
Guest User

Level Generator

a guest
Apr 19th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. //add this class to any folder
  6. public class LevelGenerator : MonoBehaviour {
  7.     public Vector3 size = new Vector3(10,10,15);
  8.     public Vector3 tileSize = new Vector3(1,1,.68f);
  9.  
  10.     public int seed = 1;
  11.     public float ruffness = 1f;
  12.     public float amplitude = 1f;
  13.  
  14.     public GridMap map  ;
  15.  
  16.     public Tile grassTile;
  17.  
  18.     public Tile[] tiles;
  19.  
  20.     public void instantiate() {
  21.         if(map != null) {
  22.             reset(map);
  23.             map.initTestMap(size, (x,y,z) => mapToTile(x,y,z), tileSize);
  24.         }
  25.     }
  26.  
  27.     public Tile mapToTile(int x, int y, int z) {
  28.         Vector2 vec = new Vector2(x,y) * ruffness + new Vector2(seed, seed);
  29.         float height = Mathf.PerlinNoise(vec.x/size.x, vec.y/size.y);
  30.  
  31.         if( z <= height * amplitude) {
  32.             var min = Mathf.Min(tiles.Length - 1 , z);
  33.             return tiles[(int) min];
  34.         } else {
  35.             return null;
  36.         }
  37.     }
  38.  
  39.     public void reset( GridMap map) {
  40.         for (int i = 0; i < map.mapSize.x; i++) {
  41.             for (int j = 0; j < map.mapSize.y; j++) {
  42.                 for (int k = 0; k < map.mapSize.z; k++) {
  43.                     if(map[i,j,k] != null)
  44.                         GameObject.DestroyImmediate(map[i,j,k].gameObject);
  45.                 }
  46.             }
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement