private void OnTriggerEnter(Collider other) { if (_healthsContainer == null) { return; } var collidingGO = other.gameObject; if (!_damageLayer.Contains(collidingGO.layer)) { return; } var healthId = collidingGO.GetInstanceID(); var health = _healthsContainer.GetHealth(healthId); if (health != null) { health.TakeDamage(_damageAmount); } var myId = gameObject.GetInstanceID(); OnDealDamage?.Invoke(this); }
public void DealDamage(CreatureStats from, CreatureStats to, int number, IDamage damage = null) { if (damage == null) { damage = new BaseDamage(); } int realDamage = damage.GetCalculate(to, number); to.HP -= realDamage; if (to.HP <= 0) { OnDeathPlayer?.Invoke(to, damage); } Debug.LogFormat("{0} наносит {1} урон {2}", from.name, realDamage, to.name); OnDealDamage?.Invoke(to, damage); }
protected virtual void OnCollisionEnter2D(Collision2D collision) { if (!collision.gameObject.tag.Contains("CanBoop") || _health <= 0) { return; } Vector2 contactPoint = collision.GetContact(0).point; Vector2 vectorToContact = contactPoint - (Vector2)transform.position; Vector2 knockbackVector = collision.GetContact(0).normal; knockbackVector = knockbackVector * knockbackStrength; if (Vector2.Angle(transform.up, vectorToContact) < DamageRecievingAngle * 0.5f) { //This DR is taking damage collision.rigidbody?.AddForce(Vector2.up * boopBoost, ForceMode2D.Impulse); TakeDamage(new DamagePacket(DamageType.COLLISION, knockbackVector, collision.transform.GetComponent <IDamageDealer>())); } else { //Other DR is taking damage if (TryGetComponent(out Rigidbody2D rb)) { rb.AddForce(knockbackVector, ForceMode2D.Impulse); } DamageReceiver otherDR = collision.transform.GetComponent <DamageReceiver>(); otherDR?.TakeDamage(new DamagePacket(DamageType.COLLISION, damageAmount, -knockbackVector, _damageSource)); if (otherDR) { OnDealDamage?.Invoke(otherDR, _damageSource); } } }
public void dealDamage() { targetAttack = null; OnDealDamage?.Invoke(); }
public bool DealDamage(GameObject hit, Vector3 hitPoint) { DamageReceiver receiver = FindObjectDamageReceiver(hit); // 检查开关 if (receiver == null || !receiver.enabled) { return(false); } // 检查忽略 if (IgnoredReceivers.Contains(receiver)) { return(false); } // 检查类型匹配 if (!damageTypes.GetType(receiver.type)) { //Debug.Log($"{hit.transform.GetPath()}目标类型与DamageDealer类型不匹配:{receiver.type} {damageTypes}"); return(false); } // 检查伤害判定间隔 if (useDamageInterval && timers.ContainsKey(receiver.GetInstanceID()) && timers[receiver.GetInstanceID()] < damageInterval) { return(false); } this.hitPoint = hitPoint; // 实际伤害值 float realDamage = 0f; // 实际伤害结算次数 int realDamageApplyCount = damageApplyCount; switch (damageSource) { case DamageSource.Player: break; case DamageSource.Enemy: if (statCollection == null) { statCollection = origin?.GetComponent <RPGStatCollection> (); } if (!dontDealDamage) { damageInfo = new DamageInfo(damage, origin.gameObject); damageInfo.damageSourceType = damageSource; damageInfo.attackType = attackType; damageInfo.attackRangeType = attackRangeType; damageInfo.movesName = moveName; } break; case DamageSource.Trap: if (!dontDealDamage) { damageInfo = new DamageInfo(damage, origin.gameObject); damageInfo.damageSourceType = damageSource; damageInfo.attackType = attackType; damageInfo.attackRangeType = attackRangeType; damageInfo.movesName = moveName; damageInfo.ignoreBlock = ignoreBlock; } break; } // 伤害结算 if (damageInfo != null) { //Debug.Log ( $"对{receiver.transform.GetPath ()}结算伤害" ); for (var i = 0; i < realDamageApplyCount; ++i) { receiver.TakeDamage(damageInfo); } } // 伤害结算完毕后,是否要忽略后续对相同Receiver的判定 if (ignoreReceiverOnDealDamage) { IgnoreReceiver(receiver); } if (useDamageInterval) { if (!timers.ContainsKey(receiver.GetInstanceID())) { timers.Add(receiver.GetInstanceID(), 0f); } timers[receiver.GetInstanceID()] = 0.0f; } OnDealDamage?.Invoke(damage * damageMultiplier, receiver, this); return(true); }