2014-10-29 2 views
0

게임에서와 같이 맵에서 맵을 생성하고 싶습니다. Unity와 C를 사용하여 Terraria와 Starbound를 사용하고 있습니다. 지금까지 한 작업이 효과가 없었으며 이유가 없습니다 ... 어떤 도움/아이디어/제안입니까?Terraria와 같은 무작위 타일 기반지도 생성이 통일 되었습니까?

using UnityEngine; 
using System.Collections; 

public class MapGeneration : MonoBehaviour { 
public int mapWidth, mapHeight, spreadDegree; 
GameObject[,] blocks; 
public GameObject baseBlock; 

public int maxHeightAllowed; 
public int nullDispersion; 
int dispersionZone; 

void Start() { 
    GenerateMap(); 
} 

void GenerateMap() { 
    dispersionZone = maxHeightAllowed - nullDispersion; 
    blocks = new GameObject[mapWidth, mapHeight]; 
    for(int x = 0; x < blocks.GetLength(0); x++) { 
      for(int y = nullDispersion; y <= maxHeightAllowed; y++) { 
       int heightDiff = y - nullDispersion; 
       float dispersionModifier = Mathf.Sin((heightDiff/dispersionZone) * 90); 
       float random = Random.Range(0, 1); 

       if(random > dispersionModifier) { 
        blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity); 
       } else if (y < blocks.GetLength(1) && blocks[x,y+1] != null) { 
        blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity); 
       } 
      } 

      for(int y = 0; y < nullDispersion; y++) { 
       blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity); 
      } 
    } 
} 
} 
(heightDiff/dispersionZone) 
+0

* 무엇이 작동하지 않는지 말할 필요가 있습니다. 지금까지 코드 덤프를 보았습니다. – BradleyDotNET

+0

아 .. 지금은 단 하나의 단색 블록을 생성합니다 ... Terraria와 같은 무작위지도를 만들어야합니다. :) –

답변

1

이 정수 분할된다. 대신 (heightDiff/(float)dispersionZone)을 사용하십시오.

* 90 

Mathf.Sin은 체중이 아닌 라디안으로 표시됩니다. 사용 * Math.PI/2

  if(random > dispersionModifier) { 
       blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity); 
      } else if (y < blocks.GetLength(1) && blocks[x,y+1] != null) { 
       blocks[x,y] = (GameObject)Instantiate(baseBlock, new Vector2(x, y), Quaternion.identity); 
      } 

동일한 유형의 블록을 만들 때 왜 사용합니까? if와 else의 시체 사이에는 어떤 차이가 없어야합니까?

관련 문제