Added new tile generation to the game
This commit is contained in:
@@ -1,204 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class NoiseTesting : MonoBehaviour
|
||||
{
|
||||
System.Random rand = new System.Random();
|
||||
Dictionary<Vector3, GameObject> mapGen;
|
||||
Vector3 current;
|
||||
Vector3 next;
|
||||
public GameObject tilePrefab;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
mapGen = new Dictionary<Vector3, GameObject>();
|
||||
current = new Vector3(0, 0, 0);
|
||||
mapGen.Add(current, Instantiate(tilePrefab, current, Quaternion.identity));
|
||||
applyNormalNoise(mapGen[current], mapGen);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Space))
|
||||
{
|
||||
applyNormalNoise(mapGen[new Vector3(0, 0, 0)], mapGen);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.A))
|
||||
{
|
||||
next = current - new Vector3(100, 0, 0);
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.W))
|
||||
{
|
||||
next = current - new Vector3(0, 0, -100);
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.D))
|
||||
{
|
||||
next = current - new Vector3(-100, 0, 0);
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.S))
|
||||
{
|
||||
next = current - new Vector3(0, 0, 100);
|
||||
}
|
||||
|
||||
if (next != current)
|
||||
{
|
||||
current = next;
|
||||
if (!mapGen.ContainsKey(current))
|
||||
{
|
||||
mapGen.Add(current, Instantiate(tilePrefab, current, Quaternion.identity));
|
||||
applyNormalNoise(mapGen[current], mapGen);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void applyNormalNoise(GameObject tile, Dictionary<Vector3, GameObject> map)
|
||||
{
|
||||
//resetMesh(tile);
|
||||
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
|
||||
Vector3[] vertices = mesh.vertices;
|
||||
//float[] samples = calculateBasicSamples(tile);
|
||||
float[] samples = calculateConnectedSamples(tile);
|
||||
|
||||
Color32[] colors;
|
||||
Color32 low = new Color32(0, 150, 0, 255);
|
||||
Color32 high = new Color32(0, 110, 20, 255);
|
||||
string tiletype = "";
|
||||
|
||||
float lowestValue = 10;
|
||||
float highestValue = 0;
|
||||
|
||||
for (int i = 0; i < samples.Length; i++)
|
||||
{
|
||||
if (lowestValue > samples[i])
|
||||
{
|
||||
lowestValue = samples[i];
|
||||
}
|
||||
if (highestValue < samples[i])
|
||||
{
|
||||
highestValue = samples[i];
|
||||
}
|
||||
}
|
||||
|
||||
float modifier = highestValue - lowestValue;
|
||||
|
||||
colors = new Color32[samples.Length];
|
||||
for (int i = 0; i < samples.Length; i++)
|
||||
{
|
||||
colors[i] = Color32.Lerp(low, high, (1 / modifier) * (samples[i] - lowestValue));
|
||||
}
|
||||
|
||||
for (int i = 0; i < samples.Length; i++)
|
||||
{
|
||||
vertices[i].y = samples[i];// * 3;
|
||||
}
|
||||
applyMesh(tile, vertices, mesh, colors);
|
||||
//tile.GetComponent<Tile>().setType(tiletype);
|
||||
}
|
||||
|
||||
private void resetMesh(GameObject tile)
|
||||
{
|
||||
Mesh mesh = tile.GetComponent<MeshCollider>().sharedMesh;
|
||||
Vector3[] vertices = mesh.vertices;
|
||||
for (int i = 0; i < vertices.Length; i++)
|
||||
{
|
||||
vertices[i].y = 0;
|
||||
}
|
||||
mesh.vertices = vertices;
|
||||
mesh.RecalculateBounds();
|
||||
mesh.RecalculateNormals();
|
||||
tile.GetComponent<MeshCollider>().sharedMesh = mesh;
|
||||
}
|
||||
|
||||
private float[] calculateBasicSamples(GameObject tile)
|
||||
{
|
||||
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
|
||||
Vector3[] vertices = mesh.vertices;
|
||||
float[] samples = new float[vertices.Length];
|
||||
for (int i = 0; i < vertices.Length; i++)
|
||||
{
|
||||
float xCord = vertices[i].x / (vertices.Length - 1) * 10;
|
||||
float zCord = vertices[i].z / (vertices.Length - 1) * 10;
|
||||
float sample = Mathf.PerlinNoise(xCord, zCord) - 0.5f * rand.Next(0, 6);
|
||||
samples[i] = sample;
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
|
||||
private float[] calculateConnectedSamples(GameObject tile)
|
||||
{
|
||||
//Working with "next" as index for tile to be generated
|
||||
//Working with "mapGen" as map Placeholder to check against neighbours
|
||||
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
|
||||
Vector3[] vertices = mesh.vertices;
|
||||
float[] samples = Enumerable.Repeat(-100f, vertices.Length).ToArray();
|
||||
int sideLength = Mathf.RoundToInt(Mathf.Sqrt(vertices.Length));
|
||||
samples = connectSamples(samples, sideLength);
|
||||
|
||||
for (int i = 0; i < vertices.Length; i++)
|
||||
{
|
||||
if (samples[i] == -100)
|
||||
{
|
||||
float xCord = vertices[i].x / (vertices.Length - 1) * 10;
|
||||
float zCord = vertices[i].z / (vertices.Length - 1) * 10;
|
||||
float sample = Mathf.PerlinNoise(xCord, zCord) - 1f * rand.Next(0, 6);
|
||||
samples[i] = sample;
|
||||
}
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
|
||||
private void applyMesh(GameObject tile, Vector3[] vertices, Mesh mesh, Color32[] colors)
|
||||
{
|
||||
mesh.vertices = vertices;
|
||||
mesh.RecalculateBounds();
|
||||
mesh.RecalculateNormals();
|
||||
mesh.colors32 = colors;
|
||||
tile.GetComponent<MeshCollider>().sharedMesh = mesh;
|
||||
}
|
||||
|
||||
private float[] connectSamples(float[] samples, int sideLength)
|
||||
{
|
||||
Mesh mesh;
|
||||
Vector3[] vertices;
|
||||
|
||||
for (int i = 0; i < sideLength; i++)
|
||||
{
|
||||
if (mapGen.ContainsKey(next - new Vector3(100, 0, 0)))
|
||||
{ //LINKS
|
||||
mesh = mapGen[next - new Vector3(100, 0, 0)].GetComponent<MeshFilter>().mesh;
|
||||
vertices = mesh.vertices;
|
||||
samples[sideLength - 1 + sideLength * i] = vertices[sideLength * i].y;
|
||||
}
|
||||
|
||||
if (mapGen.ContainsKey(next - new Vector3(-100, 0, 0)))
|
||||
{ //RECHTS
|
||||
mesh = mapGen[next - new Vector3(-100, 0, 0)].GetComponent<MeshFilter>().mesh;
|
||||
vertices = mesh.vertices;
|
||||
samples[sideLength * i] = vertices[sideLength - 1 + sideLength * i].y;
|
||||
}
|
||||
|
||||
if (mapGen.ContainsKey(next - new Vector3(0, 0, -100)))
|
||||
{ //OBEN
|
||||
mesh = mapGen[next - new Vector3(0, 0, -100)].GetComponent<MeshFilter>().mesh;
|
||||
vertices = mesh.vertices;
|
||||
samples[i] = vertices[(sideLength * sideLength) - (sideLength-i)].y;
|
||||
}
|
||||
|
||||
if (mapGen.ContainsKey(next - new Vector3(0, 0, 100)))
|
||||
{ //UNTEN
|
||||
mesh = mapGen[next - new Vector3(0, 0, 100)].GetComponent<MeshFilter>().mesh;
|
||||
vertices = mesh.vertices;
|
||||
samples[(sideLength * sideLength) - (sideLength-i)] = vertices[i].y;
|
||||
}
|
||||
}
|
||||
|
||||
return samples;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 21edae6d584d6527bbb87c3393c1274f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -131,6 +131,7 @@ GameObject:
|
||||
- component: {fileID: 7573435787306895637}
|
||||
- component: {fileID: 7573435787306895626}
|
||||
- component: {fileID: 7602326841346784209}
|
||||
- component: {fileID: 6053739292490587254}
|
||||
m_Layer: 0
|
||||
m_Name: Tile
|
||||
m_TagString: Tile
|
||||
@@ -226,3 +227,15 @@ MeshCollider:
|
||||
m_Convex: 0
|
||||
m_CookingOptions: 30
|
||||
m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
|
||||
--- !u!114 &6053739292490587254
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 7573435787306895624}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 82f0b9ba1d91bf9148eb63b088e12979, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
|
||||
Reference in New Issue
Block a user