using System; using Cthangover.Core.Items; using Cthangover.Core.Settings; namespace Cthangover.Mods.Items { /// /// Restores health of the chosen party member ("ItemAction": "action/heal"). /// The amount is item data, not code: the item's "heal" key (an extra JSON /// key, so it lands in ), which lets one action serve /// a mushroom and an elixir alike. Health never rises above its base value, and a /// fallen character (health 0) is not revived — that is a different effect. /// The party member's attributes are the very objects the battle uses, so a heal /// in the inventory during a battle shows on the battle widgets at once. /// public class HealItemAction : ITargetedItemAction { /// Item property with the amount of health restored. public const string HealProperty = "heal"; /// Action id referenced by item definitions. public string ID => "action/heal"; /// Healing always needs a target; without one nothing happens. public bool UseAction(IItem item) => false; /// /// Adds the item's heal amount to the target's health, capped at the /// base value. Returns false (item not spent) for a missing target, a /// non-positive amount, a fallen target or one already at full health. /// public bool UseOn(IItem item, CharacterInfoData target) { var health = target?.Attributes?.Health; var amount = item?.Properties?.GetInt(HealProperty) ?? 0; if (health == null || amount <= 0 || health.Value <= 0 || health.Value >= health.BaseValue) return false; health.Value = Math.Min(health.BaseValue, health.Value + amount); return true; } } }