【Unity】RPG制作 その23-ワープイベント、帰還イベントの作成
今回はワープイベントを作成していきます。eventIDを11とします。ワープイベントを知らせる処理をInformDangeonEventコルーチンに追加します。
クリックで展開
public IEnumerator InformDangeonEvent() { eventID = GetCurrentPosDangeonEventID(); DangeonEvent dangeonEvent = new DangeonEvent(); switch (eventID) { case -1://何もなし yield break; case 0://採掘イベントを知らせる StartCoroutine(dangeonEvent.IEInformMineEvent()); yield break; case 1://釣りイベントを知らせる StartCoroutine(dangeonEvent.IEInformFishingEvent()); yield break; case 2: //アイテムイベントを知らせる StartCoroutine(dangeonEvent.IEInformItemEvent()); yield break; case 3: //伐採イベントを知らせる StartCoroutine(dangeonEvent.IEInformLoggingEvent()); yield break; case 4: //金貨イベントを知らせる StartCoroutine(dangeonEvent.IEInformGoldEvent()); yield break; case 5: //下階段イベントを知らせる StartCoroutine(dangeonEvent.IEInformDownFloorEvent()); yield break; case 6: //上階段イベントを知らせる StartCoroutine(dangeonEvent.IEInformUpFloorEvent()); yield break; case 7: //トラップイベントを実行する yield return StartCoroutine(dangeonEvent.IETrapEvent(5,questManager,playerUIManager)); DeleteEvent(); stageUIManager.isEventPosition = false; dangionMap.ShowDangeonMapForSearching(); stageUIManager.ShowButtonsForSearching(); questManager.nowEvent = false; yield break; case 8: //スイッチで開く扉イベントを知らせる StartCoroutine(dangeonEvent.IEInformSwitchDoorEvent()); yield break; case 9://青いスイッチ(OFF)イベント StartCoroutine(dangeonEvent.IEInformOffBlueSwitchEvent()); yield break; case 10://青いスイッチ(ON)イベント StartCoroutine(dangeonEvent.IEInformOnBlueSwitchEvent()); yield break; //--------------New----------- case 11://ワープイベント StartCoroutine(dangeonEvent.IEInformWarpEvent()); yield break; //--------------Newここまで----------- } }
DangeonEvent.csにIEInformWarpEventコルーチンの定義を記述します。
クリックで展開
public IEnumerator IEInformWarpEvent() //New { DialogTextManager.instance.SetScenarios(new string[] { "ワープポイントがある"}); yield return new WaitForSeconds(2f); }
実際にワープするイベントも追加します。ワープポイントは2つで1組として機能するのですが、同じフロアで2組以上設置できるように、それぞれのワープポイントがどのワープポイントに関連付けられているかをマップ横数×マップ縦数の配列で設定するようにします。DangeonMap.csに新しく配列を宣言します。
クリックで展開
public int[,,] WarpEventAssociation = new int[6, 5, 5] { { //B1F { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, }, { //B2F { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, }, { //B3F { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, }, { //B4F { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, }, { //B5F { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, { -1,-1,-1,-1,-1 }, }, { //B6F { -1,-1,-1, 0,-1 }, { -1, 0,-1,-1, 1 }, { -1,-1,-1,-1,-1 }, { -1, 2,-1,-1,-1 }, { 1,-1,-1, 2,-1 }, } };
B6Fのワープイベントを3組設定するので、0~2の3組の数値を設定しています。B6Fのイベントのは位置と部屋構造は
以下のとおりです。
ワープイベントをDangeonEventに記述します。
クリックで展開
public IEnumerator IEWarpEvent(DangionMap dangionMap,QuestManager questManager,DangionController dangionController) //New { SoundManager.instance.PlaySE(31); int floor = questManager.currentFloor - 1; int px = dangionController.playerPos.x; int py = dangionController.playerPos.y; int warpID = dangionMap.WarpEventAssociation[floor, py, px]; for(int j = 0; j < dangionController.dangion_height; j++) { for (int i = 0; i < dangionController.dangion_width; i++) { if(dangionMap.WarpEventAssociation[floor, j, i] == warpID && (px != i || py != j)) { dangionController.playerPos.x = i; dangionController.playerPos.y = j; } } } DialogTextManager.instance.SetScenarios(new string[] { PlayerManager.instance.pname + "はワープした"}); yield return new WaitForSeconds(2f); }
プレイヤーの現在地にあるワープのIDでWarpEventAssociation配列の要素を検索して、一致するIDのワープポイントの位置に移動します。このコルーチンをDangeonController.csのIEDangeonEventProcessコルーチンで呼び出します。
クリックで展開
public IEnumerator IEDangeonEventProcess() { stageUIManager.SetActionButton(false); stageUIManager.SetArrowsButton(false); if (dangionMap.isOpenDangeonMap) { stageUIManager.SetCancelButton(false); } eventID = GetCurrentPosDangeonEventID(); DangeonEvent dangeonEvent = new DangeonEvent(); switch (eventID) { case -1://何もなし break; case 0://採掘イベント yield return dangeonEvent.IEMineEvent(mineItemGroup); RegistryEventMemory(dangeonEvent,3,5,playerPos.x,playerPos.y,0); break; case 1://釣りイベント yield return dangeonEvent.IEFishingEvent(fisingItemGroup); RegistryEventMemory(dangeonEvent, 3,5, playerPos.x, playerPos.y, 1); break; case 2: //アイテムイベント yield return dangeonEvent.IEItemEvent(itemEventGroup); RegistryEventMemory(dangeonEvent, 3, 5, playerPos.x, playerPos.y, 2); DeleteEvent(); break; case 3: //伐採イベント yield return dangeonEvent.IELoggingEvent(loggingItemGroup); RegistryEventMemory(dangeonEvent, 3, 5, playerPos.x, playerPos.y, 3); break; case 4: //金貨イベント yield return dangeonEvent.IEGetGoldEvent(Random.Range(minGold, maxGold + 1)); playerUIManager.UpdateUI(PlayerManager.instance); DeleteEvent(); stageUIManager.isEventPosition = false; break; case 5: //階段を降りるイベント yield return dangeonEvent.IEDownFloorEvent(dangionMap, questManager,stageUIManager,this); break; case 6: //階段を上がるイベント yield return dangeonEvent.IEUpFloorEvent(dangionMap, questManager, stageUIManager,this); break; case 7://7はトラップイベントだが配置された位置に到達した時に実行されるのでここでは何もしない(というかここには処理が来ない) break; case 8: //スイッチで開く扉イベント yield return dangeonEvent.IEUpSwitchDoorEvent(); break; case 9: ////青いスイッチ(OFF)イベント yield return dangeonEvent.IEOFFBlueSwitchEvent(dangionMap,questManager,this); break; case 10: ////青いスイッチ(ON)イベント yield return dangeonEvent.IEONBlueSwitchEvent(); break; //------------------New--------- case 11: //ワープイベント yield return dangeonEvent.IEWarpEvent(dangionMap,questManager,this); UpdatePlayerPosition(); ActivePlayerPosRoom(); ActivePlayerPosEvent(); break; //-----------------Newここまで------------ } if(shouldActiveActionButton) { stageUIManager.actionButton.SetActive(true); } else { stageUIManager.actionButton.SetActive(false); } stageUIManager.SetArrowsButton(true); if (dangionMap.isOpenDangeonMap) { stageUIManager.SetCancelButton(true); } yield return null; }
これでワープイベントは完成しました。次に帰還イベントの作成をします。帰還イベントのIDはイベントIDは12にします。帰還イベントを知らせる処理をDangeonController.csのInformDangeonEventコルーチンに追加します。
クリックで展開
public IEnumerator InformDangeonEvent() { eventID = GetCurrentPosDangeonEventID(); DangeonEvent dangeonEvent = new DangeonEvent(); stageUIManager.isEventPosition = true; switch (eventID) { case -1://何もなし stageUIManager.isEventPosition = false; yield break; case 0://採掘イベントを知らせる StartCoroutine(dangeonEvent.IEInformMineEvent()); yield break; case 1://釣りイベントを知らせる StartCoroutine(dangeonEvent.IEInformFishingEvent()); yield break; case 2: //アイテムイベントを知らせる StartCoroutine(dangeonEvent.IEInformItemEvent()); yield break; case 3: //伐採イベントを知らせる StartCoroutine(dangeonEvent.IEInformLoggingEvent()); yield break; case 4: //金貨イベントを知らせる StartCoroutine(dangeonEvent.IEInformGoldEvent()); yield break; case 5: //下階段イベントを知らせる StartCoroutine(dangeonEvent.IEInformDownFloorEvent()); yield break; case 6: //上階段イベントを知らせる StartCoroutine(dangeonEvent.IEInformUpFloorEvent()); yield break; case 7: //トラップイベントを実行する yield return StartCoroutine(dangeonEvent.IETrapEvent(5,questManager,playerUIManager)); DeleteEvent(); stageUIManager.isEventPosition = false; dangionMap.ShowDangeonMapForSearching(); stageUIManager.ShowButtonsForSearching(); questManager.nowEvent = false; yield break; case 8: //スイッチで開く扉イベントを知らせる StartCoroutine(dangeonEvent.IEInformSwitchDoorEvent()); yield break; case 9://青いスイッチ(OFF)イベント StartCoroutine(dangeonEvent.IEInformOffBlueSwitchEvent()); yield break; case 10://青いスイッチ(ON)イベント StartCoroutine(dangeonEvent.IEInformOnBlueSwitchEvent()); yield break; case 11://ワープイベントを知らせる StartCoroutine(dangeonEvent.IEInformWarpEvent()); yield break; //--------------New----------- case 12: //帰還イベントを知らせる StartCoroutine(dangeonEvent.IEInformReturnTownEvent()); yield break; //--------------Newここまで----------- } if (eventID != -1 && eventID != 7) { shouldActiveActionButton = true; } else { shouldActiveActionButton = false; } }
IEInformReturnTownEventコルーチンと実際に帰還するイベントをDangeonEvent.csで定義します。
クリックで展開
public IEnumerator IEInformReturnTownEvent() //New { DialogTextManager.instance.SetScenarios(new string[] { "帰還ポイントがある"}); yield return new WaitForSeconds(2f); } public IEnumerator IEReturnEvent(QuestManager questManager) //New { SoundManager.instance.PlaySE(32); //帰還装置SE DialogTextManager.instance.SetScenarios(new string[] { "帰還装置を作動させた。"}); yield return new WaitForSeconds(2f); yield return StartCoroutine(questManager.GotoTown()); }
DangeonController.csのIEDangeonEventProcessコルーチンでIEInformReturnTownEventコルーチンを呼び出します。
クリックで展開
public IEnumerator IEDangeonEventProcess() { stageUIManager.SetActionButton(false); stageUIManager.SetArrowsButton(false); if (dangionMap.isOpenDangeonMap) { stageUIManager.SetCancelButton(false); } eventID = GetCurrentPosDangeonEventID(); DangeonEvent dangeonEvent = new DangeonEvent(); switch (eventID) { case -1://何もなし break; case 0://採掘イベント yield return dangeonEvent.IEMineEvent(mineItemGroup); RegistryEventMemory(dangeonEvent,3,5,playerPos.x,playerPos.y,0); break; case 1://釣りイベント yield return dangeonEvent.IEFishingEvent(fisingItemGroup); RegistryEventMemory(dangeonEvent, 3,5, playerPos.x, playerPos.y, 1); break; case 2: //アイテムイベント yield return dangeonEvent.IEItemEvent(itemEventGroup); RegistryEventMemory(dangeonEvent, 3, 5, playerPos.x, playerPos.y, 2); DeleteEvent(); break; case 3: //伐採イベント yield return dangeonEvent.IELoggingEvent(loggingItemGroup); RegistryEventMemory(dangeonEvent, 3, 5, playerPos.x, playerPos.y, 3); break; case 4: //金貨イベント yield return dangeonEvent.IEGetGoldEvent(Random.Range(minGold, maxGold + 1)); playerUIManager.UpdateUI(PlayerManager.instance); DeleteEvent(); stageUIManager.isEventPosition = false; break; case 5: //階段を降りるイベント yield return dangeonEvent.IEDownFloorEvent(dangionMap, questManager,stageUIManager,this); break; case 6: //階段を上がるイベント yield return dangeonEvent.IEUpFloorEvent(dangionMap, questManager, stageUIManager,this); break; case 7://7はトラップイベントだが配置された位置に到達した時に実行されるのでここでは何もしない(というかここには処理が来ない) break; case 8: //スイッチで開く扉イベント yield return dangeonEvent.IEUpSwitchDoorEvent(); break; case 9: ////青いスイッチ(OFF)イベント yield return dangeonEvent.IEOFFBlueSwitchEvent(dangionMap,questManager,this); break; case 10: ////青いスイッチ(ON)イベント yield return dangeonEvent.IEONBlueSwitchEvent(); break; case 11: //ワープイベント yield return dangeonEvent.IEWarpEvent(dangionMap,questManager,this); UpdatePlayerPosition(); ActivePlayerPosRoom(); ActivePlayerPosEvent(); break; //------------------New--------- case 12: //帰還イベント yield return dangeonEvent.IEReturnEvent(questManager); break; //-----------------Newここまで------------ } if (shouldActiveActionButton) { stageUIManager.actionButton.SetActive(true); } else { stageUIManager.actionButton.SetActive(false); } stageUIManager.SetArrowsButton(true); if (dangionMap.isOpenDangeonMap) { stageUIManager.SetCancelButton(true); } yield return null; }
これで実行すると以下の動画のようになります。
今回は以上です。