-
Notifications
You must be signed in to change notification settings - Fork 1
/
DivingSquidProceduraGeneration.cs
68 lines (56 loc) · 1.87 KB
/
DivingSquidProceduraGeneration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class DivingSquidProceduraGeneration : MonoBehaviour{
[SerializeField] int width, height;
[SerializeField] float smoothness;
[SerializeField] float seed;
[SerializeField] TileBase groundTile;
[SerializeField] Tilemap groundTilemap;
int[,] map;
void Start() {
Generation();
}
private void Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
Generation();
}
}
void Generation() {
groundTilemap.ClearAllTiles();
map = GenerateArray(width, height, true);
map = TerrainGeneration(map);
RenderMap(map, groundTilemap, groundTile);
}
public int[,] GenerateArray(int width, int height, bool empty) {
int[,] map = new int[width, height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
map[x, y] = (empty) ? 0 : 1;
}
}
return map;
}
public int[,] TerrainGeneration(int[,] map) {
int perlinHeight;
for (int x = 0; x < width; x++) {
perlinHeight = Mathf.RoundToInt(Mathf.PerlinNoise(x / smoothness, seed) * height / 2);
perlinHeight += height / 2;
for (int y = 0; y < perlinHeight; y++) {
map[x, y] = 1;
}
}
return map;
}
public void RenderMap(int[,] map, Tilemap groundTilemap, TileBase groundTileBase) {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (map[x, y] == 1) {
groundTilemap.SetTile(new Vector3Int(x,y,0), groundTileBase);
}
}
}
}
}