Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Assets/Scripts/UI/PlayerHealth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ public class PlayerHealth : MonoBehaviour
public int maxHealth = 5; // Points de vie maximum
private int currentHealth;

public GameObject heartPrefab;
public Transform healthBar;
public GameObject heartPrefab;
public Transform healthBar;
private List<GameObject> hearts = new List<GameObject>();


private SocketIOUnity clientSocket;

public GameObject deadCanvas;

void Start()
{

currentHealth = maxHealth;
UpdateHealthBar();

Expand Down Expand Up @@ -53,32 +53,32 @@ public void Heal(int amount)
UpdateHealthBar();
}

async void SocketEmitter()
async void SocketEmitter()
{
await clientSocket.EmitAsync("rooms:end");
await clientSocket.EmitAsync("rooms:death");
}
Comment on lines +56 to 59
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ajout nécessaire de la gestion des erreurs

L'émission de socket devrait inclure une gestion des erreurs pour éviter les problèmes de connexion silencieux.

     async void SocketEmitter()
     {
-        await clientSocket.EmitAsync("rooms:death");
+        try {
+            await clientSocket.EmitAsync("rooms:death");
+        } catch (System.Exception e) {
+            Debug.LogError($"Erreur lors de l'émission de l'événement death: {e.Message}");
+        }
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async void SocketEmitter()
{
await clientSocket.EmitAsync("rooms:end");
await clientSocket.EmitAsync("rooms:death");
}
async void SocketEmitter()
{
try {
await clientSocket.EmitAsync("rooms:death");
} catch (System.Exception e) {
Debug.LogError($"Erreur lors de l'émission de l'événement death: {e.Message}");
}
}


void Die()
{

if (deadCanvas != null && !deadCanvas.activeSelf)
{
SocketEmitter();
deadCanvas.SetActive(true);
}
GetComponent<PlayerMovement>().enabled = false;
}
}
Comment on lines 62 to +70
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Vérification nécessaire de clientSocket

Il faut vérifier que clientSocket n'est pas null avant d'appeler SocketEmitter pour éviter une NullReferenceException.

     void Die()
     {
         if (deadCanvas != null && !deadCanvas.activeSelf)
         {
-            SocketEmitter();
+            if (clientSocket != null)
+                SocketEmitter();
+            else
+                Debug.LogWarning("Socket client non initialisé");
             deadCanvas.SetActive(true);
         }
         GetComponent<PlayerMovement>().enabled = false;
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{
if (deadCanvas != null && !deadCanvas.activeSelf)
{
SocketEmitter();
deadCanvas.SetActive(true);
}
GetComponent<PlayerMovement>().enabled = false;
}
}
{
if (deadCanvas != null && !deadCanvas.activeSelf)
{
if (clientSocket != null)
SocketEmitter();
else
Debug.LogWarning("Socket client non initialisé");
deadCanvas.SetActive(true);
}
GetComponent<PlayerMovement>().enabled = false;
}


void UpdateHealthBar()
{

foreach (GameObject heart in hearts)
{
Destroy(heart);
}
hearts.Clear();


Comment on lines +74 to +81
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Optimisation possible avec l'Object Pooling

La création et destruction répétée des cœurs pourrait être optimisée en utilisant l'Object Pooling, une pratique recommandée dans Unity pour les objets fréquemment instanciés/détruits.

Je peux vous aider à implémenter un système d'Object Pooling si vous le souhaitez.

for (int i = 0; i < currentHealth; i++)
{
GameObject newHeart = Instantiate(heartPrefab, healthBar);
Expand All @@ -94,7 +94,7 @@ void OnCollisionEnter2D(Collision2D collision)
}
}

public void ReturnToMenu()
public void ReturnToMenu()
{
GameObject socket = GameObject.FindWithTag("Temporary");
if (socket != null) Destroy(socket.gameObject);
Expand Down