前回は戦闘時のプレイヤーと敵の行動順の処理を実装しました。今回はプレイヤー死亡時の処理と、戦闘勝利時の処理を実装していきたいと思います。
BattleManagerを開き、以下の部分を修正します。
クリックで展開
IEnumerator PlayerDeath()
{
yield return new WaitForSeconds(0.5f);
SoundManager.instance.StopBGM();
DialogTextManager.instance.SetScenarios(new string[] { "全滅した・・・。" });
SoundManager.instance.PlayBGM("GameOver");
yield return new WaitForSeconds(10f);
yield return StartCoroutine(questManager.GotoTown());
}
PlayerDeathはプレイヤーがやられた時に呼び出される関数です。この関数をIEnumeratoで定義し、プレーヤーのHPが0になった時に呼び出すようにします。questManager.GotoTown()は単に"Town"シーンに遷移する関数です。
クリックで展開
IEnumerator EnemyTurn(int index)
{
commandUI.ShowDialogUI();
SoundManager.instance.PlaySE(1);
DialogTextManager.instance.SetScenarios(new string[] {
enemysList[index].name + "の攻撃!\n" });
yield return new WaitForSeconds(0.8f);
if (EnemyCheckHitAttack(index))
{
playerDamagePanel.DOShakePosition(0.3f, 0.5f, 20, 0, false, true);
bool isCritical = EnemyCheckCritical(index);
int damage = enemysList[index].Attack(player,isCritical);
if (isCritical)
{
SoundManager.instance.PlaySE(5);
DialogTextManager.instance.SetScenarios(new string[] {
"クリティカル!!\n" + player.pname + "に" + damage + "ダメージを与えた。" });
yield return new WaitForSeconds(0.8f);
}
else
{
DialogTextManager.instance.SetScenarios(new string[] {
player.pname + "に" + damage + "ダメージを与えた。" });
yield return new WaitForSeconds(0.8f);
}
playerUI.UpdateUI(player);
}
else
{
SoundManager.instance.PlaySE(4);
DialogTextManager.instance.SetScenarios(new string[] {
player.pname + "は攻撃をかわした。" });
yield return new WaitForSeconds(0.8f);
}
yield return new WaitForSeconds(1f);
if (player.hp <= 0)
{
yield return StartCoroutine(PlayerDeath());
}
}
EnemyTurn関数で敵の攻撃の処理を終えた後にplayer.hp<0かどうかを判定し、trueであればPlayerDeathコルーチンを開始します。
次は経験値とお金の実装をします。新規でGameManager.csを作成し、ヒエラルキーに同名のゲームオブジェクトを作成して、GameManager.csを貼り付けます。GameManager.csを作成し、次のように記述します。
クリックで展開
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager instance;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
else
{
Destroy(this.gameObject);
}
}
public int gold;
}
ゲームオブジェクトをシングルトンにして全てのシーンで共有できるようにします。中身は今は単にお金を保持するのみです。
それからプレイヤーの情報もシーンが変わっても保持するようにしたいので、PlayerManager.csの先頭に以下の記述をしてシングルトンにしておきます。
クリックで展開
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
else
{
Destroy(this.gameObject);
}
}
次にバトル中に獲得したお金や能力経験値を一時的に保持しておくスクリプトを作成します。BattkeResultスクリプトを新規作成し、次のように記述します。
クリックで展開
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
public class BattleResult : MonoBehaviour
{
public int gainGold;
public int hpExp;
public int mpExp;
public int atExp;
public int defExp;
public int agiExp;
public int lukExp;
public int senceExp;
public int magPowExp;
public int magDefExp;
public int learnAbiExp;
public BattleResult()
{
gainGold = 0;
hpExp = 0;
mpExp = 0;
atExp = 0;
defExp = 0;
agiExp = 0;
lukExp = 0;
senceExp = 0;
magPowExp = 0;
magDefExp = 0;
learnAbiExp = 0;
}
public void AddGainGold(EnemyManager enemy)
{
gainGold += enemy.gold;
}
public void AddGainExp(EnemyManager enemy)
{
hpExp += enemy.hpExp;
mpExp += enemy.mpExp;
atExp += enemy.atkExp;
defExp += enemy.defExp;
agiExp += enemy.agiExp;
lukExp += enemy.lukExp;
senceExp += enemy.senceExp;
magPowExp += enemy.magPowExp;
magDefExp += enemy.magDefExp;
learnAbiExp += enemy.learnAbiExp;
}
public void AddExpToPlayer(PlayerManager player)
{
player.hpExp += this.hpExp;
player.mpExp += this.mpExp;
player.atExp += this.atExp;
player.defExp += this.defExp;
player.agiExp += this.agiExp;
player.lukExp += this.lukExp;
player.senceExp += this.senceExp;
player.magPowExp += this.magPowExp;
player.magDefExp += this.magDefExp;
player.learnAbiExp += this.learnAbiExp;
}
public void AddGoldToPlayer()
{
GameManager.instance.gold += this.gainGold;
}
}
戦闘開始時にこのクラスのコンストラクタで初期化し、敵撃破時にAddGainGoldとAddGainExpを呼び出します。
クリックで展開
public void SetUpBattle()
{
SoundManager.instance.PlayBGM("Battle");
player = PlayerManager.instance;
playerUI.SetupUI(player);
commandUI.gameObject.SetActive(true);
commandUI.SetBattleCommand();
battleResult = new BattleResult();
}
クリックで展開
IEnumerator EnemyDeath(int enemyIndex)
{
yield return new WaitForSeconds(0.5f);
SoundManager.instance.PlaySE(3);
enemysList[enemyIndex].enemyImage.transform.DOScale(new Vector3(2.0f, 2.0f, 2.0f), 1f);
enemysList[enemyIndex].enemyImage.DOColor(new Color(1f, 0f, 0f, 0f), 1f);
yield return new WaitForSeconds(2f);
DialogTextManager.instance.SetScenarios(new string[] {
enemysList[enemyIndex].name + "を倒した" });
yield return new WaitForSeconds(0.5f);
battleResult.AddGainGold(enemysList[targetIndex]);
battleResult.AddGainExp(enemysList[targetIndex]);
Debug.Log("enemysList[targetIndex]のオブジェクト破壊");
Destroy(enemysList[targetIndex].gameObject);
enemysList.Remove(key: targetIndex);
Debug.Log("enemysListの要素数は" + enemysList.Count);
DeletAgiDicElemDeathEnemy(targetIndex);
Debug.Log("AgiDicの要素数は" + AgiDic.Count);
}
更に、戦闘が終了した時に、battleResultが保持していた能力経験値とゴールドをPlayerManagerの能力経験値とゴールドに加算します。
クリックで展開
IEnumerator EndBattle()
{
Debug.Log("戦闘終了");
yield return new WaitForSeconds(2f);
DialogTextManager.instance.SetScenarios(new string[] {
"戦闘終了!!" });
yield return new WaitForSeconds(2f);
battleResult.AddGoldToPlayer();
battleResult.AddExpToPlayer(player);
DialogTextManager.instance.SetScenarios(new string[] {
battleResult.gainGold + "ゴールドを手に入れた。" });
yield return new WaitForSeconds(2f);
SoundManager.instance.PlayBGM("Quest");
questManager.EndBattle();
}
これで戦闘で得たお金と経験値を入手する処理が完成しました。
今回はここまでです。