374 lines
13 KiB
C#
374 lines
13 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using BigSpace.Logic;
|
||
using BigSpace.XRCore.Base;
|
||
using BigSpace.XRCore.Event;
|
||
using Newtonsoft.Json.Bson;
|
||
using UnityEngine;
|
||
using UnityEngine.XR.Interaction.Toolkit;
|
||
using UnityStandardAssets.Water;
|
||
|
||
public class AnimalCatchMgr : MonoSingleton<AnimalCatchMgr>
|
||
{
|
||
//物体创建相关
|
||
private Transform leftParent_trsf; //在左手上的父级
|
||
private Transform rightParent_trsf; //在右手上的父级
|
||
public Transform parent_animals; //放开后的父级
|
||
public int leftAnimalType = 0; //当前左手创建的动物类型
|
||
public int rightAnimalType = 0; //当前右手创建的动物类型
|
||
public bool leftHandIsCatchObj = false; //左手上是否有物体
|
||
public bool rightHandIsCatchObj = false; //右手上是否有物体
|
||
private string path_hous = "Animalfabs/animal_wolf";
|
||
private string path_cattle = "Animalfabs/animal_cattle";
|
||
private string path_elephant = "Animalfabs/animal_elephant";
|
||
//private string path_deer = "Animalfabs/animal_deer";
|
||
|
||
Object obj_cattle;
|
||
//Object obj_deer;
|
||
Object obj_elephant;
|
||
Object obj_hous;
|
||
GameObject leftNowObj = null;
|
||
GameObject rightNowObj = null;
|
||
|
||
bool isCanCatch = false; //是否可以抓取
|
||
|
||
bool isLeftCanCreate = true; //左手是否可以创建动物
|
||
bool isRightCanCreate = true; //右手是否可以创建动物
|
||
//bool isCreateOver = true; //动物创建是否结束
|
||
int createAnimalType = 0; //创建动物的类型(1:抓马 2:抓象 3:抓牛)
|
||
|
||
int maxLenth = 20; //最大动物数
|
||
int index = 0;
|
||
List<GameObject> animalList = new List<GameObject>(); //所有动物的集合
|
||
|
||
//物体检测位置相关
|
||
private MeshCollider meshContainer;
|
||
private string smallObjectTag = "Animal";
|
||
private int checksPerFrame = 10; // 每帧检测数量
|
||
private int currentIndex = 0;
|
||
private bool isCheckAnimalInBox; //检测抓到的动物是否在框内
|
||
|
||
|
||
// Start is called before the first frame update
|
||
void Start()
|
||
{
|
||
leftParent_trsf = HandHelp.Instance.leftHandPalmPosition;
|
||
rightParent_trsf = HandHelp.Instance.rightHandPalmPosition;
|
||
|
||
GlobalEventMgr.Listen<int>(GameEvent.EventCatchAnimalState, GameDataManage_EventCatchAnimal);
|
||
//GlobalEventMgr.Listen<bool,int>(GameEvent.EventCtrAnimalModel, GameDataManage_EventCtrAnimalModel);
|
||
GlobalEventMgr.Listen<int, Vector3>(GameEvent.EventAnimalSync, GameDataManage_EventAnimalSync);
|
||
|
||
//obj_deer = Resources.Load(path_deer, typeof(GameObject));
|
||
obj_hous = Resources.Load(path_hous, typeof(GameObject));
|
||
obj_elephant = Resources.Load(path_elephant, typeof(GameObject));
|
||
obj_cattle = Resources.Load(path_cattle, typeof(GameObject));
|
||
|
||
animalList.AddRange(GameObject.FindGameObjectsWithTag(smallObjectTag));
|
||
}
|
||
|
||
//检测物体位置是否在范围内
|
||
void Update()
|
||
{
|
||
if (isCheckAnimalInBox)
|
||
{
|
||
int endIndex = Mathf.Min(currentIndex + checksPerFrame, animalList.Count);
|
||
if (meshContainer == null) return;
|
||
for (int i = currentIndex; i < endIndex; i++)
|
||
{
|
||
if (animalList[i] == null) continue;
|
||
GameObject obj = animalList[i];
|
||
bool isInside = meshContainer.bounds.Contains(obj.transform.position);
|
||
// 这里可以添加你的处理逻辑
|
||
if (isInside)
|
||
{
|
||
// Debug.Log($"{obj.name} 在容器内");
|
||
}
|
||
else
|
||
{
|
||
if (!obj.transform.Find("model").GetComponent<MeshCollider>().isTrigger)
|
||
{
|
||
// Debug.Log(obj.name + "不在容器内要删除:" + i);
|
||
if (obj != null)
|
||
Destroy(obj);
|
||
animalList.RemoveAt(i);
|
||
}
|
||
else
|
||
{
|
||
//Debug.Log("没有放手");
|
||
}
|
||
}
|
||
}
|
||
currentIndex = endIndex >= animalList.Count ? 0 : endIndex;
|
||
}
|
||
|
||
//if (Input.GetKeyDown(KeyCode.U))
|
||
//{
|
||
// GameDataManage_EventCtrAnimalModel(true);
|
||
//}
|
||
}
|
||
|
||
public void SetMeshCollider(MeshCollider collier)
|
||
{
|
||
meshContainer = collier;
|
||
}
|
||
|
||
void GameDataManage_EventCatchAnimal(int type)
|
||
{
|
||
createAnimalType = type;
|
||
//1:抓牛马(随机) 2:抓象 3:抓牛 8:停止抓取 9:清除所有
|
||
if (type == 1)
|
||
{
|
||
GlobalEventMgr.Dispatch(GameEvent.EventSetReadyCatchAnimal, false); //隐藏人物的碰撞,开启脚下的碰撞
|
||
isCanCatch = true;
|
||
isCheckAnimalInBox = true;
|
||
}
|
||
else if (type == 2)
|
||
{
|
||
isCanCatch = true;
|
||
}
|
||
else if (type == 3)
|
||
{
|
||
isCanCatch = true;
|
||
}
|
||
else if (type == 8)
|
||
{
|
||
isCanCatch = false;
|
||
}
|
||
else if (type == 9) //删除所有
|
||
{
|
||
isCanCatch = false;
|
||
for (int i = animalList.Count - 1; i >= 0; i--)
|
||
{
|
||
if (animalList[i] != null)
|
||
{
|
||
Destroy(animalList[i]);
|
||
}
|
||
}
|
||
animalList.Clear();
|
||
GlobalEventMgr.Dispatch(GameEvent.EventSetReadyCatchAnimal, true); //开启人物的碰撞,隐藏脚下的碰撞
|
||
|
||
}
|
||
}
|
||
|
||
void GameDataManage_EventCtrAnimalModel(bool isShow,int handType)
|
||
{
|
||
if (handType == 1)
|
||
{
|
||
CheckLeftAnimal(isShow);
|
||
}
|
||
else
|
||
{
|
||
CheckRightAnimal(isShow);
|
||
}
|
||
}
|
||
|
||
#region 创建左手动物
|
||
void CheckLeftAnimal(bool isShow)
|
||
{
|
||
Debug.Log("识别到了左手势:" + isShow);
|
||
if (isShow)
|
||
{
|
||
if (!isCanCatch) return; //未开启功能返回
|
||
if (isLeftCanCreate)// && isCreateOver
|
||
{
|
||
//isCreateOver = false;
|
||
isLeftCanCreate = false;
|
||
if (animalList.Count >= maxLenth)
|
||
{
|
||
GameObject firstObj = animalList[0];
|
||
if (firstObj != null)
|
||
Destroy(firstObj);
|
||
animalList.RemoveAt(0);
|
||
}
|
||
CrateLeftAnimal();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//Debug.Log("是否创建结束:"+ isCreateOver);
|
||
if (leftHandIsCatchObj && leftNowObj != null) //isCreateOver &&
|
||
{
|
||
leftHandIsCatchObj = false;
|
||
//Debug.Log("进入放开物体");
|
||
leftNowObj.transform.parent = leftParent_trsf;
|
||
leftNowObj.transform.localPosition = Vector3.zero;
|
||
leftNowObj.transform.parent = parent_animals;
|
||
|
||
leftNowObj.transform.Find("model").GetComponent<Rigidbody>().useGravity = true;
|
||
leftNowObj.transform.Find("model").GetComponent<Rigidbody>().isKinematic = false;
|
||
leftNowObj.transform.Find("model").GetComponent<MeshCollider>().isTrigger = false;
|
||
isLeftCanCreate = true;
|
||
//Debug.Log("放开物体结束");
|
||
GlobalEventMgr.Dispatch(GameEvent.EventAnimalSend, leftAnimalType); //这里发送同步消息 animalType
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
void CrateLeftAnimal()
|
||
{
|
||
leftHandIsCatchObj = true;
|
||
int type = createAnimalType; // Random.Range(0, 4);
|
||
Debug.Log("创建开始:" + type);
|
||
if (type == 1) //这类型需要随机
|
||
{
|
||
int rand_Type = Random.Range(1, 3);
|
||
if (rand_Type == 1)
|
||
{
|
||
leftAnimalType = 1;
|
||
leftNowObj = Instantiate(obj_hous, leftParent_trsf) as GameObject;
|
||
}
|
||
else
|
||
{
|
||
leftAnimalType = 3;
|
||
leftNowObj = Instantiate(obj_cattle, leftParent_trsf) as GameObject;
|
||
}
|
||
|
||
}
|
||
else if (type == 2)
|
||
{
|
||
leftAnimalType = 2;
|
||
leftNowObj = Instantiate(obj_elephant, leftParent_trsf) as GameObject;
|
||
}
|
||
else
|
||
{
|
||
leftAnimalType = 3;
|
||
leftNowObj = Instantiate(obj_cattle, leftParent_trsf) as GameObject;
|
||
}
|
||
leftNowObj.transform.localPosition = Vector3.zero;
|
||
leftNowObj.transform.localRotation = new Quaternion(0, 0, 90, 90);
|
||
leftNowObj.transform.localScale = new Vector3(1f, 1f, 1f);
|
||
|
||
leftNowObj.transform.Find("model").GetComponent<MeshCollider>().isTrigger = true;
|
||
leftNowObj.transform.Find("model").GetComponent<Rigidbody>().useGravity = false;
|
||
leftNowObj.transform.Find("model").GetComponent<Rigidbody>().isKinematic = true;
|
||
leftNowObj.SetActive(true);
|
||
leftNowObj.name = "animal" + (index++);
|
||
animalList.Add(leftNowObj);
|
||
}
|
||
#endregion
|
||
|
||
#region 创建右手动物
|
||
void CheckRightAnimal(bool isShow)
|
||
{
|
||
//Debug.Log("识别到了手势:" + isShow);
|
||
if (isShow)
|
||
{
|
||
if (!isCanCatch) return; //未开启功能返回
|
||
if (isRightCanCreate)// && isCreateOver
|
||
{
|
||
//isCreateOver = false;
|
||
isRightCanCreate = false;
|
||
if (animalList.Count >= maxLenth)
|
||
{
|
||
GameObject firstObj = animalList[0];
|
||
if (firstObj != null)
|
||
Destroy(firstObj);
|
||
animalList.RemoveAt(0);
|
||
}
|
||
CrateRigthAnimal();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//Debug.Log("是否创建结束:"+ isCreateOver);
|
||
if (rightHandIsCatchObj && rightNowObj != null) //isCreateOver &&
|
||
{
|
||
rightHandIsCatchObj = false;
|
||
//Debug.Log("进入放开物体");
|
||
rightNowObj.transform.parent = rightParent_trsf;
|
||
rightNowObj.transform.localPosition = Vector3.zero;
|
||
rightNowObj.transform.parent = parent_animals;
|
||
|
||
rightNowObj.transform.Find("model").GetComponent<Rigidbody>().useGravity = true;
|
||
rightNowObj.transform.Find("model").GetComponent<Rigidbody>().isKinematic = false;
|
||
rightNowObj.transform.Find("model").GetComponent<MeshCollider>().isTrigger = false;
|
||
isRightCanCreate = true;
|
||
//Debug.Log("放开物体结束");
|
||
GlobalEventMgr.Dispatch(GameEvent.EventAnimalSend, rightAnimalType); //这里发送同步消息 animalType
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
void CrateRigthAnimal()
|
||
{
|
||
rightHandIsCatchObj = true;
|
||
int type = createAnimalType; // Random.Range(0, 4);
|
||
Debug.Log("创建开始:"+ type);
|
||
if (type == 1) //这类型需要随机
|
||
{
|
||
int rand_Type = Random.Range(1, 3);
|
||
if (rand_Type == 1)
|
||
{
|
||
rightAnimalType = 1;
|
||
rightNowObj = Instantiate(obj_hous, rightParent_trsf) as GameObject;
|
||
}
|
||
else
|
||
{
|
||
rightAnimalType = 3;
|
||
rightNowObj = Instantiate(obj_cattle, rightParent_trsf) as GameObject;
|
||
}
|
||
|
||
}
|
||
else if (type == 2)
|
||
{
|
||
rightAnimalType = 2;
|
||
rightNowObj = Instantiate(obj_elephant, rightParent_trsf) as GameObject;
|
||
}
|
||
else
|
||
{
|
||
rightAnimalType = 3;
|
||
rightNowObj = Instantiate(obj_cattle, rightParent_trsf) as GameObject;
|
||
}
|
||
//else
|
||
//{
|
||
// animalType = 4;
|
||
// nowObj = Instantiate(obj_cattle, parent_trsf) as GameObject;
|
||
// //nowObj = Instantiate(obj_wolf, parent_trsf) as GameObject;
|
||
//}
|
||
rightNowObj.transform.localPosition = Vector3.zero;
|
||
rightNowObj.transform.localRotation = new Quaternion(0,0,90,90);
|
||
rightNowObj.transform.localScale = new Vector3(1f,1f,1f);
|
||
|
||
rightNowObj.transform.Find("model").GetComponent<MeshCollider>().isTrigger = true;
|
||
rightNowObj.transform.Find("model").GetComponent<Rigidbody>().useGravity = false;
|
||
rightNowObj.transform.Find("model").GetComponent<Rigidbody>().isKinematic = true ;
|
||
rightNowObj.SetActive(true);
|
||
rightNowObj.name = "animal" + (index++);
|
||
animalList.Add(rightNowObj);
|
||
}
|
||
#endregion
|
||
|
||
|
||
//收到同组玩家创建动物,这里要同步创建
|
||
void GameDataManage_EventAnimalSync(int animalType,Vector3 position)
|
||
{
|
||
GameObject animalObj = null;
|
||
Debug.Log("创建动物:"+animalType);//(1:抓马 2:抓象 3:抓牛)
|
||
if (animalType == 1)
|
||
{
|
||
animalObj = Instantiate(obj_hous, parent_animals) as GameObject;
|
||
}
|
||
else if (animalType == 2)
|
||
{
|
||
animalObj = Instantiate(obj_elephant, parent_animals) as GameObject;
|
||
}
|
||
else
|
||
{
|
||
animalObj = Instantiate(obj_cattle, parent_animals) as GameObject;
|
||
}
|
||
|
||
animalObj.transform.position = position;
|
||
|
||
animalObj.transform.Find("model").GetComponent<Rigidbody>().useGravity = true;
|
||
animalObj.transform.Find("model").GetComponent<Rigidbody>().isKinematic = false;
|
||
animalObj.transform.Find("model").GetComponent<MeshCollider>().isTrigger = false;
|
||
animalObj.SetActive(true);
|
||
animalObj.name = "animal" + (index++);
|
||
animalList.Add(animalObj);
|
||
}
|
||
}
|
||
|