Add SetDuration method and enhance command handling in TaskTimer
- Introduced SetDuration method to allow setting the timer duration when not running. - Updated ProcessTimerCommand to handle ADDTIME and SETDURATION commands with appropriate validation. - Improved logging for command processing and added warnings for invalid inputs.
This commit is contained in:
@ -1,6 +1,5 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.SceneManagement;
|
||||
using TMPro;
|
||||
|
||||
public class TaskTimer : MonoBehaviour
|
||||
@ -94,9 +93,24 @@ public class TaskTimer : MonoBehaviour
|
||||
Debug.Log("TaskTimer: Timer reset");
|
||||
}
|
||||
|
||||
public void SetDuration(float seconds)
|
||||
{
|
||||
if (isRunning)
|
||||
{
|
||||
Debug.LogWarning("TaskTimer: Cannot set duration while timer is running. Use AddTime() instead.");
|
||||
return;
|
||||
}
|
||||
|
||||
taskDuration = seconds;
|
||||
timeRemaining = taskDuration;
|
||||
UpdateTimerDisplay();
|
||||
Debug.Log($"TaskTimer: Duration set to {seconds} seconds");
|
||||
}
|
||||
|
||||
public void AddTime(float seconds)
|
||||
{
|
||||
timeRemaining += seconds;
|
||||
UpdateTimerDisplay();
|
||||
Debug.Log($"TaskTimer: Added {seconds} seconds. New time: {timeRemaining}");
|
||||
}
|
||||
|
||||
@ -139,10 +153,9 @@ public class TaskTimer : MonoBehaviour
|
||||
private void OnTimerExpired()
|
||||
{
|
||||
isRunning = false;
|
||||
Debug.Log("TaskTimer: Time expired! Returning to Lobby...");
|
||||
Debug.Log("TaskTimer: Time expired!");
|
||||
|
||||
// Load Lobby scene
|
||||
SceneManager.LoadScene("Lobby");
|
||||
// Timer expired - no automatic scene switching
|
||||
}
|
||||
|
||||
// Public methods for external control
|
||||
@ -164,7 +177,37 @@ public class TaskTimer : MonoBehaviour
|
||||
// Public method to process timer commands (called from UDP listener in TaskButtonManager)
|
||||
public void ProcessTimerCommand(string command)
|
||||
{
|
||||
command = command.ToUpper();
|
||||
command = command.ToUpper().Trim();
|
||||
|
||||
// Check for ADDTIME command with value (e.g., "ADDTIME 30")
|
||||
if (command.StartsWith("ADDTIME"))
|
||||
{
|
||||
string[] parts = command.Split(' ');
|
||||
if (parts.Length >= 2 && float.TryParse(parts[1], out float seconds))
|
||||
{
|
||||
AddTime(seconds);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("TaskTimer: ADDTIME command requires a numeric value (e.g., 'ADDTIME 30')");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for SETDURATION command with value (e.g., "SETDURATION 300")
|
||||
if (command.StartsWith("SETDURATION"))
|
||||
{
|
||||
string[] parts = command.Split(' ');
|
||||
if (parts.Length >= 2 && float.TryParse(parts[1], out float seconds))
|
||||
{
|
||||
SetDuration(seconds);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("TaskTimer: SETDURATION command requires a numeric value (e.g., 'SETDURATION 300')");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
switch (command)
|
||||
{
|
||||
@ -181,7 +224,7 @@ public class TaskTimer : MonoBehaviour
|
||||
ResetTimer();
|
||||
break;
|
||||
default:
|
||||
Debug.LogWarning($"TaskTimer: Unknown command: {command}. Valid commands: START, PAUSE, RESUME, RESET");
|
||||
Debug.LogWarning($"TaskTimer: Unknown command: {command}. Valid commands: START, PAUSE, RESUME, RESET, ADDTIME <seconds>, SETDURATION <seconds>");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user