ItemUseController.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 _itemUseId = null; // 暂存使用item数据,调用另外一个Monobehaviour Class来执行网络通讯
  34. public static void ItemUsed(string itemId)
  35. {
  36. // 在Home场景使用道具调用_itemUseId来启动道具使用
  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. }
  54. // 消费水牛奶等
  55. if (waterBowlItems.Contains(itemId))
  56. {
  57. targetBowl = bowls.transform.Find("Bowl_water").gameObject;
  58. targetBowl.transform.localPosition = CalcBowlShowLoc();
  59. //var water = targetBowl.transform.Find("Water").gameObject;
  60. //water.SetActive(true);
  61. foreach (var dogInScene in HomeController.dogsInScene)
  62. {
  63. dogInScene.StartItemConsume(ItemGroup.WATER);
  64. dogInScene.SetMoveSpeed(UnityEngine.Random.Range(0.3f, 0.6f));
  65. }
  66. }
  67. // 通过将_itemUseId 设置为非空后,启动网络通讯
  68. ItemUseController._itemUseId = itemId;
  69. }
  70. // 使用飞盘类道具
  71. if (frisbees.Contains(itemId))
  72. {
  73. GameData.playedToy = itemId; // 存储使用哪一个玩具
  74. //SceneManager.LoadScene("Playground", LoadSceneMode.Single);
  75. MaskTransitions.TransitionManager.Instance.LoadLevel("Playground");
  76. }
  77. // 使用洗澡页面道具
  78. if (showers.Contains(itemId))
  79. {
  80. GameData.bathItemId = itemId; // 存储使用哪一个玩具
  81. // SceneManager.LoadScene("Bathroom", LoadSceneMode.Single);
  82. MaskTransitions.TransitionManager.Instance.LoadLevel("Bathroom");
  83. }
  84. }
  85. #region 食物和喝水控制代码
  86. // 计算出一个碗的位置,距离所有只狗都至少2米
  87. private static Vector3 CalcBowlShowLoc()
  88. {
  89. while (true)
  90. {
  91. float x = UnityEngine.Random.Range(-3f, 3f);
  92. float z = UnityEngine.Random.Range(-3f, 3f);
  93. bowlLocation = new Vector3(x, 0, z);
  94. bool allowToPlace = true;
  95. int dogQty = HomeController.dogsInScene.Count;
  96. for (int i = 0; i < dogQty; i++) {
  97. GameObject dog = HomeController.dogsInScene[i].gameObject;
  98. GameObject dogNext;
  99. if (i == dogQty - 1)
  100. {
  101. dogNext = HomeController.dogsInScene[0].gameObject; // 如果是最后一只狗,next dog就是第一值
  102. }
  103. else
  104. {
  105. dogNext = HomeController.dogsInScene[i+1].gameObject;
  106. }
  107. if (Vector3.Distance(bowlLocation, dog.transform.position) < 2) // 距离所有只狗都至少2米
  108. {
  109. allowToPlace = false;
  110. continue;
  111. }
  112. //if (dogQty > 1) // 需要计算狗与狗的角度
  113. //{
  114. // if(CalcAngle(bowlLocation, dog.transform.position, dogNext.transform.position) < 45)
  115. // {
  116. // allowToPlace = false;
  117. // continue;
  118. // }
  119. //}
  120. }
  121. if (allowToPlace)
  122. {
  123. return bowlLocation;
  124. }
  125. }
  126. }
  127. #endregion
  128. // 废弃,待删除
  129. private static float CalcAngle(Vector3 bowlLoc, Vector3 targetLoc1, Vector3 targetPoc2)
  130. {
  131. // 计算向量AB和向量AC
  132. Vector3 vectorAB = bowlLoc - targetLoc1;
  133. Vector3 vectorAC = bowlLoc - targetPoc2;
  134. // 计算向量AB和向量AC的模长
  135. float magnitudeAB = vectorAB.magnitude;
  136. float magnitudeAC = vectorAC.magnitude;
  137. // 计算点积
  138. float dotProduct = Vector3.Dot(vectorAB, vectorAC);
  139. // 计算夹角的余弦值
  140. float cosTheta = dotProduct / (magnitudeAB * magnitudeAC);
  141. // 计算夹角θ
  142. float angleTheta = Mathf.Acos(cosTheta) * Mathf.Rad2Deg; // 将弧度转换为度数
  143. Debug.Log("夹角θ: " + angleTheta + "度");
  144. return angleTheta;
  145. }
  146. }