RoundManager.cs 3.6 KB

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