wangguan
2020-12-26 b1f72b6a7f2c3846eacf26721e114e05ffe3a3db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using Core.Health;
using TowerDefense.Agents;
using TowerDefense.Level;
using UnityEngine;
using KTGMGemClient;
using System.Collections.Generic;
using Core.Utilities;
using DG.Tweening;
 
namespace TowerDefense.Economy
{
    /// <summary>
    /// A class that adds money to the currency when the attached DamagableBehaviour dies
    /// 每一个Agent都会有一个LootDrop类,对应的DamageableBehaviour分别派生自:
    /// Agent--Targetable--DamageableBehaviour
    /// </summary>
    [RequireComponent(typeof(DamageableBehaviour))]
    public class LootDrop : MonoBehaviour
    {
        /// <summary>
        /// The amount of loot/currency dropped when object "dies"
        /// </summary>
        public int lootDropped;
 
        /// <summary>
        /// The attached DamagableBehaviour
        /// </summary>
        protected DamageableBehaviour m_DamageableBehaviour;
 
        /// <summary>
        /// Caches attached DamageableBehaviour
        /// </summary>
        protected virtual void OnEnable()
        {
            if (m_DamageableBehaviour == null)
            {
                m_DamageableBehaviour = GetComponent<DamageableBehaviour>();
            }
            m_DamageableBehaviour.configuration.died += OnDeath;
        }
 
        /// <summary>
        /// Unsubscribed from the <see cref="m_DamageableBehaviour"/> died event
        /// </summary>
        protected virtual void OnDisable()
        {
            m_DamageableBehaviour.configuration.died -= OnDeath;
        }
 
        /// <summary>
        /// The callback for when the attached object "dies".
        /// Add <see cref="lootDropped"/> to current currency
        /// </summary>
        protected virtual void OnDeath(HealthChangeInfo info)
        {
            m_DamageableBehaviour.configuration.died -= OnDeath;
 
            if (LevelManager.instanceExists)
            {
                // 当前LootDrop所在的Agent死亡后,给全局的CurrencyManager增加金币.
                // 当前Agent死亡之后,需要在对手盘加入新的Agent
                LevelManager levelManager = LevelManager.instance;
                OpponentMgr opmgr = OpponentMgr.instance;
                if (levelManager == null)
                    return;
                Agent agent = gameObject.GetComponent<Agent>();
                if ((agent != null) && agent.opponentAgent)
                {
                    if (opmgr == null)
                    {
                        return;
                    }
                    opmgr.currency.AddCurrency(lootDropped);
                    /* 修改为不再死亡后发布怪物到对方视野.
                    if( agent.bRespawn)
                    {
                        AgentSetData sd = agent.mAgentData;
                        int attid = OpponentMgr.instance.GetTowerAttID(agent.waveLineID);
                        levelManager.waveManager.SpawnAgent(agent.waveLineID,sd.hp,sd.speed, attid );
                    }*/
                }
                else
                {
                    levelManager.currency.AddCurrency(lootDropped);
                    /*
                    if (agent.bRespawn)
                    {
                        AgentSetData sd = agent.mAgentData;
                        int attid = levelManager.GetTowerAttID(agent.waveLineID);
                        opmgr.m_WaveManager.SpawnAgent(agent.waveLineID, sd.hp,sd.speed, attid );
                    }*/
                }
            }
            else if (EndlessLevelManager.instanceExists)
            {
                Agent agent = gameObject.GetComponent<Agent>();
 
 
                if (agent != null)
                {
                    // if (EndlessLevelManager.instance.DropRate > 0)
                    // {
                    //     //查询
                    //     float rate = Random.Range(0, 100) * 0.01f;
                    //     //Debug.Log($"需要处理掉落特效 rate:{rate}  EndlessLevelManager.instance.DropRate:{EndlessLevelManager.instance.DropRate}");
                    //     if (rate <= EndlessLevelManager.instance.DropRate)
                    //     {
                    //         PlayDropCoinPS(agent.position);
                    //     }
                    // }
 
                    EndlessLevelManager.instance.Currency.AddCurrency(lootDropped);
                    PlayDropGold(lootDropped, agent.position);
 
                    // 处理掉落
                    // int tunel = agent.waveLineID + 1;
 
                    // List<reward> rewardList = EndlessPortData.GetDropRewardList(EndlessLevelManager.instance.CurrentLevel,
                    //     EndlessLevelManager.instance.WaveManager.CurrentWaveIndex + 1, tunel);
 
                    // if (rewardList.Count > 0)
                    //     EndlessDropManager.instance.AddDrop(rewardList, gameObject.transform.position);
                }
            }
        }
        GameObject dropGoldPrefab;
        GameObject mainUI;
        Camera camera3D;
        /// <summary>
        /// Awake is called when the script instance is being loaded.
        /// </summary>
        void Awake()
        {
            mainUI = GameObject.Find("MainUI/CoinPanel");
            dropGoldPrefab = Resources.Load<GameObject>("Prefabs/DropGold");
            camera3D = GameObject.Find("SceneCamera3D").GetComponent<Camera>();
        }
 
        private void PlayDropGold(int lootDropped, Vector3 worldPos)
        {
            if (lootDropped == 0) return;
 
            GameObject obj = Poolable.TryGetPoolable(dropGoldPrefab);
 
            DropGold dropGold = obj.GetComponent<DropGold>();
            dropGold.SetDrop(lootDropped);
 
            obj.GetComponent<Transform>().SetParent(mainUI.GetComponent<Transform>(), false);
            Vector3 screenPos = camera3D.WorldToScreenPoint(worldPos);
            screenPos.z = 0;
            obj.transform.position = screenPos;
            obj.transform.localScale = new Vector3(0.8f, 0.8f, 0.8f);
            float preY = obj.transform.position.y;
            CanvasGroup canvasGroup = obj.GetComponent<CanvasGroup>();
 
            Sequence sequence = DOTween.Sequence();
            sequence.Append(DOTween.To(
                () => obj.transform.position.y,
                (float v) =>
                {
                    Vector3 pos = obj.transform.position;
                    pos.y = v;
                    obj.transform.position = pos;
                }, preY + 45, 0.5f)
                .SetEase(Ease.OutCubic));
            sequence.Append(DOTween.To(
                () => canvasGroup.alpha,
                (float v) =>
                {
                    canvasGroup.alpha = v;
                }, 0, 0.8f).SetDelay(0.5f));
            sequence.AppendCallback(() =>
                {
                    canvasGroup.alpha = 1;
                    Poolable.TryPool(obj);
                });
        }
 
        private void PlayDropCoinPS(Vector3 worldPos)
        {
            GameObject prefab = Resources.Load<GameObject>("Prefabs/DropCoin");
            GameObject obj = Poolable.TryGetPoolable(prefab);
            obj.transform.position = worldPos;
 
            GameObject prefabIcon = Resources.Load<GameObject>("Prefabs/DropCoinImage");
            GameObject obj1 = Poolable.TryGetPoolable(prefabIcon);
            obj1.GetComponent<Transform>().SetParent(mainUI.GetComponent<Transform>(), false);
            Vector3 screenPos = camera3D.WorldToScreenPoint(worldPos);
            screenPos.z = 0;
            obj1.transform.position = screenPos;
            obj1.GetComponent<DropCoinImage>().Init();
        }
    }
}