wangguan
2020-12-17 adf42e9f9f0ea328e4cd3d3d8d63cc111f3f72a1
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
using Core.Health;
using Core.Utilities;
using System.Collections;
using System.Collections.Generic;
using TowerDefense.Agents;
using UnityEngine;
 
public class AgentResetPosEffect : AgentEffect
{
    protected bool bPosReset = false;
    /// <summary>
    /// 重设到开始位置的特效
    /// </summary>
    protected GameObject m_PosResetFx;
 
    protected Timer m_DestroyTimer;
    protected Timer m_PosResetTimer;
    /// <summary>
    /// 用于备份粒子系统的默认Scale,在设置完Parent之后,会有对自己Scale的设置,从而导致还回内存池之后,
    /// LocalScale会缩小的情况发生。
    /// </summary>
    protected Vector3 m_BackScale;
 
    /// <summary>
    /// 初始化当前的位置重设特效
    /// </summary>
    /// <param name="posResetFx"></param>
    /// <param name="position"></param>
    /// <param name="scale"></param>
    public void Initialize( float maxPlaySec,GameObject posResetFx = null,Vector3 position = default(Vector3), float scale = 1.0f)
    {
        if ((posResetFx == null) || (m_PosResetFx != null)) return;
 
        if( !m_Agent)
            return;
 
        position.y = 0.2f;
        m_PosResetFx = Poolable.TryGetPoolable(posResetFx);
 
        m_BackScale = m_PosResetFx.transform.localScale;
        m_PosResetFx.transform.SetParent(transform, true);
        m_PosResetFx.transform.localPosition = position;
        m_PosResetFx.transform.localScale *= scale;
        var particle = m_PosResetFx.GetComponent<ParticleSystem>();
        if( particle)
        {
            particle.Simulate(0.0f);
            particle.Play();
        }
        
        m_Agent.removed += OnRemoved;
 
        m_DestroyTimer = new Timer(maxPlaySec, RemoveEffect);
        m_PosResetTimer = new Timer(maxPlaySec / 5.0f, resetInitPosition);
        m_Agent.navMeshNavMeshAgent.speed = 0.0f;
    }
 
    public void Update()
    {
        m_DestroyTimer.Tick(Time.deltaTime);
 
        if( !bPosReset )
            m_PosResetTimer.Tick(Time.deltaTime);
    }
 
    protected void RemoveEffect()
    {
        m_Agent.removed -= OnRemoved;
        ResetAgent();
    }
 
    /// <summary>
    /// 重设当前的Agent到开始位置.
    /// </summary>
    protected void resetInitPosition()
    {
        m_Agent.navMeshNavMeshAgent.speed = m_Agent.originalMovementSpeed;
        m_Agent.ResetAgentToInitialPos();
        bPosReset = true;
       
    }
 
    void OnRemoved(DamageableBehaviour targetable)
    {
        m_Agent.removed -= OnRemoved;
        ResetAgent();
    }
 
    void ResetAgent()
    {
        m_Agent.navMeshNavMeshAgent.speed = m_Agent.originalMovementSpeed;
        if (m_PosResetFx != null)
        {
            var particle = m_PosResetFx.GetComponent<ParticleSystem>();
            if (particle)
                particle.Stop();
            Poolable.TryPool(m_PosResetFx);
            m_PosResetFx.transform.localScale = m_BackScale;
            m_PosResetFx = null;
        }
        Destroy(this);
    }
}