private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.layer == impactLayer && !invulnerable) { Impacted?.Invoke(); } }
public bool Raycast(Vector3 position, Vector3 direction, out HitscanInfo scanInfo, float maxDistance, Vector3 muzzle) { scanInfo = default; scanInfo.origin = position; scanInfo.direction = direction; scanInfo.maxDistance = maxDistance; scanInfo.muzzle = muzzle; bool success = Physics.Raycast(position, direction, out scanInfo.hitInfo, maxDistance, layerMask); scanInfo.success = success; // Events processing HitscanEventArgs args = new HitscanEventArgs(scanInfo); Launched?.Invoke(this, args); if (success) { Impacted?.Invoke(this, args); } return(success); }
/// <summary> /// Take damage /// </summary> /// <param name="damageDealer"></param> /// <returns></returns> private void TakeDamage(DamageDealer damageDealer, GameObject damageSourceObject) { // check if damage dealer is self if (GetComponentsInChildren <DamageDealer>().ToList().Contains(damageDealer)) { return; } // Get hit direction HitDirection hitDirection = GetHitDirection(damageDealer.gameObject.transform.position); // Check for immunity if (directionImmunity != 0 && (directionImmunity | hitDirection) == hitDirection) { onImmuneToDamage?.Invoke(); return; } if (immuneRemaining > 0) { return; } // Add Effects #if STATS_COG if (damageDealer.effects != null) { foreach (StatEffect effect in damageDealer.effects) { if (effectList.availableEffects.Contains(effect)) { AddEffect(effect); } } } #endif float adjustedDamage = 0; float totalAdjustedDamage = 0; // Apply weakness foreach (Damage damage in damageDealer.damage) { #if STATS_COG if (damageDealer.StatsSource != null) { adjustedDamage = damageDealer.StatsSource.GetExpressionValue(damage.baseAmount); } else { adjustedDamage = float.Parse(damage.baseAmount); } #endif if (damage.damageType != null) { List <DamageModifier> modifiers = FindDamageModifiers(damage.damageType.name); foreach (DamageModifier dm in modifiers) { if (dm.modifierType == DamageModType.Resistance) { adjustedDamage -= adjustedDamage * dm.CurrentValue; if (dm.CurrentValue == 1) { onImmuneToDamage?.Invoke(); } } else { adjustedDamage *= dm.CurrentValue; } } } else { Debug.LogError("Damage is missing a DamageType"); } totalAdjustedDamage += adjustedDamage; } // Apply damage StatValue hp = FindStat(healthStat); if (hp != null) { adjustedDamage = Mathf.Clamp(GetExpressionValue(ReplaceInsensitive(damageValue, "[damage]", totalAdjustedDamage.ToString())), 0, float.MaxValue); hp.SetValue(hp.CurrentValue - adjustedDamage); lastDmgSource = damageDealer.StatsSource; onDamageTaken?.Invoke(adjustedDamage, damageDealer, damageSourceObject); onHitDamageDirection?.Invoke(hitDirection); immuneRemaining = immunityAfterHit; onHitDirection?.Invoke(hitDirection); if (hp.CurrentValue <= 0) { onDeath?.Invoke(); } } else { Debug.LogWarning("Could not find '" + healthStat + "' to apply damage"); } }