Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.22 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Projectile : MonoBehaviour
  6. {
  7.     [Tooltip("The force to apply to the rigidbody when fired.")]
  8.     public float shotForce = 10f;
  9.  
  10.     [Tooltip("How long this projectile can exist.")]
  11.     public float lifeTime = 5f;
  12.  
  13.     [Space]
  14.     public int damageAmount = 10;
  15.     [TagSelector]
  16.     public string damageTag;
  17.  
  18.     [Space()]
  19.     public bool destroyOnCollision = true;
  20.     public GameObject explosionPrefab;
  21.     public AnimationClip deathAnimClip;
  22.     public SoundEffectBase.SoundEffect deathSound;
  23.  
  24.     //Hidden from inspector as value is set by script when fired
  25.     [HideInInspector]
  26.     public ElementManager.Element element;
  27.  
  28.     private Rigidbody2D body;
  29.     private GameObject owner;
  30.     private Animator animator;
  31.     private SoundEffectBase soundEffects;
  32.  
  33.     void Awake()
  34.     {
  35.         body = GetComponent<Rigidbody2D>();
  36.         animator = GetComponentInChildren<Animator>();
  37.     }
  38.  
  39.     void Start()
  40.     {
  41.         soundEffects = GameManager.instance.GetComponent<SoundEffectBase>();
  42.     }
  43.  
  44.     void OnEnable()
  45.     {
  46.         //Reset values (object pooling)
  47.         body.velocity = Vector2.zero;
  48.         body.angularVelocity = 0;
  49.         body.rotation = 0;
  50.  
  51.         if (animator)
  52.             animator.SetBool("isAlive", true);
  53.  
  54.         //Disable after its lifetime
  55.         StartCoroutine("DisableAfterTime", lifeTime);
  56.  
  57.         ParticleSystem particles = GetComponentInChildren<ParticleSystem>();
  58.  
  59.         if(particles)
  60.         {
  61.             particles.Clear(true);
  62.         }
  63.     }
  64.  
  65.     IEnumerator DisableAfterTime(float time)
  66.     {
  67.         yield return new WaitForSeconds(time);
  68.  
  69.         Die();
  70.     }
  71.  
  72.     public void SetOwner(GameObject obj)
  73.     {
  74.         owner = obj;
  75.  
  76.         //Ignore collision with owner (if there is one)
  77.         if(owner)
  78.             Physics2D.IgnoreCollision(GetComponent<Collider2D>(), owner.GetComponent<Collider2D>());
  79.     }
  80.  
  81.     public void Fire(Vector2 direction)
  82.     {
  83.         //Set to face firection of fire
  84.         transform.rotation = Quaternion.AngleAxis(Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg, Vector3.forward);
  85.  
  86.         //Add initial force
  87.         body.AddForce(direction * shotForce, ForceMode2D.Impulse);
  88.     }
  89.  
  90.     void OnCollisionEnter2D(Collision2D col)
  91.     {
  92.         bool hitCharacter = false;
  93.  
  94.         //If it hit something that should be damaged
  95.         if(col.gameObject.tag == damageTag)
  96.         {
  97.             //Record hit to spawn effect later
  98.             hitCharacter = true;
  99.  
  100.             //Get characterstats
  101.             CharacterStats stats = col.gameObject.GetComponent<CharacterStats>();
  102.  
  103.             //If hit gameobject has characterstats
  104.             if(stats)
  105.             {
  106.                 //Apply damage
  107.                 stats.RemoveHealth(damageAmount, element);
  108.  
  109.                 //Enemy aggro on hit
  110.                 if(col.collider.tag == "Enemy")
  111.                     col.gameObject.SendMessage("SetAggro", true);
  112.             }
  113.         }
  114.  
  115.         //If it should destroy on collision with ground, or character
  116.         if (destroyOnCollision || hitCharacter)
  117.         {
  118.             //Make sure the timer coroutine is no longer running
  119.             StopCoroutine("DisableAfterTime");
  120.  
  121.             //Return to pool
  122.             Die();
  123.         }
  124.     }
  125.  
  126.     public void SpawnEffect()
  127.     {
  128.         //If there is an explosion prefab
  129.         if (explosionPrefab)
  130.         {
  131.             //Get from pool
  132.             GameObject effect = ObjectPooler.GetPooledObject(explosionPrefab);
  133.  
  134.             //Position at projectile position
  135.             effect.transform.position = transform.position;
  136.         }
  137.     }
  138.  
  139.     void Die()
  140.     {
  141.         StartCoroutine("DeathAnimation");
  142.     }
  143.  
  144.     IEnumerator DeathAnimation()
  145.     {
  146.         if (animator)
  147.         {
  148.             animator.SetBool("isAlive", false);
  149.         }
  150.  
  151.         if(soundEffects && deathSound.clip)
  152.         {
  153.             soundEffects.PlaySound(deathSound);
  154.         }
  155.  
  156.         body.velocity = Vector2.zero;
  157.         body.angularVelocity = 0;
  158.  
  159.         if (deathAnimClip)
  160.             yield return new WaitForSeconds(deathAnimClip.length);
  161.  
  162.         SpawnEffect();
  163.  
  164.         gameObject.SetActive(false);
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement