I’m trying to learn how to procedurally create a world in 2d. After some research i did this :
I create a DataWorld.cs a DataChunk.cs and a DataSprite.cs to store all the data and separate Data from graphic creation.
And i create an other script WorldGenerator.cs to actually create the world.
it looks like this :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class WorldGenerator : MonoBehaviour {
public Sprite[] sprites;
public SpriteRenderer sp;
public int chunk_size_x;
public int chunk_size_y;
public int world_size_x;
public int world_size_y;
int _chunkNum = -1;
int _scale;
int _magnitude;
float _power = 1;
int _treshold = 15;
GameObject _chunkGO;
SpriteRenderer spriteClone;
void Start ()
{
sprites = Resources.LoadAll<Sprite>("spriteTest");
_scale = (int)Random.Range(10f,50f);
_magnitude =(int) Random.Range((float)_scale-10, _scale+20);
MakeWorld(world_size_x, world_size_y, chunk_size_x, chunk_size_y);
}
public void MakeWorld(int world_size_x, int world_size_y, int chunk_size_x, int chunk_size_y)
{
DataWorld world = new DataWorld(world_size_x, world_size_y, chunk_size_x, chunk_size_y);
for(int x = 0; x < world_size_x; x++)
{
for(int y = 0; y < world_size_y; y++)
{
MakeChunk(world, chunk_size_x,chunk_size_y);
_chunkGO.transform.position = new Vector3 (world.chunks[x,y].posX, world.chunks[x,y].posY,0) ;
}
}
}
public void MakeChunk(DataWorld world, int chunk_size_x, int chunk_size_y)
{
_chunkNum++;
GameObject chunkGO = new GameObject();
chunkGO.name = "CHUNK" + " " + _chunkNum;
_chunkGO = chunkGO;
for(int x=0; x< chunk_size_x; x++)
{
for(int y = 0; y< chunk_size_y; y++)
{
if(y<PerlinNoise(x/8,0,_scale,_magnitude,_power))
{
spriteClone = (SpriteRenderer)Instantiate(sp, new Vector2(x,y), Quaternion.identity);
spriteClone.transform.parent = chunkGO.transform;
spriteClone.name = "sprite " +x + " , " +y;
world.GetAllChunks().spriteTile[x,y].spr = spriteClone;
world.GetAllChunks().spriteTile[x,y].type = DataSprite.TYPE.BLACK;
}
}
}
for(int x=0; x< chunk_size_x; x++)
{
for(int y = 0; y< chunk_size_y; y++)
{
CheckSpritePos(world.GetAllChunks(),x,y);
UpdateSpritesGraphics(world.GetAllChunks(),world.GetAllChunks().spriteTile[x,y].spr,x,y);
}
}
}
int PerlinNoise(int x,int y, float scale, float mag, float power){
float rValue;
rValue=Noise.Noise.GetNoise (((double)x) / scale, ((double)y)/ scale,0);
rValue*=mag;
if(power!=0)
{
rValue=Mathf.Pow( rValue, power);
}
return (int) rValue;
}
But the way i do it all instantiates chunks are the same.
I understand why there are all the same, but i can’t figure out how to make them continue each other base on the PerlinNoise function.
EDIT :
So i fixed the continuity on the axis X by adding
if(y<PerlinNois((x+chunk.posX),0,50,100,1f))
It works fine when i only have one dimension in my world ( i only add chunk along the X axis).
But when i have 2 dimension let say my world size is
- world_size_x =4
- world_size_y =2
I can’t figure out how to add chunk.posY to sample the noise corectly.
if(y<PerlinNoise((x+chunk.posX+chunk.posY),0,50,100,1f))
does not work. I did plenty of other test but none of them worked. Where and how do i have to add the chunk.posY ?
EDDIT 2
nevermind after several hour of break i finally had the right brain wave :
if(y+chunk.posY<PerlinNoise((x+chunk.posX),0,50,100,1f))