/// <summary> /// Handles damage and the associated feedbacks /// </summary> protected virtual void HandleDamage() { if (_hitObject == null) { return; } _health = _hitObject.MMGetComponentNoAlloc <Health>(); if (_health == null) { // hit non damageable if (HitNonDamageable != null) { HitNonDamageable.transform.position = _hitPoint; HitNonDamageable.transform.LookAt(this.transform); HitNonDamageable.PlayFeedbacks(); } if (NonDamageableImpactParticles != null) { NonDamageableImpactParticles.transform.position = _hitPoint; NonDamageableImpactParticles.transform.LookAt(this.transform); NonDamageableImpactParticles.Play(); } } else { // hit damageable _health.Damage(DamageCaused, this.gameObject, DamageCausedInvincibilityDuration, DamageCausedInvincibilityDuration); if (HitDamageable != null) { HitDamageable.transform.position = _hitPoint; HitDamageable.transform.LookAt(this.transform); HitDamageable.PlayFeedbacks(); } if (DamageableImpactParticles != null) { DamageableImpactParticles.transform.position = _hitPoint; DamageableImpactParticles.transform.LookAt(this.transform); DamageableImpactParticles.Play(); } } }
/// <summary> /// Describes what happens when colliding with a damageable object /// </summary> /// <param name="health">Health.</param> protected virtual void OnCollideWithDamageable(Health health) { // if what we're colliding with is a TopDownController, we apply a knockback force _colliderTopDownController = health.gameObject.MMGetComponentNoAlloc <TopDownController>(); _colliderRigidBody = health.gameObject.MMGetComponentNoAlloc <Rigidbody>(); if ((_colliderTopDownController != null) && (DamageCausedKnockbackForce != Vector3.zero) && (!_colliderHealth.Invulnerable) && (!_colliderHealth.ImmuneToKnockback)) { _knockbackForce.x = DamageCausedKnockbackForce.x; _knockbackForce.y = DamageCausedKnockbackForce.y; if (DamageCausedKnockbackDirection == KnockbackDirections.BasedOnSpeed) { Vector3 totalVelocity = _colliderTopDownController.Speed + _velocity; _knockbackForce = Vector3.RotateTowards(DamageCausedKnockbackForce, totalVelocity.normalized, 10f, 0f); } if (DamagedTakenKnockbackDirection == KnockbackDirections.BasedOnOwnerPosition) { if (Owner == null) { Owner = this.gameObject; } Vector3 relativePosition = _colliderTopDownController.transform.position - Owner.transform.position; _knockbackForce = Vector3.RotateTowards(DamageCausedKnockbackForce, relativePosition.normalized, 10f, 0f); } if (DamageCausedKnockbackType == KnockbackStyles.AddForce) { _colliderTopDownController.Impact(_knockbackForce.normalized, _knockbackForce.magnitude); } } HitDamageableFeedback?.PlayFeedbacks(this.transform.position); // we apply the damage to the thing we've collided with _colliderHealth.Damage(DamageCaused, gameObject, InvincibilityDuration, InvincibilityDuration); if (DamageTakenEveryTime + DamageTakenDamageable > 0) { SelfDamage(DamageTakenEveryTime + DamageTakenDamageable); } }
/// <summary> /// Applies damage to itself /// </summary> /// <param name="damage">Damage.</param> protected virtual void SelfDamage(int damage) { if (_health != null) { _health.Damage(damage, gameObject, 0f, DamageTakenInvincibilityDuration); if ((_health.CurrentHealth <= 0) && PerfectImpact) { this.transform.position = _collisionPoint; } } // if what we're colliding with is a TopDownController, we apply a knockback force if (_topDownController != null) { Vector2 totalVelocity = _colliderTopDownController.Speed + _velocity; Vector2 knockbackForce = Vector3.RotateTowards(DamageCausedKnockbackForce, totalVelocity.normalized, 10f, 0f); if (DamageTakenKnockbackType == KnockbackStyles.AddForce) { _topDownController.AddForce(knockbackForce); } } }