RoundManager.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using RPG_Fight_Test.Assets.Scripts.Creatures;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public class RoundManager : MonoBehaviour {
  9. [SerializeField] Button executeRoundButton;
  10. private List<Creature> combatants;
  11. int round;
  12. private bool executing = false;
  13. private void Start() {
  14. executeRoundButton.onClick.AddListener(ExecuteRound);
  15. }
  16. private void ExecuteRound() {
  17. executing = true;
  18. executeRoundButton.interactable = false;
  19. foreach (Creature c in combatants) {
  20. StartCoroutine(c.GetFirstAction().PerformAction());
  21. }
  22. round++;
  23. }
  24. private void FixedUpdate() {
  25. if (executing) {
  26. foreach (Creature c in combatants) {
  27. c.CurrentPos = Vector3Int.RoundToInt(c.gameObject.transform.position);
  28. }
  29. }
  30. }
  31. internal void StartFirstRound(List<Creature> combatants) {
  32. this.combatants = combatants;
  33. round = 1;
  34. MakeDecisions();
  35. }
  36. internal void SetupNextRound() {
  37. this.combatants = combatants.FindAll(c => c.IsCreatureAlive);
  38. MakeDecisions();
  39. }
  40. private void MakeDecisions() {
  41. foreach (Creature creature in combatants) {
  42. if (creature.IsHumanControlled()) {
  43. CameraManager.GetInstance().FocusOnGameObject(creature.gameObject);
  44. // TODO REMOVE testing
  45. if (creature.name.Equals("Human2")) {
  46. MoveAction ma = new MoveAction();
  47. ma.SetTargetPosition(new Vector3(2.5f, -27.5f, 0f));
  48. ma.SetStartPosition(creature.CurrentPos);
  49. ma.SetCreatureToMove(creature);
  50. creature.SetFirstAction(ma);
  51. } else {
  52. creature.SetFirstAction(new AttackAction(combatants[0]));
  53. }
  54. creature.SetSecondAction(new AttackAction(combatants[0])); // TODO REMOVE testing
  55. // Wait for human decision
  56. } else {
  57. ((Skeleton)creature).DecideActions(combatants); // FIXME Skeleton won't work.. need to be more general
  58. // AI make decision
  59. }
  60. }
  61. if (CheckIfAllCombatantsHaveTheirActions()) {
  62. executeRoundButton.interactable = true;
  63. }
  64. }
  65. private bool CheckIfAllCombatantsHaveTheirActions() {
  66. return combatants.All(c => c.HasFirstAction() && c.HasSecondAction());
  67. }
  68. }