【Unity】RPG制作 その21-到達していない部屋を非表示にする
前回の終わりで探索時のトラップを作成すると書きましたが、今回はその前に、到達していないダンジョンの部屋とその部屋に配置されているイベントを非表示にする処理を実装していきます。
はじめに、イベント及び部屋を表示するかどうかのデータを保持する配列をDangeonMap.csに宣言します。
クリックで展開
public int [,,]isViewRoom = new int[4, 5, 5] { //0;見えない 1:見える New { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, }, { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, }, { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, }, { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, } }; public int[,,] isViewEvent = new int[4, 5, 5] { //0;見えない 1:見える New { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, }, { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, }, { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, }, { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, } }; public int[] isAlreadyEnterFloor = new int[20] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //New
isAlreadyEnterFloor配列は、既に訪れたことがある部屋かどうかのFloorごとの状態を保持する配列です。
次に部屋とイベントの表示非表示の初期化処理の関数を作成します。
クリックで展開
public void InitViewEvent(int currentFloor) //New { if(isAlreadyEnterFloor[currentFloor - 1] == 0) { for (int j = 0; j < 5; j++) { for (int i = 0; i < 5; i++) { isViewEvent[currentFloor - 1, j, i] = 0; } } isAlreadyEnterFloor[currentFloor - 1] = 1; //ここでそのフロアに訪れたフラグを立てる。 } } public void InitViewRoom(int currentFloor) //New { if (isAlreadyEnterFloor[currentFloor - 1] == 0) { for (int j = 0; j < 5; j++) { for (int i = 0; i < 5; i++) { isViewRoom[currentFloor - 1, j, i] = 0; } } } isViewRoom[currentFloor - 1, dangionController.playerPos.y, dangionController.playerPos.x] = 1; //プレイヤーの現在位置の部屋のみ表示する }
これらの関数をDangeonMap.csのStart関数と、DangeonEvent.csのIEDownFloorEventコルーチンとIEUpFloorEventコルーチンで呼び出します。
クリックで展開
void Start() { this.gameObject.SetActive(false); dangionController = dangionMapSystem.GetComponent<DangionController>(); //新しく入るフロアの場合は部屋の表示非表示、イベントの初期化処理を行う if(isAlreadyEnterFloor[questManager.currentFloor] == 0) //New { InitViewRoom(questManager.currentFloor); //New InitViewEvent(questManager.currentFloor); //New InitEventNowFloor(questManager.currentFloor); SetEventNowFloorAtRandam(questManager.currentFloor, 3, 5); } SetDangion(questManager.currentFloor); SetEvent(questManager.currentFloor); }
クリックで展開
public IEnumerator IEDownFloorEvent(DangionMap dangionMap,QuestManager questManager,StageUIManager stageUIManager,DangionController dangionController) { stageUIManager.HideButtons(); dangionMap.HideDangeonMapForSearching(); dangionMap.DestroyMapInfo(); questManager.currentFloor++; DialogTextManager.instance.SetScenarios(new string[] { "階段を降りた。\n現在地 B" + questManager.currentFloor}); yield return new WaitForSeconds(2f); dangionController.DeployPlayerPositionAtRandom(); //-----------ここからNew------------------ int floor = questManager.currentFloor - 1; if (dangionMap.isAlreadyEnterFloor[floor] == 0) { //一度も入っていないFloorなら dangionMap.InitEventNowFloor(questManager.currentFloor); dangionMap.SetEventNowFloorAtRandam(questManager.currentFloor, 3, 5); dangionMap.InitViewRoom(questManager.currentFloor); dangionMap.InitViewEvent(questManager.currentFloor); } //-----------Newここまで------------------ dangionMap.SetDangion(questManager.currentFloor); dangionMap.SetEvent(questManager.currentFloor); dangionMap.ShowDangeonMapForSearching(); stageUIManager.ShowButtonsForSearching(); stageUIManager.UpdateUI(questManager.currentFloor);//New } public IEnumerator IEUpFloorEvent(DangionMap dangionMap, QuestManager questManager, StageUIManager stageUIManager, DangionController dangionController) { stageUIManager.HideButtons(); dangionMap.HideDangeonMapForSearching(); dangionMap.DestroyMapInfo(); questManager.currentFloor--; DialogTextManager.instance.SetScenarios(new string[] { "階段を上がった。\n現在地 B" + questManager.currentFloor}); yield return new WaitForSeconds(2f); dangionController.DeployPlayerPositionAtRandom(); //-----------ここからNew------------------ int floor = questManager.currentFloor - 1; if (dangionMap.isAlreadyEnterFloor[floor] == 0) { //一度も入っていないFloorなら dangionMap.InitEventNowFloor(questManager.currentFloor); dangionMap.SetEventNowFloorAtRandam(questManager.currentFloor, 3, 5); dangionMap.InitViewRoom(questManager.currentFloor); dangionMap.InitViewEvent(questManager.currentFloor); } //-----------Newここまで------------------ dangionMap.SetDangion(questManager.currentFloor); dangionMap.InitEventNowFloor(questManager.currentFloor); //New dangionMap.SetEventNowFloorAtRandam(questManager.currentFloor, 3, 5); //New dangionMap.SetEvent(questManager.currentFloor); dangionMap.ShowDangeonMapForSearching(); stageUIManager.ShowButtonsForSearching(); stageUIManager.UpdateUI(questManager.currentFloor);//New }
それから、現状プレイヤーが階段を上り下りした時に、プレイヤーの位置がランダムで決定されるようになっているのをやめて、階段を降りたときは次のフロアの上り階段に、階段を登ったときは次のフロアの下り階段に配置されるようにします。DangeonController.csに以下の関数を作成します。
クリックで展開
/// <summary> /// プレイヤーを登り階段に配置する関数 /// </summary> public void PlayerPositionPlaceInUpStairs() //New { for (int j = 0; j < 5; j++) { for (int i = 0; i < 5; i++) { if(dangionMap.posOfEvent[questManager.currentFloor-1,j,i] == 6) { playerPos.x = i; playerPos.y = j; } } } } /// <summary> /// プレイヤーを下り階段に配置する関数 /// </summary> public void PlayerPositionPlaceInDownStairs() //New { for (int j = 0; j < 5; j++) { for (int i = 0; i < 5; i++) { if (dangionMap.posOfEvent[questManager.currentFloor - 1, j, i] == 5) { playerPos.x = i; playerPos.y = j; } } } }
この関数をDangeonEvent.csのIEDownFloorEventコルーチンとIEUpFloorEventコルーチンで呼び出すようにします。
クリックで展開
public IEnumerator IEDownFloorEvent(DangionMap dangionMap,QuestManager questManager,StageUIManager stageUIManager,DangionController dangionController) { stageUIManager.HideButtons(); dangionMap.HideDangeonMapForSearching(); dangionMap.DestroyMapInfo(); questManager.currentFloor++; DialogTextManager.instance.SetScenarios(new string[] { "階段を降りた。\n現在地 B" + questManager.currentFloor}); yield return new WaitForSeconds(2f); dangionController.PlayerPositionPlaceInUpStairs(); //New int floor = questManager.currentFloor - 1; if (dangionMap.isAlreadyEnterFloor[floor] == 0) { //一度も入っていないFloorなら dangionMap.InitEventNowFloor(questManager.currentFloor); dangionMap.SetEventNowFloorAtRandam(questManager.currentFloor, 3, 5); dangionMap.InitViewRoom(questManager.currentFloor); dangionMap.InitViewEvent(questManager.currentFloor); } dangionMap.SetDangion(questManager.currentFloor); dangionMap.SetEvent(questManager.currentFloor); dangionMap.ShowDangeonMapForSearching(); stageUIManager.ShowButtonsForSearching(); stageUIManager.UpdateUI(questManager.currentFloor); } public IEnumerator IEUpFloorEvent(DangionMap dangionMap, QuestManager questManager, StageUIManager stageUIManager, DangionController dangionController) { stageUIManager.HideButtons(); dangionMap.HideDangeonMapForSearching(); dangionMap.DestroyMapInfo(); questManager.currentFloor--; DialogTextManager.instance.SetScenarios(new string[] { "階段を上がった。\n現在地 B" + questManager.currentFloor}); yield return new WaitForSeconds(2f); dangionController.PlayerPositionPlaceInDownStairs(); //New int floor = questManager.currentFloor - 1; if (dangionMap.isAlreadyEnterFloor[floor] == 0) { //一度も入っていないFloorなら dangionMap.InitEventNowFloor(questManager.currentFloor); dangionMap.SetEventNowFloorAtRandam(questManager.currentFloor, 3, 5); dangionMap.InitViewRoom(questManager.currentFloor); dangionMap.InitViewEvent(questManager.currentFloor); } dangionMap.SetDangion(questManager.currentFloor); dangionMap.InitEventNowFloor(questManager.currentFloor); dangionMap.SetEventNowFloorAtRandam(questManager.currentFloor, 3, 5); dangionMap.SetEvent(questManager.currentFloor); dangionMap.ShowDangeonMapForSearching(); stageUIManager.ShowButtonsForSearching(); stageUIManager.UpdateUI(questManager.currentFloor); }
あとは、移動ボタンを押した時に、移動先の部屋とイベントを表示するようにします。DangeonController.csに以下の関数を作成します。
クリックで展開
/// <summary> /// プレイヤー位置の部屋を表示状態にする関数 /// </summary> public void ActivePlayerPosRoom() { dangionMap.roomImage[playerPos.y, playerPos.x].SetActive(true); dangionMap.isViewRoom[questManager.currentFloor-1,playerPos.y,playerPos.x] = 1; } /// <summary> /// プレイヤー位置のイベントを表示状態にする関数 /// /// </summary> public void ActivePlayerPosEvent() { if(dangionMap.posOfEvent[questManager.currentFloor-1,playerPos.y,playerPos.x] == -1) { dangionMap.eventIcons[playerPos.y, playerPos.x].SetActive(true); } dangionMap.isViewEvent[questManager.currentFloor - 1, playerPos.y, playerPos.x] = 1; }
これらの関数を、questManagerのOnCompleteMoveRoomに登録します。
以上の処理を追加して実行すると以下の動画のようになります。
これでたどり着いていない部屋とその部屋のイベントを非表示にするようになりました。次回はトラップや扉を開くためのスイッチなどを実装していきます。