public void GetDamage(float damage)
    {
        currentHealth -= damage;

        if (currentHealth <= 0)
        {
            _deathDelegate.Invoke();
        }
    }
Exemplo n.º 2
0
 private void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.tag != "Missile")
     {
         return;
     }
     Destroy(other.gameObject);
     isDead      = true;
     timeOfDeath = Time.time;
     AudioManager.Instance.PlaySound("Explosion");
     DeathDelegate?.Invoke();
 }
Exemplo n.º 3
0
    public void Death()
    {
        callback?.Invoke();
        float val = UnityEngine.Random.value;

        if (val <= HealthDropChance)
        {
            Instantiate(healthPickup, transform.position, healthPickup.transform.rotation);
        }

        Destroy(this.gameObject);
    }
Exemplo n.º 4
0
        public void ApplyDamage(DamageMessage data)
        {
            if (currentHitPoints <= 0)
            {//ignore damage if already dead. TODO : may have to change that if we want to detect hit on death...
                return;
            }

            if (isInvulnerable)
            {
                OnHitWhileInvulnerable.Invoke();
                return;
            }

            Vector3 forward = transform.forward;

            forward = Quaternion.AngleAxis(hitForwardRotation, transform.up) * forward;

            //we project the direction to damager to the plane formed by the direction of damage
            Vector3 positionToDamager = data.damageSource - transform.position;

            positionToDamager -= transform.up * Vector3.Dot(transform.up, positionToDamager);

            if (Vector3.Angle(forward, positionToDamager) > hitAngle * 0.5f)
            {
                return;
            }

            isInvulnerable    = true;
            currentHitPoints -= data.amount;

            if (currentHitPoints <= 0)
            {
                schedule += OnDeath.Invoke; //This avoid race condition when objects kill each other.
                myOnDeath.Invoke(this, data);
            }
            else
            {
                OnReceiveDamage.Invoke();
                myOnDamageReceived?.Invoke(this, data);
            }

            var messageType = currentHitPoints <= 0 ? MessageType.DEAD : MessageType.DAMAGED;

            for (var i = 0; i < onDamageMessageReceivers.Count; ++i)
            {
                var receiver = onDamageMessageReceivers[i] as IMessageReceiver;
                receiver.OnReceiveMessage(messageType, this, data);
            }
        }
Exemplo n.º 5
0
 public void Die()
 {
     isDead = true;
     DeathEvent?.Invoke();
 }
Exemplo n.º 6
0
 public static void Restart()
 {
     RestartEvent?.Invoke();
     DeathEvent?.Invoke(SceneManager.GetActiveScene().buildIndex);
 }