Skip to content
Snippets Groups Projects
Commit 41e53d2f authored by Tanmorik's avatar Tanmorik
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
Showing with 749 additions and 0 deletions
class Cell {
public int type {set; get;}
public Cell() {
this.type = 0;
}
}
\ No newline at end of file
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DefaultGenerator
{
public static int[] GetRandomNoise(int seed, int width, int maxHeight)
{
Random.InitState(seed);
int[] heigtValues = new int[width];
for (int x = 0; x < width; x++)
{
int height = Random.Range(0, maxHeight);
heigtValues[x] = height;
}
return heigtValues;
}
public static int[] GetRandomNoise(int seed, int width, int maxHeight, int minHeight)
{
Random.InitState(seed);
int[] heigtValues = new int[width];
for (int x = 0; x < width; x++)
{
int height = Random.Range(minHeight, maxHeight);
heigtValues[x] = height;
}
return heigtValues;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class TerrainGeneration : MonoBehaviour
{
[SerializeField] int height, width;
[SerializeField] TilemapController tilemapController;
// Start is called before the first frame update
void Start()
{
Generation();
}
void Generation()
{
for (int x = 0; x < width; x++)
{
int minHeight = height - 1;
int maxHeigt = height + 2;
height = Random.Range(minHeight, maxHeigt);
int minStoneSpawnDistance = height - 5;
int maxStoneSpawnDistance = height - 6;
int totalStoneSpawnDistance = Random.Range(minStoneSpawnDistance, maxStoneSpawnDistance);
for (int y = 0; y < height; y++)
{
if(y < totalStoneSpawnDistance)
{
tilemapController.spawnTile(x, y, TileType.STONE);
}
else
tilemapController.spawnTile(x, y, TileType.DIRT);
}
tilemapController.spawnTile(x, height, TileType.GRASS);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainGenerationEasy : MonoBehaviour
{
[SerializeField] int height, width;
[SerializeField] TilemapController tilemapController;
// Start is called before the first frame update
void Start()
{
Generation();
}
void Generation()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
tilemapController.spawnTile(x, y, TileType.DIRT);
}
tilemapController.spawnTile(x, height, TileType.GRASS);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainGenerationEasy2 : MonoBehaviour
{
[SerializeField] int maxHeight, width;
[SerializeField] TilemapController tilemapController;
// Start is called before the first frame update
void Start()
{
Random.InitState(0);
Generation();
}
void Generation()
{
for (int x = 0; x < width; x++)
{
int height = Random.Range(0, maxHeight);
for (int y = 0; y < height; y++)
{
tilemapController.spawnTile(x, y, TileType.DIRT);
}
tilemapController.spawnTile(x, height, TileType.GRASS);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainGenerationEasy3 : MonoBehaviour
{
[SerializeField] int height, width;
[SerializeField] TilemapController tilemapController;
// Start is called before the first frame update
void Start()
{
Random.InitState(0);
Generation();
}
void Generation()
{
for (int x = 0; x < width; x++)
{
int minHeight = height - 1;
int maxHeigt = height + 2;
height = Random.Range(minHeight, maxHeigt);
for (int y = 0; y < height; y++)
{
tilemapController.spawnTile(x, y, TileType.DIRT);
}
tilemapController.spawnTile(x, height, TileType.GRASS);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainGenerationEasy4 : MonoBehaviour
{
[SerializeField] int height, width;
[SerializeField] TilemapController tilemapController;
[Range(0, 100)]
[SerializeField] float heightValue, smoothness;
// Start is called before the first frame update
void Start()
{
Generation();
}
void Generation()
{
for (int x = 0; x < width; x++)
{
height = Mathf.RoundToInt(heightValue * Mathf.PerlinNoise(x / smoothness, 0));
//int minHeight = height - 1;
//int maxHeigt = height + 2;
//height = Random.Range(minHeight, maxHeigt);
for (int y = 0; y < height; y++)
{
tilemapController.spawnTile(x, y, TileType.DIRT);
}
tilemapController.spawnTile(x, height, TileType.GRASS);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Generation : MonoBehaviour
{
abstract public int[] GetHeightValues(int width, int maxHeight, int minHeight, int seed);
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerationController : MonoBehaviour
{
[SerializeField] Generation GenerationType;
[SerializeField] int maxHeight, minHeight, width;
[SerializeField] TilemapController tilemapController;
[SerializeField] int seed = 0;
// Start is called before the first frame update
void Start()
{
Generation(GenerationType.GetHeightValues(width, maxHeight, minHeight, seed));
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Bam");
tilemapController.Clear();
Generation(GenerationType.GetHeightValues(width, maxHeight, minHeight, Mathf.RoundToInt(Time.time)));
}
}
void Generation(int[] heightValues)
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < heightValues[x]; y++)
{
tilemapController.spawnTile(x, y, TileType.DIRT);
}
tilemapController.spawnTile(x, heightValues[x], TileType.GRASS);
}
}
}
Grid.cs 0 → 100644
class Grid{
private int width {get;}
private int height {get;}
private Cell [,] cellArray;
public Grid(int width, int height) {
this.width = width;
this.height = height;
cellArray = new Cell[width,height];
Init();
}
public void Init() {
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
cellArray[x,y] = new Cell();
}
}
}
public void setCell (int x, int y, int type = 1) {
cellArray[x, y].type = type;
}
public int getCell (int x, int y){
return cellArray[x,y].type;
}
}
\ No newline at end of file
using UnityEngine;
using System;
using System.Linq;
using System.Collections.Generic;
public class HorizontalGeneration : MonoBehaviour
{
public TilemapController tilemapController;
public int width = 100;
public int height = 100;
public float noiseScaleX = 2;
public float noiseScaleY = 50;
public int offsetX = 0;
public int offsetY = 0;
public float frequency = 1;
public float frequencyXNoise = 3;
public ushort seed = 0b1100000011;
Grid grid;
// Start is called before the first frame update
void Start()
{
Generation();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
tilemapController.Clear();
Generation();
}
}
private void Generation()
{
ushort seedX = (ushort)((ushort)(seed << 8)>>8);
byte seedY = (byte)(seed >> 8);
Debug.Log("Seed X: " + Convert.ToString(seedX, 2) + " Seed Y: " + Convert.ToString(seedY, 2));
Debug.Log("NoiseScaleX: " + noiseScaleX + " NoiseScaleY: " + noiseScaleY);
Debug.Log("OffsetX: " + offsetX + " OffsetY: " + offsetY);
Debug.Log("FrequencyY: " + frequency + " FrequencyX: " + frequencyXNoise);
grid = new Grid(width, height);
int startX = 0;
int startY = 50;
int perlinX = 0;
float perlinY = 0;
int x = startX;
int y = startY;
int breakCounter = 0;
Vector2Int pos = new Vector2Int(x, y);
Vector2Int oldPos = pos;
while (x < width-1 && breakCounter < 200000)
{
//Erstellen des Höhenwertes für die nächste Iteration
float xCoord = (float)(perlinX + offsetX) / 1000 * frequency;
float xNoise = Mathf.PerlinNoise(xCoord, (float)seedY / 0b11111111)-0.5f;
y = startY + Mathf.RoundToInt(xNoise * noiseScaleY);
//Erstellen des Horizontalen Wertes für die nächste Iteration
float yCoord = (float)(perlinY + offsetY) / 1000 * frequencyXNoise;
int noise = Mathf.RoundToInt((Mathf.PerlinNoise((float)seedX / 0b11111111, yCoord)-0.5f) * noiseScaleX*2);
x = perlinX + noise;
//Prüfen ob die Koordinaten die Beschränkungen der Welt überschreiten
if (x < 0)
{
x = 0;
}
else if (x >= width)
{
x = width - 1;
}
if (y < 0)
{
y = 0;
}
else if (y >= height)
{
y = height - 1;
}
pos = new Vector2Int(x, y);
//Interpolieren zwischen altem und neuem Punkt
if (distanceToHigh(oldPos, pos))
{
Interpolate(oldPos, pos);
}
//if (y-yOld>1)
// {
// x--;
// grid.setCell(x, yOld + 1);
//} else if(y-yOld < -1)
// {
// x--;
// grid.setCell(x, yOld - 1);
//}
grid.setCell(x, y);
oldPos = pos;
perlinX++;
perlinY++;
//Debug.Log(perlinX + ":" + x);
breakCounter++;
}
//floddfill
Fill4();
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
if (grid.getCell(x, y) == 1)
{
tilemapController.spawnTile(x, y, TileType.GRASS);
}
else if (grid.getCell(x,y) == 2)
{
tilemapController.spawnTile(x, y, TileType.DIRT);
}
}
}
}
void Fill4(int x = 0, int y = 0, int oldTile = 0, int newTile = 2)
{
if (!isPlaceable(x, y, oldTile, newTile))
return;
if (grid.getCell(x, y) == oldTile)
{
grid.setCell(x, y, newTile);
Fill4(x + 1, y, oldTile, newTile); // rechts
Fill4(x, y + 1, oldTile, newTile); // oben
Fill4(x - 1, y, oldTile, newTile); // links
//Fill4(x, y - 1, oldTile, newTile); // unten
}
}
bool isPlaceable(int x = 0, int y = 0, int oldTile = 0, int newTile = 2)
{
if (x < 0 || x > width - 1 || y < 0 || y > height - 1 || grid.getCell(x, y) == newTile)
{
return false;
}
return true;
}
void Interpolate(Vector2Int start, Vector2Int target)
{
//Richtung bestimmen
Vector2 direction = target - start;
direction.Normalize();
//Einmal Interpolieren
Vector2Int directionInt = new Vector2Int(Mathf.RoundToInt(direction.x), Mathf.RoundToInt(direction.y));
start += directionInt;
grid.setCell(start.x, start.y);
//Prüfen ob fertig
if(distanceToHigh(start, target))
{
Interpolate(start, target);
}
//ggf Recursiv ausführen
}
private bool distanceToHigh(Vector2 start, Vector2 target)
{
float distance = Mathf.Sqrt(Mathf.Pow((start.x - target.x), 2) + Mathf.Pow((start.y - target.y), 2));
return distance > Mathf.Sqrt(2);
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Overworld : MonoBehaviour
{
public TilemapController tilemapController;
public int maxHeight;
public int horizonHeight;
public int width;
// Start is called before the first frame update
void Start()
{
Generation();
}
private void Generation()
{
for(int x = 0; x < maxHeight; x++)
{
for (int y = 0; y < width; y++)
{
float xCoord = (float)x / width;
float yCoord = (float)y / width;
float field = Mathf.PerlinNoise(xCoord, yCoord);
Debug.Log(x + ":"+ y +"="+ field);
}
}
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PerlinTest : MonoBehaviour
{
[SerializeField] int height, width;
[SerializeField] TilemapController tilemapController;
[Range(0, 100)]
[SerializeField] float heightValue, smoothness;
// Start is called before the first frame update
void Start()
{
Generation();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
tilemapController.Clear();
Generation();
}
}
void Generation()
{
for (int x = 0; x < width; x++)
{
height = Mathf.RoundToInt(heightValue*Mathf.PerlinNoise(x/smoothness,x/smoothness));
//int minHeight = height - 1;
//int maxHeigt = height + 2;
//height = Random.Range(minHeight, maxHeigt);
for (int y = 0; y < height; y++)
{
tilemapController.spawnTile(x, y, TileType.DIRT);
}
tilemapController.spawnTile(x, height, TileType.GRASS);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainGenerationEasyCosineInterpolated : Generation
{
public override int[] GetHeightValues(int width, int maxHeight,int minHeight, int seed)
{
int[] heightValues = DefaultGenerator.GetRandomNoise(seed, width, maxHeight, minHeight);
int[] newHeightValues = new int[width];
System.Array.Copy(heightValues, newHeightValues, width);
bool finished = false;
int index = 0;
for (int i = 1; i < heightValues.Length && !finished; i++)
{
newHeightValues[index] = heightValues[i - 1];
index++;
int diff = heightValues[i] - heightValues[i - 1];
if (diff > 1 || diff < -1)
{
bool ascend = true;
if (diff < 0)
{
ascend = false;
diff = Mathf.Abs(diff);
}
int[] interpolatedValues = new int[diff - 1];
for (int j = 1; j < diff; j++)
{
int temp = Mathf.RoundToInt((1 - Mathf.Cos(Mathf.PI * j / diff)) * diff / 2);
if (ascend)
{ // Aufstieg
interpolatedValues[j - 1] = heightValues[i - 1] + temp;
}
else
{ // Abstieg
interpolatedValues[j - 1] = heightValues[i - 1] - temp;
}
}
int indexStart = index;
while (index < indexStart + diff - 1 && !finished)
{
if (index < width && !finished)
{
newHeightValues[index] = interpolatedValues[index - indexStart];
index++;
}
if (index >= width)
{
finished = true;
}
}
}
}
return newHeightValues;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainGenerationEasyLinearInterpolated : Generation
{
public override int[] GetHeightValues(int width, int maxHeight,int minHeight, int seed)
{
int[] heightValues = DefaultGenerator.GetRandomNoise(seed, width, maxHeight, minHeight);
int[] newHeightValues = new int[width];
System.Array.Copy(heightValues, newHeightValues, width);
bool finished = false;
int index = 0;
for (int i = 1; i < heightValues.Length && !finished; i++)
{
newHeightValues[index] = heightValues[i - 1];
index++;
int diff = heightValues[i] - heightValues[i - 1];
if (diff > 1 || diff < -1)
{
bool ascend = true;
if (diff < 0)
{
ascend = false;
diff = Mathf.Abs(diff);
}
int[] interpolatedValues = new int[diff - 1];
for (int j = 1; j < diff; j++)
{
if (ascend) // Aufstieg
interpolatedValues[j - 1] = heightValues[i - 1] + j;
else // Abstieg
interpolatedValues[j - 1] = heightValues[i - 1] - j;
}
int indexStart = index;
while (index < indexStart + diff - 1 && !finished)
{
if (index < width && !finished)
{
newHeightValues[index] = interpolatedValues[index - indexStart];
index++;
}
if (index >= width)
{
finished = true;
}
}
}
}
return newHeightValues;
}
}
public enum TileType
{
DIRT, STONE, GRASS
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class TilemapController : MonoBehaviour
{
[Header("Tilemaps")]
[SerializeField] Tilemap tilemap;
[Header("Tiles")]
[SerializeField] Tile grassTile;
[SerializeField] Tile stoneTile;
[SerializeField] Tile dirtTile;
public void spawnTile(int x, int y, TileType tileType)
{
Tile tile;
switch (tileType)
{
case TileType.DIRT:
tile = dirtTile;
break;
case TileType.GRASS:
tile = grassTile;
break;
case TileType.STONE:
tile = stoneTile;
break;
default:
throw new System.Exception("Unexpected case");
}
tilemap.SetTile(new Vector3Int(x, y, 0), tile);
}
internal void Clear()
{
tilemap.ClearAllTiles();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment