GameManagerScript.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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] GameObject logPanel;
  18. [SerializeField] TileBase hilightTile;
  19. [SerializeField] Grid grid;
  20. [SerializeField] Tilemap tileMap;
  21. [SerializeField] Tilemap hilightTileMap;
  22. [SerializeField] LineRenderer lineRenderer;
  23. public Tilemap TileMap { get => tileMap; set => tileMap = value; }
  24. public Tilemap HightlightTileMap { get => hilightTileMap; set => hilightTileMap = value; }
  25. public TileBase HighlightTileBase { get => hilightTile; set => hilightTile = value; }
  26. public Grid Grid { get => grid; set => grid = value; }
  27. List<Creature> combatants = new List<Creature>();
  28. static GameManagerScript instance;
  29. public static GameManagerScript GetInstance() {
  30. return instance;
  31. }
  32. public LineRenderer GetLineRenderer() {
  33. return lineRenderer;
  34. }
  35. private void Start() {
  36. instance = this;
  37. combatants.AddRange(enemies.Select(e => e.GetComponent<Creature>()));
  38. combatants.AddRange(humans.Select(h => h.GetComponent<Creature>()));
  39. }
  40. private void Awake() {
  41. if (camBrain == null) {
  42. camBrain = Camera.main.GetComponent<CinemachineBrain>();
  43. }
  44. if (roundManager == null) {
  45. roundManager = GameObject.Find("RoundManager").GetComponent<RoundManager>();
  46. }
  47. }
  48. public CinemachineVirtualCamera GetActiveCamera() {
  49. CinemachineVirtualCamera cam = camBrain.ActiveVirtualCamera as CinemachineVirtualCamera;
  50. return cam;
  51. }
  52. internal void RollInitiative() {
  53. combatants.ForEach(creature => {
  54. creature.RollInit();
  55. });
  56. combatants.Sort((x, y) => x.GetInit().CompareTo(y.GetInit()));
  57. ClearInitPanel();
  58. BuildInitPanel();
  59. ShowRoundActionsPanel(true);
  60. ActivateLogPanel(true);
  61. roundManager.StartFirstRound(combatants);
  62. }
  63. private void ActivateLogPanel(bool value) {
  64. logPanel.SetActive(value);
  65. }
  66. private void ShowRoundActionsPanel(bool show) {
  67. roundActionsPanel.SetActive(show);
  68. }
  69. private void ClearInitPanel() {
  70. int children = initiativePanel.transform.childCount;
  71. for (int i = children - 1; i >= 0; i--) {
  72. GameObject.Destroy(initiativePanel.transform.GetChild(i).gameObject);
  73. }
  74. }
  75. public void ShowInitPanel(Boolean show) {
  76. initiativePanel.SetActive(show);
  77. }
  78. internal void BuildInitPanel() {
  79. initiativePanel.SetActive(true);
  80. combatants.ForEach(c => {
  81. GameObject creaturePanel = GameObject.Instantiate(initiativeCreaturePanel, Vector3.zero, Quaternion.identity);
  82. creaturePanel.transform.localScale = new Vector3(1, 1, 1);
  83. creaturePanel.transform.SetParent(initiativePanel.transform);
  84. creaturePanel.GetComponent<InitCreaturePanelScript>().SetImage(c.GetComponent<Creature>().GetSprite());
  85. Debug.Log(c.name + " init: " + c.GetComponent<Creature>().GetInit() + " Dex " + c.GetComponent<Creature>().Dex +
  86. " pos: " + Grid.WorldToCell(c.transform.localPosition));
  87. c.InitPanelGameObject = creaturePanel;
  88. c.SetPositionInGrid(Grid.WorldToCell(c.transform.localPosition));
  89. });
  90. }
  91. public List<Creature> GetCombatants() {
  92. return combatants;
  93. }
  94. public void LogRow(string text) {
  95. logPanel.GetComponent<LogPanelScript>().AddText(text);
  96. }
  97. }