chenxin
2020-11-09 172d8ffbf5fe3bdd60b3d71a3a60feeed1cb1762
Assets/Scripts/TowerDefense/UI/EndlessBossSkill/BossSkillBubbleBomb.cs
@@ -1,11 +1,7 @@
using TowerDefense.UI.HUD;
using Core.Utilities;
using System.Collections.Generic;
using UnityEngine;
using TowerDefense.Agents;
using TowerDefense.Level;
using DG.Tweening;
using MoreMountains.NiceVibrations;
/**
 * 泡泡炸弹
@@ -14,9 +10,14 @@
 */
namespace KTGMGemClient
{
    /// <summary>
    /// 泡泡炸弹配置数据
    /// </summary>
    public class BubbleBombConfig
    {
        public BubbleBombAgent Agent { get; set; }
        public GameObject obj { get; set; }
        /// <summary>
        /// 需要被攻击的次数,攻击 * 次炸弹才会被销毁
@@ -73,15 +74,15 @@
        /// </summary>
        public bool IsAttack { get; set; }
        /// <summary>
        /// 延迟产生伤害时间
        /// </summary>
        public float AttackTime { get; set; }
    }
    public class BossSkillBubbleBomb : EndlessBossSkill
    {
        public BossSkillBubbleBomb(boss_skill param) : base(param)
        {
            random = new System.Random();
        }
        public BossSkillBubbleBomb(boss_skill param) : base(param) { }
        /// <summary>
        /// 被攻击的次数
@@ -95,7 +96,16 @@
        /// </summary>
        protected List<BubbleBombConfig> bubbleBombList;
        protected System.Random random;
        /// <summary>
        /// 泡泡炸弹对象池
        /// </summary>
        protected List<GameObject> bubbleBombPool;
        protected int capacity = 10;
        private int getCount;
        private int recycleCount;
        /// <summary>
        /// 释放技能
@@ -104,7 +114,6 @@
        {
            base.ReleaseSkill();
            Debug.Log("--------------------- 泡泡炸弹释放 ---------------------");
            // cx test
            SpawnBubbleBomb();
        }
@@ -132,13 +141,11 @@
        /// </summary>
        protected void SpawnOnTunel()
        {
            // 有可能只在某一条赛道生成,也有可能在所有赛道生成
            List<int> tunelIdList = GetTunelList();
            GameObject prefab = Resources.Load<GameObject>(prefabPath);
            for (int i = 0; i < tunelIdList.Count; ++i)
            {
                GameObject obj = Poolable.TryGetPoolable(prefab);
                GameObject obj = GetBubbleBomb();
                BubbleBombAgent bubbleBomb = obj.GetComponent<BubbleBombAgent>();
                // 分配唯一id
@@ -147,15 +154,17 @@
                bubbleBomb.AgentType = SpawnAgentType.BubbleBomb;
                bubbleBomb.opponentAgent = false;
                bubbleBomb.Reset();
                bubbleBomb.Initialize();
                // 出生位置
                Vector3 spawnPosition = EndlessLevelManager.instance.GetTunelWorldPosition(tunelIdList[i], (EndlessBossSkillTunelType)SkillData.target[1]);
                bubbleBomb.transform.position = spawnPosition;
                obj.transform.position = spawnPosition;
                bubbleBomb.PlayNormalEffect();
                AgentInsManager.instance.addAgent(bubbleBomb);
                BubbleBombConfig config = new BubbleBombConfig();
                config.Agent = bubbleBomb;
                config.obj = obj;
                config.NeedAttackCount = (int)SkillData.effect[0];
                config.StartPosition = spawnPosition;
                config.TargetPosition = EndlessLevelManager.instance.GetHomeBasePosition(tunelIdList[i]);
@@ -163,39 +172,6 @@
                config.MoveSpeed = SkillData.effect[1];
                bubbleBombList.Add(config);
            }
        }
        /// <summary>
        /// 获取赛道列表
        /// </summary>
        protected List<int> GetTunelList()
        {
            List<int> tunelIdList = new List<int>();
            int count = 0;
            // 剩余没有用到的赛道
            List<int> remainList = new List<int>() { 1, 2, 3, 4, 5 };
            for (int i = 0; i < SkillData.tunnel.Count; ++i)
            {
                // 统计6这种类型的数量
                if (SkillData.tunnel[i] == 6)
                {
                    ++count;
                    continue;
                }
                tunelIdList.Add(SkillData.tunnel[i]);
                remainList.Remove(SkillData.tunnel[i]);
            }
            for (int i = 0; i < count; ++i)
            {
                int num = random.Next(remainList.Count);
                tunelIdList.Add(remainList[num]);
                remainList.Remove(remainList[num]);
            }
            return tunelIdList;
        }
        public override void Reset()
@@ -209,6 +185,7 @@
        {
            base.Clear();
            ClearBubbleBomb();
            ClearBubbleBombPool();
        }
        /// <summary>
@@ -222,10 +199,8 @@
                bubbleBombList[i].Agent.StopNormalEffect();
                bubbleBombList[i].Agent.StopExplodeEffect();
                if (!bubbleBombList[i].Agent.bInDeathState)
                    AgentInsManager.instance.removeAgent(bubbleBombList[i].Agent);
                Poolable.TryPool(bubbleBombList[i].Agent.gameObject);
                AgentInsManager.instance.removeAgent(bubbleBombList[i].Agent);
                RecycleBubbleBomb(bubbleBombList[i].obj);
            }
            bubbleBombList.Clear();
@@ -236,6 +211,65 @@
            base.Init();
            Debug.Log("--------------------- 泡泡炸弹技能初始化 ---------------------");
            bubbleBombList = new List<BubbleBombConfig>();
            bubbleBombPool = new List<GameObject>();
            GameObject prefab = Resources.Load<GameObject>(prefabPath);
            for (int i = 0; i < capacity; ++i)
            {
                GameObject obj = GameObject.Instantiate(prefab);
                obj.SetActive(false);
                bubbleBombPool.Add(obj);
            }
        }
        /// <summary>
        /// 从对象池中获取一个炸弹
        /// </summary>
        protected GameObject GetBubbleBomb()
        {
            GameObject ret;
            if (bubbleBombPool.Count > 0)
            {
                ret = bubbleBombPool[0];
                bubbleBombPool.Remove(bubbleBombPool[0]);
            }
            else
            {
                GameObject prefab = Resources.Load<GameObject>(prefabPath);
                ret = GameObject.Instantiate(prefab);
            }
            ret.SetActive(true);
            return ret;
        }
        /// <summary>
        /// 回收泡泡炸弹
        /// </summary>
        /// <param name="obj"></param>
        protected void RecycleBubbleBomb(GameObject obj)
        {
            if (obj != null)
            {
                obj.SetActive(false);
                bubbleBombPool.Add(obj);
            }
        }
        /// <summary>
        /// 清空泡泡炸弹对象池
        /// </summary>
        protected void ClearBubbleBombPool()
        {
            while (bubbleBombPool.Count > 0)
            {
                GameObject obj = bubbleBombPool[0];
                bubbleBombPool.Remove(obj);
                GameObject.Destroy(obj);
            }
        }
        protected override void AddEvent()
@@ -267,6 +301,7 @@
                    if (config.Agent.bInDeathState) return;
                    ++config.AttackCount;
                    Debug.Log($"--------------------- BubbleBombAgent Id: {id}, AttackCount:{config.AttackCount} ---------------------");
                    if (config.AttackCount >= config.NeedAttackCount)
                        AgentDead(config);
@@ -352,10 +387,9 @@
            for (int i = 0; i < bubbleBombList.Count; ++i)
            {
                if (bubbleBombList[i] == null) continue;
                BubbleBombConfig config = bubbleBombList[i];
                // 泡泡炸弹已经触发了攻击,延时一会过后掉爱心
                if (config.IsAttack)
                {
                    config.AttackTime -= deltaTime;
@@ -363,8 +397,6 @@
                    if (config.AttackTime <= 0)
                    {
                        EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessLoseHeart, 3);
                        ViewPortAdj.instance.cachedCamera.DOShakePosition(0.25f, 1.5f, 4);
                        MMVibrationManager.Haptic(HapticTypes.HeavyImpact);
                        config.IsAttack = false;
                    }
                }
@@ -377,7 +409,8 @@
                    if (config.DestroyTime <= 0)
                    {
                        bubbleBombList[i].Agent.Reset();
                        Poolable.TryPool(bubbleBombList[i].Agent.gameObject);
                        RecycleBubbleBomb(bubbleBombList[i].obj);
                        AgentInsManager.instance.removeAgent(bubbleBombList[i].Agent);
                        bubbleBombList.Remove(bubbleBombList[i]);
                        --i;
                    }
@@ -400,11 +433,11 @@
                }
                // 更新移动
                if (!config.Agent.bInDeathState && config.Agent.CanMove)
                if (config.Agent != null && !config.Agent.bInDeathState && config.Agent.CanMove)
                {
                    Vector3 pos = config.Agent.transform.position;
                    Vector3 pos = config.obj.transform.position;
                    pos.z -= deltaTime * config.MoveSpeed;
                    config.Agent.transform.position = pos;
                    config.obj.transform.position = pos;
                    if (pos.z <= config.TargetPosition.z)
                    {