public static void Apply(CapacityInfoModifier modifier, ref CapacityInfo capacityInfo)
 {
     if (modifier.Multiplicative)
     {
         capacityInfo *= modifier.CapacityInfo;
     }
     else
     {
         capacityInfo += modifier.CapacityInfo;
     }
 }
Beispiel #2
0
        public bool AddCapacitiesModifier(CapacityInfoModifier modifier, bool addOnlyIfUnique = false)
        {
            bool needAdd = true;

            if (addOnlyIfUnique)
            {
                // TODO: test if modifier is already present
                // needAdd = false
            }

            if (needAdd)
            {
                this.CapacitiesModifiers.Add(modifier);
            }

            return(true);
        }
Beispiel #3
0
        protected override void Update(float time, float deltaTime)
        {
            var capacitiesComponents = EntityManager.GetComponents <CapacitiesComponent>();

            foreach (var capacitiesComponent in capacitiesComponents)
            {
                // first: check existing modifiers, remove if duration has expired
                int removedCount = 0;
                int count        = capacitiesComponent.CapacitiesModifiers.Count;
                for (int i = 0; i < count; i++)
                {
                    // check expiration time and remove if needed
                    var modifier = capacitiesComponent.CapacitiesModifiers[i - removedCount];

                    float timeSinceSpawn = time - modifier.timed.TimeAtSpawn;
                    if (modifier.timed.Duration <= timeSinceSpawn)
                    {
                        capacitiesComponent.CapacitiesModifiers.RemoveAt(i);
                        removedCount++;
                    }
                }

                // if modifiers were changed, mark component to be updated
                capacitiesComponent.CapacitiesDirty |= 0 < removedCount;

                if (capacitiesComponent.CapacitiesDirty)
                {
                    // get the base value of capacities
                    var capacities = capacitiesComponent.CapacityInfoBase.Clone();

                    // apply modifiers
                    foreach (var modifier in capacitiesComponent.CapacitiesModifiers)
                    {
                        CapacityInfoModifier.Apply(modifier, ref capacities);
                    }

                    // apply modifiers given bodyPart's damage state
                    foreach (var bodyPart in capacitiesComponent.DamageableComponent.Body.BodyParts)
                    {
                        // get bodyPart's capacities influence
                        var capacitiesBaseBp = bodyPart.CapacityInfo;

                        // convert bodyPart's damage state to impact multiplier
                        var   damageState    = bodyPart.GetDamageState();
                        float capacityImpact = DamageStateToModifierAmount(damageState);

                        // apply modifier
                        capacitiesBaseBp *= capacityImpact;

                        // subtract from base component capacities
                        capacities -= capacitiesBaseBp;
                    }

                    // write back
                    capacitiesComponent.capacityInfoCurrent = capacities;

                    capacitiesComponent.CapacitiesDirty = false;

                    capacitiesComponent.NotifyListeners();
                }
            }
        }