using ML.SDK;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class FoodCut : MonoBehaviour
{
public GameObject FoodObject_Long;
public GameObject FoodObject_Chop;
public Transform spawnlocation;
public GameObject whiteSmoke;
public GameObject blackSmoke;
public GameObject BarEnable;
[SerializeField] public Slider CookingProgressBar; // Reference to the UI Slider representing the health bar
const string EVENT_ID_Long = "OnObjectInstantiate_Long";
const string EVENT_ID_Chop = "OnObjectInstantiate_Chopped";
const string EVENT_ID_OVEN_COOK = "OnOvenCook";
const string EVENT_ID_GRILL_COOK = "OnGrillCook";
const string EVENT_ID_STOP_COOKING = "StopCooking";
const string EVENT_ID_TRANSFER_FOOD_DATA = "TransferFoodData";
public GameObject SlashEffect;
public GameObject CookedVersion;
public GameObject BurntVersion;
private bool isCooking = false;
private Coroutine cookingCoroutine;
public bool hasBeenCooked = false; // Track if this food has already been cooked or burnt
private MLGrab grab;
public string TrueFoodName;
public bool isFruitNinjaFruit = false;
public bool isMeat = false;
public void OnNetworkSpawnObject_Long(object[] args)
{
// Check if the GameObject or its name is null (destroyed) before proceeding
if (this == null || gameObject == null || gameObject.name == null || args[0] == null)
{
return;
}
if (gameObject.name == (string)args[0])
{
SpawnObject_Long();
}
else
{
return;
}
}
public void OnNetworkSpawnObject_Chop(object[] args)
{
// Check if the GameObject or its name is null (destroyed) before proceeding
if (this == null || gameObject == null || gameObject.name == null || args[0] == null)
{
return;
}
if (gameObject.name == (string)args[0])
{
SpawnObject_Chopped();
}
else
{
return;
}
}
public void OnNetwork_Cook_Oven(object[] args)
{
// Debug.Log("Game object name : " + (string)args[0]);
// Check if the GameObject or its name is null (destroyed) before proceeding
if (this == null || gameObject == null || gameObject.name == null || args[0] == null)
{
return;
}
if (gameObject.name == (string)args[0])
{
HandleCookingEvent_Grill();
}
else
{
return;
}
}
public void OnNetworkTransferFoodData(object[] args)
{
Debug.Log("OnNetworkTransferFoodData called");
if (this == null || gameObject == null || gameObject.name == null || args[0] == null)
{
return;
}
if (gameObject.name == (string)args[0])
{
Debug.Log($"Passed in object : {(string)args[0]}");
}
else
{
return;
}
}
public void OnNetwork_Cook_Grill(object[] args)
{
// Debug.Log("Game object name : " + (string)args[0]);
// Check if the GameObject or its name is null (destroyed) before proceeding
if (this == null || gameObject == null || gameObject.name == null || args[0] == null)
{
return;
}
if (gameObject.name == (string)args[0])
{
HandleCookingEvent_Grill();
}
else
{
return;
}
}
public void OnNetwork_Stop_Cooking(object[] args)
{
// Debug.Log("Game object name : " + (string)args[0]);
// Check if the GameObject or its name is null (destroyed) before proceeding
if (this == null || gameObject == null || gameObject.name == null || args[0] == null)
{
return;
}
if (gameObject.name == (string)args[0])
{
StopCooking();
}
else
{
return;
}
}
EventToken InstantiateToken_Long;
EventToken InstantiateToken_Chopped;
EventToken CookOvenToken;
EventToken CookGrillToken;
EventToken StopCooking_Token;
EventToken TransferFoodInfo;
IEnumerator WaitCoroutine()
{
// Wait for 1 second
yield return new WaitForSeconds(0.25f);
// Code to execute after the wait
// Debug.Log("1 second has passed.");
if (grab.CurrentUser != null)
{
grab.ForceRelease();
}
Object.Destroy(gameObject);
}
IEnumerator WaitCoroutine_FruitSlicer()
{
// Wait for 1 second
yield return new WaitForSeconds(0.15f);
// Code to execute after the wait
// Debug.Log("1 second has passed.");
Object.Destroy(gameObject);
}
public void OnCollisionEnter(Collision other)
{
if (this == null || gameObject == null || gameObject.name == null)
{
return;
}
// Debug.Log("Collision detected with: " + other.gameObject.name);
if (other.gameObject.name.Contains("LongKnife"))
{
// if (grab.CurrentUser != null){
// grab.ForceRelease();
// }
spawnlocation = other.gameObject.transform;
BoxCollider boxCollider = this.gameObject.GetComponent<BoxCollider>();
if (FoodObject_Long != null)
{
// boxCollider.enabled = false;
// if (grab.CurrentUser != null)
// {
// grab.ForceRelease();
// }
this.InvokeNetwork(EVENT_ID_Long, EventTarget.Master, null, this.gameObject.name);
StartCoroutine(WaitCoroutine());
}
}
else if (other.gameObject.name.Contains("ChoppingKnife"))
{
/* if (grab.CurrentUser != null)
{
grab.ForceRelease();
}*/
BoxCollider boxCollider = this.gameObject.GetComponent<BoxCollider>();
if (FoodObject_Chop != null)
{
spawnlocation = other.gameObject.transform;
this.InvokeNetwork(EVENT_ID_Chop, EventTarget.Master, null, this.gameObject.name);
if (!isFruitNinjaFruit)
{
StartCoroutine(WaitCoroutine());
GameObject slashEffect = Object.Instantiate(SlashEffect, gameObject.transform.position, Quaternion.identity);
Object.Destroy(slashEffect, 1);
}
else
{
GameObject slashEffect = Object.Instantiate(SlashEffect, gameObject.transform.position, Quaternion.identity);
Object.Destroy(slashEffect, 1);
StartCoroutine(WaitCoroutine_FruitSlicer());
}
}
}
}
public void OnTriggerEnter(Collider other)
{
// Debug.Log("TriggerEntered");
if (this == null || gameObject == null || gameObject.name == null)
{
return;
}
if (other.gameObject.name.Contains("Oven_Cooker"))
{
Debug.Log("OvenCooker detected, cooking food object");
this.InvokeNetwork(EVENT_ID_OVEN_COOK, EventTarget.All, null, gameObject.name);
}
else if (other.gameObject.name.Contains("Grill"))
{
Debug.Log("Griller detected, grilling food object");
this.InvokeNetwork(EVENT_ID_GRILL_COOK, EventTarget.All, null, gameObject.name);
}
else if (other.gameObject.name.Contains("trash"))
{
Object.Destroy(gameObject);
}
}
public void OnTriggerExit(Collider other)
{
if (this == null || gameObject == null || gameObject.name == null)
{
return;
}
// Stop cooking when the food is removed from the oven or grill
if (other.gameObject.name.Contains("Oven_Cooker") || other.gameObject.name.Contains("Grill"))
{
Debug.Log("Food removed from cooking area");
StopCooking();
this.InvokeNetwork(EVENT_ID_STOP_COOKING, EventTarget.All, null, gameObject.name);
}
}
private string[] possibleNames = { "Apple", "Banana", "Carrot", "Steak", "Burger", "Fish" };
// Method to generate a random name from the list of possible names, with a random number appended
private string GenerateRandomName()
{
int randomIndex = Random.Range(0, possibleNames.Length); // Get a random name
int randomNumber = Random.Range(0, 100000); // Generate a random number between 0 and 99999
return possibleNames[randomIndex] + "_" + randomNumber.ToString("D5"); // Append the number with leading zeros
}
void Start()
{
// clickableComponent.OnClick.AddListener(OnClickFood);
InstantiateToken_Long = this.AddEventHandler(EVENT_ID_Long, OnNetworkSpawnObject_Long);
InstantiateToken_Chopped = this.AddEventHandler(EVENT_ID_Chop, OnNetworkSpawnObject_Chop);
CookOvenToken = this.AddEventHandler(EVENT_ID_OVEN_COOK, OnNetwork_Cook_Oven);
CookGrillToken = this.AddEventHandler(EVENT_ID_GRILL_COOK, OnNetwork_Cook_Grill);
StopCooking_Token = this.AddEventHandler(EVENT_ID_STOP_COOKING, OnNetwork_Stop_Cooking);
TransferFoodInfo = this.AddEventHandler(EVENT_ID_TRANSFER_FOOD_DATA, OnNetworkTransferFoodData);
if (!isFruitNinjaFruit)
{
string randomName = GenerateRandomName();
gameObject.name = randomName;
Debug.Log("Food object name set to: " + gameObject.name);
}
grab = gameObject.GetComponent<MLGrab>();
}
void Update()
{
}
public void SpawnObject_Long()
{
if (spawnlocation != null && MassiveLoopClient.IsMasterClient)
{
Object.Instantiate(FoodObject_Long, spawnlocation.position, Quaternion.identity);
}
else
{
// Debug.LogError("Spawn location is null or client is not the master.");
}
}
public void SpawnObject_Chopped()
{
if (spawnlocation != null && MassiveLoopClient.IsMasterClient)
{
GameObject newFood = Object.Instantiate(FoodObject_Chop, spawnlocation.position, Quaternion.identity);
}
else
{
// Debug.LogError("Spawn location is null or client is not the master.");
}
}
public void HandleCookingEvent_Oven()
{
if (!isCooking & BarEnable != null)
{
BarEnable.SetActive(true);
whiteSmoke.SetActive(true);
isCooking = true;
cookingCoroutine = StartCoroutine(CookingProcess(CookedVersion, BurntVersion, 10f, 15f));
}
else
{
return;
}
}
public void HandleCookingEvent_Grill()
{
if (!isCooking & BarEnable != null)
{
BarEnable.SetActive(true);
whiteSmoke.SetActive(true);
isCooking = true;
cookingCoroutine = StartCoroutine(CookingProcess(CookedVersion, BurntVersion, 8f, 13f));
}
else { return; }
}
private IEnumerator CookingProcess(GameObject cookedVersion, GameObject burntVersion, float cookingTime, float overcookTime)
{
float elapsedTime = 0f;
bool isOvercooked = false;
// Enable cooking progress bar
CookingProgressBar.gameObject.SetActive(true);
CookingProgressBar.maxValue = overcookTime;
CookingProgressBar.value = 0f;
while (elapsedTime < overcookTime && isCooking)
{
elapsedTime += Time.deltaTime;
CookingProgressBar.value = elapsedTime;
// Food is fully cooked
if (elapsedTime >= cookingTime && !isOvercooked && !hasBeenCooked)
{
GetComponent<MeshRenderer>().enabled = false;
cookedVersion.SetActive(true);
whiteSmoke.SetActive(false);
isOvercooked = true;
CookingProgressBar.fillRect.GetComponentInChildren<Image>().color = Color.green; // Turn green
blackSmoke.SetActive(true);
hasBeenCooked = true; // Mark as cooked
// Wait for a couple of seconds before transitioning to red
yield return new WaitForSeconds(2f);
CookingProgressBar.fillRect.GetComponentInChildren<Image>().color = Color.red; // Transition to red
}
yield return null;
}
// Overcooked and burnt
if (elapsedTime >= overcookTime && isCooking && hasBeenCooked)
{
GetComponent<MeshRenderer>().enabled = false;
cookedVersion.SetActive(false);
burntVersion.SetActive(true);
hasBeenCooked = true; // Mark as burnt
}
CookingProgressBar.gameObject.SetActive(false);
isCooking = false;
}
private void StopCooking()
{
if (isCooking && cookingCoroutine != null)
{
StopCoroutine(cookingCoroutine);
cookingCoroutine = null;
isCooking = false;
// Hide the progress bar and stop smoke effects
CookingProgressBar.gameObject.SetActive(false);
whiteSmoke.SetActive(false);
blackSmoke.SetActive(false);
}
}
}