River Jiang
2020-10-28 d2bc86161bf01b9ac01ba7b4b6ee7e341778c0c2
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Core.Utilities;
 
public class ProjectileMoveScript : MonoBehaviour
{
    public float speed;
    private float nowSpeed;
    public float recycleDelay = 0.8f;
    public float fireRate;
    public GameObject muzzlePrefab;
    public GameObject hitPrefab;
    public AudioClip shotSFX;
    public AudioClip hitSFX;
    public List<GameObject> trails;
    private bool collided;
 
    private void Start()
    {
        nowSpeed = speed;
        // if (muzzlePrefab != null)
        // {
        //     GameObject muzzleObj = Poolable.TryGetPoolable(muzzlePrefab);
        //     ParticleSystem ps = muzzleObj.GetComponent<ParticleSystem>();
        //     // ps = ps ?? muzzleObj.transform.GetChild(0).GetComponent<ParticleSystem>();
        //     if (ps == null)
        //         ps = muzzleObj.transform.GetChild(0).GetComponent<ParticleSystem>();
        //     ps.transform.position = transform.position;
        //     ps.transform.forward = gameObject.transform.forward;
        //     ps.Play();
        //     StartCoroutine(RecycleParticle(muzzleObj, ps.main.duration));
        // }
 
        if (muzzlePrefab != null)
        {
            var muzzleVFX = Instantiate(muzzlePrefab, transform.position, Quaternion.identity);
            muzzleVFX.transform.forward = gameObject.transform.forward;
            var ps = muzzleVFX.GetComponent<ParticleSystem>();
            if (ps != null)
            {
                ps.Play();
                Destroy(muzzleVFX, ps.main.duration);
            }
            else
            {
                var psChild = muzzleVFX.transform.GetChild(0).GetComponent<ParticleSystem>();
                psChild.Play();
                Destroy(muzzleVFX, psChild.main.duration);
            }
        }
 
        if (shotSFX != null && GetComponent<AudioSource>())
        {
            GetComponent<AudioSource>().PlayOneShot(shotSFX);
        }
 
        // StartCoroutine(RecycleParticle(gameObject, recycleDelay));
    }
 
    void Update()
    {
        if (nowSpeed != 0)
            transform.position += transform.forward * (nowSpeed * Time.deltaTime);
    }
 
    public IEnumerator RecycleParticle(GameObject particleObj, float waitTime)
    {
        yield return new WaitForSeconds(waitTime);
        // nowSpeed = 0;
        // ParticleSystem ps = particleObj.GetComponent<ParticleSystem>();
        // ps = ps ?? particleObj.transform.GetChild(0).GetComponent<ParticleSystem>();
        // ps.Stop();
        // Poolable.TryPool(particleObj);
        nowSpeed = 0;
        if (trails.Count > 0)
        {
            for (int i = 0; i < trails.Count; i++)
            {
                trails[i].transform.parent = null;
                var ps = trails[i].GetComponent<ParticleSystem>();
                if (ps != null)
                {
                    ps.Stop();
                    Destroy(ps.gameObject, ps.main.duration + ps.main.startLifetime.constantMax);
                }
            }
        }
 
        Destroy(gameObject);
    }
}