ItemUseController.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. }
  76. // 使用洗澡页面道具
  77. if (showers.Contains(itemId))
  78. {
  79. GameData.bathItemId = itemId; // 存储使用哪一个玩具
  80. SceneManager.LoadScene("Bathroom", LoadSceneMode.Single);
  81. }
  82. }
  83. #region 食物和喝水控制代码
  84. // 计算出一个碗的位置,距离所有只狗都至少2米
  85. private static Vector3 CalcBowlShowLoc()
  86. {
  87. while (true)
  88. {
  89. float x = UnityEngine.Random.Range(-3f, 3f);
  90. float z = UnityEngine.Random.Range(-3f, 3f);
  91. bowlLocation = new Vector3(x, 0, z);
  92. bool allowToPlace = true;
  93. int dogQty = HomeController.dogsInScene.Count;
  94. for (int i = 0; i < dogQty; i++) {
  95. GameObject dog = HomeController.dogsInScene[i].gameObject;
  96. GameObject dogNext;
  97. if (i == dogQty - 1)
  98. {
  99. dogNext = HomeController.dogsInScene[0].gameObject; // 如果是最后一只狗,next dog就是第一值
  100. }
  101. else
  102. {
  103. dogNext = HomeController.dogsInScene[i+1].gameObject;
  104. }
  105. if (Vector3.Distance(bowlLocation, dog.transform.position) < 2) // 距离所有只狗都至少2米
  106. {
  107. allowToPlace = false;
  108. continue;
  109. }
  110. //if (dogQty > 1) // 需要计算狗与狗的角度
  111. //{
  112. // if(CalcAngle(bowlLocation, dog.transform.position, dogNext.transform.position) < 45)
  113. // {
  114. // allowToPlace = false;
  115. // continue;
  116. // }
  117. //}
  118. }
  119. if (allowToPlace)
  120. {
  121. return bowlLocation;
  122. }
  123. }
  124. }
  125. #endregion
  126. // 废弃,待删除
  127. private static float CalcAngle(Vector3 bowlLoc, Vector3 targetLoc1, Vector3 targetPoc2)
  128. {
  129. // 计算向量AB和向量AC
  130. Vector3 vectorAB = bowlLoc - targetLoc1;
  131. Vector3 vectorAC = bowlLoc - targetPoc2;
  132. // 计算向量AB和向量AC的模长
  133. float magnitudeAB = vectorAB.magnitude;
  134. float magnitudeAC = vectorAC.magnitude;
  135. // 计算点积
  136. float dotProduct = Vector3.Dot(vectorAB, vectorAC);
  137. // 计算夹角的余弦值
  138. float cosTheta = dotProduct / (magnitudeAB * magnitudeAC);
  139. // 计算夹角θ
  140. float angleTheta = Mathf.Acos(cosTheta) * Mathf.Rad2Deg; // 将弧度转换为度数
  141. Debug.Log("夹角θ: " + angleTheta + "度");
  142. return angleTheta;
  143. }
  144. }