using ML.SDK;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using static OverCookedGameManager;
using System.Linq; // For easier list comparison
public class OverCookedGameManager : MonoBehaviour
{
// Button and GameObject References
public MLClickable StartButton;
public MLClickable EndButton;
public MLClickable DifficultyUpButton;
public MLClickable DifficultyDownButton;
public MLClickable RolePlayButtonMode;
public GameObject[] Dishes;
public GameObject FoodCardPrefab; // Prefab with dish name, ingredients list, timer, and progress bar
public GameObject TurnInCollider;
public GameObject SpawnStart;
public GameObject SpawnEnd;
public TextMeshPro TextOfActiveQuests;
public TextMeshPro ScoreIntText;
public int Score;
public List<FoodQuest> activeFoodCards = new List<FoodQuest>(); // List of active food cards on screen
// Enums for Dishes and Ingredients
public enum EasyFoods { Steak, TomatoSoup, Hamburger }
public enum Ingredients { Steak, Chicken, Tomato, Potato, GreenBellPepper, RedBellPepper, YellowBellPepper }
EventToken OnButtonClickEnd;
const string EVENT_ID_BUTTON_CLICKEND = "OnButtonClickEnd";
EventToken OnButtonClickStart;
const string EVENT_ID_BUTTON_CLICKStart = "OnButtonClickBegin_";
EventToken Initialize_FoodQuest;
const string EVENT_InitalizeFoodQuest = "InitFoodQuest_manager";
public bool isGameRunning = false;
void OnButtonClickEnd_Event(object[] args)
{
isGameRunning = false;
StopAllCoroutines();
foreach (var foodCard in activeFoodCards)
{
Destroy(foodCard.gameObject);
}
activeFoodCards.Clear();
}
void OnButtonClickBegin_(object[] args)
{
isGameRunning = true;
ScoreIntText.text = 0.ToString();
if (MassiveLoopClient.IsMasterClient)
{
StartCoroutine(SpawnFoodCards());
}
}
void InitFoodQuest_Manager(object[] args)
{
}
private void Start()
{
// Setting up button listeners
StartButton.OnPlayerClick.AddListener(OnPlayerClickStart);
EndButton.OnPlayerClick.AddListener(OnPlayerClickEnd);
RolePlayButtonMode.OnPlayerClick.AddListener(OnPlayerClickRoleplayMode);
DifficultyUpButton.OnPlayerClick.AddListener(OnPlayerClickDifficultyUp);
DifficultyDownButton.OnPlayerClick.AddListener(OnPlayerClickDifficultyDown);
OnButtonClickEnd = this.AddEventHandler(EVENT_ID_BUTTON_CLICKEND, OnButtonClickEnd_Event);
OnButtonClickStart = this.AddEventHandler(EVENT_ID_BUTTON_CLICKStart, OnButtonClickBegin_);
Initialize_FoodQuest = this.AddEventHandler(EVENT_InitalizeFoodQuest, InitFoodQuest_Manager);
}
private void Update()
{
CheckActiveFoodCards();
}
private void CheckActiveFoodCards()
{
for (int i = activeFoodCards.Count - 1; i >= 0; i--)
{
if (activeFoodCards[i] == null)
{
// Remove null (destroyed) FoodQuest from the list
activeFoodCards.RemoveAt(i);
// Optional: Reduce points or trigger other logic when a food card is removed
Score -= 10; // Adjust the score deduction as needed
Debug.Log("A FoodQuest card was removed. Updated Score: " + Score);
}
}
}
private void OnPlayerClickStart(MLPlayer player)
{
Debug.Log("on click end invoked locally, attempting to do so for all clients");
this.InvokeNetwork(EVENT_ID_BUTTON_CLICKStart, EventTarget.All, null);
}
private IEnumerator SpawnFoodCards()
{
// Continuously spawns food cards with a delay, simulating a queue of orders
while (true)
{
SpawnFoodCard();
yield return new WaitForSeconds(5f); // Adjust the interval as needed
}
}
private void SpawnFoodCard()
{
// Determine spawn area bounds
Vector3 startPos = SpawnStart.transform.position;
Vector3 endPos = SpawnEnd.transform.position;
float spawnBuffer = 0.5f; // Adjust buffer distance as needed for spacing between cards
// Try to find a non-overlapping position
Vector3 spawnPosition;
int attempts = 0;
bool positionFound = false;
do
{
// Calculate a random position between start and end points
float randomX = Random.Range(startPos.x, endPos.x);
float randomY = Random.Range(startPos.y, endPos.y);
float randomZ = Random.Range(startPos.z, endPos.z);
spawnPosition = new Vector3(randomX, randomY, randomZ);
// Check for overlap with existing cards
positionFound = true;
foreach (var card in activeFoodCards)
{
if (Vector3.Distance(spawnPosition, card.transform.position) < spawnBuffer)
{
positionFound = false;
break;
}
}
attempts++;
}
while (!positionFound && attempts < 10); // Limit attempts to avoid infinite loops
// Instantiate the food card if a position is found
if (positionFound)
{
GameObject foodCardGO = Instantiate(FoodCardPrefab, spawnPosition, SpawnStart.transform.rotation);
FoodQuest foodCard = (FoodQuest)foodCardGO.GetComponent(typeof(FoodQuest));
// Debug.Log($"[OverCookedGameManager] Instantiated FoodCard with ingredients list : {foodCard.name} - {foodCard.IngredientsText.text} ");
foodCard.SetText("Attempting to pass in game object", this.gameObject);
activeFoodCards.Add(foodCard);
}
else
{
// Debug.LogWarning("Could not find non-overlapping position for food card.");
}
}
private void OnPlayerClickEnd(MLPlayer player)
{
Debug.Log("on click end invoked locally, attempting to do so for all clients");
this.InvokeNetwork(EVENT_ID_BUTTON_CLICKEND, EventTarget.All, null);
}
private void OnPlayerClickRoleplayMode(MLPlayer player)
{
// Toggle roleplay mode here
}
private void OnPlayerClickDifficultyUp(MLPlayer player)
{
// Adjust difficulty logic here
}
public void RetrieveInfo()
{
Debug.Log("Retrieve info called from found game manager, found and successfully called public function!");
}
public void RemoveGameObjectFromList(GameObject x)
{
if (x == null) return;
// Get the FoodQuest component from the GameObject
FoodQuest foodQuest = (FoodQuest)x.GetComponent(typeof(FoodQuest));
if (foodQuest != null)
{
// Check if the FoodQuest is in the activeFoodCards list
if (activeFoodCards.Contains(foodQuest))
{
activeFoodCards.Remove(foodQuest);
Debug.Log($"Removed {x.name} from activeFoodCards.");
}
else
{
//Debug.LogWarning($"The object {x.name} is not in the activeFoodCards list.");
}
}
else
{
// Debug.LogWarning($"No FoodQuest component found on {x.name}. Cannot remove from activeFoodCards.");
}
}
public void AddScore(int x)
{
Score += x;
Debug.Log($"New Score : {Score}");
ScoreIntText.text = Score.ToString();
}
private void OnPlayerClickDifficultyDown(MLPlayer player)
{
// Adjust difficulty logic here
}
}