123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace HQFPSWeapons
- {
- public class LingeringFire : MonoBehaviour
- {
- [SerializeField]
- private LightEffect m_LightEffect = null;
- [SerializeField]
- [Clamp(0f, 1000f)]
- private float m_DamagePerSecond = 20f;
- [SerializeField]
- [Range(0f, 10f)]
- private float m_LingeringTime = 5f;
- [SerializeField]
- private float m_StopDuration = 1f;
- [SerializeField]
- private LayerMask m_LayerMask = new LayerMask();
- private ParticleSystem m_FireParticles;
- private AudioSource m_AudioS;
- private bool m_CanDamage = true;
- private List<LivingEntity> m_AffectedEntities = new List<LivingEntity>();
- private float m_DamageMultiplier = 1f;
- public void StartFire()
- {
- m_AudioS = GetComponent<AudioSource>();
- m_FireParticles = GetComponent<ParticleSystem>();
- m_FireParticles.Stop();
- var main = m_FireParticles.main;
- main.duration = m_LingeringTime;
- m_FireParticles.Play();
- m_LightEffect.Play(true);
- PositionFire();
- StartCoroutine(C_FireLingering());
- }
- private void OnTriggerEnter(Collider other)
- {
- if (other.GetComponent<LivingEntity>() != null)
- m_AffectedEntities.Add(other.GetComponent<LivingEntity>());
- }
- private void OnTriggerExit(Collider other)
- {
- if (other.GetComponent<LivingEntity>() != null)
- m_AffectedEntities.Remove(other.GetComponent<LivingEntity>());
- }
- private void OnTriggerStay(Collider other)
- {
- Fire();
- }
- private void PositionFire()
- {
- RaycastHit hit;
- if (Physics.Raycast(transform.position, Vector3.down, out hit, 10f, m_LayerMask))
- {
- transform.position = hit.point;
- }
- }
- private void Fire()
- {
- if (!m_CanDamage)
- return;
- foreach (var entity in m_AffectedEntities)
- {
- if (entity != null)
- entity.ChangeHealth.Try(new HealthEventData(-m_DamagePerSecond * m_DamageMultiplier * Time.deltaTime));
- }
- }
- IEnumerator C_FireLingering()
- {
- yield return new WaitForSeconds(m_LingeringTime);
- m_FireParticles.Stop();
- m_LightEffect.Stop(true);
- float stopDuration = Time.time + m_StopDuration;
- WaitForEndOfFrame wait = new WaitForEndOfFrame();
- while (stopDuration > Time.time)
- {
- m_AudioS.volume -= Time.deltaTime * ( 1 / m_StopDuration);
- m_DamageMultiplier -= Time.deltaTime * (1 / m_StopDuration);
- yield return wait;
- }
- m_CanDamage = false;
- }
- }
- }
|