Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [System.Serializable]
- public class DamagedTarget
- {
- public GameObject target;
- public float damageTime;
- public DamagedTarget( GameObject g, float t)
- {
- damageTime = t;
- target = g;
- }
- }
- public class CollisionDamage : MonoBehaviour
- {
- // 1 for player, 2 for enemies
- public int target;
- public enum eos { None, Enter, Stay };
- public eos enterOrStay = eos.None;
- public Entity parentEntity;
- public Stats myStats = new Stats();
- public Damage.type type = Damage.type.Physical;
- public Damage.lifeDamage lifeDamage = Damage.lifeDamage.Normal;
- private float delay = 0;
- public float maxDelay = 0.5f;
- /*
- * Okay, so this works like a list saying who this unit damaged in the past (maxDelay) seconds.
- * If an object is here, then it means it was damaged in the past (maxDelay) seconds... theorically.
- * */
- public List<DamagedTarget> recentlyDamagedTargets = new List<DamagedTarget>();
- private void LateUpdate()
- {
- foreach (DamagedTarget i in recentlyDamagedTargets)
- {
- // This lessens every delay every update until it goes slightly past zero.
- if (i.damageTime > 0)
- i.damageTime -= Time.deltaTime;
- // Not using "else" here because the value changes within the first condition.
- // 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
- // checks if the gameObject has already been destroyed. You can't colide with null, theorically. ;/
- if (i.damageTime <= 0 && i.target == null)
- recentlyDamagedTargets.Remove(i);
- }
- }
- private void Start()
- {
- if (parentEntity != null) myStats = parentEntity.myStats;
- }
- void OnTriggerEnter2D(Collider2D col)
- {
- if (enterOrStay == eos.Enter)
- {
- Entity d = null;
- if (target == 1 && col.gameObject.tag == "Player")
- {
- d = col.gameObject.GetComponent(typeof(PlayerController)) as PlayerController;
- }
- if (target == 2 && col.gameObject.tag == "Enemy")
- {
- d = col.gameObject.GetComponent(typeof(Enemy)) as Enemy;
- }
- if (d != null)
- {
- Damage dmg = new Damage(myStats.baseDamage, type, lifeDamage);
- dmg.myCritChance = myStats.critcalChance;
- dmg.myCritMultiplier = myStats.criticalMultiplier;
- int index = recentlyDamagedTargets.FindIndex(r => r.target == col.gameObject);
- if (index < 0) {
- recentlyDamagedTargets.Add(new DamagedTarget(col.gameObject, maxDelay));
- d.Damage(dmg);
- }
- else
- {
- if (recentlyDamagedTargets[index].damageTime <= 0)
- {
- recentlyDamagedTargets[index].damageTime = maxDelay;
- d.Damage(dmg);
- }
- }
- }
- }
- }
- // There's a OnCollisionStay under here, but I'll cut it for simplicity, since it isn't even being called for now
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement