/// <summary> /// Apply a delta health object to this controller /// </summary> /// <param name="dHealth"></param> public void ApplyDeltaHealth(DeltaHealth dHealth) { float delta = dHealth.GetDelta(this); health += delta; if (maxHealth >= 0f) { health = Mathf.Max(maxHealth, health); } if (health <= 0f) { OnDeath(this, dHealth); } if (delta != 0f) { OnDelta?.Invoke(this, dHealth); } if (delta > 0f) { OnHeal?.Invoke(this, dHealth); } else if (delta < 0f) { OnHurt?.Invoke(this, dHealth); } }
/// <summary> /// Create a new hitbox on the gameObject with the DeltaHealth object /// </summary> /// <param name="gameObject"></param> /// <param name="dHealth"></param> /// <returns></returns> public static Hitbox Create(GameObject gameObject, DeltaHealth dHealth) { var hitbox = gameObject.AddComponent <Hitbox>(); hitbox.dHealth = dHealth; return(hitbox); }
/// <summary> /// Check a raycast hit, and call appropriate event handlers /// </summary> /// <param name="scanbox"></param> /// <param name="dHealth"></param> /// <param name="hit"></param> /// <param name="OnHit"></param> private static void ApplyCastHit(Scanbox scanbox, HitscanType type, DeltaHealth dHealth, CEvent cEvent, RaycastHit hit, Action <Scanbox, RaycastHit> OnHit) { if (scanbox != null) { scanbox.AcceptScan(type, dHealth, cEvent); } OnHit?.Invoke(scanbox, hit); }
/// <summary> /// Check a raycast hit, and call appropriate event handlers /// </summary> /// <param name="hurtbox"></param> /// <param name="dHealth"></param> /// <param name="hit"></param> /// <param name="OnHit"></param> private static void ApplyCastHit(Hurtbox hurtbox, DeltaHealth dHealth, RaycastHit hit, Action <Hurtbox, RaycastHit> OnHit) { if (dHealth != null && hurtbox != null) { hurtbox.ApplyDeltaHealth(dHealth); } OnHit?.Invoke(hurtbox, hit); }
public void AcceptScan(HitscanType type, DeltaHealth dHealth, CEvent cEvent) { if (hasHurtbox && (type & HitscanType.damage) == HitscanType.damage) { hurtbox.ApplyDeltaHealth(dHealth); } if (hasEventbox && (type & HitscanType.eventScan) == HitscanType.eventScan) { eventbox.AcceptEvent(cEvent); } }
/// <summary> /// Set the health of this controller. /// </summary> /// <param name="value"></param> /// <param name="dHealth">Delta Health object to apply on death (default null)</param> public void SetHealth(float value, DeltaHealth dHealth = null) { if (maxHealth >= 0f) { value = Mathf.Max(maxHealth, value); } health = value; if (health <= 0f) { OnDeath(this, dHealth); } }
/// <summary> /// Cast a hitscan over the scene /// </summary> /// <param name="ray">The ray to cast</param> /// <param name="dHealth">The Delta Health object to use for damage/healing</param> /// <param name="distance">The distance to apply the hitscan over. Default, infinity</param> /// <param name="layerMask">The layers to cast the hitscan over. Default all</param> /// <param name="penetrate">This hitscan penetrates walls. Default false</param> /// <param name="targets">The targets to cast the hitscan over. Default, whole scene</param> /// <param name="OnHit">The function to call on a hit. Default None</param> public static void Cast( Ray ray, HitscanType type, DeltaHealth dHealth = null, CEvent cEvent = null, float distance = Mathf.Infinity, int layerMask = -1, bool penetrate = false, Collider[] targets = null, Action <Scanbox, RaycastHit> OnHit = null ) => Cast(ray.origin, ray.direction, type, dHealth, cEvent, distance, layerMask, penetrate, targets, OnHit);
/// <summary> /// Cast a hitscan over the scene /// </summary> /// <param name="origin">The point to cast the hitscan from</param> /// <param name="direction">The direction to cast the hitscan</param> /// <param name="dHealth">The Delta Health object to use for damage/healing</param> /// <param name="distance">The distance to apply the hitscan over. Default, infinity</param> /// <param name="layerMask">The layers to cast the hitscan over. Default all</param> /// <param name="penetrate">This hitscan penetrates walls. Default false</param> /// <param name="targets">The targets to cast the hitscan over. Default, whole scene</param> /// <param name="OnHit">The function to call on a hit. Default None</param> public static void Cast(Vector3 origin, Vector3 direction, HitscanType type, DeltaHealth dHealth = null, CEvent cEvent = null, float distance = Mathf.Infinity, int layerMask = -1, bool penetrate = false, Collider[] targets = null, Action <Scanbox, RaycastHit> OnHit = null) { if (penetrate) { if (targets == null) { foreach (var hit in Physics.RaycastAll(origin, direction, distance, layerMask)) { ApplyCastHit(type, dHealth, cEvent, hit, OnHit); } } else { foreach (var coll in targets) { RaycastHit hit; if (coll.Raycast(new Ray(origin, direction), out hit, distance)) { ApplyCastHit(type, dHealth, cEvent, hit, OnHit); } } } } else { if (targets == null) { RaycastHit hit; if (Physics.Raycast(origin, direction, out hit, distance, layerMask)) { ApplyCastHit(type, dHealth, cEvent, hit, OnHit); } } else { foreach (var coll in targets) { RaycastHit hit; if (coll.Raycast(new Ray(origin, direction), out hit, distance)) { ApplyCastHit(type, dHealth, cEvent, hit, OnHit); } break; } } } }
/// <summary> /// Apply delta health to this hurtbox /// </summary> /// <param name="dHealth"></param> public virtual void ApplyDeltaHealth(DeltaHealth dHealth) { health.ApplyDeltaHealth(dHealth); }
/// <summary> /// Check a raycast hit, and call appropriate event handlers. /// </summary> /// <param name="dHealth"></param> /// <param name="hit"></param> /// <param name="OnHit"></param> private static void ApplyCastHit(HitscanType type, DeltaHealth dHealth, CEvent cEvent, RaycastHit hit, Action <Scanbox, RaycastHit> OnHit) { ApplyCastHit(hit.collider.GetComponent <Scanbox>(), type, dHealth, cEvent, hit, OnHit); }
/// <summary> /// Check a raycast hit, and call appropriate event handlers. /// </summary> /// <param name="dHealth"></param> /// <param name="hit"></param> /// <param name="OnHit"></param> private static void ApplyCastHit(DeltaHealth dHealth, RaycastHit hit, Action <Hurtbox, RaycastHit> OnHit) { ApplyCastHit(hit.collider.GetComponent <Hurtbox>(), dHealth, hit, OnHit); }
/// <summary> /// Cast a hitscan over the scene /// </summary> /// <param name="ray">The ray to cast</param> /// <param name="dHealth">The Delta Health object to use for damage/healing</param> /// <param name="distance">The distance to apply the hitscan over. Default, infinity</param> /// <param name="layerMask">The layers to cast the hitscan over. Default all</param> /// <param name="penetrate">This hitscan penetrates walls. Default false</param> /// <param name="targets">The targets to cast the hitscan over. Default, whole scene</param> /// <param name="OnHit">The function to call on a hit. Default None</param> public static void Cast(Ray ray, DeltaHealth dHealth, float distance = Mathf.Infinity, int layerMask = -1, bool penetrate = false, Collider[] targets = null, Action <Hurtbox, RaycastHit> OnHit = null) => Cast(ray.origin, ray.direction, dHealth, distance, layerMask, penetrate, targets, OnHit);