456 lines
19 KiB
C#
456 lines
19 KiB
C#
/********************************************************************************
|
||
* 文件名:CollectItem.cs
|
||
* 全路径: \Script\Mission\CollectItem.cs
|
||
* 创建人: 贺文鹏
|
||
* 创建时间:2014-02-17
|
||
*
|
||
* 功能说明: 客户端采集物品逻辑,刷新场景内物品 CollectItem.txt表配置。
|
||
*
|
||
* 修改记录:
|
||
*********************************************************************************/
|
||
|
||
using System.Collections.Generic;
|
||
using Games.Events;
|
||
using Games.GlobeDefine;
|
||
using Games.LogicObj;
|
||
using Games.Mission;
|
||
using GCGame.Table;
|
||
using UnityEngine;
|
||
using Module.Log;
|
||
|
||
public class CollectItem : Singleton<CollectItem>
|
||
{
|
||
private readonly List<CollectItemData> _itemList = new List<CollectItemData>();
|
||
private GameObject m_ItemObj;
|
||
private int m_MissionID;
|
||
|
||
public CollectItem()
|
||
{
|
||
CleanUp();
|
||
}
|
||
|
||
public void CleanUp()
|
||
{
|
||
m_MissionID = -1;
|
||
}
|
||
|
||
// 创建item
|
||
private int _curSeceneId = -1;
|
||
public void InitCollectItem(int nSceneId)
|
||
{
|
||
if (nSceneId < 0)
|
||
return;
|
||
ClearMissionCollectitemInfo();
|
||
_curSeceneId = nSceneId;
|
||
CleanUp();
|
||
// 注:这个管理器实际是无法管理物体析构情况的,物体析构由Unity场景切换处理,因此每次重新生成都试图重新扫描物体
|
||
// 隐藏非当前场景的采集物体
|
||
for (var i = _itemList.Count - 1; i >= 0; i--)
|
||
{
|
||
var item = _itemList[i];
|
||
if (item.collectItem == null || item.collectItem.gameObject == null)
|
||
_itemList.RemoveAt(i);
|
||
else if (item.sceneId != nSceneId)
|
||
item.collectItem.gameObject.SetActive(false);
|
||
}
|
||
var collectItemList = TableManager.GetCollectItemByID(nSceneId);
|
||
// 开启全部需要的采集物
|
||
if (collectItemList != null)
|
||
{
|
||
foreach (var collectItem in collectItemList)
|
||
{
|
||
if (collectItem != null)
|
||
{
|
||
for (var i = 0; i < collectItem.Count; i++)
|
||
{
|
||
//判断是否需要任务才显示
|
||
if(!collectItem.ShowMissionParam.Equals("-1"))
|
||
{
|
||
continue;
|
||
}
|
||
|
||
var strName = "CollectItem" + collectItem.Index + "_" + i;
|
||
var item = _itemList.Find(a => a.sceneId == nSceneId && a.index == collectItem.Index);
|
||
if (item == null)
|
||
{
|
||
var initData = new Obj_Init_CollectItem
|
||
{
|
||
m_fX = collectItem.PosX,
|
||
m_fZ = collectItem.PosZ,
|
||
collectItem = collectItem,
|
||
index = i,
|
||
m_StrName = strName
|
||
};
|
||
var newObj = Singleton<ObjManager>.Instance.NewCharacterObj(
|
||
GameDefine_Globe.OBJ_TYPE.OBJ_COLLECTITEM,
|
||
initData);
|
||
item = new CollectItemData
|
||
{
|
||
collectItem = newObj,
|
||
index = collectItem.Index,
|
||
sceneId = nSceneId,
|
||
};
|
||
_itemList.Add(item);
|
||
}
|
||
else
|
||
item.collectItem.gameObject.SetActive(true);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//切换场景的时候要刷新一次当前任务需要创建的Item
|
||
InitMissionCollectItem();
|
||
}
|
||
|
||
private readonly List<ShowMissionCollectItem> missionCollectItemList = new List<ShowMissionCollectItem>();
|
||
private struct ShowMissionCollectItem
|
||
{
|
||
public int showMissionId;
|
||
public int deleteMissionId;
|
||
public int showMissionIndex;
|
||
public int deleMissionIndex;
|
||
public bool isShowCollectItemWhenMissionComplete;
|
||
public bool isDeleteCollectItemWhenMissionComplete;
|
||
public Tab_CollectItem collectItemTab;
|
||
public ShowMissionCollectItem(int showId, int showIndex, int deleteId, int deleteIndex, bool isShow, bool isDelete, Tab_CollectItem tab)
|
||
{
|
||
showMissionId = showId;
|
||
showMissionIndex = showIndex;
|
||
isShowCollectItemWhenMissionComplete = isShow;
|
||
|
||
deleteMissionId = deleteId;
|
||
deleMissionIndex = deleteIndex;
|
||
isDeleteCollectItemWhenMissionComplete = isDelete;
|
||
|
||
collectItemTab = tab;
|
||
}
|
||
}
|
||
|
||
//要在切换场景的时候调用
|
||
public void ClearMissionCollectitemInfo()
|
||
{
|
||
missionCollectItemList.Clear();
|
||
}
|
||
|
||
void GetAllMissionCollectItemInfo()
|
||
{
|
||
var collectItemList = TableManager.GetCollectItemByID(_curSeceneId);
|
||
|
||
if (collectItemList == null)
|
||
return;
|
||
|
||
for(int index = 0; index < collectItemList.Count; index++)
|
||
{
|
||
if (!collectItemList[index].ShowMissionParam.Equals("-1"))
|
||
{
|
||
var showMissionParam = collectItemList[index].ShowMissionParam;
|
||
var deleteMissionParam = collectItemList[index].DeleteMissionParam;
|
||
|
||
var showMissionId = int.Parse(showMissionParam.Split('|')[0]);
|
||
var deleteMisisonId = int.Parse(deleteMissionParam.Split('|')[0]);
|
||
|
||
var showMissionIndex = int.Parse(showMissionParam.Split('|')[1]);
|
||
var deleteMissionIndex = int.Parse(deleteMissionParam.Split('|')[1]);
|
||
|
||
var isShowWhenComplete = showMissionParam.Split('|')[1].Equals("-1");
|
||
var isDeleteWhenComplete = deleteMissionParam.Split('|')[1].Equals("-1");
|
||
|
||
ShowMissionCollectItem info = new ShowMissionCollectItem(showMissionId,
|
||
showMissionIndex, deleteMisisonId, deleteMissionIndex, isShowWhenComplete, isDeleteWhenComplete, collectItemList[index]);
|
||
|
||
missionCollectItemList.Add(info);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
public void InitMissionCollectItem()
|
||
{
|
||
if (missionCollectItemList.Count == 0)
|
||
GetAllMissionCollectItemInfo();
|
||
|
||
foreach (var mission in GameManager.gameManager.MissionManager.MissionList.m_aMission)
|
||
{
|
||
//只用判断主线任务的ID
|
||
var missionBase = TableManager.GetMissionBaseByID(mission.Key, 0);
|
||
if(missionBase.MissionType == (int)MISSIONTYPE.MISSION_MAIN)
|
||
{
|
||
var missionId = missionBase.Id;
|
||
|
||
for(int index = 0; index < missionCollectItemList.Count; index++)
|
||
{
|
||
var _isRemoveItem = false;
|
||
var missionCollectItemInfo = missionCollectItemList[index];
|
||
if (missionId >= missionCollectItemInfo.showMissionId && missionId <= missionCollectItemInfo.deleteMissionId)
|
||
{
|
||
//是否已经达到删除的条件
|
||
if (missionId == missionCollectItemInfo.deleteMissionId)
|
||
{
|
||
if(missionCollectItemInfo.isDeleteCollectItemWhenMissionComplete )
|
||
{
|
||
if(GameManager.gameManager.MissionManager.GetMissionState(missionId) == (int)MissionState.Mission_Completed)
|
||
{
|
||
//达到删除条件
|
||
for (int itemListIndex = 0; itemListIndex < _itemList.Count; itemListIndex++)
|
||
{
|
||
if (_itemList[itemListIndex].index == missionCollectItemInfo.collectItemTab.Index)
|
||
{
|
||
RemoveItem(_itemList[itemListIndex].collectItem.gameObject, itemListIndex);
|
||
_isRemoveItem = true;
|
||
}
|
||
}
|
||
}
|
||
} else
|
||
{
|
||
if (GameManager.gameManager.MissionManager.getCurMissionIndex(missionId) >= missionCollectItemInfo.deleMissionIndex)
|
||
{
|
||
//达到删除条件
|
||
for (int itemListIndex = 0; itemListIndex < _itemList.Count; itemListIndex++)
|
||
{
|
||
if (_itemList[itemListIndex].index == missionCollectItemInfo.collectItemTab.Index)
|
||
{
|
||
RemoveItem(_itemList[itemListIndex].collectItem.gameObject, itemListIndex);
|
||
_isRemoveItem = true;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if (_isRemoveItem)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
//没有达到删除的条件,判断是否满足创建条件
|
||
//分两种情况:1.MisisonId相等,那就判断Index跟是否全部完成
|
||
//2.missionId大于创建Item需要的MisisonId就直接创建,删除的在上面已经判断了。
|
||
if (missionId == missionCollectItemInfo.showMissionId)
|
||
{
|
||
if(missionCollectItemInfo.isShowCollectItemWhenMissionComplete)
|
||
{
|
||
if(GameManager.gameManager.MissionManager.GetMissionState(missionId) == (int)MissionState.Mission_Completed)
|
||
CreateMissionCollectItem(missionCollectItemInfo);
|
||
}else
|
||
{
|
||
if (GameManager.gameManager.MissionManager.getCurMissionIndex(missionId) >= missionCollectItemInfo.showMissionIndex)
|
||
{
|
||
CreateMissionCollectItem(missionCollectItemInfo);
|
||
}else
|
||
{
|
||
}
|
||
}
|
||
}else
|
||
{
|
||
CreateMissionCollectItem(missionCollectItemInfo); //处于创建跟删除之间
|
||
}
|
||
}
|
||
}
|
||
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
private void CreateMissionCollectItem(ShowMissionCollectItem info)
|
||
{
|
||
if (_curSeceneId != info.collectItemTab.Id)
|
||
return; //不是当前场景
|
||
|
||
var strName = "CollectItem" + info.collectItemTab.Index + "_" + _itemList.Count;
|
||
var item = _itemList.Find(a => a.sceneId == _curSeceneId && a.index == info.collectItemTab.Index);
|
||
if (item == null || item.collectItem==null || item.collectItem.gameObject == null)
|
||
{
|
||
if(item!=null)
|
||
{
|
||
_itemList.Remove(item);
|
||
}
|
||
var initData = new Obj_Init_CollectItem
|
||
{
|
||
m_fX = info.collectItemTab.PosX,
|
||
m_fZ = info.collectItemTab.PosZ,
|
||
collectItem = info.collectItemTab,
|
||
index = _itemList.Count,
|
||
m_StrName = strName
|
||
};
|
||
var newObj = Singleton<ObjManager>.Instance.NewCharacterObj(
|
||
GameDefine_Globe.OBJ_TYPE.OBJ_COLLECTITEM,
|
||
initData);
|
||
item = new CollectItemData
|
||
{
|
||
collectItem = newObj,
|
||
index = info.collectItemTab.Index,
|
||
sceneId = info.collectItemTab.Id,
|
||
};
|
||
_itemList.Add(item);
|
||
}
|
||
else
|
||
item.collectItem.gameObject.SetActive(true);
|
||
}
|
||
|
||
// 开始从场景中移除物品
|
||
public bool RemoveItem(GameObject ItemObj, int deleteMissionItemIndex = -1)
|
||
{
|
||
if (ItemObj)
|
||
{
|
||
if(deleteMissionItemIndex != -1)
|
||
{
|
||
ItemObj.SetActive(false);
|
||
ItemObj.transform.SetParent(null);
|
||
return true;
|
||
}
|
||
|
||
// 玩家任务检测
|
||
var nMissionIDList = GameManager.gameManager.MissionManager.GetAllMissionID();
|
||
var nMissionCount = nMissionIDList.Count;
|
||
if (nMissionCount <= 0) return false;
|
||
|
||
//距离检测
|
||
if (Singleton<ObjManager>.GetInstance().MainPlayer == null) return false;
|
||
var userPos = Singleton<ObjManager>.GetInstance().MainPlayer.Position;
|
||
|
||
var TargetPos = ItemObj.transform.position;
|
||
TargetPos.y = userPos.y;
|
||
//var dis = Vector3.Distance(userPos, TargetPos);
|
||
//if (dis > 3f) return false;
|
||
|
||
|
||
Tab_MissionCollectItem CItem = null;
|
||
Obj_CollectItem otherGameObj = null;
|
||
// 遍历任务
|
||
foreach (var nMissionID in nMissionIDList)
|
||
{
|
||
var MissionTab = TableManager.GetMissionBaseByID(nMissionID, 0);
|
||
if (MissionTab == null) continue;
|
||
var missionLogic = TableManager.GetMissionLogicByID(MissionTab.LogicID, 0);
|
||
if (missionLogic == null) continue;
|
||
var m_MissionLogicType =
|
||
missionLogic.GetLogicTypebyIndex(
|
||
GameManager.gameManager.MissionManager.getCurMissionIndex(nMissionID));
|
||
if (m_MissionLogicType == -1) continue;
|
||
if (m_MissionLogicType != (int) TableType.Table_CollectItem) continue;
|
||
|
||
// 任务状态 已完成
|
||
if ((int) MissionState.Mission_Completed ==
|
||
GameManager.gameManager.MissionManager.GetMissionState(nMissionID)) continue;
|
||
CItem = TableManager.GetMissionCollectItemByID(
|
||
missionLogic.GetLogicIDbyIndex(
|
||
GameManager.gameManager.MissionManager.getCurMissionIndex(nMissionID)), 0);
|
||
if (CItem == null) continue;
|
||
var gItemObj = Singleton<ObjManager>.GetInstance().FindOtherGameObj(ItemObj.name);
|
||
if (gItemObj)
|
||
{
|
||
otherGameObj = gItemObj.GetComponent<Obj_CollectItem>();
|
||
if (otherGameObj && otherGameObj.GetIntParamByIndex(0) == CItem.CharModelID)
|
||
{
|
||
m_MissionID = nMissionID;
|
||
break;
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
// 无任务
|
||
if (m_MissionID < 0) return false;
|
||
|
||
m_ItemObj = ItemObj;
|
||
GameManager.gameManager.SoundManager.PlaySoundEffect(9); //collect
|
||
|
||
if (Singleton<ObjManager>.GetInstance().MainPlayer)
|
||
{
|
||
//切换到动作合集状态(各种动作)
|
||
Singleton<ObjManager>.GetInstance().MainPlayer.FaceTo(otherGameObj.Position);
|
||
Singleton<ObjManager>.GetInstance().MainPlayer
|
||
.OnSwithObjAnimState(GameDefine_Globe.OBJ_ANIMSTATE.STATE_FINSH);
|
||
if(Singleton<ObjManager>.GetInstance().MainPlayer.AnimLogic)
|
||
Singleton<ObjManager>.GetInstance().MainPlayer.AnimLogic.Play(165);
|
||
}
|
||
|
||
// 通知采集条
|
||
UIManager.ShowUI(UIInfo.CollectItemSlider,
|
||
delegate
|
||
{
|
||
CollectItemSliderLogic.Instance().setMissionTime(otherGameObj, CItem, CItem.Time, m_MissionID, -1,
|
||
CItem.ColliderDesc);
|
||
});
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
// 采集条出发 真正删除物品
|
||
public void SafeDeleteItem()
|
||
{
|
||
if (m_ItemObj)
|
||
if (m_MissionID >= 0)
|
||
{
|
||
var MissionTab = TableManager.GetMissionBaseByID(m_MissionID, 0);
|
||
var missionLogic = TableManager.GetMissionLogicByID(MissionTab.LogicID, 0);
|
||
var m_Count = missionLogic.getLogicTypeCount();
|
||
var m_LogicType = -1;
|
||
if (missionLogic == null) return;
|
||
|
||
if (MissionTab == null) return;
|
||
m_LogicType =
|
||
missionLogic.GetLogicTypebyIndex(
|
||
GameManager.gameManager.MissionManager.getCurMissionIndex(m_MissionID));
|
||
if (m_LogicType == -1) return;
|
||
if (m_LogicType != (int) TableType.Table_CollectItem) return;
|
||
var m_CollectItemId =
|
||
missionLogic.GetLogicIDbyIndex(
|
||
GameManager.gameManager.MissionManager.getCurMissionIndex(m_MissionID));
|
||
var CItem = TableManager.GetMissionCollectItemByID(m_CollectItemId, 0);
|
||
if (CItem == null) return;
|
||
var gItemObj = Singleton<ObjManager>.GetInstance().FindOtherGameObj(m_ItemObj.name);
|
||
if (gItemObj)
|
||
{
|
||
var otherGameObj = gItemObj.GetComponent<Obj_CollectItem>();
|
||
if (otherGameObj && otherGameObj.GetIntParamByIndex(0) == CItem.CharModelID)
|
||
{
|
||
var index = GameManager.gameManager.MissionManager.getCurMissionIndex(m_MissionID);
|
||
GameManager.gameManager.MissionManager.ReqSetMissionParam(m_MissionID, index, m_LogicType);
|
||
|
||
m_ItemObj = null;
|
||
m_MissionID = -1;
|
||
ReSeedItemEvent(GameManager.gameManager.RunningScene, otherGameObj.GetIntParamByIndex(1),
|
||
otherGameObj.GetIntParamByIndex(2));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void ReSeedItemEvent(int nSceneID, int nSceneIndex, int nItemIndex)
|
||
{
|
||
var cItem = TableManager.GetCollectItemByID(nSceneID, nSceneIndex);
|
||
if (cItem == null) return;
|
||
|
||
var _event = new GameEvent(GameDefine_Globe.EVENT_DEFINE.EVENT_COLLECTITEM_RESEED);
|
||
_event.IsDelay = true;
|
||
_event.DelayTime = cItem.AutoLifeTime;
|
||
_event.AddIntParam(nSceneID);
|
||
_event.AddIntParam(nSceneIndex);
|
||
_event.AddIntParam(nItemIndex);
|
||
Singleton<EventSystem>.GetInstance().PushEvent(_event);
|
||
}
|
||
|
||
public void ReSeedItems(int nSceneID, int nSceneIndex, int nItemIndex)
|
||
{
|
||
if (nSceneID < 0 || nSceneID != GameManager.gameManager.RunningScene) return;
|
||
|
||
var cItem = TableManager.GetCollectItemByID(nSceneID, nItemIndex);
|
||
if (cItem == null) return;
|
||
|
||
var strName = "CollectItem" + cItem.Index + nItemIndex;
|
||
var gItemObj = Singleton<ObjManager>.GetInstance().FindOtherGameObj(strName);
|
||
if (gItemObj) gItemObj.gameObject.SetActive(true);
|
||
}
|
||
|
||
private class CollectItemData
|
||
{
|
||
public int sceneId;
|
||
public int index;
|
||
public ObjParent collectItem;
|
||
}
|
||
} |