using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;
using GameFramework.Event;
namespace YLBattle
{
public class ZoneSpawnerLogic : MonoBehaviour
{
///
/// 专属GUID生成器
///
protected static int _SGuid = 0;
//protected static int SpawnerGUID { get { return ++_SGuid; } }
protected static int CaretSpawnerGUID { get { return _SGuid; } }
///
/// 孵化器ID
///
//protected int spawnerId = 0;
protected bool isClose = false;
public bool IsClose { get { return isClose; } set { isClose = value; } }
///
/// 运行时间
///
public float runTime = 0;
protected int nowUnitNum { get { return roleList.Count; } }
protected int nowPointIndex = 0;
protected List roleList = null;
protected bool isStart = false; // 是否开始
public ZoneSpawner spawnerInfo;
///
/// 近战攻击角色
///
private List mNearAttackRoles = new List();
///
/// 远程攻击角色
///
private List mFarAttackRoles = new List();
//private List mAreaLocks = new List();
private bool isCreateComplete = false;
private Dictionary patrolDict;
private SceneEventAreaLockLogic areaLock;
private void Awake()
{
roleList = new List();
patrolDict = new Dictionary();
//spawnerId = CaretSpawnerGUID;
spawnerInfo = this.gameObject.GetComponent();
SpawnerSystem.Instance.Add(this);
isCreateComplete = false;
}
private void Start()
{
EventComponent eventCmpt = GameEntry.GetComponent();
eventCmpt.Subscribe(BattleEventRemoveUnit.EventId, RemoveMonsterByEvent);
SpawnerPatrolPoint[] ppoints = this.GetComponentsInChildren();
// 初始化巡逻点
foreach(var item in ppoints)
{
this.AddPatrolPoint(item);
}
if (spawnerInfo.areaLockIndex != 0)
{
areaLock = AIManager.Instance.GetAreaLockCmpt(spawnerInfo.areaLockIndex);
}
if (spawnerInfo.triggerType == ESpawnerTriggerType.TRIGGER_DEFAULT)
{
this.isStart = true;
}
if (!spawnerInfo.isEnable)
{
this.enabled = false;
}
if (spawnerInfo.enableType == ESpawnerEnableType.Enable_TC_DEFAULT)
{
bool isOk = false;
List taskVos = UserProxy.Instance.player.collectTaskCard.GetActivingCards();
foreach (var item in taskVos)
{
if (int.Parse(item.typeId) != spawnerInfo.requirementTaskId)
{
continue;
}
List stepVos = item.curSteps;
foreach (var step in stepVos)
{
if (step.typeId == spawnerInfo.requirementStepId)
{
if (!step.isFinish())
{
isOk = true;
}
}
}
}
if (!isOk)
{
this.isStart = true;
this.enabled = true;
}
}
runTime = spawnerInfo.produceTime;
sm_gate gateMo = sm_gate.GetMoById(UserProxy.Instance.player.newMap.curMapId);
if ((BattleDefine.EBattleSceneType)int.Parse(gateMo.battleType) == BattleDefine.EBattleSceneType.EBattleType_Endless ||
(BattleDefine.EBattleSceneType)int.Parse(gateMo.battleType) == BattleDefine.EBattleSceneType.EBattleType_Defense)
{
return;
}
// 通过网络数据刷新关卡数据
if (MapTcpProxy.Instance.Peer.MapData.spnSys != null)
{
Dictionary tbDict = MapTcpProxy.Instance.Peer.MapData.spnSys.GetAllSpawner();
if(!tbDict.ContainsKey(spawnerInfo.index))
{
return;
}
Dictionary rDict = tbDict[this.spawnerInfo.index].roleDict;
runTime = 0;
foreach (var roleData in rDict)
{
sm_hero monster = sm_hero.GetMoByID(roleData.Value.tid);
Vector3 pos = new Vector3(roleData.Value.posX, roleData.Value.posY, roleData.Value.posZ);
RoleManager.Instance.CreateMonster(spawnerInfo.point, monster, roleData.Value.dropId, pos, pos, 0, roleData.Value.rType,
(role) =>
{
role.SetPropertyFinal(null, EBattleProperty.HP, roleData.Value.hp);
role.mBehaviorTree.SetVariableValue("WanderType", spawnerInfo.waitType); // 0静止1随机 2路径点巡逻,配合WanderPoints使用
List trasList = new List();
for (int i = 1; i <= patrolDict.Count; ++i)
{
trasList.Add(patrolDict[i].transform);
}
role.mBehaviorTree.SetVariableValue("WanderPoints", trasList);// WanderType类型2追加参数
roleList.Add(role);
});
}
this.isClose = tbDict[this.spawnerInfo.index].isClose;
this.isCreateComplete = tbDict[this.spawnerInfo.index].isComplate;
this.enabled = tbDict[this.spawnerInfo.index].isEnable;
this.runTime = tbDict[this.spawnerInfo.index].runTime;
}
// 如果是副本 修正 isFill 填充怪物为false
string[] spaName = this.transform.parent.name.Split('_');
if (spaName.Length >= 2)
{
spawnerInfo.isFill = false;
}
}
private void OnDestroy()
{
EventComponent eventCmpt = GameEntry.GetComponent();
if (eventCmpt.Check(BattleEventRemoveUnit.EventId, RemoveMonsterByEvent))
{
eventCmpt.Unsubscribe(BattleEventRemoveUnit.EventId, RemoveMonsterByEvent);
}
}
private void Update()
{
if (!isStart)
{
return;
}
if (isClose)
{
return;
}
if (isCreateComplete)
{
return;
}
if (spawnerInfo.spawnType == ESpawnerCreateType.CREATE_DEFAULT)
{
isCreateComplete = true;
StartCoroutine("CreateAllUnit");
}
else if (spawnerInfo.spawnType == ESpawnerCreateType.CREATE_LINE)
{
if(spawnerInfo.produceType == SpawnerNodeBase.ProduceType.Incomplete)
{
this.runTime += Time.deltaTime;
if (runTime > spawnerInfo.produceTime)
{
runTime = 0;
StartCoroutine("CreateMaxNumUnit");
}
}
else if (spawnerInfo.produceType == SpawnerNodeBase.ProduceType.Clean)
{
if(roleList.Count == 0)
{
this.runTime += Time.deltaTime;
if (runTime > spawnerInfo.produceTime)
{
runTime = 0;
StartCoroutine("CreateMaxNumUnit");
}
}
}
}
else if (spawnerInfo.spawnType == ESpawnerCreateType.CREATE_QUEUE)
{
// 读取存储怪物队列数据信息
}
}
public virtual IEnumerator CreateAllUnit()
{
for (int i = 0; i < spawnerInfo.maxNum; ++i)
{
CreateUnit();
yield return new WaitForSeconds(0.1f);
}
isClose = true;
}
public virtual IEnumerator CreateMaxNumUnit()
{
for (int i = roleList.Count; i < spawnerInfo.maxNum; ++i)
{
CreateUnit();
yield return new WaitForSeconds(0.1f);
}
}
///
/// 创建单位
///
/// 是否为一次性创建完成
///
public void CreateUnit()
{
if (isClose)
{
return;
}
spawnerInfo.createCount += 1;
SpawnPoint point = GetSpawnerPoint();
PointNext();
if (point.unitId == 0)
{
return;
}
Vector3 pos = point.transform.Find("Cube").transform.position;
Quaternion qua = point.transform.Find("Cube").transform.rotation;
sm_hero monster = sm_hero.GetMoByID(point.unitId);
RoleManager.Instance.CreateUnitBySpawner(spawnerInfo.index, monster, point.dropId, pos, qua, point.readius, point.roleType,
(role) =>
{
role.mBehaviorTree.SetVariableValue("WanderType", spawnerInfo.waitType); // 0静止1随机 2路径点巡逻,配合WanderPoints使用
List trasList = new List();
for(int i = 1; i <= patrolDict.Count; ++i)
{
trasList.Add(patrolDict[i].transform);
}
role.mBehaviorTree.SetVariableValue("WanderPoints", trasList);// WanderType类型2追加参数
role.bubType = point.bubbleType;
role.bubStr = point.bubbleText;
role.spawnerID = this.spawnerInfo.index;
roleList.Add(role);
// 地图怪物同步
MapTcpProxy.Instance.Peer.MapData.spnSys.ResetData();
if (MapTcpProxy.Instance.Peer.MapData.spnSys._spawnerDict.ContainsKey(spawnerInfo.index))
{
MapTcpProxy.Instance.Peer.ReportDataChange(MapTcpProxy.Instance.Peer.MapData.spnSys._spawnerDict[spawnerInfo.index]);
}
// 创建对话气泡框
SceneBubbleWindow buCmpt = role.gameObject.AddComponent();
buCmpt.SetContent(point.bubbleText);
buCmpt.SetEnableType(point.bubbleType);
});
}
public SpawnPoint GetSpawnerPoint()
{
return spawnerInfo.spawnList[nowPointIndex];
}
public List GetAllRole()
{
return roleList;
}
///
/// 激活本孵化器
///
///
public void EnableSpawner()
{
this.enabled = true;
this.StartSpawner();
}
public bool PointNext()
{
++nowPointIndex;
if (nowPointIndex >= spawnerInfo.spawnList.Count)
{
nowPointIndex = 0;
}
if (!spawnerInfo.isFill)
{
if (spawnerInfo.createCount >= spawnerInfo.maxNum)
{
isClose = true;
isCreateComplete = true;
nowPointIndex = 0;
}
}
return !isCreateComplete;
}
public void Reset()
{
//foreach (var item in roleList)
//{
// RoleManager.Instance.DestoryRole(item);
//}
//IsClose = false;
//nowPointIndex = 0;
}
public bool IsCreateFinish()
{
return isCreateComplete;
}
public int GetMonsterNumber()
{
return roleList.Count;
}
public bool IsLastNode()
{
return spawnerInfo.isLastNode;
}
public void StartSpawner()
{
this.isStart = true;
}
public void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.layer == LayerMask.NameToLayer("Player"))
{
foreach (var r in roleList)
{
// 创建对话气泡框
if (r.bubType == SpawnPoint.BubbleType.Area && !r.bubEnable)
{
SceneBubbleWindow buCmpt = r.gameObject.AddComponent();
buCmpt.SetContent(r.bubStr);
buCmpt.SetEnableType(r.bubType);
r.bubEnable = true;
}
}
}
if (spawnerInfo.triggerType != ESpawnerTriggerType.TRIGGER_COLLIDER)
{
return;
}
Role role = collider.GetComponent();
if (role == null || !role.IsHero)
{
return;
}
this.isStart = true;
}
public void OnTriggerExit(Collider collider)
{
if (collider.gameObject.layer == LayerMask.GetMask("Enemy"))
{
}
}
///
/// 初始化
///
public void Init()
{
spawnerInfo.mPointMos.Clear();
}
public int GetPointMaxNumber()
{
return this.transform.childCount;
}
public Transform GetPointMOByIndex(int index)
{
return this.transform.GetChild(index);
}
public bool CreateMonsterByPoint()
{
foreach (var item in spawnerInfo.mPointMos)
{
sm_hero monster = sm_hero.GetMoByID(item.Value.unitId);
Vector3 pos = item.Value.transform.position;
float readius = item.Value.readius;
RoleManager.Instance.CreateMonster(spawnerInfo.point, monster, item.Value.dropId, pos, pos, readius, RoleType.Boss);
}
EventComponent nowEvent = GameEntry.GetComponent();
nowEvent.FireNow(this, new NoviceEventSpawnerIndex(spawnerInfo.point));
return true;
}
public void RemoveMonsterByEvent(object sender, GameEventArgs e)
{
if (roleList.Count == 0)
{
return;
}
BattleEventRemoveUnit rmEvent = e as BattleEventRemoveUnit;
if(roleList.Remove(rmEvent.role) != true)
{
return;
}
;
if (roleList.Count == 0)
{
// 禁区组件开启
AIManager.Instance.RemoveAreaLockCmpt(spawnerInfo.areaLockIndex);
// 激活下组孵化器
foreach (var item in spawnerInfo.enableSpawnerList)
{
if (item != null)
{
item.enabled = true;
item.gameObject.GetComponent().StartSpawner();
}
}
// 关闭本孵化器
if (spawnerInfo.spawnType != ESpawnerCreateType.CREATE_LINE)
{
this.enabled = false;
this.spawnerInfo.isLastNode = true;
}
}
// 地图怪物同步
MapTcpProxy.Instance.Peer.MapData.spnSys.ResetData();
if(MapTcpProxy.Instance.Peer.MapData.spnSys._spawnerDict.ContainsKey(spawnerInfo.index))
{
MapTcpProxy.Instance.Peer.ReportDataChange(MapTcpProxy.Instance.Peer.MapData.spnSys._spawnerDict[spawnerInfo.index]);
}
}
///
/// 获取孵化器所孵化所有怪物
///
///
public int GetMonsterMaxNumber()
{
return spawnerInfo.mPointMos.Count;
}
public void AddPatrolPoint(SpawnerPatrolPoint point)
{
int index = int.Parse(point.name.Split('_')[1]);
patrolDict[index] = point;
MeshRenderer render = point.GetComponent();
render.enabled = false;
}
public bool IsCleanComplete()
{
return IsCreateFinish() && this.roleList.Count == 0;
}
}
}