Recharging Items
RogueLibs doesn't provide any explicit functionality for rechargeable items, but you can easily implement that yourself, using the IDoUpdate
interface. You'll find some useful code snippets below, that you can reuse for your own items.
Making items rechargeable
Make your custom item's class implement the IDoUpdate
interface:
public class MyRechargeableItem : CustomItem, IDoUpdate
{
/* ... */
}
Presets
- Cooldown
- Charge
Cooldown
represents the amount of seconds to wait until full recharge.
public float Cooldown { get; private set; }
public void Update() => Cooldown = Mathf.Max(Cooldown - Time.deltaTime, 0f);
With adjustable recharging speed:
public float RechargeSpeed = 1f;
public float Cooldown { get; private set; }
public void Update() => Cooldown = Mathf.Max(Cooldown - Time.deltaTime * RechargeSpeed, 0f);
Usage:
public bool UseItem()
{
if (Cooldown != 0f) return false;
/* ... */
Cooldown = 1.5f;
return true;
}
You can use other activation methods too, like CombineItems
, TargetObject
, TargetPosition
and etc.
If you want to display Cooldown
as the item's count, then override the GetCountString
method:
public override CustomTooltip GetCountString()
{
if (Cooldown != 0f) return new CustomTooltip(Cooldown, Color.red);
return base.GetCountString(); // display default count
}
Charge
represents the amount of seconds of "stored up energy".
public float Charge { get; private set; } = 5f;
public void Update() => Charge = Mathf.Min(Charge + Time.deltaTime, 5f);
With adjustable charging speed and maximum charge:
public float ChargeSpeed = 1f, MaxCharge = 5f;
public float Charge { get; private set; } = 5f;
public void Update() => Charge = Mathf.Min(Charge + Time.deltaTime * ChargeSpeed, MaxCharge);
Plus, with a charging delay:
public float DelayThreshold = 3f, ChargeSpeed = 1f, MaxCharge = 5f;
private float lastUsage;
public float Charge { get; private set; } = 5f;
public void Update()
{
if (lastUsage + DelayThreshold < Time.time)
Charge = Mathf.Min(Charge + Time.deltaTime * ChargeSpeed, MaxCharge);
}
Usage:
public bool UseItem()
{
if (Charge < 1.5f) return false;
/* ... */
Charge -= 1.5f;
lastUsage = Time.time;
return true;
}
You can use other activation methods too, like CombineItems
, TargetObject
, TargetPosition
and etc.
If you want to display Charge
as the item's count, then override the GetCountString
method:
public override CustomTooltip GetCountString()
{
string text = $"{Charge} ({base.GetCountString()})"; // display both charge and count
Color color = Color.Lerp(Color.red, Color.white, Charge / MaxCharge);
// color between red and white: red - no charge, white - full charge
return new CustomTooltip(text, color);
}
There's also a vanilla way of recharging items, but it's really messy and unreliable.
Examples
- Quantum Fud
using UnityEngine;
namespace RogueLibsCore.Test
{
[ItemCategories(RogueCategories.Food, RogueCategories.Technology)]
public class QuantumFud : CustomItem, IItemUsable, IDoUpdate
{
[RLSetup]
public static void Setup()
{
RogueLibs.CreateCustomItem<QuantumFud>()
.WithName(new CustomNameInfo("Quantum Fud"))
.WithDescription(new CustomNameInfo("A very complicated piece of quantum technology. When you eat it, its quantum equivalent clone is consumed, while the original thing remains intact."))
.WithSprite(Properties.Resources.QuantumFud)
.WithUnlock(new ItemUnlock
{
UnlockCost = 10,
LoadoutCost = 15,
CharacterCreationCost = 10,
Prerequisites = { VanillaItems.FoodProcessor },
});
}
public override void SetupDetails()
{
Item.itemType = ItemTypes.Food;
Item.itemValue = 180;
Item.healthChange = 1;
Item.cantBeCloned = true;
Item.goesInToolbar = true;
}
public float Cooldown { get; set; }
public void Update() => Cooldown = Mathf.Max(Cooldown - Time.deltaTime, 0f);
public bool UseItem()
{
if (Cooldown != 0f) return false;
int heal = new ItemFunctions().DetermineHealthChange(Item, Owner);
Owner!.statusEffects.ChangeHealth(heal);
if (Owner.HasTrait(VanillaTraits.ShareTheHealth)
|| Owner.HasTrait(VanillaTraits.ShareTheHealth2))
new ItemFunctions().GiveFollowersHealth(Owner, heal);
gc.audioHandler.Play(Owner, VanillaAudio.UseFood);
Cooldown = 0.5f;
return true;
}
}
}