GameManagerScript.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 initiativePanel;
  15. [SerializeField] GameObject initiativeCreaturePanel;
  16. [SerializeField] GameObject roundActionsPanel;
  17. [SerializeField] Grid grid;
  18. [SerializeField] Tilemap tileMap;
  19. public Tilemap TileMap { get => tileMap; set => tileMap = value; }
  20. public Grid Grid { get => grid; set => grid = value; }
  21. List<Creature> combatants = new List<Creature>();
  22. static GameManagerScript instance;
  23. public static GameManagerScript GetInstance() {
  24. return instance;
  25. }
  26. private void Start() {
  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. if (camBrain == null) {
  33. camBrain = Camera.main.GetComponent<CinemachineBrain>();
  34. }
  35. if (roundManager == null) {
  36. roundManager = GameObject.Find("RoundManager").GetComponent<RoundManager>();
  37. }
  38. }
  39. public CinemachineVirtualCamera GetActiveCamera() {
  40. CinemachineVirtualCamera cam = camBrain.ActiveVirtualCamera as CinemachineVirtualCamera;
  41. return cam;
  42. }
  43. internal void RollInitiative() {
  44. combatants.ForEach(creature => {
  45. creature.RollInit();
  46. });
  47. combatants.Sort((x, y) => x.GetInit().CompareTo(y.GetInit()));
  48. ClearInitPanel();
  49. BuildInitPanel();
  50. ShowRoundActionsPanel(true);
  51. roundManager.StartFirstRound(combatants);
  52. }
  53. private void ShowRoundActionsPanel(bool show) {
  54. roundActionsPanel.SetActive(show);
  55. }
  56. private void ClearInitPanel() {
  57. int children = initiativePanel.transform.childCount;
  58. for (int i = children - 1; i >= 0; i--) {
  59. GameObject.Destroy(initiativePanel.transform.GetChild(i).gameObject);
  60. }
  61. }
  62. public void ShowInitPanel(Boolean show) {
  63. initiativePanel.SetActive(show);
  64. }
  65. internal void BuildInitPanel() {
  66. initiativePanel.SetActive(true);
  67. combatants.ForEach(c => {
  68. GameObject creaturePanel = GameObject.Instantiate(initiativeCreaturePanel, Vector3.zero, Quaternion.identity);
  69. creaturePanel.transform.localScale = new Vector3(1, 1, 1);
  70. creaturePanel.transform.SetParent(initiativePanel.transform);
  71. creaturePanel.GetComponent<InitCreaturePanelScript>().SetImage(c.GetComponent<Creature>().GetSprite());
  72. Debug.Log(c.name + " init: " + c.GetComponent<Creature>().GetInit() + " Dex " + c.GetComponent<Creature>().Dex +
  73. " pos: " + Grid.WorldToCell(c.transform.localPosition));
  74. c.InitPanelGameObject = creaturePanel;
  75. c.SetPositionInGrid(Grid.WorldToCell(c.transform.localPosition));
  76. });
  77. }
  78. public List<Creature> GetCombatants() {
  79. return combatants;
  80. }
  81. }