/// <summary> /// Stageを生成する関数 /// </summary> /// <param name="ROOM_NUM"> 作成するステージの部屋の数 </param> /// <param name="max"> ステージの最大サイズ max * max </param> /// <returns></returns> public List <List <StageChip> > Generate(int ROOM_NUM, int max) { // 乱数生成のインスタンス rnd = new System.Random(System.Environment.TickCount); // ステージの最大サイズを設定 this.max = max; // ステージ上の壁のリストのインスタンス生成 border = new List <Border>(); // 障害物生成用のクラスのインスタンス生成 ah = this.GetComponent <AnaHori>(); //マップのリストのインスタンス生成 map = new List <List <StageChip> >(max); // マップを空白で初期化 for (int i = 0; i < max; i++) { map.Add(new List <StageChip>(max)); for (int j = 0; j < max; j++) { map[i].Add(new StageChip(" ", 0)); } } //スタート地点(真ん中)の取得 startPos = new int[] { (max - 1) / 2, (max - 1) / 2 }; int[] tmp = startPos; // 部屋の半径サイズをランダムで設定 int len = rnd.Next(minSize, maxSize); // 初期の部屋の大きさを格納 int init_len = len; // 最初の部屋を作成 List <RoomChip> tmp_room = MakeRoom(startPos, 1, len); // 最初の部屋の障害物を作成 MakeObst(len, tmp_room); // ROOMNUMの数だけ部屋と障害物を作成 for (int i = 2; i < ROOM_NUM + 2; i++) { // 最後に作成する部屋は人質のいる部屋なので // 障害物を作成しない if (i != ROOM_NUM + 1) { // 作成する部屋の半径サイズを設定 len = rnd.Next(minSize, maxSize); // 部屋を作成する座標を設定 startPos = SearchPos(len, i, init_len, ROOM_NUM); // 部屋を作成 tmp_room = MakeRoom(startPos, i, len); // 部屋に障害物を作成 MakeObst(len, tmp_room); //Debug.Log("MakeRoom"); } else { // 部屋を作成する座標を設定 startPos = SearchPos(minSize, i, init_len, ROOM_NUM); // 部屋を作成 tmp_room = MakeRoom(startPos, i, minSize); } } // スタート地点を設定 map[tmp[0]][tmp[1]].type = "S"; // 部屋と部屋の間の通路をふさぐ障害物を除去 int[] RemoveRange = CreateRange(3); for (int i = 0; i < map.Count; i++) { for (int j = 0; j < map.Count; j++) { if (map[i][j].type == "b" || map[i][j].type == "t") { for (int h = 0; h < RemoveRange.Length; h++) { for (int w = 0; w < RemoveRange.Length; w++) { if (map[i + RemoveRange[h]][j + RemoveRange[w]].type == "+") { map[i + RemoveRange[h]][j + RemoveRange[w]].type = "#"; } } } for (int h = -1; h < 2; h++) { for (int w = -1; w < 2; w++) { if (h == -1 && w == -1) { continue; } if (h == 1 && w == 1) { continue; } if (h == -1 && w == 1) { continue; } if (h == 1 && w == -1) { continue; } if (map[i + h][j + w].type == "#" || map[i + h][j + w].type == "b") { map[i + h][j + w].type = "*"; } } } } } } // 人質の居る場所を設定 map[startPos[0]][startPos[1]].type = "G"; // 作成したステージをテキストファイルに書き込み string StageFile = Application.dataPath + "/" + "Resources" + "/" + "stage3.txt"; ReadWrite.ListWrite(StageFile, map, max, max); return(map); }
public string Generate(int ROOM_NUM, int max) { rnd = new System.Random(System.Environment.TickCount); this.max = max; border = new List <Border>(); ah = this.GetComponent <AnaHori>(); //マップ状態初期化 walls = new List <List <StageChip> >(max); for (int i = 0; i < max; i++) { walls.Add(new List <StageChip>(max)); for (int j = 0; j < max; j++) { walls[i].Add(new StageChip(" ", 0)); //walls[i][j].type = " "; } } //スタート地点の取得 startPos = new int[] { (max - 1) / 2, (max - 1) / 2 }; int[] tmp = startPos; //ランダムでx,yを設定 int len = rnd.Next(minSize, maxSize); List <RoomChip> tmp_room = MakeRoom(startPos, 1, len); MakeObst(len, tmp_room); for (int i = 1; i < ROOM_NUM + 1; i++) { len = rnd.Next(minSize, maxSize); startPos = SearchPos(len, i); tmp_room = MakeRoom(startPos, 1, len); MakeObst(len, tmp_room); } walls[tmp[0]][tmp[1]].type = "S"; int[] RemoveRange = CreateRange(3); for (int i = 0; i < walls.Count; i++) { for (int j = 0; j < walls.Count; j++) { if (walls[i][j].type == "b") { for (int h = 0; h < RemoveRange.Length; h++) { for (int w = 0; w < RemoveRange.Length; w++) { if (walls[i + RemoveRange[h]][j + RemoveRange[w]].type == "+") { walls[i + RemoveRange[h]][j + RemoveRange[w]].type = "#"; } } } } } } string StageFile = Application.dataPath + "/" + "Resources" + "/" + "stage3.txt"; ReadWrite.ListWrite(StageFile, walls, max, max); string data = ""; for (int i = 0; i < max; i++) { for (int j = 0; j < max; j++) { data = data + walls[i][j].type; } data = data + "\n"; } return(data); }
public void Create(Vector3 pos, int ROOM_NUM) { max = 80; wall_height = 1; ah = this.GetComponent <AnaHori>(); ad = this.GetComponent <AnahoriDungeon>(); iwidth = 0; List <List <StageChip> > map = ad.Generate(ROOM_NUM, max); string textdata = ""; for (int i = 0; i < max; i++) { for (int j = 0; j < max; j++) { textdata = textdata + map[i][j].type; } textdata = textdata + "\n"; } GameObject obj = null; Vector3 init_pos; Vector3 floor_pos; GameMgr.stageCenterPos = Vector3.zero; int cnt = 0; int startPosInd = 0; int endPosInd = 0; int mapCount = 0; int stageCount = 0; int countX = 0; int countY = -1; /* * 変数に保存したステージマップを走査する * #ならCubeを生成し、Cubeの大きさだけx軸に右に移動 * 改行文字ならz軸に下に移動して、x軸を初期化 * 空白、-、ならそのままx軸に右に移動 */ foreach (char c in textdata) { countY++; if (c == '#' || c == 'S' || c == 'b' || c == 'G') { /*---------------------------------*/ // Stage(床)を生成 init_pos = new Vector3(pos.x, pos.y - 1, pos.z); obj = Instantiate(gstage, init_pos, Quaternion.identity, GameObject.Find("Astage").transform) as GameObject; obj.name = gstage.name; Stage s = new Stage(); // ゲームオブジェクトの初期化 s.obj = obj; // 確率の初期化 s.prob = 0.0f; // s.probIndex = stageCount; s.material = s.obj.GetComponent <Renderer>().material; s.target = new Vector3(s.obj.transform.position.x, s.obj.transform.position.y + 2, s.obj.transform.position.z); s.id = map[countX][countY].id; GameMgr.stageList.Add(s); stageCount++; GameMgr.stageCenterPos += init_pos; /*---------------------------------*/ pos.x += obj.transform.lossyScale.x; iwidth++; cnt++; if (c == 'S') { GameMgr.startPos = init_pos; } if (c == 'G') { GameMgr.endPos = init_pos; } } else if (c == '*') { init_pos = new Vector3(pos.x, pos.y - 1, pos.z); floor_pos = new Vector3(pos.x, pos.y + 0.1f, pos.z); /*---------------------------------*/ // Stage(床)を生成 obj = Instantiate(gstage, init_pos, Quaternion.identity, GameObject.Find("Astage").transform) as GameObject; obj.name = gstage.name; Stage s = new Stage(); // ゲームオブジェクトの初期化 s.obj = obj; // 確率の初期化 s.prob = 0.0f; // s.probIndex = stageCount; s.material = s.obj.GetComponent <Renderer>().material; s.target = new Vector3(s.obj.transform.position.x, s.obj.transform.position.y + 2, s.obj.transform.position.z); s.id = map[countX][countY].id; GameMgr.stageList.Add(s); stageCount++; GameMgr.stageCenterPos += init_pos; /*---------------------------------*/ // Stage(ウェイポイント)を生成 obj = Instantiate(gpoint, floor_pos, Quaternion.identity, GameObject.Find("Apoint").transform) as GameObject; obj.name = gpoint.name; pos.x += obj.transform.lossyScale.x; iwidth++; cnt++; } else if (c == '\n') { obj = null; GameMgr.mapList.Add(new List <Map>()); mapCount++; countY = -1; countX++; Vector3 origin = new Vector3((float)iwidth, 1.0f, 0f); pos.z -= space.z; pos.x -= origin.x; iwidth = 0; } else if (c == ' ') { pos.x += 1.0f; iwidth++; } else if (c == '+') { init_pos = new Vector3(pos.x, pos.y, pos.z); // Stage(障害物)を生成 obj = Instantiate(gwall, init_pos, Quaternion.identity, GameObject.Find("Awall").transform) as GameObject; obj.name = gwall.name; Wall w = new Wall(obj, obj.GetComponent <Renderer>().material.color, false); GameMgr.wallList.Add(w); // Stage(障害物のコライダー)を生成 obj = Instantiate(gwallcol, new Vector3(pos.x, pos.y + 1, pos.z), Quaternion.identity, GameObject.Find("Awall").transform) as GameObject; obj.name = gwallcol.name; pos.x += obj.transform.lossyScale.x; iwidth++; //pos.x += space.x; //iwidth++; } else if (c == '-') { for (int i = 0; i < wall_height; i++) { init_pos = new Vector3(pos.x, pos.y, pos.z); // Stage(壁)を生成 obj = Instantiate(gwall, init_pos, Quaternion.identity, GameObject.Find("Awall").transform) as GameObject; obj.name = gwall.name; Wall w = new Wall(obj, obj.GetComponent <Renderer>().material.color, false); GameMgr.wallList.Add(w); // Stage(壁のコライダー)を生成 obj = Instantiate(gwallcol, new Vector3(pos.x, pos.y + 1, pos.z), Quaternion.identity, GameObject.Find("Awall").transform) as GameObject; obj.name = gwallcol.name; } pos.x += obj.transform.lossyScale.x; iwidth++; } else if (c == 't') { init_pos = new Vector3(pos.x, pos.y - 1, pos.z); /*---------------------------------*/ // Stage(床)を生成 obj = Instantiate(gstage, init_pos, Quaternion.identity, GameObject.Find("Astage").transform) as GameObject; obj.name = gstage.name; Stage s = new Stage(); // ゲームオブジェクトの初期化 s.obj = obj; // 確率の初期化 s.prob = 0.0f; // s.probIndex = stageCount; s.material = s.obj.GetComponent <Renderer>().material; s.target = new Vector3(s.obj.transform.position.x, s.obj.transform.position.y + 2, s.obj.transform.position.z); s.id = map[countX][countY].id; GameMgr.stageList.Add(s); stageCount++; GameMgr.stageCenterPos += init_pos; /*---------------------------------*/ init_pos = new Vector3(pos.x, pos.y, pos.z); // Stage(障害物)を生成 obj = Instantiate(ggate, init_pos, Quaternion.identity, GameObject.Find("Awall").transform) as GameObject; obj.name = gwall.name; Wall w = new Wall(obj, obj.GetComponent <Renderer>().material.color, true); GameMgr.wallList.Add(w); pos.x += obj.transform.lossyScale.x; iwidth++; } if (c != '\n') { if (c == '*') { GameMgr.mapList[mapCount].Add(new Map(obj, "#")); } else { GameMgr.mapList[mapCount].Add(new Map(obj, c.ToString())); } } } GameMgr.stageCenterPos /= stageCount; string StageFile = Application.dataPath + "/" + "Resources" + "/" + "stage4.txt"; ReadWrite.ListWrite(StageFile, GameMgr.mapList, max, max); //Debug.Log("Genarate"); }
public void Create(Vector3 pos, int ROOM_NUM) { max = 50; ah = this.GetComponent <AnaHori>(); ad = this.GetComponent <AnahoriDungeon>(); iwidth = 0; /* * ステージマップが保存されているテキストのパス */ //string StageFile = System.IO.Path.GetFileName(@"C:\Users\stage.txt"); //string StageFile = Application.dataPath + "/" + "Resources" + "/" + "stage2.txt"; //var ws = ad.Generate(ROOM_NUM, max); string textdata = ad.Generate(ROOM_NUM, max); //string textdata = LoadStage(StageFile); GameObject obj = null; Vector3 init_pos; /* * 変数に保存したステージマップを走査する * #ならCubeを生成し、Cubeの大きさだけx軸に右に移動 * 改行文字ならz軸に下に移動して、x軸を初期化 * 空白、-、ならそのままx軸に右に移動 */ foreach (char c in textdata) { if (c == '#' || c == 'S' || c == 'b') { init_pos = new Vector3(pos.x, pos.y - 2, pos.z); obj = Instantiate(gstage, init_pos, Quaternion.identity, GameObject.Find("Astage").transform) as GameObject; obj.name = gstage.name; pos.x += obj.transform.lossyScale.x; iwidth++; } else if (c == '*') { init_pos = new Vector3(pos.x, pos.y - 2, pos.z); obj = Instantiate(gstage, init_pos, Quaternion.identity, GameObject.Find("Astage").transform) as GameObject; obj.name = gstage.name; obj = Instantiate(gpoint, pos, Quaternion.identity, GameObject.Find("Apoint").transform) as GameObject; obj.name = gpoint.name; pos.x += obj.transform.lossyScale.x; iwidth++; } else if (c == '\n') { Vector3 origin = new Vector3((float)iwidth, 1.0f, 0f); pos.z -= space.z; pos.x -= origin.x; iwidth = 0; } else if (c == ' ') { pos.x += 1.0f; iwidth++; } else if (c == '-' || c == '+') { //obj = Instantiate(gearth, pos, Quaternion.identity, GameObject.Find("Astage").transform) as GameObject; //obj.name = gearth.name; init_pos = new Vector3(pos.x, pos.y - 2, pos.z); obj = Instantiate(gwall, init_pos, Quaternion.identity, GameObject.Find("Awall").transform) as GameObject; obj.name = gwall.name; pos.x += obj.transform.lossyScale.x; iwidth++; //pos.x += space.x; //iwidth++; } } }