Пример #1
0
 public HealthEvent(int changeValue, int missingValue, byte currentHealth, HealthEventState eventState, Health effectedHealth, GameObject effectedGameObject, GameObject effectorGameObject)
 {
     this.eventState         = eventState;
     this.missingValue       = missingValue;
     this.changeValue        = changeValue;
     this.currentHealth      = currentHealth;
     this.effectedHealth     = effectedHealth;
     this.effectedGameObject = effectedGameObject;
     this.effectorGameObject = effectorGameObject;
 }
Пример #2
0
    /// <summary>Set the <see cref="CurrentValue"/> to a specified value</summary>
    /// <returns>Health change amount</returns>
    public int SetHealth(int value, GameObject effectedGameObject, GameObject effectorGameObject, bool invokeChangeEvent = true)
    {
        int currentHealth = _currentValue;
        var newHealth     = Mathf.Clamp(value, 0, byte.MaxValue);

        if (newHealth < 0)
        {
            newHealth = 0;
        }
        else if (newHealth > _maximumValue)
        {
            newHealth = _maximumValue;
        }

        var healthChange = newHealth - currentHealth;

        if (healthChange != 0)
        {
            HealthEventState eventState;

            if (healthChange > 0)
            {
                if (_incurable)
                {
                    return(0);
                }

                eventState = newHealth == _maximumValue ? HealthEventState.Full : HealthEventState.Healed;
            }
            else
            {
                if (_invincible)
                {
                    return(0);
                }

                eventState = newHealth == 0 ? HealthEventState.Died : HealthEventState.Damaged;
            }

            _currentValue = (byte)newHealth;

            var healthEvent = new HealthEvent(healthChange, _maximumValue - _currentValue, _currentValue, eventState, this, effectedGameObject, effectorGameObject);

            _lastHealthState = eventState;

            if (invokeChangeEvent)
            {
                InvokeOnChangeHealthEvent(healthEvent);
            }

            return(healthChange);
        }

        return(healthChange);
    }