GameManagerScript.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Cinemachine;
  6. using UnityEngine;
  7. using UnityEngine.Tilemaps;
  8. using UnityEngine.UI;
  9. public class GameManagerScript : MonoBehaviour
  10. {
  11. CinemachineBrain camBrain;
  12. RoundManager roundManager;
  13. [SerializeField] List<GameObject> enemies;
  14. [SerializeField] List<GameObject> humans;
  15. [SerializeField] GameObject initPanel;
  16. [SerializeField] GameObject initCreaturePanel;
  17. [SerializeField] Grid grid;
  18. [SerializeField] Tilemap tileMap;
  19. List<Creature> combatants = new List<Creature>();
  20. static GameManagerScript instance;
  21. public static GameManagerScript getInstance()
  22. {
  23. return instance;
  24. }
  25. private void Start()
  26. {
  27. instance = this;
  28. combatants.AddRange(enemies.Select(e => e.GetComponent<Creature>()));
  29. combatants.AddRange(humans.Select(h => h.GetComponent<Creature>()));
  30. }
  31. private void Awake()
  32. {
  33. if (camBrain == null)
  34. {
  35. camBrain = Camera.main.GetComponent<CinemachineBrain>();
  36. }
  37. if (roundManager == null)
  38. {
  39. roundManager = GameObject.Find("RoundManager").GetComponent<RoundManager>();
  40. }
  41. }
  42. public CinemachineVirtualCamera getActiveCamera()
  43. {
  44. CinemachineVirtualCamera cam = camBrain.ActiveVirtualCamera as CinemachineVirtualCamera;
  45. return cam;
  46. }
  47. internal void rollInitiative()
  48. {
  49. combatants.ForEach(creature =>
  50. {
  51. creature.rollInit();
  52. });
  53. combatants.Sort((x, y) => x.getInit().CompareTo(y.getInit()));
  54. clearInitPanel();
  55. buildInitPanel();
  56. roundManager.startFirstRound(combatants);
  57. }
  58. private void clearInitPanel()
  59. {
  60. int children = initPanel.transform.childCount;
  61. for (int i = children - 1; i >= 0; i--)
  62. {
  63. GameObject.Destroy(initPanel.transform.GetChild(i).gameObject);
  64. }
  65. }
  66. public void showInitPanel(Boolean show)
  67. {
  68. initPanel.SetActive(show);
  69. }
  70. internal void buildInitPanel()
  71. {
  72. initPanel.SetActive(true);
  73. combatants.ForEach(c =>
  74. {
  75. GameObject creturePanel = GameObject.Instantiate(initCreaturePanel, Vector3.zero, Quaternion.identity);
  76. creturePanel.transform.localScale = new Vector3(1, 1, 1);
  77. creturePanel.transform.SetParent(initPanel.transform);
  78. creturePanel.GetComponent<InitCreaturePanelScript>().setImage(c.GetComponent<Creature>().getSprite());
  79. Debug.Log(c.name + " init: " + c.GetComponent<Creature>().getInit() + " Dex " + c.GetComponent<Creature>().Dex + " pos: " + grid.WorldToCell(c.transform.localPosition));
  80. c.setPositionInGrid(grid.WorldToCell(c.transform.localPosition));
  81. });
  82. }
  83. public List<Creature> getCombatants()
  84. {
  85. return combatants;
  86. }
  87. }