summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/TimelineEventProxy.cs
blob: 4a71400c3912876f7c715b8ab437c9a8101a1dfa (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using UnityEngine;

// 执行帧事件
[DisallowMultipleComponent]
public partial class TimelineEventProxy
{

#if UNITY_EDITOR // ActionTool里
    private static GameObject m_Root_Particles;
    public static GameObject Root_Particles
    {
        get
        {
            if (m_Root_Particles == null)
            {
                m_Root_Particles = GameObject.Find("Root_Particles");
                if(m_Root_Particles == null) 
                    m_Root_Particles = new GameObject("Root_Particles");
            }
            return m_Root_Particles;
        }
    }
    private static GameObject m_Root_Projectiles;
    public static GameObject Root_Projectiles
    {
        get
        {
            m_Root_Projectiles = GameObject.Find("Root_Projectiles");
            if (m_Root_Projectiles == null)
                m_Root_Projectiles = new GameObject("Root_Projectiles");
            return m_Root_Projectiles;
        }
    }

    public bool isInEditMode;

    public delegate void RegisterProjectileHandle(Projectile projectile);
    public RegisterProjectileHandle registerProjectile;

    public delegate void RegisterParticleSystemHandle(FxClear vfx);
    public RegisterParticleSystemHandle registerParticleSystem;
#endif

    public enum EEventType
    {
        EventGame_TimeScale,  // 缩放时间

        EventCamera_Zoom,     // 相机聚焦
        EventCamera_Shake,    // 相机晃动
        EventCamera_Blur,     // 相机模糊
        EventCamera_WhiteOut, // 相机白屏

        EventMesh_AfterImage, // 角色残像
		EventMesh_AfterImageStop, // 角色残像停止事件
		EventMesh_FadeIn,     // 角色透明度
		EventMesh_FadeOut,    // 角色透明度
        EventMesh_Gloss,      // 角色泛光

		EventEnv_Dark,
        EventEnv_Exposure,

        EventUI_Drift,        // 
        EventUI_Blur,         //  

        EventProjectile,      // 发射体
        EventEffect,          // 特效
        EventSound,           // 音效

        EventBulletTime,      // 子弹时间
    }

    public const int kMaxEventsPerFrame = 10;
    public const int FPS = 30; // timeline 每秒采样30次

    private int m_PrevFrame = -1;

	private Transform m_Root;

	public UnitController owner { get; private set; }

	private UnitAnimation m_UnitAnimation { get { return owner.unitAnimation; } }

    private AnimationData m_PrevAnimationData;

    public TimelineEventProxy(Transform root, UnitController owner = null)
	{
		m_Root = root;
		this.owner = owner;
        m_PrevAnimationData = null;
    }

    public static Type GetTypeByName(string name)
    {
        Type type = Type.GetType(name);
        return type;
    }

    public void ResetPrevAnimationData()
    {
        m_PrevAnimationData = null;
    }

    public void ExecuteAnimationEvents(AnimationData animData, float animFrame)
    {
        if (animData == null)
            return;

        int frame = (int)animFrame;
        if (m_PrevAnimationData != animData)
        {
            m_PrevFrame = frame;
            m_PrevAnimationData = animData;
        }
        if (frame != m_PrevFrame)
        {
            for (int i = m_PrevFrame + 1; i <= frame; i++)
            {
                List<int> framesHasEvent = animData.GetAnimationEventFrameIndices();
                if (framesHasEvent.Contains(i))
                {
                    List<AnimationEventBase> events = animData.GetAnimationEventsAtFrame(i);
                    ExecuteEvents(events);
                    ListPool<AnimationEventBase>.Release(events);
                }
                ListPool<int>.Release(framesHasEvent);
            }
        }
        m_PrevFrame = frame;
    }

    #region Event Handles

    void ExecuteEvents(List<AnimationEventBase> events)
    {
        if (events == null || events.Count == 0)
            return;
        foreach (var e in events)
        {
            string name = e.type.ToString();
            MethodInfo method = GetType().GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(AnimationEventBase) }, null);
            if (method != null)
            {
                object[] param = new object[] { e };
                method.Invoke(this, param);
            }
        }
    }

    void EventEffect(AnimationEventBase animEvent)
    {
        EventEffect effect = animEvent as EventEffect;
        if (effect == null)
            return;
        string path = effect.effectPath;
        GameObject prefab = ResourceManager.Instance.LoadAsset<GameObject>(path);
        if (prefab != null)
        {
            GameObject root = new GameObject();

            GameObject go = GameObject.Instantiate(prefab);
            go.transform.SetParent(root.transform);
            FxClear onClear = root.AddComponent<FxClear>();
            onClear.gameObject.name = prefab.name + "(Clone)";
            onClear.Initialize(new PlayEffectInfo(path, EffectPlayTypes.Oneshot, m_Root, effect.position, effect.rotation, effect.scale, 0, false));
#if UNITY_EDITOR
            if (isInEditMode && registerParticleSystem != null)
            {
                registerParticleSystem(onClear);
            }
            onClear.gameObject.transform.SetParent(Root_Particles.transform);
#endif
        }
    }

    void EventCamera_Shake(AnimationEventBase animEvent)
    {

    }

    void EventProjectile(AnimationEventBase animEvent)
    {
        EventProjectile e = animEvent as EventProjectile;
        if (e == null)
            return;
        string projectilePath = e.projectilePath;
        GameObject prefab = ResourceManager.Instance.LoadAsset<GameObject>(projectilePath);
        if(prefab == null)
        {
            LogHelper.LogError("缺少对应的projectile, " + projectilePath);
            return;
        }
        if(prefab.GetComponent<Projectile>() == null)
        {
            LogHelper.LogError("没有projectile脚本");
            return;
        }
        GameObject obj = GameObject.Instantiate(prefab);
        Projectile projectile = obj.GetComponent<Projectile>();
        ProjectileInfo info = new ProjectileInfo();
        info.owner = owner;
        info.position = m_Root.transform.position + e.posOffset;
        info.rotation = e.rotation;
        info.scale = e.scale;
        info.direction = Vector3.right;
        info.velocity = Vector3.right * 10f;
        info.lifetime = 5;
        info.sparkPath = e.sparkPath;
        projectile.Initialize(info);

#if UNITY_EDITOR
        if(isInEditMode && registerProjectile != null)
        {
            registerProjectile(projectile);
        }
        obj.transform.SetParent(Root_Projectiles.transform);
#endif
    }

	void EventMesh_AfterImage(AnimationEventBase animEvent)
	{
		EventMesh_AfterImage afterImage = animEvent as EventMesh_AfterImage;
		if (afterImage == null)
			return ;

	}

	#endregion

}