chenxin
2020-12-25 dcd118c76ba7a035b3c6b25a0bed9abeceac7251
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
using ActionGameFramework.Health;
using Core.Utilities;
using DG.Tweening;
using TMPro.Examples;
using TowerDefense.Affectors;
using TowerDefense.Agents;
using TowerDefense.Towers.Projectiles;
using UnityEngine;
using UnityEngine.UIElements;
 
namespace TowerDefense.Towers.TowerLaunchers
{
    /// <summary>
    /// An implementation of the tower launcher for hitscan attacks
    /// </summary>
    public class HitscanLauncher : Launcher
    {
        /// <summary>
        /// The particle system used for providing launch feedback
        /// </summary>
        public GameObject fireParticleObj;
        public ParticleSystem fireParticleSystem1;
        public ParticleSystem fireParticleSystem2;
 
        protected Timer effectStopTimer;
        protected bool bTimerStart = false;
 
 
        void Awake()
        {
            effectStopTimer = new Timer(1.0f, stopParticle);
            bTimerStart = false;
        }
 
 
        protected void stopParticle()
        {
            if (particleToPlay)
                particleToPlay.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
        }
 
        void Update()
        {
            if (bTimerStart)
            {
                if (effectStopTimer.Tick(Time.deltaTime))
                {
                    bTimerStart = false;
                    effectStopTimer.Reset();
                }
            }
 
        }
 
 
        protected int times = 0;
        /// <summary>
        /// Assigns the correct damage to the hitscan object and
        /// attacks the enemy immediately.
        /// Early return if there is not HitscanAttack.cs attached to the attact object
        /// </summary>
        /// <param name="enemy">
        /// The enemy this tower is targeting
        /// </param>
        /// <param name="attack">
        /// The attacking component used to damage the enemy
        /// </param>
        /// <param name="firingPoint"></param>
        public override void Launch(Targetable enemy, GameObject attack, Transform firingPoint, int idx = 0)
        {
            var hitscanAttack = attack.GetComponent<HitscanAttack>();
            if (hitscanAttack == null)
            {
                return;
            }
 
            /*            if(times != 0)
                        {
                            times++;
                            if (times >= 5)
                                times = 0;
                            return;
                        }
                        else
                        {
                            times++;
                        }
            */
            // 是否有AttackAffector,获取AttackRate,计算最终的伤害延迟
            AttackAffector aaf = GetComponent<AttackAffector>();
            float delayAttack = 0.0f;
            if (aaf)
            {
                float attackRate = aaf.towerLevel.GetFireRate();
                if (attackRate > 0)
                    delayAttack = 1.0f / attackRate;
            }
            TowerLevel tl = transform.parent.gameObject.GetComponent<TowerLevel>();
            hitscanAttack.attackRise = 0.0f;
            if (tl != null)
                hitscanAttack.attackRise = tl.attackRise;
            hitscanAttack.transform.position = firingPoint.position;
            hitscanAttack.AttackEnemy(firingPoint.position, enemy, delayAttack);
 
            if (idx == 0)
            {
                // GameObject obj = Poolable.TryGetPoolable(fireParticleObj);
                // obj.transform.position = firingPoint.position;
                // obj.transform.LookAt(enemy.position);
                // ParticleSystem ps = obj.GetComponent<ParticleSystem>();
                // ps = ps ?? obj.transform.GetChild(0).GetComponent<ParticleSystem>();
                // ps.Play();
                GameObject obj = Instantiate(fireParticleObj, firingPoint.position, Quaternion.identity);
                obj.transform.LookAt(enemy.position);
                ParticleSystem ps = obj.GetComponent<ParticleSystem>();
                if (ps == null)
                    ps = obj.transform.GetChild(0).GetComponent<ParticleSystem>();
                ps?.Play();
            }
            else if (idx == 1)
            {
                PlayParticles(fireParticleSystem1, firingPoint.position, enemy.position);
            }
            else
            {
                PlayParticles(fireParticleSystem2, firingPoint.position, enemy.position);
            }
 
            effectStopTimer.SetTime(delayAttack / 5.0f);
            bTimerStart = true;
 
        }
    }
}