Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [System.Serializable]
  6. public class DamagedTarget
  7. {
  8.     public GameObject target;
  9.     public float damageTime;
  10.  
  11.     public DamagedTarget( GameObject g,  float t)
  12.     {
  13.         damageTime = t;
  14.         target = g;
  15.     }
  16. }
  17.  
  18. public class CollisionDamage : MonoBehaviour
  19. {
  20.  
  21.     // 1 for player, 2 for enemies
  22.     public int target;
  23.  
  24.  
  25.     public enum eos { None, Enter, Stay };
  26.     public eos enterOrStay = eos.None;
  27.  
  28.     public Entity parentEntity;
  29.     public Stats myStats = new Stats();
  30.  
  31.     public Damage.type type = Damage.type.Physical;
  32.     public Damage.lifeDamage lifeDamage = Damage.lifeDamage.Normal;
  33.  
  34.  
  35.     private float delay = 0;
  36.     public float maxDelay = 0.5f;
  37.  
  38.     /*
  39.      *  Okay, so this works like a list saying who this unit damaged in the past (maxDelay) seconds.
  40.      *  If an object is here, then it means it was damaged in the past (maxDelay) seconds... theorically.
  41.      * */
  42.     public List<DamagedTarget> recentlyDamagedTargets = new List<DamagedTarget>();
  43.    
  44.  
  45.     private void LateUpdate()
  46.     {
  47.         foreach (DamagedTarget i in recentlyDamagedTargets)
  48.         {
  49.             // This lessens every delay every update until it goes slightly past zero.
  50.             if (i.damageTime > 0)
  51.                 i.damageTime -= Time.deltaTime;
  52.  
  53.  
  54.             // Not using "else" here because the value changes within the first condition.
  55.             // This is where the error resides. I don't know why, since the only other place that uses this list is on collision, and this condition
  56.             // checks if the gameObject has already been destroyed. You can't colide with null, theorically. ;/
  57.             if (i.damageTime <= 0 && i.target == null)
  58.                 recentlyDamagedTargets.Remove(i);
  59.  
  60.         }
  61.     }
  62.  
  63.     private void Start()
  64.     {
  65.         if (parentEntity != null) myStats = parentEntity.myStats;
  66.     }
  67.  
  68.     void OnTriggerEnter2D(Collider2D col)
  69.     {
  70.         if (enterOrStay == eos.Enter)
  71.         {
  72.  
  73.             Entity d = null;
  74.             if (target == 1 && col.gameObject.tag == "Player")
  75.             {
  76.                 d = col.gameObject.GetComponent(typeof(PlayerController)) as PlayerController;
  77.             }
  78.  
  79.             if (target == 2 && col.gameObject.tag == "Enemy")
  80.             {
  81.                 d = col.gameObject.GetComponent(typeof(Enemy)) as Enemy;
  82.             }
  83.  
  84.             if (d != null)
  85.             {
  86.  
  87.                 Damage dmg = new Damage(myStats.baseDamage, type, lifeDamage);
  88.                 dmg.myCritChance = myStats.critcalChance;
  89.                 dmg.myCritMultiplier = myStats.criticalMultiplier;
  90.                
  91.                 int index = recentlyDamagedTargets.FindIndex(r => r.target == col.gameObject);
  92.                 if (index < 0) {
  93.                     recentlyDamagedTargets.Add(new DamagedTarget(col.gameObject, maxDelay));
  94.                     d.Damage(dmg);
  95.                 }
  96.  
  97.                 else
  98.                 {
  99.                     if (recentlyDamagedTargets[index].damageTime <= 0)
  100.                     {
  101.                         recentlyDamagedTargets[index].damageTime = maxDelay;
  102.                         d.Damage(dmg);
  103.                     }
  104.                 }
  105.  
  106.  
  107.             }
  108.  
  109.         }
  110.  
  111.     }
  112.  
  113.     // There's a OnCollisionStay under here, but I'll cut it for simplicity, since it isn't even being called for now
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement