RoundManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class RoundManager : MonoBehaviour {
  8. [SerializeField] Button executeRoundButton;
  9. private List<Creature> combatants;
  10. int round;
  11. private bool executing = false;
  12. public Creature activeCreature;
  13. [SerializeField] GameObject decitionButtonsPanel;
  14. [SerializeField] Button moveActionButton;
  15. [SerializeField] Button attackActionButton;
  16. private bool movingAction;
  17. private List<Vector3> highlightedTiles;
  18. static RoundManager instance;
  19. private void Start() {
  20. executeRoundButton.onClick.AddListener(ExecuteRound);
  21. moveActionButton.onClick.AddListener(CreateMoveAction);
  22. attackActionButton.onClick.AddListener(CreateAttackAction);
  23. instance = this;
  24. }
  25. public static RoundManager GetInstance() {
  26. return instance;
  27. }
  28. private void CreateAttackAction() {
  29. }
  30. private void CreateMoveAction() {
  31. movingAction = true;
  32. MoveAction ma = new MoveAction();
  33. ma.SetStartPosition(activeCreature.transform.position);
  34. if (!activeCreature.HasFirstAction()) {
  35. activeCreature.SetFirstAction(ma);
  36. } else {
  37. activeCreature.SetSecondAction(ma);
  38. }
  39. GameManagerScript.GetInstance().Grid.GetComponent<GridController>().HilightAction = ma;
  40. }
  41. private void ExecuteRound() {
  42. executing = true;
  43. executeRoundButton.interactable = false;
  44. foreach (Creature c in combatants) {
  45. StartCoroutine(c.GetFirstAction().PerformAction());
  46. }
  47. round++;
  48. }
  49. private void FixedUpdate() {
  50. if (executing) {
  51. foreach (Creature c in combatants) {
  52. c.CurrentPos = Vector3Int.RoundToInt(c.gameObject.transform.position);
  53. }
  54. }
  55. if (movingAction) {
  56. LineRenderer lineRenderer = GameManagerScript.GetInstance().GetLineRenderer();
  57. lineRenderer.startWidth = 0.01f;
  58. lineRenderer.endWidth = 0.01f;
  59. Vector3 mousePos = Input.mousePosition;
  60. mousePos.z = Camera.main.nearClipPlane;
  61. Vector3 realMousePos = Camera.main.ScreenToWorldPoint(mousePos);
  62. lineRenderer.SetPosition(0, activeCreature.transform.position);
  63. lineRenderer.SetPosition(1, realMousePos);
  64. }
  65. }
  66. internal void StartFirstRound(List<Creature> combatants) {
  67. this.combatants = combatants;
  68. round = 1;
  69. MakeDecisions();
  70. }
  71. internal void SetupNextRound() {
  72. this.combatants = combatants.FindAll(c => c.IsCreatureAlive);
  73. MakeDecisions();
  74. }
  75. private void MakeDecisions() {
  76. foreach (Creature creature in combatants) {
  77. if (creature.IsHumanControlled()) {
  78. CameraManager.GetInstance().FocusOnGameObject(creature.gameObject);
  79. activeCreature = creature;
  80. StartCoroutine(creature.WaitForPlayerAction());
  81. // Wait for human decision
  82. } else {
  83. creature.DecideActions(combatants);
  84. // AI make decision
  85. }
  86. }
  87. }
  88. private void Update() {
  89. if (CheckIfAllCombatantsHaveTheirActions()) {
  90. executeRoundButton.interactable = true;
  91. }
  92. if (activeCreature != null && activeCreature.IsHumanControlled()) {
  93. decitionButtonsPanel.SetActive(true);
  94. } else {
  95. decitionButtonsPanel.SetActive(false);
  96. }
  97. }
  98. private bool CheckIfAllCombatantsHaveTheirActions() {
  99. return combatants != null && combatants.All(c => c.HasFirstAction() && c.HasSecondAction());
  100. }
  101. }