PositionGridManager.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. public class PositionGridManager : MonoBehaviour
  6. {
  7. public int gridWidth = 5;
  8. public int gridHeight = 5;
  9. public List<CharacterInGroup> characters;
  10. public GameObject gridCellPrefab;
  11. public Transform gridParent;
  12. private CharacterInGroup selectedCharacter;
  13. void Start()
  14. {
  15. GenerateGrid();
  16. UpdateGridDisplay();
  17. }
  18. void GenerateGrid()
  19. {
  20. for (int x = 0; x < gridWidth; x++)
  21. {
  22. for (int y = 0; y < gridHeight; y++)
  23. {
  24. GameObject cell = Instantiate(gridCellPrefab, gridParent);
  25. GridCell cellComponent = cell.GetComponent<GridCell>();
  26. cellComponent.Setup(this, x, y);
  27. }
  28. }
  29. }
  30. public void OnCellClicked(int x, int y)
  31. {
  32. if (selectedCharacter != null)
  33. {
  34. selectedCharacter.gridX = x;
  35. selectedCharacter.gridY = y;
  36. selectedCharacter = null;
  37. UpdateGridDisplay();
  38. }
  39. else
  40. {
  41. CharacterInGroup character = characters.Find(c => c.gridX == x && c.gridY == y);
  42. if (character != null)
  43. {
  44. selectedCharacter = character;
  45. }
  46. }
  47. }
  48. void UpdateGridDisplay()
  49. {
  50. GridCell[] cells = gridParent.GetComponentsInChildren<GridCell>();
  51. foreach (var cell in cells)
  52. {
  53. CharacterInGroup character = characters.Find(c => c.gridX == cell.gridX && c.gridY == cell.gridY);
  54. if (character != null)
  55. {
  56. cell.SetLabel(character.characterName);
  57. }
  58. else
  59. {
  60. cell.SetLabel("");
  61. }
  62. }
  63. }
  64. public void AddCharacter(CharacterInGroup c)
  65. {
  66. if (!characters.Contains(c))
  67. characters.Add(c);
  68. UpdateGridDisplay();
  69. }
  70. public CharacterInGroup GetFrontlineTarget(Vector3 attackerDirection)
  71. {
  72. int line = attackerDirection.z > 0 ? 0 : gridHeight - 1;
  73. List<CharacterInGroup> frontline = characters.FindAll(c => c.gridY == line);
  74. if (frontline.Count > 0)
  75. return frontline[0]; // ici, on prend le premier pour simplifier
  76. return null;
  77. }
  78. public float GetPositionBonus(CharacterInGroup character)
  79. {
  80. if (character.characterType == CharacterInGroup.CharacterType.Warrior && character.gridY > 1)
  81. return -0.5f; // Malus Guerrier derrière
  82. if (character.characterType == CharacterInGroup.CharacterType.Priest && character.gridY == 0)
  83. return -0.3f; // Malus Prêtre en front
  84. return 0f; // Aucun bonus/malus
  85. }
  86. #if UNITY_EDITOR
  87. private void OnDrawGizmos()
  88. {
  89. Gizmos.color = Color.cyan;
  90. for (int x = 0; x < gridWidth; x++)
  91. {
  92. for (int y = 0; y < gridHeight; y++)
  93. {
  94. Gizmos.DrawWireCube(transform.position + new Vector3(x, 0, y), Vector3.one);
  95. }
  96. }
  97. }
  98. #endif
  99. }