ItemUseController.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Newtonsoft.Json;
  2. using NUnit.Framework;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Net.NetworkInformation;
  7. using UnityEngine;
  8. using UnityEngine.SceneManagement;
  9. /* 具体使用哪个道具产生的效果,包括结束的效果回调函数
  10. */
  11. public class ItemUseController : ScriptableObject
  12. {
  13. private static Vector3 bowlLocation;
  14. private static List<string> foodBowlItems = new()
  15. {
  16. "food_00001", // 狗粮
  17. "food_00002", // 狗罐头
  18. "food_00003" // 冻肉干
  19. };
  20. private static List<string> waterBowlItems = new()
  21. {
  22. "water_00001", // 水
  23. "water_00002" // 牛奶
  24. };
  25. private static List<string> frisbees = new()
  26. {
  27. "toy_00001" // 飞盘
  28. };
  29. private static List<string> showers = new()
  30. {
  31. "other_00001" // 打开洗澡页面的用品
  32. };
  33. public static string _homeUseItemId = null; // 暂存使用item数据,调用另外一个Monobehaviour Class来执行网络通讯
  34. public static void ItemUsed(string itemId)
  35. {
  36. // 在Home场景使用道具调用_homeUseItemId来启动道具使用
  37. if (SceneManager.GetActiveScene().name == "Home")
  38. {
  39. var bowls = GameObject.Find("Bowls");
  40. GameObject targetBowl = null;
  41. // 消费食物类
  42. if (foodBowlItems.Contains(itemId))
  43. {
  44. targetBowl = bowls.transform.Find("Bowl_food").gameObject;
  45. targetBowl.transform.localPosition = CalcBowlShowLoc();
  46. //var food = targetBowl.transform.Find("Food").gameObject;
  47. //food.SetActive(true);
  48. foreach (var dogInScene in HomeController.dogsInScene)
  49. {
  50. dogInScene.StartItemConsume(ItemGroup.FOOD);
  51. dogInScene.SetMoveSpeed(UnityEngine.Random.Range(0.3f, 0.6f));
  52. }
  53. // 通过将_homeUseItemId 设置为非空后,启动网络通讯,仅限于在Home场景消耗的道具
  54. ItemUseController._homeUseItemId = itemId;
  55. }
  56. // 消费水牛奶等
  57. if (waterBowlItems.Contains(itemId))
  58. {
  59. targetBowl = bowls.transform.Find("Bowl_water").gameObject;
  60. targetBowl.transform.localPosition = CalcBowlShowLoc();
  61. //var water = targetBowl.transform.Find("Water").gameObject;
  62. //water.SetActive(true);
  63. foreach (var dogInScene in HomeController.dogsInScene)
  64. {
  65. dogInScene.StartItemConsume(ItemGroup.WATER);
  66. dogInScene.SetMoveSpeed(UnityEngine.Random.Range(0.3f, 0.6f));
  67. }
  68. // 通过将_itemUseId 设置为非空后,启动网络通讯,仅限于在Home场景消耗的道具
  69. ItemUseController._homeUseItemId = itemId;
  70. }
  71. }
  72. // 使用飞盘类道具
  73. if (frisbees.Contains(itemId))
  74. {
  75. GameData.playedToy = itemId; // 存储使用哪一个玩具
  76. //SceneManager.LoadScene("Playground", LoadSceneMode.Single);
  77. MaskTransitions.TransitionManager.Instance.LoadLevel("Playground");
  78. }
  79. // 使用洗澡页面道具
  80. if (showers.Contains(itemId))
  81. {
  82. GameData.bathItemId = itemId; // 存储使用哪一个玩具
  83. // SceneManager.LoadScene("Bathroom", LoadSceneMode.Single);
  84. MaskTransitions.TransitionManager.Instance.LoadLevel("Bathroom");
  85. }
  86. }
  87. #region 食物和喝水控制代码
  88. // 计算出一个碗的位置,距离所有只狗都至少2米
  89. private static Vector3 CalcBowlShowLoc()
  90. {
  91. while (true)
  92. {
  93. float x = UnityEngine.Random.Range(-3f, 3f);
  94. float z = UnityEngine.Random.Range(-3f, 3f);
  95. bowlLocation = new Vector3(x, 0, z);
  96. bool allowToPlace = true;
  97. int dogQty = HomeController.dogsInScene.Count;
  98. for (int i = 0; i < dogQty; i++) {
  99. GameObject dog = HomeController.dogsInScene[i].gameObject;
  100. GameObject dogNext;
  101. if (i == dogQty - 1)
  102. {
  103. dogNext = HomeController.dogsInScene[0].gameObject; // 如果是最后一只狗,next dog就是第一值
  104. }
  105. else
  106. {
  107. dogNext = HomeController.dogsInScene[i+1].gameObject;
  108. }
  109. if (Vector3.Distance(bowlLocation, dog.transform.position) < 2) // 距离所有只狗都至少2米
  110. {
  111. allowToPlace = false;
  112. continue;
  113. }
  114. //if (dogQty > 1) // 需要计算狗与狗的角度
  115. //{
  116. // if(CalcAngle(bowlLocation, dog.transform.position, dogNext.transform.position) < 45)
  117. // {
  118. // allowToPlace = false;
  119. // continue;
  120. // }
  121. //}
  122. }
  123. if (allowToPlace)
  124. {
  125. return bowlLocation;
  126. }
  127. }
  128. }
  129. #endregion
  130. }