예제 #1
0
        /// <summary>
        /// Send a command to StatsCog
        /// </summary>
        /// <param name="command"></param>
        public void SendCommand(string command)
        {
            command = command.Replace("  ", " ");
            int   i = command.IndexOf(' ');
            int   e = command.IndexOf('=');
            float res;

            switch (command.Substring(0, i).Trim().ToLower())
            {
            case "add":             // Add effect
                AddEffect(command.Substring(i).Trim());
                break;

            case "remove":          // Remove effect
                RemoveEffect(command.Substring(i).Trim());
                break;

            case "removeall":       // Remove effect all
                RemoveEffectAll(command.Substring(i).Trim());
                break;

            case "clear":           // Clear effects
                ClearEffects();
                break;

            case "max":             // Set max value
                res = ParseStatValue(command.Substring(e + 1));
                FindStat(command.Substring(i, e - i - 1).Trim()).SetMaximum(res);
                break;

            case "min":             // Set min value
                res = ParseStatValue(command.Substring(e + 1));
                FindStat(command.Substring(i, e - i - 1).Trim()).SetMinimum(res);
                break;

            case "restore-min":     // Retore value to a minimum of passed value
                e   = command.IndexOf(' ', i + 1);
                res = ParseStatValue(command.Substring(e + 1).Trim());
                StatValue val = FindStat(command.Substring(i, e - i).Trim());
                if (val != null)
                {
                    if (val.CurrentValue < res)
                    {
                        val.SetValue(res);
                    }
                }
                break;

            case "setmax":          // Set value to maximum
                StatValue stat = FindStat(command.Substring(i).Trim());
                if (stat != null)
                {
                    stat.SetValue(stat.CurrentBaseMaximum);
                }
                break;

            case "value":           // Set value
                res = ParseStatValue(command.Substring(e + 1));
                FindStat(command.Substring(i, e - i - 1).Trim()).SetValue(res);
                break;
            }
        }
예제 #2
0
        /// <summary>
        /// Take damage
        /// </summary>
        /// <param name="damageDealer"></param>
        /// <returns></returns>
        private void TakeDamage(DamageDealer damageDealer, GameObject damageSourceObject)
        {
            // check if damage dealer is self
            if (GetComponentsInChildren <DamageDealer>().ToList().Contains(damageDealer))
            {
                return;
            }

            // Get hit direction
            HitDirection hitDirection = GetHitDirection(damageDealer.gameObject.transform.position);

            // Check for immunity
            if (directionImmunity != 0 && (directionImmunity | hitDirection) == hitDirection)
            {
                onImmuneToDamage?.Invoke();
                return;
            }

            if (immuneRemaining > 0)
            {
                return;
            }

            // Add Effects
#if STATS_COG
            if (damageDealer.effects != null)
            {
                foreach (StatEffect effect in damageDealer.effects)
                {
                    if (effectList.availableEffects.Contains(effect))
                    {
                        AddEffect(effect);
                    }
                }
            }
#endif

            float adjustedDamage      = 0;
            float totalAdjustedDamage = 0;

            // Apply weakness
            foreach (Damage damage in damageDealer.damage)
            {
#if STATS_COG
                if (damageDealer.StatsSource != null)
                {
                    adjustedDamage = damageDealer.StatsSource.GetExpressionValue(damage.baseAmount);
                }
                else
                {
                    adjustedDamage = float.Parse(damage.baseAmount);
                }
#endif
                if (damage.damageType != null)
                {
                    List <DamageModifier> modifiers = FindDamageModifiers(damage.damageType.name);
                    foreach (DamageModifier dm in modifiers)
                    {
                        if (dm.modifierType == DamageModType.Resistance)
                        {
                            adjustedDamage -= adjustedDamage * dm.CurrentValue;
                            if (dm.CurrentValue == 1)
                            {
                                onImmuneToDamage?.Invoke();
                            }
                        }
                        else
                        {
                            adjustedDamage *= dm.CurrentValue;
                        }
                    }
                }
                else
                {
                    Debug.LogError("Damage is missing a DamageType");
                }

                totalAdjustedDamage += adjustedDamage;
            }

            // Apply damage
            StatValue hp = FindStat(healthStat);
            if (hp != null)
            {
                adjustedDamage = Mathf.Clamp(GetExpressionValue(ReplaceInsensitive(damageValue, "[damage]", totalAdjustedDamage.ToString())), 0, float.MaxValue);
                hp.SetValue(hp.CurrentValue - adjustedDamage);
                lastDmgSource = damageDealer.StatsSource;
                onDamageTaken?.Invoke(adjustedDamage, damageDealer, damageSourceObject);
                onHitDamageDirection?.Invoke(hitDirection);
                immuneRemaining = immunityAfterHit;
                onHitDirection?.Invoke(hitDirection);
                if (hp.CurrentValue <= 0)
                {
                    onDeath?.Invoke();
                }
            }
            else
            {
                Debug.LogWarning("Could not find '" + healthStat + "' to apply damage");
            }
        }