Projekt_SI_automatyczny_kelner/Assets/Logic/Graph/CustomerTable.cs
2021-05-19 10:35:58 +02:00

174 lines
6.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Logic.Agent;
using Logic.Data;
using UnityEditor.SearchService;
using UnityEngine;
namespace Logic.Graph
{
public enum TableState
{
Empty = 0,
WaitingForMenu = 1,
ViewingTheMenu = 2,
WaitingForWaitress = 3,
WaitingForFood = 4,
Eating = 5,
WaitingForPay = 6
}
public class CustomerTable : Table
{
public TableState state;
public Customer customer;
private Coroutine _prepareRecipeCoroutine;
private Coroutine _eatMealCoroutine;
public void HandleSpaceClick()
{
switch (state)
{
case TableState.WaitingForMenu:
HandleMenuView();
break;
case TableState.WaitingForWaitress:
HandleRecipeReady();
break;
case TableState.WaitingForFood:
HandleFoodDelivery();
break;
case TableState.WaitingForPay:
HandlePayment();
break;
}
}
private void HandleMenuView()
{
state = TableState.ViewingTheMenu;
UIManager.Instance.uiTableManager.ChangeStatus(tableNumber, state);
_prepareRecipeCoroutine = StartCoroutine(HandleRecipePrepare(3));
}
private void HandleRecipeReady()
{
if (IsFull && state == TableState.WaitingForWaitress)
{
Recipe item = CurrentItem as Recipe;
if (item == null) return;
item.tableNumber = tableNumber;
bool isSuccess = AgentManager.Instance.AddItem(item);
if (isSuccess)
{
ClearItemObject();
IsFull = false;
state = TableState.WaitingForFood;
UIManager.Instance.uiTableManager.ChangeStatus(tableNumber, state);
UIManager.Instance.uiTableManager.ChangeItem(tableNumber, null);
}
}
}
private void HandleFoodDelivery()
{
Food food = AgentManager.Instance.GETMealForTable(tableNumber);
if (food != null)
{
CurrentItem = food;
Spawn();
AgentManager.Instance.RemoveItem(food);
_eatMealCoroutine = StartCoroutine(HandleEatMeal(3));
state = TableState.Eating;
UIManager.Instance.uiTableManager
.ChangeStatus(tableNumber, state);
UIManager.Instance.uiTableManager
.ChangeItem(tableNumber, CurrentItem);
}
}
private IEnumerator HandleEatMeal(int secs)
{
yield return new WaitForSeconds(secs);
if (state == TableState.WaitingForPay)
{
StopCoroutine(_eatMealCoroutine);
}
else
{
ClearItemObject();
Food food = CurrentItem as Food;
if (food != null)
{
Money money = food.moneyItem;
money.cost = food.cost;
CurrentItem = Instantiate(food.moneyItem);
Spawn();
state = TableState.WaitingForPay;
UIManager.Instance.uiTableManager.ChangeStatus(tableNumber, state);
UIManager.Instance.uiTableManager.ChangeItem(tableNumber, CurrentItem);
}
}
}
private void HandlePayment()
{
IsFull = false;
CurrentItem = null;
ClearItemObject();
state = TableState.Empty;
UIManager.Instance.uiTableManager.ChangeStatus(tableNumber, state);
UIManager.Instance.uiTableManager.ChangeItem(tableNumber, null);
}
private IEnumerator HandleRecipePrepare(int secs)
{
yield return new WaitForSeconds(secs);
if (state == TableState.WaitingForWaitress)
{
StopCoroutine(_prepareRecipeCoroutine);
}
else
{
var valuesForQuery = new Dictionary<string, object>();
valuesForQuery.Add("IfPizza", customer.IfPizza);
valuesForQuery.Add("DislikedIngredient", customer.DislikedIngredient);
valuesForQuery.Add("Price", customer.MaxPrice);
valuesForQuery.Add("Kcal", customer.MaxCalories);
valuesForQuery.Add("TimeOfDay", SceneContext.Instance.TimeOfDay);
valuesForQuery.Add("IfVegetarian", customer.IfVegetarian);
var decisionTree = SceneContext.Instance.DecisionTree;
var result = decisionTree.GetResult(decisionTree.Root, valuesForQuery);
var recipe =
SceneContext.Instance.Recipes[int.Parse(result.ToString())];
if (recipe.name == "Null")
{
SetRandomRecipe();
}
else
{
CurrentItem = Instantiate(recipe);
state = TableState.WaitingForFood;
UIManager.Instance.uiTableManager.ChangeStatus(tableNumber, state);
}
Spawn();
IsFull = true;
state = TableState.WaitingForWaitress;
UIManager.Instance.uiTableManager.ChangeStatus(tableNumber, state);
UIManager.Instance.uiTableManager.ChangeItem(tableNumber, CurrentItem);
}
}
private void SetRandomRecipe()
{
CurrentItem = Instantiate(CustomerTableManager.Instance.GetRandomRecipe());
state = TableState.WaitingForFood;
UIManager.Instance.uiTableManager.ChangeStatus(tableNumber, state);
}
}
}