ItemUseController.cs 5.3 KB

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