using System.Collections;
using System.Collections.Generic;
public class CreateWorld : MonoBehaviour {
public Transform worldMap;
private const int MAP_WIDTH = 50;
private const int MAP_HEIGHT = 50;
private const int TILE_SIZE = 32;
private const int IMG_ID_ARETI = 0;
private const int IMG_ID_GRASS = 1;
private const int IMG_ID_DESEART = 2;
private const int IMG_ID_DART = 3;
private const int IMG_ID_SNOW = 4;
private const int IMG_ID_SEA = 5;
private const int IMG_ID_DARK = 6;
private List<WorldTileData> tileList;
Vector2[] dir = new Vector2[]
// Use this for initialization
tileList = new List<WorldTileData>();
for(int x = -1; x <= MAP_WIDTH; x++)
CreateSea(x, MAP_HEIGHT);
for(int y = -1; y < MAP_HEIGHT; y++)
for(int y = 0; y < MAP_HEIGHT; y++)
for(int x = 0; x < MAP_WIDTH; x++)
WorldTileData tile = new WorldTileData();
tile.SetTileType((TileType)Random.Range(1, 9));
tile.Obj.transform.parent = worldMap;
tile.Obj.transform.position = new Vector3(x * TILE_SIZE * 0.01f, y * TILE_SIZE * 0.01f, 0);
private void CreateSea(int x, int y)
GameObject tileObj = Instantiate(Resources.Load("Prefabs/WorldTile")) as GameObject;
tileObj.transform.position = new Vector3(x * TILE_SIZE * 0.01f, y * TILE_SIZE * 0.01f, 0);
tileObj.GetComponent<tk2dSprite>().spriteId = IMG_ID_SEA;
tileObj.transform.parent = worldMap;
private void UpdateWorld()
for(int y = 0; y < MAP_HEIGHT; y++)
for(int x = 0; x < MAP_WIDTH; x++)
int dx = (int)dir[Random.Range(0,4)].x;
int dy = (int)dir[Random.Range(0,4)].y;
if(x + dx < 0 || MAP_WIDTH <= x + dx || y + dy < 0 || MAP_HEIGHT <= y + dy)
type = GetTile(x + dx, y + dy).TileType;
TileType sameTileType = TileType.NONE;
for(int i = 0; i < 4; i++)
if(x + dx < 0 || MAP_WIDTH <= x + dx || y + dy < 0 || MAP_HEIGHT <= y + dy)
type = GetTile(x + dx, y + dy).TileType;
if(sameTileType == TileType.NONE)
else if(sameTileType != type)
SetTile(x, y, sameTileType);
// Update is called once per frame
private WorldTileData GetTile(int x, int y)
return tileList[x + MAP_WIDTH * y];
private void SetTile(int x, int y, TileType type)
tileList[x + MAP_WIDTH * y].SetTileType(type);
public class WorldTileData
private TileType tileType;
obj = Instantiate(Resources.Load("Prefabs/WorldTile")) as GameObject;
tileType = TileType.ARETI;
public void SetTileType(TileType tileType)
this.tileType = tileType;
if(tileType == TileType.ARETI)
obj.GetComponent<tk2dSprite>().spriteId = IMG_ID_ARETI;
else if(tileType == TileType.GRASS)
obj.GetComponent<tk2dSprite>().spriteId = IMG_ID_GRASS;
else if(tileType == TileType.DESEART)
obj.GetComponent<tk2dSprite>().spriteId = IMG_ID_DESEART;
else if(tileType == TileType.GRASS)
obj.GetComponent<tk2dSprite>().spriteId = IMG_ID_GRASS;
else if(tileType == TileType.DART)
obj.GetComponent<tk2dSprite>().spriteId = IMG_ID_DART;
else if(tileType == TileType.SNOW)
obj.GetComponent<tk2dSprite>().spriteId = IMG_ID_SNOW;
else if(tileType == TileType.SEA)
obj.GetComponent<tk2dSprite>().spriteId = IMG_ID_SEA;
else if(tileType == TileType.DARK)
obj.GetComponent<tk2dSprite>().spriteId = IMG_ID_DARK;