/// <summary> /// Sets the given propery's value. /// </summary> /// <param name="propertyId"></param> /// <param name="value"></param> public void Set(int propertyId, float value) { if (GetPropertyType(propertyId) != PropertyType.Float) { throw new ArgumentException($"Property '{propertyId}' is not supposed to be a float, or is missing from the property type switch."); } lock (_properties) { if (!_properties.TryGetValue(propertyId, out var property)) { _properties[propertyId] = new FloatProperty(propertyId, value); } else { (property as FloatProperty).Value = value; } } }
/// <summary> /// Sets up automatic update for a property when any of its /// "sub-properties" change. /// </summary> /// <remarks> /// Use for calculated properties, whichs' values depend on other /// properties. For example, STR should be calculated based on /// other properties and factors and should be updated automatically /// if any property it depends on changes, like STR_ADD. /// </remarks> /// <param name="propertyId"></param> /// <param name="subPropertyIds"></param> public void AutoUpdate(int propertyId, int[] subPropertyIds) { if (subPropertyIds == null || subPropertyIds.Length == 0) { throw new ArgumentException($"No sub-property ids defined."); } if (!this.TryGet(propertyId, out var property)) { throw new ArgumentException($"Property '{propertyId}' not found."); } if (!(property is CalculatedFloatProperty floatProperty)) { throw new ArgumentException($"Property '{propertyId}' is not a calculated float property."); } foreach (var subPropertyId in subPropertyIds) { // Add sub-property if it doesn't exist yet? There are quite // a few buff and bonus properties, and this way we don't // need to explicitly define all of them. if (!this.TryGet(subPropertyId, out var subProperty)) { //throw new ArgumentException($"Sub-property '{subPropertyId}' not found."); this.Add(subProperty = new FloatProperty(subPropertyId)); } // Subscribe to sub-property's ValueChanged event, so we // automatically trigger a recalculation of the "parent" // property. For Example, STR might be updated automatically // when STR_Bonus changes. // Before subscribing, unsubscribe, just in case, so we // don't get duplicate subscriptions. subProperty.ValueChanged -= floatProperty.TriggerCalculation; subProperty.ValueChanged += floatProperty.TriggerCalculation; } }