Skip to main content
The project's repository is archived as part of the GitHub Archive Program. RogueLibs' code and the documentation will no longer be updated.

Targetable Items +

Custom items can be made targetable+ (targetable anywhere) by implementing the IItemTargetableAnywhere. Normal targetable items can only be used on something actually present in the game, but as that parenthesised text implies, targetable+ items can be used anywhere on the screen. And so, this interface uses in-game positions instead of objects.

Making items targetable anywhere

Just implement the IItemTargetableAnywhere interface in your item's class:

MyTargetableAnywhereItem.cs
public class MyTargetableAnywhereItem : CustomItem, IItemTargetableAnywhere
{
public bool TargetFilter(Vector2 position) { /* ... */ }
public bool TargetPosition(Vector2 position) { /* ... */ }
public CustomTooltip TargetCursorText(Vector2 position) { /* ... */ }
}

TargetFilter determines where the cursor should be highlighted, when using the current item.

TargetPosition uses the current item on the position. The return value indicates whether it was a success or not. You can play a "CantDo" sound and make the player say something, if the item cannot be used. Returning true will also play an animation.

TargetCursorText determines the text under the cursor when hovering over the specified position. Text set to null will default to "Use", and Color set to null will default to

#FFFFFF
.

Examples

using UnityEngine;

namespace RogueLibsCore.Test
{
[ItemCategories(RogueCategories.Usable, RogueCategories.Technology, RogueCategories.Stealth)]
public class UsableTeleporter : CustomItem, IItemTargetableAnywhere
{
[RLSetup]
public static void Setup()
{
RogueLibs.CreateCustomItem<UsableTeleporter>()
.WithName(new CustomNameInfo("Usable Teleporter"))
.WithDescription(new CustomNameInfo("Teleports you somewhere. Has limited uses."))
.WithSprite(Properties.Resources.UsableTeleporter)
.WithUnlock(new ItemUnlock
{
UnlockCost = 10,
LoadoutCost = 9,
CharacterCreationCost = 5,
Prerequisites = { VanillaItems.QuickEscapeTeleporter, nameof(WildBypasser) },
});

TeleportCursorText = RogueLibs.CreateCustomName("TeleportHere", NameTypes.Interface, new CustomNameInfo("Teleport here"));
}
private static CustomName TeleportCursorText = null!;

public override void SetupDetails()
{
Item.itemType = ItemTypes.Tool;
Item.itemValue = 80;
Item.initCount = 2;
Item.rewardCount = 3;
Item.stackable = true;
Item.goesInToolbar = true;
}
public bool TargetFilter(Vector2 position)
{
TileData tileData = gc.tileInfo.GetTileData(position);
return !gc.tileInfo.IsOverlapping(position, "Anything") && tileData.wallMaterial == wallMaterialType.None;
}
public bool TargetPosition(Vector2 position)
{
if (!TargetFilter(position)) return false;

Owner!.SpawnParticleEffect("Spawn", Owner.tr.position);
Owner.Teleport(position, false, true);
Owner.rb.velocity = Vector2.zero;
Owner.SpawnParticleEffect("Spawn", Owner.tr.position, false);
gc.audioHandler.Play(Owner, VanillaAudio.Spawn);

Count--;
return true;
}
public CustomTooltip TargetCursorText(Vector2 position) => TeleportCursorText;
}
}

The project's repository is archived as part of the GitHub Archive Program. RogueLibs' code and the documentation will no longer be updated.