Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- //add this class to any folder
- public class LevelGenerator : MonoBehaviour {
- public Vector3 size = new Vector3(10,10,15);
- public Vector3 tileSize = new Vector3(1,1,.68f);
- public int seed = 1;
- public float ruffness = 1f;
- public float amplitude = 1f;
- public GridMap map ;
- public Tile grassTile;
- public Tile[] tiles;
- public void instantiate() {
- if(map != null) {
- reset(map);
- map.initTestMap(size, (x,y,z) => mapToTile(x,y,z), tileSize);
- }
- }
- public Tile mapToTile(int x, int y, int z) {
- Vector2 vec = new Vector2(x,y) * ruffness + new Vector2(seed, seed);
- float height = Mathf.PerlinNoise(vec.x/size.x, vec.y/size.y);
- if( z <= height * amplitude) {
- var min = Mathf.Min(tiles.Length - 1 , z);
- return tiles[(int) min];
- } else {
- return null;
- }
- }
- public void reset( GridMap map) {
- for (int i = 0; i < map.mapSize.x; i++) {
- for (int j = 0; j < map.mapSize.y; j++) {
- for (int k = 0; k < map.mapSize.z; k++) {
- if(map[i,j,k] != null)
- GameObject.DestroyImmediate(map[i,j,k].gameObject);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement