Skip to content

Commit

Permalink
Merge pull request #25 from OUCC/#1-戦闘シーン
Browse files Browse the repository at this point in the history
ゲーム終了画面の実装とバグ修正
  • Loading branch information
watanabeRRR authored Jun 24, 2023
2 parents 398ec5f + 742d2e1 commit 68f2b1f
Show file tree
Hide file tree
Showing 12 changed files with 1,475 additions and 34 deletions.
File renamed without changes.
File renamed without changes.
1,334 changes: 1,334 additions & 0 deletions Assets/Scenes/ResultScene.unity

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Assets/Scenes/ResultScene.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions Assets/Scripts/BattleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@ void Start()
battleSystem = new BattleSystem();
}

/*
StateUpdateAsync
引数: プレイヤーの行動
返り値: (文章・プレイヤーのHP・敵のHP)の順のタプル
*/
public async UniTask<(string, string, int, int, bool)> StateUpdateAsync(int playerAction)
/// <summary>
/// プレイヤーの選択を受け取って、戦闘結果を返す
/// </summary>
/// <param name="playerAction">プレイヤーの行動</param>
/// <returns> (文章・プレイヤーのHP・敵のHP・カウンター判定・勝敗判定)の順のタプル</returns>
public async UniTask<(string, string, int, int, bool, int)> StateUpdateAsync(int playerAction)
{
BattleData battleData = await battleSystem.BattleProcessAsync(playerAction);

int playerHp = battleData.Player.Hp;
int enemyHp = battleData.Enemy.Hp;

string playerSendMessage, enemySendMessage;

playerSendMessage = GenerateMessage(battleData.Player);
enemySendMessage = GenerateMessage(battleData.Enemy);
return (playerSendMessage, enemySendMessage, playerHp, enemyHp, battleData.ActionJudge);

return (playerSendMessage, enemySendMessage, playerHp, enemyHp, battleData.ActionJudge, battleData.BattleJudge);
}

private string GenerateMessage(ICharacter character)
{
string message = "";
Expand Down
16 changes: 13 additions & 3 deletions Assets/Scripts/BattleSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Cysharp.Threading.Tasks;

namespace HackathonA
Expand All @@ -23,7 +23,7 @@ public BattleSystem()
public async UniTask<BattleData> BattleProcessAsync(int playerAction)
{
//ChatGPTからEnemyのActionTypeを取得
enemy.ActionType = await enemyAI.GetEnemyActionAsync(player.Hp, enemy.Hp, playerAction);
enemy.ActionType = await enemyAI.GetEnemyActionAsync(player.Hp, enemy.Hp, player.ActionType);

player.ActionType = playerAction;

Expand Down Expand Up @@ -209,6 +209,16 @@ public async UniTask<BattleData> BattleProcessAsync(int playerAction)
enemyHp -= playerDamageValue;
}

// 0以下になったときに0にする
if(playerHp < 0)
{
playerHp = 0;
}
if(enemyHp < 0)
{
enemyHp = 0;
}

return (playerHp, enemyHp);
}

Expand Down Expand Up @@ -247,4 +257,4 @@ private bool ActionJudge(bool counterJudge)
}
}
}
}
}
54 changes: 34 additions & 20 deletions Assets/Scripts/UIManager.cs → Assets/Scripts/BattleUIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;

namespace HackathonA
{
public class UIManager : MonoBehaviour, IDisposable
public class BattleUIManager : MonoBehaviour
{
[SerializeField]
private TextMeshProUGUI messageText;
Expand Down Expand Up @@ -36,7 +38,6 @@ public class UIManager : MonoBehaviour, IDisposable
[SerializeField]
private GameObject battleManagerObject;
private BattleManager battleManager;
private CancellationTokenSource cancellationTokenSource = new();

// Start is called before the first frame update
void Start()
Expand All @@ -50,50 +51,63 @@ void Start()
messageTextBackground.gameObject.SetActive(false);
}

private void OnDestroy()
{
cancellationTokenSource?.Cancel();
cancellationTokenSource?.Dispose();
}

private async UniTask ButtonCliledAsync(int playerAction)
{
int battleJudge = 0;
var ct = this.GetCancellationTokenOnDestroy();
try
{
buttonGroup.gameObject.SetActive(false);
backButtonGroup.gameObject.SetActive(false);
(string playerMessage, string enemyMessage, int playerHP, int enemyHP, bool actionJudge) = await battleManager.StateUpdateAsync(playerAction);
(string playerMessage, string enemyMessage, int playerHP, int enemyHP, bool actionJudge, int battleJudge1) = await battleManager.StateUpdateAsync(playerAction);
battleJudge = battleJudge1;
playerHpText.SetText($"HP:{playerHP}");
enemyHpText.SetText($"HP:{enemyHP}");
messageTextBackground.gameObject.SetActive(true);
backButtonGroup.gameObject.SetActive(true);
if (actionJudge)
{
messageText.SetText(playerMessage);
await UniTask.Delay(TimeSpan.FromSeconds(3), cancellationToken: cancellationTokenSource.Token);
await UniTask.Delay(TimeSpan.FromSeconds(4), cancellationToken: ct);
messageText.SetText(enemyMessage);
}
else
{
messageText.SetText(enemyMessage);
await UniTask.Delay(TimeSpan.FromSeconds(3), cancellationToken: cancellationTokenSource.Token);
await UniTask.Delay(TimeSpan.FromSeconds(4), cancellationToken: ct);
messageText.SetText(playerMessage);
}
await UniTask.Delay(TimeSpan.FromSeconds(5), cancellationToken: cancellationTokenSource.Token);
}
if (battleJudge == 0)
{
await UniTask.Delay(TimeSpan.FromSeconds(4), cancellationToken: ct);
}
else
{
PlayerPrefs.SetInt("result", battleJudge);
PlayerPrefs.Save();
await SceanLoadCoroutine(LoadSceneMode.Single, 4.0f).ToUniTask();
}
}
finally
{
messageTextBackground.gameObject.SetActive(false);
buttonGroup.gameObject.SetActive(true);
cancellationTokenSource.Dispose();
cancellationTokenSource = new();
if (battleJudge == 0)
{
messageTextBackground.gameObject.SetActive(false);
buttonGroup.gameObject.SetActive(true);
}
}
}

public void Dispose()
internal IEnumerator SceanLoadCoroutine(LoadSceneMode sceneMode, float i_waitTime)
{
cancellationTokenSource?.Cancel();
cancellationTokenSource?.Dispose();
var ao = SceneManager.LoadSceneAsync("ResultScene", sceneMode);
if (i_waitTime > 0.0f)
{
ao.allowSceneActivation = false;
yield return new WaitForSeconds(i_waitTime);
ao.allowSceneActivation = true;
}
yield return ao;
}
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion Assets/Scripts/EnemyAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async UniTask<int> GetEnemyActionAsync(int playerHP, int enemyHP, int pla
3 => "physical counter",
4 => "magic counter",
_ => throw new ArgumentOutOfRangeException()
}}before.
}} before.
Choose from the options above and respond with the corresponding number.
Also, your response have to be only figure. You can not respond anything other than figure.";
}
Expand Down
61 changes: 61 additions & 0 deletions Assets/Scripts/ResultUIManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace HackathonA
{

public class ResultUIManager : MonoBehaviour
{
[SerializeField]
private TextMeshProUGUI resultText;
[SerializeField]
private Button restartButton;
[SerializeField]
private Button quitButton;
// Start is called before the first frame update
void Start()
{
resultText.SetText(PlayerPrefs.GetInt("result") switch
{
-1 => "Playerの負け",
1 => "Playerの勝ち",
_ => "Error"
});
restartButton.onClick.AddListener(() => ButtonClicked(0).Forget());
quitButton.onClick.AddListener(() => ButtonClicked(1).Forget());
}

private async UniTaskVoid ButtonClicked(int buttonId)
{
if(buttonId == 0)
{
await SceanLoadCoroutine(LoadSceneMode.Single, 0f).ToUniTask();
}
else if(buttonId == 1)
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}

internal IEnumerator SceanLoadCoroutine(LoadSceneMode sceneMode, float i_waitTime)
{
var ao = SceneManager.LoadSceneAsync("BattleScene", sceneMode);
if (i_waitTime > 0.0f)
{
ao.allowSceneActivation = false;
yield return new WaitForSeconds(i_waitTime);
ao.allowSceneActivation = true;
}
yield return ao;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/ResultUIManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion ProjectSettings/EditorBuildSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ EditorBuildSettings:
path: Assets/Scenes/SampleScene.unity
guid: 2cda990e2423bbf4892e6590ba056729
- enabled: 1
path: Assets/Scenes/BattleUIScene.unity
path: Assets/Scenes/BattleScene.unity
guid: f7eebae42cbe1e64493573d76b4171bc
- enabled: 1
path: Assets/Scenes/ResultScene.unity
guid: 7ba007a11ef4b5847bf989d0a980cc10
m_configObjects: {}

0 comments on commit 68f2b1f

Please sign in to comment.