Advertisement
Guest User

customerdata

a guest
Dec 13th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.27 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. using TMPro;
  6. using System.Linq;
  7. using Pixelplacement;
  8.  
  9. public class DrinkData
  10. {
  11. public int icecubes = 0;
  12. public Color color;
  13. }
  14.  
  15. public class Customer : MonoBehaviour
  16. {
  17. public DrinkData drink;
  18. public List<Order> potentialOrders;
  19. public AudioClip screamClip;
  20. public AudioClip victoryClip;
  21. public float currentPatience = 0f;
  22. public float basePatience = 10f;
  23. public GameObject receiverPrefab;
  24. public Transform lineForward;
  25. public NavMeshAgent agent;
  26. public Vector3 currentPosition;
  27. public int numberOfPizza = 2;
  28. public PizzaData pizzaOrder;
  29. public Transform patienceBar;
  30. MeshRenderer patienceBarRenderer;
  31. public Transform exitPosition;
  32. public NavMeshObstacle obstacle;
  33. public GameObject ratingPrefab;
  34. public Transform counterHeight;
  35. public Animator animator;
  36.  
  37. public Transform bodyModels;
  38. public void InitCustomerData(CustomerData customerData)
  39. {
  40. // print(customerData.basePatience);
  41. potentialOrders = customerData.potentialOrders;
  42. basePatience = customerData.basePatience;
  43. }
  44. // Start is called before the first frame update
  45. void Start()
  46. {
  47. Transform bodyModel = bodyModels.GetChild(Random.Range(0, bodyModels.childCount));
  48. bodyModel.gameObject.SetActive(true);
  49. animator = bodyModel.GetComponent<Animator>();
  50. counterHeight = GameObject.FindWithTag("CounterHeight").transform;
  51. obstacle = GetComponent<NavMeshObstacle>();
  52. exitPosition = GameObject.FindWithTag("ExitPosition").transform;
  53. patienceBarRenderer = patienceBar.GetComponent<MeshRenderer>();
  54. patienceBarRenderer.material.EnableKeyword("_EMISSION");
  55. //basePatience -= UniversalData.gameManager.day * UniversalData.gameManager.patienceDecreaseRate;
  56. currentPatience = basePatience;
  57. lineForward = GameObject.FindWithTag("LineForward").transform;
  58. agent = GetComponent<NavMeshAgent>();
  59. UniversalData.lineManager.FindPosition(this);
  60. DecideOrder();
  61. }
  62.  
  63. Dictionary<string, int> AggregateOrder(List<string> order)
  64. {
  65. Dictionary<string, int> aggregatedOrder = new Dictionary<string, int>();
  66. foreach (string topping in order)
  67. {
  68. if (aggregatedOrder.ContainsKey(topping))
  69. {
  70. aggregatedOrder[topping]++;
  71. }
  72. else
  73. {
  74. aggregatedOrder[topping] = 1;
  75. }
  76. }
  77. return aggregatedOrder;
  78. }
  79. protected bool pathComplete()
  80. {
  81. if (Vector3.Distance(agent.destination, agent.transform.position) <= agent.stoppingDistance)
  82. {
  83. if (!agent.hasPath || agent.velocity.sqrMagnitude == 0f)
  84. {
  85. return true;
  86. }
  87. }
  88.  
  89. return false;
  90. }
  91. public void MoveTo(Vector3 pos)
  92. {
  93. reachedDestination = false;
  94. agent.SetDestination(pos);
  95. currentPosition = pos;
  96. fullyInLine = false;
  97. }
  98.  
  99. List<int> Shuffle(List<int> list)
  100. {
  101. int listCount = list.Count;
  102. //print(listCount);
  103. List<int> newList = new List<int>();
  104. for (int i = 0; i < listCount; i++)
  105. {
  106. int index = Random.Range(0, list.Count);
  107. newList.Add(list[index]);
  108. list.RemoveAt(index);
  109. }
  110. return newList;
  111. }
  112.  
  113. public DrinkData DecideDrink()
  114. {
  115. DrinkData drinkData;
  116. drinkData = new DrinkData();
  117. drinkData.icecubes = Random.Range(1, 5);
  118. drinkData.color = UniversalData.drinkData[Random.Range(0, UniversalData.drinkData.Count)].color;
  119. return drinkData;
  120. }
  121. public void DecideOrder()
  122. {
  123. PizzaData order;
  124. List<Order> orderDistribution = new List<Order>();
  125. foreach (Order potentialOrder in potentialOrders)
  126. {
  127. for (int i = 0; i < potentialOrder.weight; i++)
  128. {
  129. orderDistribution.Add(potentialOrder);
  130. }
  131. }
  132. order = ScriptableObject.CreateInstance<PizzaData>();
  133. Order orderData = orderDistribution[Random.Range(0, orderDistribution.Count)];
  134. if (orderData.hasCheese)
  135. {
  136. order.toppings.Add("Cheese");
  137. }
  138. else if (orderData.hasTomato)
  139. {
  140. order.toppings.Add("Tomato");
  141. }
  142. foreach (IngredientData topping in orderData.toppings)
  143. {
  144. order.toppings.Add(topping.name);
  145. }
  146. pizzaOrder = order;
  147. if(orderData.hasDrink)
  148. {
  149. drink = DecideDrink();
  150. }
  151. else
  152. {
  153. drink = null;
  154. }
  155. }
  156. public GameObject currentReceiver;
  157. IEnumerator Leave()
  158. {
  159. print("Leave");
  160. if (currentReceiver != null)
  161. {
  162. Destroy(currentReceiver.transform.gameObject);
  163. }
  164. print("leave");
  165. pizzaOrderBubble.gameObject.SetActive(false);
  166. drinkBubble.gameObject.SetActive(false);
  167. expressionBubbleTemplate.SetActive(false);
  168. MoveTo(exitPosition.position);
  169. yield return new WaitForSeconds(1f);
  170. UniversalData.lineManager.Remove(this);
  171. }
  172.  
  173. float CheckSimilarity(List<string> pizzaToppings, List<string> orderToppings)
  174. {
  175. int current = 0;
  176. int total = 0;
  177. float similarity = 1f;
  178. foreach (string topping in pizzaToppings)
  179. {
  180. print("i: " + topping);
  181. }
  182. Dictionary<string, int> aggregatedPizzaData = AggregateOrder(pizzaToppings);
  183. Dictionary<string, int> aggregatedOrderData = AggregateOrder(orderToppings);
  184. List<string> keys = new List<string>(aggregatedOrderData.Keys);
  185. foreach (string key in keys)
  186. {
  187. total += aggregatedOrderData[key];
  188. current += aggregatedOrderData[key];
  189. int difference = 0;
  190. if (aggregatedPizzaData.ContainsKey(key))
  191. {
  192. difference = Mathf.Abs(aggregatedPizzaData[key] - aggregatedOrderData[key]);
  193. }
  194. else
  195. {
  196. difference = aggregatedOrderData[key];
  197. }
  198. current -= difference;
  199. }
  200. if (current < 0)
  201. {
  202. current = 0;
  203. }
  204. print("total: " + total + "current: " + current);
  205. similarity = (float)current / (float)total;
  206. return similarity;
  207. }
  208. public float moneyConstant = 2.5f;
  209. public float repPatienceConstant = 1f;
  210. public float repSimilarityConstant = 2.5f;
  211.  
  212. public Transform pizzaOrderBubble;
  213. public GameObject drinkExpressionBubbleTemplate;
  214. public GameObject expressionBubbleTemplate;
  215. public Transform drinkBubble;
  216.  
  217. public void ReceiveDrink(DrinkType drinkType, int cubes)
  218. {
  219. float similarity = 0f;
  220. if (drinkType.color == drink.color)
  221. {
  222. similarity = cubes / drink.icecubes;
  223. }
  224. drink = null;
  225. float reputation = 0f;
  226. float money = similarity * moneyConstant + similarity * moneyConstant * currentPatience / basePatience;
  227. if (similarity == 0f)
  228. {
  229. reputation = -1f;
  230. }
  231. else if (similarity < 0.5f)
  232. {
  233. reputation = 1f;
  234. }
  235. else
  236. {
  237. reputation = 0.25f;
  238. }
  239. // add money
  240. UniversalData.playerManager.AddMoney(money);
  241. UniversalData.playerManager.AddReputation(reputation);
  242. drinkBubble.gameObject.SetActive(false);
  243. drink = null;
  244. CreateExpression(drinkExpressionBubbleTemplate, reputation);
  245. StartCoroutine(ReceiveItemRoutine());
  246. }
  247.  
  248. void CreateLabels(float money, float reputation)
  249. {
  250. var moneyLabel = Instantiate(ratingPrefab);
  251. moneyLabel.transform.position = transform.position + new Vector3(0f, 1f, -0.3f);
  252. moneyLabel.transform.Find("Text").GetComponent<TextMeshPro>().text = "+$" + money;
  253. var repLabel = Instantiate(ratingPrefab);
  254. repLabel.transform.position = transform.position + new Vector3(0f, 0.8f, -0.3f);
  255. repLabel.transform.Find("Text").GetComponent<TextMeshPro>().text = "+R" + reputation;
  256. }
  257.  
  258. void CreateExpression(GameObject expression, float reputation)
  259. {
  260. expression.gameObject.SetActive(true);
  261. expression.transform.localScale = Vector3.zero;
  262.  
  263.  
  264. if (reputation < 0f)
  265. {
  266. animator.SetTrigger("angry");
  267. expression.transform.Find("Sprite").GetComponent<SpriteRenderer>().sprite = UniversalData.expressionIcons["angry"];
  268. }
  269. else
  270. {
  271. UniversalData.soundManager.PlaySound(victoryClip);
  272. animator.SetTrigger("happy");
  273. expression.transform.Find("Sprite").GetComponent<SpriteRenderer>().sprite = UniversalData.expressionIcons["happy"];
  274. }
  275. Tween.LocalScale(expression.transform, new Vector3(0.08001078f, 0.08001078f, 0.08001078f), 0.3f, 0f, Tween.EaseSpring, Tween.LoopType.None);
  276. }
  277. public void ReceiveItem(PizzaData pizzaData)
  278. {
  279.  
  280. float similarity = CheckSimilarity(pizzaData.toppings, new List<string>(pizzaOrder.toppings));
  281.  
  282. bool cooked = false;
  283. if (pizzaData.cookProgress > pizzaOrder.progressNeeded && pizzaData.cookProgress < pizzaOrder.burntProgress)
  284. {
  285. cooked = true;
  286. }
  287.  
  288. // similarity
  289. //
  290. float money = similarity * moneyConstant + similarity * moneyConstant * currentPatience / basePatience;
  291. //
  292. float reputation = 0f;
  293. if (similarity == 1f && currentPatience / basePatience > 0.7f)
  294. {
  295. reputation = 0.5f;
  296. }
  297. if (!cooked)
  298. {
  299. money = 0f;
  300. reputation = -1f;
  301. }
  302. if (similarity < 0.5f)
  303. {
  304. reputation -= 1f;
  305. }
  306. // bubble
  307. pizzaOrderBubble.gameObject.SetActive(false);
  308. CreateExpression(expressionBubbleTemplate, reputation);
  309.  
  310. CreateLabels(money, reputation);
  311. // add money
  312. UniversalData.playerManager.AddMoney(money);
  313. UniversalData.playerManager.AddReputation(reputation);
  314. pizzaOrder = null;
  315. StartCoroutine(ReceiveItemRoutine());
  316.  
  317. }
  318.  
  319. IEnumerator ReceiveItemRoutine()
  320. {
  321. yield return new WaitForSeconds(1.5f);
  322. if (pizzaOrder == null)
  323. {
  324. Tween.LocalScale(expressionBubbleTemplate.transform, Vector3.zero, 0.1f, 0f, Tween.EaseLinear, Tween.LoopType.None);
  325. }
  326. else
  327. {
  328. Tween.LocalScale(drinkExpressionBubbleTemplate.transform, Vector3.zero, 0.1f, 0f, Tween.EaseLinear, Tween.LoopType.None);
  329. }
  330. if (pizzaOrder == null && drink == null)
  331. {
  332. StartCoroutine(Leave());
  333. }
  334. }
  335.  
  336. void StatePizza()
  337. {
  338. // rename this
  339. pizzaOrderBubble.gameObject.SetActive(true);
  340. Transform top = pizzaOrderBubble.Find("Top");
  341. Dictionary<string, int> aggregatedOrder = AggregateOrder(pizzaOrder.toppings);
  342. var templatePrefab = pizzaOrderBubble.Find("Template");
  343. templatePrefab.gameObject.SetActive(false);
  344. //1.0633
  345. List<string> keys = new List<string>(aggregatedOrder.Keys);
  346. int i = 0;
  347. foreach (string key in keys)
  348. {
  349. // get icon
  350. Sprite newIcon = UniversalData.materialIcons.FirstOrDefault(x => x.name == key);
  351. int quantity = aggregatedOrder[key];
  352.  
  353. var template = Instantiate(templatePrefab, pizzaOrderBubble.transform);
  354. template.Find("Image").GetComponent<SpriteRenderer>().sprite = newIcon;
  355. template.Find("Quantity").GetComponent<TextMeshPro>().text = "" + quantity;
  356. Transform startPos = pizzaOrderBubble.transform.Find("StartPos");
  357. template.gameObject.SetActive(true);
  358. template.transform.name = key;
  359. template.transform.localPosition = new Vector3(-2.942f, startPos.localPosition.y + 1.51f * i, 0f);
  360. if (i + 1 == keys.Count)
  361. {
  362. top.transform.localPosition = new Vector3(-2.942f, template.transform.localPosition.y + 1.0633f, 0f);
  363. }
  364. i++;
  365. }
  366. pizzaOrderBubble.transform.localScale = new Vector3(0f, 0f, 0f);
  367. Tween.LocalScale(pizzaOrderBubble.transform, new Vector3(0.08001078f, 0.08001078f, 0.08001078f), 0.1f, 0f, Tween.EaseLinear, Tween.LoopType.None);
  368. }
  369. void StateDrink()
  370. {
  371. if(drink == null)
  372. {
  373. return;
  374. }
  375. // rename this
  376. drinkBubble.gameObject.SetActive(true);
  377. Transform top = pizzaOrderBubble.Find("Top");
  378. Dictionary<string, int> aggregatedOrder = AggregateOrder(pizzaOrder.toppings);
  379. drinkBubble.transform.Find("IceCube").Find("Quantity").GetComponent<TextMeshPro>().text = "" + drink.icecubes;
  380. drinkBubble.transform.Find("Drink").Find("Fill").GetComponent<SpriteRenderer>().color = drink.color;
  381. var templatePrefab = pizzaOrderBubble.Find("Template");
  382. templatePrefab.gameObject.SetActive(false);
  383. drinkBubble.transform.localScale = new Vector3(0f, 0f, 0f);
  384. Tween.LocalScale(drinkBubble.transform, new Vector3(0.08001078f, 0.08001078f, 0.08001078f), 0.1f, 0f, Tween.EaseLinear, Tween.LoopType.None);
  385. }
  386. void StateOrder()
  387. {
  388. patienceBar.gameObject.SetActive(false);
  389. firstInLine = true;
  390. var receiver = Instantiate(receiverPrefab);
  391. receiver.transform.position = new Vector3(transform.position.x, counterHeight.transform.position.y, counterHeight.position.z);
  392. receiver.GetComponent<Receiver>().customer = this;
  393. currentReceiver = receiver;
  394. StatePizza();
  395. StateDrink();
  396. }
  397.  
  398. bool fullyInLine = false;
  399. bool reachedDestination = false;
  400. bool lostPatience = false;
  401. bool firstInLine = false;
  402. // Update is called once per frame
  403. void Update()
  404. {
  405. if (agent.enabled)
  406. {
  407. animator.SetFloat("speed", agent.velocity.magnitude);
  408. }
  409.  
  410.  
  411. if (currentPatience <= 0 && !lostPatience)
  412. {
  413. lostPatience = true;
  414. StartCoroutine(Leave());
  415. }
  416. if (!reachedDestination && currentPosition != null && currentPosition != Vector3.zero)
  417. {
  418. if (pathComplete())
  419. {
  420. reachedDestination = true;
  421. }
  422. }
  423. if (reachedDestination)
  424. {
  425. transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.LookRotation(lineForward.forward), Time.deltaTime * 5f);
  426. }
  427. if (reachedDestination && !fullyInLine && Vector3.Dot(transform.forward, lineForward.forward) >= 0.99f)
  428. {
  429. // we've faced the right position
  430. fullyInLine = true;
  431. if (UniversalData.lineManager.IsFirstInLine(this))
  432. {
  433. StateOrder();
  434. }
  435. else
  436. {
  437. patienceBar.gameObject.SetActive(true);
  438. }
  439. }
  440. if (fullyInLine && firstInLine)
  441. {
  442. if (currentPatience > 0f)
  443. {
  444. currentPatience -= Time.deltaTime;
  445. }
  446. }
  447.  
  448. Color newColor = Color.Lerp(Color.red, Color.green, currentPatience / basePatience);
  449. //patienceBarRenderer.material.color = newColor;
  450. patienceBarRenderer.material.color = newColor;
  451. patienceBarRenderer.material.SetColor("_EmissionColor", newColor);
  452. }
  453.  
  454. private void OnCollisionEnter(Collision collision)
  455. {
  456. Rigidbody rb = collision.transform.GetComponent<Rigidbody>();
  457. if (rb != null && rb.velocity.magnitude > 0.1f)
  458. {
  459. UniversalData.soundManager.PlaySound(screamClip);
  460. UniversalData.playerManager.AddReputation(-10f);
  461. animator.applyRootMotion = true;
  462. agent.enabled = false;
  463. animator.SetTrigger("fallover");
  464. UniversalData.lineManager.Remove(this);
  465. Destroy(gameObject, 2f);
  466. }
  467. }
  468. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement