using System.Collections;
using System.Collections.Generic;
public class Main : MonoBehaviour {
private const int MAP_WIDTH = 39;
private const int MAP_HEIGHT = 39;
private const int TILE_SIZE = 32;
private List<TileData> tileList;
private const int IMG_ID_ROAD = 0;
private const int IMG_ID_WALL = 10;
tileList = new List<TileData>();
for(int y = 0; y < MAP_HEIGHT; y++)
for(int x = 0; x < MAP_WIDTH; x++)
TileData tile = new TileData();
tile.tileType = TileType.WALL;
int posX = Random.Range(0, (MAP_WIDTH + 1) / 2) * 2;
int posY = Random.Range(0, (MAP_HEIGHT + 1) / 2) * 2;
private void DigTile(int posX, int posY)
Vector2[] dir = new Vector2[]
for (int t = 0; t < dir.Length; t++ )
int r = Random.Range(t, dir.Length);
for(int i = 0; i < 4; i++)
if(posX + dx < 0 || MAP_WIDTH <= posX + dx || posY + dy < 0 || MAP_HEIGHT <= posY + dy) continue;
if (GetTile(posX + dx * 2, posY + dy * 2).tileType != TileType.ROAD)
SetTile(posX + dx, posY + dy, TileType.ROAD);
SetTile(posX + dx * 2, posY + dy * 2, TileType.ROAD);
DigTile(posX + dx * 2, posY + dy * 2);
private TileData 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].tileType = type;
private void CreateWall(int x, int y)
GameObject tileObj = Instantiate(Resources.Load("Prefabs/Tile")) as GameObject;
tileObj.transform.position = new Vector3(x * TILE_SIZE * 0.01f, y * TILE_SIZE * 0.01f, 0);
tileObj.GetComponent<tk2dSprite>().spriteId = IMG_ID_WALL;
for(int y = 0; y < MAP_HEIGHT; y++)
for(int x = 0; x < MAP_WIDTH; x++)
GameObject tileObj = Instantiate(Resources.Load("Prefabs/Tile")) as GameObject;
TileData tile = GetTile(x, y);
if(tile.tileType == TileType.WALL)
tileObj.GetComponent<tk2dSprite>().spriteId = IMG_ID_WALL;
if(tile.tileType == TileType.ROAD)
tileObj.GetComponent<tk2dSprite>().spriteId = IMG_ID_ROAD;
tileObj.transform.position = new Vector3(x * TILE_SIZE * 0.01f, y * TILE_SIZE * 0.01f, 0);
for(int x = -1; x <= MAP_WIDTH; x++)
CreateWall(x, MAP_HEIGHT);
for(int y = -1; y < MAP_HEIGHT; y++)
CreateWall(MAP_WIDTH, y);
public TileType tileType;