Exemplo n.º 1
0
    protected override MonsterAIDataStruct CloneChild(MonsterAIDataStruct target)
    {
        MonsterAIData_Trigger monsterAIData_Trigger = null;

        if (target == null)
        {
            monsterAIData_Trigger = new MonsterAIData_Trigger();
            target = monsterAIData_Trigger;
        }
        else
        {
            monsterAIData_Trigger = (MonsterAIData_Trigger)target;
        }
        //赋值
        monsterAIData_Trigger.Count        = Count;
        monsterAIData_Trigger.TriggerRange = TriggerRange;
        //返回
        return(base.CloneChild(target));
    }
Exemplo n.º 2
0
    /// <summary>
    /// 检测怪物数据的状态
    /// 和玩家位置进行对比,用于创建移动或设置怪物AI
    /// </summary>
    private void CheckMonsterDataInfoState()
    {
        if (monsterDataInfos == null || thisSceneCanCheckMonsterObjDic == null || allMonsterObjDic == null || thisSceneCanCheckMonsterObjList == null)
        {
            return;
        }
        IPlayerState iPlayerState = GameState.Instance.GetEntity <IPlayerState>();

        if (iPlayerState.PlayerObj == null)
        {
            return;
        }
        //判断大区域
        bool objListChanged = false;//对象集合是否发生了变化

        //检测触发区域是否会发生变化
        foreach (MonsterDataInfo monsterDataInfo in monsterDataInfos)
        {
            float distance = Vector2.Distance(
                new Vector2(monsterDataInfo.Center.x, monsterDataInfo.Center.z)
                , new Vector2(iPlayerState.PlayerObj.transform.position.x, iPlayerState.PlayerObj.transform.position.z));
            if (distance > monsterDataInfo.Range + MaxRangeLeaveDistance)
            {
                if (thisSceneCanCheckMonsterObjDic.ContainsKey(monsterDataInfo))
                {
                    thisSceneCanCheckMonsterObjDic.Remove(monsterDataInfo);
                    objListChanged = true;
                }
            }
            else if (distance <= monsterDataInfo.Range)
            {
                //获取当前任务状态是否可以显示
                bool taskPass = monsterDataInfo.CanShowThis(temp =>
                {
                    TaskMap.RunTimeTaskInfo tempTaskInfoStruct = runtimeTaskData.GetTasksWithID(temp, false);
                    if (tempTaskInfoStruct == null)
                    {
                        return(TaskMap.Enums.EnumTaskProgress.NoTake);
                    }
                    return(tempTaskInfoStruct.TaskProgress);
                });
                if (taskPass)//可以显示
                {
                    if (!thisSceneCanCheckMonsterObjDic.ContainsKey(monsterDataInfo))
                    {
                        if (!allMonsterObjDic.ContainsKey(monsterDataInfo))
                        {
                            allMonsterObjDic.Add(monsterDataInfo, new List <GameObject>());
                        }
                        thisSceneCanCheckMonsterObjDic.Add(monsterDataInfo, allMonsterObjDic[monsterDataInfo]);
                        objListChanged = true;
                    }
                }
                else//不可以显示
                {
                    if (thisSceneCanCheckMonsterObjDic.ContainsKey(monsterDataInfo))
                    {
                        thisSceneCanCheckMonsterObjDic.Remove(monsterDataInfo);
                        objListChanged = true;
                    }
                }
            }
        }

        //移除所有对象字典空元素
        foreach (KeyValuePair <MonsterDataInfo, List <GameObject> > tempData in allMonsterObjDic)
        {
            tempData.Value.RemoveAll(temp => temp == null);
        }

        //检测触发区域内的对象生成情况
        foreach (KeyValuePair <MonsterDataInfo, List <GameObject> > tempData in thisSceneCanCheckMonsterObjDic)
        {
            MonsterDataInfo monsterDataInfo = tempData.Key;
            switch (monsterDataInfo.AIType)
            {
            case EnumMonsterAIType.Trigger:    //触发型(更新时间到了并且在触发区域被且没有任何怪物时生成)
                MonsterAIData_Trigger monsterAIData_Trigger = monsterDataInfo.AIData as MonsterAIData_Trigger;
                if (monsterAIData_Trigger != null && tempData.Value.Count == 0 && monsterAIData_Trigger.NowUpdateTime <= 0)
                {
                    float distance = Vector2.Distance(
                        new Vector2(monsterDataInfo.Center.x, monsterDataInfo.Center.z)
                        , new Vector2(iPlayerState.PlayerObj.transform.position.x, iPlayerState.PlayerObj.transform.position.z));
                    if (distance < monsterAIData_Trigger.TriggerRange && monsterDataInfo.MonsterPrefab != null)
                    {
                        monsterAIData_Trigger.NowUpdateTime = monsterAIData_Trigger.UpdateTime;
                        //生成怪物
                        for (int i = 0; i < monsterAIData_Trigger.Count; i++)
                        {
                            Vector3 createPos = new Vector3(
                                UnityEngine.Random.Range(monsterDataInfo.Center.x - monsterAIData_Trigger.CreateRange, monsterDataInfo.Center.x + monsterAIData_Trigger.CreateRange),
                                monsterDataInfo.Center.y,
                                UnityEngine.Random.Range(monsterDataInfo.Center.z - monsterAIData_Trigger.CreateRange, monsterDataInfo.Center.z + monsterAIData_Trigger.CreateRange));
                            GameObject     createObj      = GameObject.Instantiate <GameObject>(monsterDataInfo.MonsterPrefab, createPos, Quaternion.identity);
                            MonsterControl monsterControl = createObj.AddComponent <MonsterControl>();
                            monsterControl.SameGroupObjList = tempData.Value;
                            monsterControl.monsterDataInfo  = tempData.Key;
                            tempData.Value.Add(createObj);
                            //设置位置(MonsterControl内部会进行y轴的设置)
                            //createObj.transform.position = new Vector3(
                            //    UnityEngine.Random.Range(monsterDataInfo.Center.x - monsterAIData_Trigger.TriggerRange, monsterDataInfo.Center.x + monsterAIData_Trigger.TriggerRange),
                            //    monsterDataInfo.Center.y,
                            //    UnityEngine.Random.Range(monsterDataInfo.Center.z - monsterAIData_Trigger.TriggerRange, monsterDataInfo.Center.z + monsterAIData_Trigger.TriggerRange));
                        }
                        objListChanged = true;
                    }
                }
                break;

            case EnumMonsterAIType.GoOnPatrol:    //巡逻型(更新时间到了并且怪物集合数量少与最大数量时生成)
                MonsterAIData_GoOnPatrol monsterAIData_GoOnPatrol = monsterDataInfo.AIData as MonsterAIData_GoOnPatrol;
                if (monsterAIData_GoOnPatrol != null && tempData.Value.Count < monsterAIData_GoOnPatrol.Count && monsterAIData_GoOnPatrol.NowUpdateTime <= 0)
                {
                    monsterAIData_GoOnPatrol.NowUpdateTime = monsterAIData_GoOnPatrol.UpdateTime;
                    //生成怪物
                    int createCount = monsterAIData_GoOnPatrol.Count - tempData.Value.Count;
                    for (int i = 0; i < createCount; i++)
                    {
                        Vector3 createPos = new Vector3(
                            UnityEngine.Random.Range(monsterDataInfo.Center.x - monsterAIData_GoOnPatrol.CreateRange, monsterDataInfo.Center.x + monsterAIData_GoOnPatrol.CreateRange),
                            monsterDataInfo.Center.y,
                            UnityEngine.Random.Range(monsterDataInfo.Center.z - monsterAIData_GoOnPatrol.CreateRange, monsterDataInfo.Center.z + monsterAIData_GoOnPatrol.CreateRange));
                        GameObject     createObj      = GameObject.Instantiate <GameObject>(monsterDataInfo.MonsterPrefab, createPos, Quaternion.identity);
                        MonsterControl monsterControl = createObj.AddComponent <MonsterControl>();
                        monsterControl.SameGroupObjList = tempData.Value;
                        monsterControl.monsterDataInfo  = tempData.Key;
                        tempData.Value.Add(createObj);
                        //设置位置(MonsterControl内部会进行y轴的设置)
                        //createObj.transform.position = new Vector3(
                        //    UnityEngine.Random.Range(monsterDataInfo.Center.x - monsterAIData_GoOnPatrol.CreateRange, monsterDataInfo.Center.x + monsterAIData_GoOnPatrol.CreateRange),
                        //    monsterDataInfo.Center.y,
                        //    UnityEngine.Random.Range(monsterDataInfo.Center.z - monsterAIData_GoOnPatrol.CreateRange, monsterDataInfo.Center.z + monsterAIData_GoOnPatrol.CreateRange));
                    }
                    objListChanged = true;
                }
                break;

            case EnumMonsterAIType.Boss:    //boss型(更新时间到了并且怪物没有生成是生成)
                MonsterAIData_Boss monsterAIData_Boss = monsterDataInfo.AIData as MonsterAIData_Boss;
                if (monsterAIData_Boss != null && tempData.Value.Count == 0 && monsterAIData_Boss.NowUpdateTime <= 0)
                {
                    monsterAIData_Boss.NowUpdateTime = monsterAIData_Boss.UpdateTime;
                    //生成怪物
                    GameObject     createObj      = GameObject.Instantiate <GameObject>(monsterDataInfo.MonsterPrefab);
                    MonsterControl monsterControl = createObj.AddComponent <MonsterControl>();
                    monsterControl.SameGroupObjList = tempData.Value;
                    monsterControl.monsterDataInfo  = tempData.Key;
                    tempData.Value.Add(createObj);
                    //设置位置(MonsterControl内部会进行y轴的设置)
                    createObj.transform.position = new Vector3(
                        monsterDataInfo.Center.x,
                        monsterDataInfo.Center.y,
                        monsterDataInfo.Center.z);
                    objListChanged = true;
                }
                break;
            }
        }

        //刷新时间
        foreach (KeyValuePair <MonsterDataInfo, List <GameObject> > tempData in allMonsterObjDic)
        {
            switch (tempData.Key.AIType)
            {
            case EnumMonsterAIType.Trigger:
                if (tempData.Value.Count == 0 && tempData.Key.AIData != null)
                {
                    tempData.Key.AIData.NowUpdateTime -= UpdateTime;
                }
                break;

            case EnumMonsterAIType.GoOnPatrol:
                MonsterAIData_GoOnPatrol monsterAIData_GoOnPatrol = tempData.Key.AIData as MonsterAIData_GoOnPatrol;
                if (monsterAIData_GoOnPatrol != null && tempData.Value.Count < monsterAIData_GoOnPatrol.Count)
                {
                    monsterAIData_GoOnPatrol.NowUpdateTime -= UpdateTime;
                }
                break;

            case EnumMonsterAIType.Boss:
                if (tempData.Value.Count == 0 && tempData.Key.AIData != null)
                {
                    tempData.Key.AIData.NowUpdateTime -= UpdateTime;
                }
                break;
            }
        }

        //重新构建检测集合
        if (objListChanged)
        {
            thisSceneCanCheckMonsterObjList.Clear();
            foreach (KeyValuePair <MonsterDataInfo, List <GameObject> > tempData in thisSceneCanCheckMonsterObjDic)
            {
                thisSceneCanCheckMonsterObjList.AddRange(tempData.Value);
            }
        }

        //移除检测集合的空元素
        thisSceneCanCheckMonsterObjList.RemoveAll(temp => temp == null);
    }