Skip to content

Commit

Permalink
Merge pull request #8 from OUCC/#2-BattleSystemの作成
Browse files Browse the repository at this point in the history
#2 battle systemの作成
  • Loading branch information
aiueo-1234 authored Jun 18, 2023
2 parents 5aa8714 + 5c952ad commit 6070ad4
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 32 deletions.
45 changes: 13 additions & 32 deletions Assets/Scripts/BattleData.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,22 @@
using System.Collections;
using System.Collections.Generic;
using System;

namespace HackathonA
{
public class BattleData
{
//playerのHP
public readonly int playerHp;

//敵のHP
public readonly int enemyHp;

//playerの行動(0:物理攻撃、1:魔法攻撃、2:回復、3:物理カウンター、4:魔法カウンター)
public readonly int playerAction;

//敵の行動(0:物理攻撃、1:魔法攻撃、2:回復、3:物理カウンター、4:魔法カウンター)
public readonly int enemyAction;

//playerのダメージ
public readonly int playerDamageValue;

//敵のダメージ
public readonly int enemyDamageValue;

//終了判定(TrueがPlayer先行、Falseが敵先行)
public readonly bool actionJudge;

public BattleData(int _playerHp,int _enemyHp, int _playerAction, int _enemyAction, int _playerDamageValue, int _enemyDamageValue, bool _actionJudge)
//勝敗判定(1:player勝利、0:継戦、ー1:enemy勝利)
public readonly int BattleJudge;
//先行判定(true:player先行)
public readonly bool MoveJudge;
public readonly Player Player;
public readonly Enemy Enemy;

public BattleData(int battleJudge, bool moveJudge, Player player, Enemy enemy)
{
this.playerHp = _playerHp;
this.enemyHp = _enemyHp;
this.playerAction = _playerAction;
this.enemyAction = _enemyAction;
this.playerDamageValue = _playerDamageValue;
this.enemyDamageValue = _enemyDamageValue;
this.actionJudge = _actionJudge;

this.BattleJudge = battleJudge;
this.MoveJudge = moveJudge;
this.Player = player;
this.Enemy = enemy;
}
}
}
Expand Down
251 changes: 251 additions & 0 deletions Assets/Scripts/BattleSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
using System;
using Cysharp.Threading.Tasks;

namespace HackathonA
{
public class BattleSystem
{
private EnemyAI enemyAI;
public BattleSystem()
{
var config = Resources.Load("ApiKey") as TextAsset;
var _apiKey = config.text.Trim();
enemyAI = new EnemyAI(_apiKey);
}

public async UniTask<BattleData> BattleProcessAsync(int playerAction)
{
Player player = new Player();
Enemy enemy = new Enemy();

player.ActionType = playerAction;
//ChatGPTからEnemyのActionTypeを取得
enemy.ActionType = await enemyAI.GetEnemyActionAsync(player.Hp, enemy.Hp);

(player.DamageValue, enemy.DamageValue, player.CounterJudge, enemy.CounterJudge) = Damage(player.ActionType, enemy.ActionType);

player.Hp = HpCalculate(player.Hp, player.ActionType, player.DamageValue);
enemy.Hp = HpCalculate(enemy.Hp, enemy.ActionType, enemy.DamageValue);


int battleJudge = BattleJudge(player.Hp, enemy.Hp);
bool moveJudge = MoveJudge(player.CounterJudge);
return new BattleData(battleJudge, moveJudge, player, enemy);
}

//カウンターの成功判定(成功が1、失敗が0)
private (bool playerCounterJudge, bool enemyCounterJudge) CounterJudge(int playerAction, int enemyAction)
{
bool playerCounterJudge = false;
bool enemyCounterJudge = false;

//player側の判定
if (playerAction == 3)
{
switch (enemyAction)
{
case 0:
playerCounterJudge = true;
break;
case 1:
playerCounterJudge = false;
break;
case 2:
playerCounterJudge = false;
break;
case 3:
playerCounterJudge = false;
break;
case 5:
playerCounterJudge = false;
break;
}
}
else if (playerAction == 4)
{
switch (enemyAction)
{
case 0:
playerCounterJudge = false;
break;
case 1:
playerCounterJudge = true;
break;
case 2:
playerCounterJudge = false;
break;
case 3:
playerCounterJudge = false;
break;
case 5:
playerCounterJudge = false;
break;
}
}

//enemy側の判定
else if (enemyAction == 3)
{
switch (playerAction)
{
case 0:
enemyCounterJudge = true;
break;
case 1:
enemyCounterJudge = false;
break;
case 2:
enemyCounterJudge = false;
break;
case 3:
enemyCounterJudge = false;
break;
case 5:
enemyCounterJudge = false;
break;
}
}
else if (enemyAction == 4)
{
switch (playerAction)
{
case 0:
enemyCounterJudge = false;
break;
case 1:
enemyCounterJudge = true;
break;
case 2:
enemyCounterJudge = false;
break;
case 3:
enemyCounterJudge = false;
break;
case 5:
enemyCounterJudge = false;
break;
}
}
return (playerCounterJudge, enemyCounterJudge);
}

//ダメージ計算
private (int playerDamageValue, int enemyDamageValue, bool playerCounterJudge, bool enemyCounterJudge) Damage(int playerAction, int enemyAction)
{
int playerDamageValue = 0;
int enemyDamageValue = 0;
(bool playerCounterJudge, bool enemyCounterJudge) = CounterJudge(playerAction, enemyAction);

var rand = new Random();
switch (playerAction)
{
case 0:
playerDamageValue = rand.Next(20, 30);
break;
case 1:
playerDamageValue = rand.Next(20, 30);
break;
case 2:
playerDamageValue = rand.Next(10, 15);
break;
}
switch (enemyAction)
{
case 0:
enemyDamageValue = rand.Next(20, 30);
break;
case 1:
enemyDamageValue = rand.Next(20, 30);
break;
case 2:
enemyDamageValue = rand.Next(10, 15);
break;
}


//カウンターの処理
switch (playerCounterJudge)
{
case true:
playerAction = 0;
break;

case false:
playerDamageValue = enemyDamageValue;
enemyDamageValue = 0;
break;
}
switch (enemyCounterJudge)
{
case true:
enemyDamageValue = 0;
break;

case false:
enemyDamageValue = playerDamageValue;
playerDamageValue = 0;
break;
}
return (playerDamageValue, enemyDamageValue, playerCounterJudge, enemyCounterJudge);
}

//HP計算
private int HpCalculate(int hp, int action, int damageValue)
{
int _hp = hp;
int _damageValue = damageValue;

//回復だけ区別
if (action == 2)
{
_hp = _hp + _damageValue;
}
else
{
_hp = _hp - _damageValue;
}

if (_hp <= 0)
{
_hp = 0;
}

return _hp;
}

//勝敗判定(1:player勝利、0:継戦、ー1:enemy勝利)
private int BattleJudge(int playerHp, int enemyHp)
{

if (playerHp == 0 && enemyHp == 0)
{
return 1;
}
else if (playerHp == 0)
{
return -1;
}
else if (enemyHp == 0)
{
return 1;
}
else
{
return 0;
}
}

//先行判定
private bool MoveJudge(bool counterJudge)
{
if (counterJudge == true)
{
return false;
}
else
{
return true;
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/BattleSystem.cs.meta

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

23 changes: 23 additions & 0 deletions Assets/Scripts/Enemy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace HackathonA
{
public class Enemy : ICharactar
{
public Enemy(int hp = 100, int actionType = 0, int damageValue = 0, bool counterJudge = false, int enemyType = 0)
{
Hp = hp;
ActionType = actionType;
DamageValue = damageValue;
CounterJudge = counterJudge;
this.EnemyType = enemyType;
}

public int Hp { get; set; }
public int ActionType { get; set; }
public int DamageValue { get; set; }
public bool CounterJudge { get; set; }
public int EnemyType { get; set; }

}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Enemy.cs.meta

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

12 changes: 12 additions & 0 deletions Assets/Scripts/ICharactar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace HackathonA
{
interface ICharactar
{
int Hp { get; set; }
int ActionType { get; set; }
int DamageValue { get; set; }
bool CounterJudge { get; set; }
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/ICharactar.cs.meta

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

Loading

0 comments on commit 6070ad4

Please sign in to comment.