ItemUseController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. public static string _itemUseId = null; // 暂存使用item数据,调用另外一个Monobehaviour Class来执行网络通讯
  26. public static void ItemUsed(string itemId)
  27. {
  28. // 消费食物类
  29. if (foodBowlItems.Contains(itemId))
  30. {
  31. var bowls = GameObject.Find("Bowls");
  32. var targetBowl = bowls.transform.Find("Bowl_food").gameObject;
  33. targetBowl.transform.localPosition = CalcBowlShowLoc();
  34. var food = targetBowl.transform.Find("Food").gameObject;
  35. food.SetActive(true);
  36. foreach (var dogInScene in HomeController.dogsInScene)
  37. {
  38. dogInScene.StartItemConsume(ItemGroup.food);
  39. dogInScene.SetMoveSpeed(UnityEngine.Random.Range(0.3f, 0.6f));
  40. }
  41. }
  42. // 消费水牛奶等
  43. if (waterBowlItems.Contains(itemId))
  44. {
  45. var bowls = GameObject.Find("Bowls");
  46. var targetBowl = bowls.transform.Find("Bowl_water").gameObject;
  47. targetBowl.transform.localPosition = CalcBowlShowLoc();
  48. var water = targetBowl.transform.Find("Water").gameObject;
  49. water.SetActive(true);
  50. foreach (var dogInScene in HomeController.dogsInScene)
  51. {
  52. dogInScene.StartItemConsume(ItemGroup.water);
  53. dogInScene.SetMoveSpeed(UnityEngine.Random.Range(0.3f, 0.6f));
  54. }
  55. }
  56. // TODO 以后打开 网络通讯后刷新数据
  57. //ItemUseController._itemUseId = itemId;
  58. }
  59. // 计算出一个碗的位置,距离所有只狗都至少2米,夹角至少要45度
  60. private static Vector3 CalcBowlShowLoc()
  61. {
  62. while (true)
  63. {
  64. float x = UnityEngine.Random.Range(-3f, 3f);
  65. float z = UnityEngine.Random.Range(-3f, 3f);
  66. bowlLocation = new Vector3(x, 0, z);
  67. bool allowToPlace = true;
  68. int dogQty = HomeController.dogsInScene.Count;
  69. for (int i = 0; i < dogQty; i++) {
  70. GameObject dog = HomeController.dogsInScene[i].gameObject;
  71. GameObject dogNext;
  72. if (i == dogQty - 1)
  73. {
  74. dogNext = HomeController.dogsInScene[0].gameObject; // 如果是最后一只狗,next dog就是第一值
  75. }
  76. else
  77. {
  78. dogNext = HomeController.dogsInScene[i+1].gameObject;
  79. }
  80. if (Vector3.Distance(bowlLocation, dog.transform.position) < 2) // 距离所有只狗都至少2米
  81. {
  82. allowToPlace = false;
  83. continue;
  84. }
  85. //if (dogQty > 1) // 需要计算狗与狗的角度
  86. //{
  87. // if(CalcAngle(bowlLocation, dog.transform.position, dogNext.transform.position) < 45)
  88. // {
  89. // allowToPlace = false;
  90. // continue;
  91. // }
  92. //}
  93. }
  94. if (allowToPlace)
  95. {
  96. return bowlLocation;
  97. }
  98. }
  99. }
  100. // 废弃,待删除
  101. private static float CalcAngle(Vector3 bowlLoc, Vector3 targetLoc1, Vector3 targetPoc2)
  102. {
  103. // 计算向量AB和向量AC
  104. Vector3 vectorAB = bowlLoc - targetLoc1;
  105. Vector3 vectorAC = bowlLoc - targetPoc2;
  106. // 计算向量AB和向量AC的模长
  107. float magnitudeAB = vectorAB.magnitude;
  108. float magnitudeAC = vectorAC.magnitude;
  109. // 计算点积
  110. float dotProduct = Vector3.Dot(vectorAB, vectorAC);
  111. // 计算夹角的余弦值
  112. float cosTheta = dotProduct / (magnitudeAB * magnitudeAC);
  113. // 计算夹角θ
  114. float angleTheta = Mathf.Acos(cosTheta) * Mathf.Rad2Deg; // 将弧度转换为度数
  115. Debug.Log("夹角θ: " + angleTheta + "度");
  116. return angleTheta;
  117. }
  118. }