Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5.  
  6. public class Battle : MonoBehaviour
  7. {
  8. AudioManager AM;
  9. public GameObject BattleSystem;
  10. public GameObject UIHandle;
  11. public GameObject PlayerControls;
  12. public GameObject BattlePlayer;
  13. public TextMeshProUGUI PlayerHealthBox;
  14. public TextMeshProUGUI DamageBox;
  15. public Sprite MorningBattle;
  16. public Sprite EveningBattle;
  17. public List<CombatEnemy> Enemies = new List<CombatEnemy>();
  18. public static bool inbattle = false;
  19. public Transform[] SpawnPoints;
  20. List<CombatEnemy> Spawned = new List<CombatEnemy>();
  21. List<TurnScheduleItem> turnSchedule = new List<TurnScheduleItem>();
  22. PlayerMovement player;
  23. HeartSystem PH;
  24. bool IsAttacking = false;
  25. int AttackingTarget;
  26. int Attacker;
  27. float AttackTimer;
  28. bool HasTakenDmg;
  29. public Camera BattleCam;
  30. void Start()
  31. {
  32. AM = AudioManager.instance;
  33. player = GameObject.FindObjectOfType<PlayerMovement>();
  34. PH = player.GetComponent<HeartSystem>();
  35. }
  36.  
  37. // Update is called once per frame
  38. void Update()
  39. {
  40. AttackTimer += Time.deltaTime;
  41. if (Input.GetKeyDown(KeyCode.L))
  42. {
  43. Encounter(0);
  44. }
  45.  
  46. PlayerHealthBox.text = PH.curHealth.ToString();
  47.  
  48. if (!inbattle)
  49. {
  50. return;
  51. }
  52. if(turnSchedule.Count <= 0)
  53. {
  54. BattleSchedule();
  55. }
  56. if (!IsAttacking)
  57. {
  58. TurnScheduleItem currentTurn = turnSchedule[0];
  59. if (currentTurn.index != -1)
  60. {
  61.  
  62. PlayerControls.SetActive(false);
  63. StartAttack(currentTurn.index,-1);
  64. }
  65. else
  66. {
  67. PlayerControls.SetActive(true);
  68. }
  69. }
  70. if(AttackTimer > 2 && !HasTakenDmg && IsAttacking)
  71. {
  72. HasTakenDmg = true;
  73. Debug.Log("Attacking " + AttackingTarget);
  74. if (AttackingTarget == -1)
  75. {
  76. PH.TakeDamage(1);
  77. DamageBox.text = "-1";
  78. DamageBox.gameObject.SetActive(true);
  79. DamageBox.transform.position = BattleCam.WorldToScreenPoint(BattlePlayer.transform.position + new Vector3(0,0.5f,0));
  80. }
  81. else
  82. {
  83. Spawned[AttackingTarget].HP -= 5;
  84. Debug.Log("Enemy down to " + Spawned[AttackingTarget].HP);
  85. DamageBox.text = "-5";
  86. DamageBox.gameObject.SetActive(true);
  87. DamageBox.transform.position = BattleCam.WorldToScreenPoint(Spawned[AttackingTarget].transform.position + new Vector3(0, 0.5f, 0));
  88.  
  89. if (Spawned[AttackingTarget].HP <= 0)
  90. {
  91. Destroy(Spawned[AttackingTarget].gameObject);
  92. Spawned.RemoveAt(AttackingTarget);
  93. foreach(var foo in turnSchedule)
  94. {
  95. if(foo.index == AttackingTarget)
  96. {
  97. turnSchedule.Remove(foo);
  98. break;
  99. }
  100. }
  101. Debug.Log("Enemy Died");
  102.  
  103. if (Spawned.Count <= 0)
  104. {
  105. EndBattle();
  106. }
  107. }
  108. }
  109. }
  110. DamageBox.color = new Color(1, 1, 1, 3 - AttackTimer);
  111. if(AttackTimer > 5 && IsAttacking)
  112. {
  113. Debug.Log("Attack Over " + Attacker);
  114. turnSchedule.RemoveAt(0);
  115. IsAttacking = false;
  116. }
  117. }
  118.  
  119. public void Encounter(int kind)
  120. {
  121. UIHandle.SetActive(false);
  122. inbattle = true;
  123. BattleSystem.SetActive(true);
  124. int enemyCount = Random.Range(1, 3);
  125. for (int j = 0; j < enemyCount; j++)
  126. {
  127. Spawned.Add(Instantiate(Enemies[kind], SpawnPoints[j].position, Quaternion.identity));
  128. }
  129. BattleSchedule();
  130.  
  131. }
  132. //Enemies.Add(Instantiate(Enemies[Type], SpawnPoints[0].position, Quaternion.identity));
  133.  
  134. //for (int i = 0; i < SpawnPoints.Length - 1; i++)
  135. //{
  136. // for (int j = 1; j < Random.Range(1, 2); j++)
  137. // {
  138. // //Vector3 position = SpawnPoints[i].position + j * (SpawnPoints[i + 1].position - SpawnPoints[i].position) / j;
  139.  
  140. // }
  141.  
  142. //}
  143. private void StartAttack(int Attackerin, int AttackTargetin)
  144. {
  145. Attacker = Attackerin;
  146. AttackingTarget = AttackTargetin;
  147. AttackTimer = 0;
  148. IsAttacking = true;
  149. HasTakenDmg = false;
  150. PlayerControls.SetActive(false);
  151. Debug.Log("Attack started " + Attacker + " " + AttackingTarget);
  152.  
  153.  
  154. }
  155.  
  156. private void EndBattle()
  157. {
  158. inbattle = false;
  159. BattleSystem.SetActive(false);
  160. UIHandle.SetActive(true);
  161. }
  162. internal void CloseUI()
  163. {
  164. BattleSystem.SetActive(false);
  165.  
  166. }
  167. public void Attack()
  168. {
  169. if (turnSchedule[0].index != -1)
  170. {
  171. return;
  172. }
  173. if (IsAttacking)
  174. return;
  175.  
  176. int AttackIndex = Random.Range(0, Spawned.Count);
  177. StartAttack(-1, AttackIndex);
  178.  
  179. Debug.Log("You struck the enemy.");
  180.  
  181. }
  182.  
  183.  
  184.  
  185. private void Abilities()
  186. {
  187.  
  188. }
  189.  
  190.  
  191.  
  192. private void Item()
  193. {
  194.  
  195. }
  196.  
  197. private void Escape()
  198. {
  199. EndBattle();
  200. }
  201.  
  202. public void BattleSchedule()
  203. {
  204. turnSchedule.Clear();
  205. turnSchedule.Add(new TurnScheduleItem(-1,player.combatSpeed));
  206. for(int i = 0; i < Spawned.Count; i++)
  207. {
  208. turnSchedule.Add(new TurnScheduleItem(i, Spawned[i].Speed));
  209. }
  210. turnSchedule.Sort(new TurnSort());
  211.  
  212. }
  213.  
  214.  
  215. }
  216.  
  217. class TurnScheduleItem
  218. {
  219. public int index;
  220. public float speed;
  221.  
  222. public TurnScheduleItem(int i, float s)
  223. {
  224. index = i;
  225. speed = s;
  226.  
  227. }
  228. }
  229.  
  230. class TurnSort : IComparer<TurnScheduleItem>
  231. {
  232. public int Compare(TurnScheduleItem x, TurnScheduleItem y)
  233. {
  234. if (x.speed > y.speed)
  235. return -1;
  236. if (x.speed < y.speed)
  237. return 1;
  238. return 0;
  239. }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement