Esempio n. 1
0
    public void TakeDamage(int amount, GameObject from)
    {
        if (debug)
        {
            Debug.Log(name + " taking damage: " + amount + " from: " + from);
        }
        // if health is already zero (or zero damage), can't take more damage
        if (_health == 0 || amount <= 0)
        {
            return;
        }

        // apply damage
        _health -= amount;

        // check for death
        if (_health <= 0)
        {
            _health = 0;
            onDeath.Invoke(from);
            if (debug)
            {
                Debug.Log("Death from: " + from);
            }
        }

        // communicate health change
        onChange.Invoke(_health);
        var percent = (_health * 100) / maxHealth;

        onChangePercent.Invoke(percent);
        onTakeDamage.Invoke(from, amount);
    }
Esempio n. 2
0
    public void Switch(DropHandler other, bool notify = true)
    {
        Dragable ownDragable = GetComponentInChildren <Dragable>();

        ownDragable.transform.SetParent(other.transform);
        ownDragable.GetComponent <RectTransform>().localPosition = Vector3.zero;
        Dragable otherDragable = other.GetComponentInChildren <Dragable>();

        otherDragable.transform.SetParent(transform);
        otherDragable.GetComponent <RectTransform>().localPosition = Vector3.zero;
        if (notify)
        {
            OnDropEvent.Invoke(other.transform.gameObject, index);
            other.OnDropEvent.Invoke(ownDragable.gameObject, other.index);
            OnSwapEvent.Invoke(other.index, index);
        }
    }