GameManagerScript.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. CinemachineBrain camBrain;
  11. RoundManager roundManager;
  12. [SerializeField] List<GameObject> enemies;
  13. [SerializeField] List<GameObject> humans;
  14. [SerializeField] GameObject initPanel;
  15. [SerializeField] GameObject initCreaturePanel;
  16. [SerializeField] Grid grid;
  17. [SerializeField] Tilemap tileMap;
  18. List<Creature> combatants = new();
  19. static GameManagerScript instance;
  20. public static GameManagerScript GetInstance() {
  21. return instance;
  22. }
  23. private void Start() {
  24. instance = this;
  25. combatants.AddRange(enemies.Select(e => e.GetComponent<Creature>()));
  26. combatants.AddRange(humans.Select(h => h.GetComponent<Creature>()));
  27. }
  28. private void Awake() {
  29. if (camBrain == null) {
  30. camBrain = Camera.main.GetComponent<CinemachineBrain>();
  31. }
  32. if (roundManager == null) {
  33. roundManager = GameObject.Find("RoundManager").GetComponent<RoundManager>();
  34. }
  35. }
  36. public CinemachineVirtualCamera GetActiveCamera() {
  37. CinemachineVirtualCamera cam = camBrain.ActiveVirtualCamera as CinemachineVirtualCamera;
  38. return cam;
  39. }
  40. internal void RollInitiative() {
  41. combatants.ForEach(creature => {
  42. creature.RollInit();
  43. });
  44. combatants.Sort((x, y) => x.GetInit().CompareTo(y.GetInit()));
  45. ClearInitPanel();
  46. BuildInitPanel();
  47. roundManager.StartFirstRound(combatants);
  48. }
  49. private void ClearInitPanel() {
  50. int children = initPanel.transform.childCount;
  51. for (int i = children - 1; i >= 0; i--) {
  52. GameObject.Destroy(initPanel.transform.GetChild(i).gameObject);
  53. }
  54. }
  55. public void ShowInitPanel(Boolean show) {
  56. initPanel.SetActive(show);
  57. }
  58. internal void BuildInitPanel() {
  59. initPanel.SetActive(true);
  60. combatants.ForEach(c => {
  61. GameObject creturePanel = GameObject.Instantiate(initCreaturePanel, Vector3.zero, Quaternion.identity);
  62. creturePanel.transform.localScale = new Vector3(1, 1, 1);
  63. creturePanel.transform.SetParent(initPanel.transform);
  64. creturePanel.GetComponent<InitCreaturePanelScript>().SetImage(c.GetComponent<Creature>().GetSprite());
  65. Debug.Log(c.name + " init: " + c.GetComponent<Creature>().GetInit() + " Dex " + c.GetComponent<Creature>().Dex + " pos: " + grid.WorldToCell(c.transform.localPosition));
  66. c.SetPositionInGrid(grid.WorldToCell(c.transform.localPosition));
  67. });
  68. }
  69. public List<Creature> GetCombatants() {
  70. return combatants;
  71. }
  72. }