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 foodBowlItems = new() { "food_00001", // 狗粮 "food_00002", // 狗罐头 "food_00003" // 冻肉干 }; private static List waterBowlItems = new() { "water_00001", // 水 "water_00002" // 牛奶 }; private static List frisbees = new() { "toy_00001" // 飞盘 }; private static List showers = new() { "other_00001" // 打开洗澡页面的用品 }; public static string _homeUseItemId = null; // 暂存使用item数据,调用另外一个Monobehaviour Class来执行网络通讯 public static void ItemUsed(string itemId) { // 在Home场景使用道具调用_homeUseItemId来启动道具使用 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)); } // 通过将_homeUseItemId 设置为非空后,启动网络通讯,仅限于在Home场景消耗的道具 ItemUseController._homeUseItemId = itemId; } // 消费水牛奶等 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 设置为非空后,启动网络通讯,仅限于在Home场景消耗的道具 ItemUseController._homeUseItemId = 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 }