123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- using Newtonsoft.Json;
- using NUnit.Framework;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Net.NetworkInformation;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- /* 具体使用哪个道具产生的效果,包括结束的效果回调函数
- */
- public class ItemUseController : ScriptableObject
- {
- private static Vector3 bowlLocation;
- private static List<string> foodBowlItems = new()
- {
- "food_00001", // 狗粮
- "food_00002", // 狗罐头
- "food_00003" // 冻肉干
- };
- private static List<string> waterBowlItems = new()
- {
- "water_00001", // 水
- "water_00002" // 牛奶
- };
- private static List<string> frisbees = new()
- {
- "toy_00001" // 飞盘
- };
- private static List<string> showers = new()
- {
- "other_00001" // 打开洗澡页面的用品
- };
- public static string _itemUseId = null; // 暂存使用item数据,调用另外一个Monobehaviour Class来执行网络通讯
- public static void ItemUsed(string itemId)
- {
- // 在Home场景使用道具调用_itemUseId来启动道具使用
- if (SceneManager.GetActiveScene().name == "Home")
- {
- var bowls = GameObject.Find("Bowls");
- GameObject targetBowl = null;
- // 消费食物类
- if (foodBowlItems.Contains(itemId))
- {
-
- targetBowl = bowls.transform.Find("Bowl_food").gameObject;
- targetBowl.transform.localPosition = CalcBowlShowLoc();
- //var food = targetBowl.transform.Find("Food").gameObject;
- //food.SetActive(true);
- foreach (var dogInScene in HomeController.dogsInScene)
- {
- dogInScene.StartItemConsume(ItemGroup.FOOD);
- dogInScene.SetMoveSpeed(UnityEngine.Random.Range(0.3f, 0.6f));
- }
- }
- // 消费水牛奶等
- if (waterBowlItems.Contains(itemId))
- {
- targetBowl = bowls.transform.Find("Bowl_water").gameObject;
- targetBowl.transform.localPosition = CalcBowlShowLoc();
- //var water = targetBowl.transform.Find("Water").gameObject;
- //water.SetActive(true);
- foreach (var dogInScene in HomeController.dogsInScene)
- {
- dogInScene.StartItemConsume(ItemGroup.WATER);
- dogInScene.SetMoveSpeed(UnityEngine.Random.Range(0.3f, 0.6f));
- }
- }
- // 通过将_itemUseId 设置为非空后,启动网络通讯
- ItemUseController._itemUseId = itemId;
- }
-
- // 使用飞盘类道具
- if (frisbees.Contains(itemId))
- {
- GameData.playedToy = itemId; // 存储使用哪一个玩具
- //SceneManager.LoadScene("Playground", LoadSceneMode.Single);
- MaskTransitions.TransitionManager.Instance.LoadLevel("Playground");
- }
- // 使用洗澡页面道具
- if (showers.Contains(itemId))
- {
- GameData.bathItemId = itemId; // 存储使用哪一个玩具
- // SceneManager.LoadScene("Bathroom", LoadSceneMode.Single);
- MaskTransitions.TransitionManager.Instance.LoadLevel("Bathroom");
- }
-
- }
- #region 食物和喝水控制代码
- // 计算出一个碗的位置,距离所有只狗都至少2米
- private static Vector3 CalcBowlShowLoc()
- {
- while (true)
- {
- float x = UnityEngine.Random.Range(-3f, 3f);
- float z = UnityEngine.Random.Range(-3f, 3f);
- bowlLocation = new Vector3(x, 0, z);
- bool allowToPlace = true;
- int dogQty = HomeController.dogsInScene.Count;
- for (int i = 0; i < dogQty; i++) {
- GameObject dog = HomeController.dogsInScene[i].gameObject;
- GameObject dogNext;
- if (i == dogQty - 1)
- {
- dogNext = HomeController.dogsInScene[0].gameObject; // 如果是最后一只狗,next dog就是第一值
- }
- else
- {
- dogNext = HomeController.dogsInScene[i+1].gameObject;
- }
- if (Vector3.Distance(bowlLocation, dog.transform.position) < 2) // 距离所有只狗都至少2米
- {
- allowToPlace = false;
- continue;
- }
- //if (dogQty > 1) // 需要计算狗与狗的角度
- //{
- // if(CalcAngle(bowlLocation, dog.transform.position, dogNext.transform.position) < 45)
- // {
- // allowToPlace = false;
- // continue;
- // }
- //}
- }
- if (allowToPlace)
- {
- return bowlLocation;
- }
- }
- }
- #endregion
- // 废弃,待删除
- private static float CalcAngle(Vector3 bowlLoc, Vector3 targetLoc1, Vector3 targetPoc2)
- {
- // 计算向量AB和向量AC
- Vector3 vectorAB = bowlLoc - targetLoc1;
- Vector3 vectorAC = bowlLoc - targetPoc2;
- // 计算向量AB和向量AC的模长
- float magnitudeAB = vectorAB.magnitude;
- float magnitudeAC = vectorAC.magnitude;
- // 计算点积
- float dotProduct = Vector3.Dot(vectorAB, vectorAC);
- // 计算夹角的余弦值
- float cosTheta = dotProduct / (magnitudeAB * magnitudeAC);
- // 计算夹角θ
- float angleTheta = Mathf.Acos(cosTheta) * Mathf.Rad2Deg; // 将弧度转换为度数
- Debug.Log("夹角θ: " + angleTheta + "度");
- return angleTheta;
- }
- }
|