PositionGridManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. Debug.Log("Personnage Ajouté");
  67. if (!characters.Contains(c))
  68. characters.Add(c);
  69. UpdateGridDisplay(); // bonus : met à jour la grille visuelle !
  70. }
  71. public CharacterInGroup GetFrontlineTarget(Vector3 attackerDirection)
  72. {
  73. int line = attackerDirection.z > 0 ? 0 : gridHeight - 1;
  74. List<CharacterInGroup> frontline = characters.FindAll(c => c.gridY == line);
  75. if (frontline.Count > 0)
  76. return frontline[0]; // ici, on prend le premier pour simplifier
  77. return null;
  78. }
  79. public float GetPositionBonus(CharacterInGroup character)
  80. {
  81. if (character.characterType == CharacterInGroup.CharacterType.Warrior && character.gridY > 1)
  82. return -0.5f; // Malus Guerrier derrière
  83. if (character.characterType == CharacterInGroup.CharacterType.Priest && character.gridY == 0)
  84. return -0.3f; // Malus Prêtre en front
  85. return 0f; // Aucun bonus/malus
  86. }
  87. #if UNITY_EDITOR
  88. private void OnDrawGizmos()
  89. {
  90. Gizmos.color = Color.cyan;
  91. for (int x = 0; x < gridWidth; x++)
  92. {
  93. for (int y = 0; y < gridHeight; y++)
  94. {
  95. Gizmos.DrawWireCube(transform.position + new Vector3(x, 0, y), Vector3.one);
  96. }
  97. }
  98. }
  99. #endif
  100. }