Player.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. [System.Serializable]
  6. public class player_data
  7. {
  8. public int STR;
  9. public int DEX;
  10. public int VIT;
  11. public int INT;
  12. public int minStr { get { return STR / 6 + weapon_lv*2; } }
  13. public int maxStr { get { return STR / 4 + weapon_lv; } }
  14. public int maxvit { get { return VIT * 3 + 3; } }
  15. public int Def { get { return DEX / 3; } }
  16. public int weapon_lv;
  17. public int weapon_exp;
  18. public int weapon_nextexp { get { return (((int)Mathf.Pow((weapon_lv - 1), 3)) + 15) / 5 * ((weapon_lv - 1) * 2 + 20) + (10 - ((((int)Mathf.Pow((weapon_lv - 1), 3)) + 15) / 5 * ((weapon_lv - 1) * 2 + 20) % 10)) + (weapon_lv - 1) * 30; } }
  19. public int weapon_nextprice { get { return weapon_lv * 2 + 20; } }
  20. public float weapon_upexp { get { return (weapon_nextexp * 0.1f); } }
  21. }
  22. public class Player : MonoBehaviour {
  23. Animator anim;
  24. private bool isalive;
  25. public bool IsAlive { get { return isalive; } }
  26. private int maxHP;
  27. private int currHP;
  28. [SerializeField]
  29. private Text hp_text;
  30. [SerializeField]
  31. private Image hp_image;
  32. [SerializeField]
  33. private int level;
  34. [SerializeField]
  35. private Text level_text;
  36. public int Level { get { return level; } }
  37. private float currExp;
  38. private float nextExp;
  39. [SerializeField]
  40. private Text nextexp_text;
  41. [SerializeField]
  42. private int point;
  43. public int Point { get { return point; } set { point += value; } }
  44. [SerializeField]
  45. private Text point_text;
  46. [SerializeField]
  47. player_data data;
  48. private Enemy target;
  49. public player_data PlayerData { get { return data; } }
  50. public int Price { get; set; }
  51. [SerializeField]
  52. private Text price_txt;
  53. public bool StartFlag { get; set; }
  54. void Start () {
  55. StartFlag = false;
  56. anim = GetComponent<Animator>();
  57. maxHP = data.maxvit;
  58. currHP = maxHP;
  59. isalive = true;
  60. point_text.text = "ポイント:" + point;
  61. nextExp = (((int)Mathf.Pow((level - 1), 3)) + 15) / 5 * ((level - 1) * 2 + 20) + (10 - ((((int)Mathf.Pow((level - 1), 3)) + 15) / 5 * ((level - 1) * 2 + 20) % 10)) + (level - 1) * 30;
  62. StartCoroutine(Info_Updata());
  63. }
  64. public void start()
  65. {
  66. anim.SetBool("Start",true);
  67. StageController.instance.mapMoveStart();
  68. StartFlag = true;
  69. }
  70. void HP_check()
  71. {
  72. maxHP = data.maxvit;
  73. currHP = currHP > maxHP ? maxHP : currHP;
  74. currHP = currHP <= 0 ? 0 : currHP;
  75. if (currHP <= 0 && isalive) {
  76. isalive = false;
  77. anim.SetBool("Dead", true);
  78. StartCoroutine(ReSet());
  79. }
  80. }
  81. IEnumerator ReSet()
  82. {
  83. yield return new WaitForSeconds(3);
  84. isalive = true;
  85. currHP = maxHP;
  86. anim.SetBool("Dead", false);
  87. anim.SetBool("Battle", false);
  88. StageController.instance.MapReset();
  89. StageController.instance.mapMoveStart();
  90. }
  91. public void Damage(int damage)
  92. {
  93. int d = damage - data.Def;
  94. Debug.Log(d);
  95. if (d > 0) currHP -= d;
  96. else currHP -= 1;
  97. anim.SetTrigger("Damage");
  98. HP_check();
  99. }
  100. IEnumerator Info_Updata()
  101. {
  102. while (true)
  103. {
  104. HP_check();
  105. hp_image.fillAmount = (float)currHP / (float)maxHP;
  106. hp_text.text = currHP + " / " + maxHP;
  107. level_text.text = level.ToString();
  108. nextexp_text.text = "Next Level: " + currExp + " / " + nextExp;
  109. point_text.text = "ポイント:" + point;
  110. price_txt.text = "Coin:" + Price;
  111. yield return null;
  112. }
  113. }
  114. IEnumerator Attack()
  115. {
  116. while (isalive && target.IsAlive)
  117. {
  118. int index = Random.Range(0, 2);
  119. switch (index)
  120. {
  121. case 0:
  122. anim.SetTrigger("Attack");
  123. break;
  124. case 1:
  125. anim.SetTrigger("Attack2");
  126. break;
  127. }
  128. target.Damage(Random.Range(data.minStr, data.maxStr));
  129. yield return new WaitForSeconds(1);
  130. }
  131. if (isalive)
  132. {
  133. ExpUp(target.Exp);
  134. anim.SetBool("Battle", false);
  135. StageController.instance.mapMoveStart();
  136. }
  137. }
  138. void ExpUp(int exp)
  139. {
  140. currExp += exp;
  141. if (currExp >= nextExp)
  142. {
  143. StartCoroutine(LevelUp());
  144. }
  145. }
  146. IEnumerator LevelUp()
  147. {
  148. while (currExp >= nextExp)
  149. {
  150. level += 1;
  151. point += 5;
  152. currExp -= nextExp;
  153. currHP = maxHP;
  154. nextExp = (((int)Mathf.Pow((level - 1), 3)) + 15) / 5 * ((level - 1) * 2 + 20) + (10 - ((((int)Mathf.Pow((level - 1), 3)) + 15) / 5 * ((level - 1) * 2 + 20) % 10)) + (level - 1) * 30;
  155. yield return null;
  156. }
  157. }
  158. public void HpPlus()
  159. {
  160. int lossHP = maxHP - currHP;
  161. if(Price >= lossHP * 10)
  162. {
  163. Price -= lossHP * 10;
  164. currHP += lossHP;
  165. }
  166. else
  167. {
  168. int cure = Price / 10;
  169. Price -= cure * 10;
  170. currHP += cure;
  171. }
  172. }
  173. private void OnTriggerEnter(Collider other)
  174. {
  175. if (other.tag == "Enemy")
  176. {
  177. anim.SetBool("Battle", true);
  178. StageController.instance.mapStop();
  179. target = other.GetComponent<Enemy>();
  180. StartCoroutine(Attack());
  181. }
  182. }
  183. }