Big script cleanup. Remove the examples from Ropes' external package

This commit is contained in:
Michal Pikulski
2025-09-06 21:01:54 +02:00
parent 045bd7966e
commit d3c6b838b4
134 changed files with 719 additions and 26298 deletions

View File

@@ -0,0 +1,113 @@
using UnityEngine;
using UnityEngine.Events;
using System.Collections.Generic;
/// <summary>
/// Interaction requirement that allows slotting, swapping, or picking up items in a slot.
/// </summary>
[RequireComponent(typeof(Interactable))]
[RequireComponent(typeof(Pickup))]
public class SlotItemBehavior : InteractionRequirementBase
{
[Header("Slot State")]
/// <summary>
/// The item currently slotted in this slot.
/// </summary>
public PickupItemData currentlySlottedItem;
/// <summary>
/// The renderer for the slotted item's sprite.
/// </summary>
public SpriteRenderer slottedItemRenderer;
/// <summary>
/// Attempts to interact with the slot, handling slotting, swapping, or picking up items.
/// </summary>
/// <param name="follower">The follower attempting the interaction.</param>
/// <returns>True if the interaction was successful, false otherwise.</returns>
public override bool TryInteract(FollowerController follower)
{
var heldItem = follower.currentlyHeldItem;
var pickup = GetComponent<Pickup>();
var slotItem = pickup != null ? pickup.itemData : null;
var config = GameManager.Instance.GetSlotItemConfig(slotItem);
var allowed = config?.allowedItems ?? new List<PickupItemData>();
var forbidden = config?.forbiddenItems ?? new List<PickupItemData>();
// CASE 1: No held item, slot has item -> pick up slotted item
if (heldItem == null && currentlySlottedItem != null)
{
follower.SetHeldItem(currentlySlottedItem);
currentlySlottedItem = null;
UpdateSlottedSprite();
return true;
}
// CASE 2: Held item, slot has item -> swap
if (heldItem != null && currentlySlottedItem != null)
{
var temp = currentlySlottedItem;
currentlySlottedItem = heldItem;
UpdateSlottedSprite();
follower.SetHeldItem(temp);
return true;
}
// CASE 3: Held item, slot empty -> slot the held item
if (heldItem != null && currentlySlottedItem == null)
{
if (forbidden.Contains(heldItem))
{
DebugUIMessage.Show("Can't place that here.");
return false;
}
currentlySlottedItem = heldItem;
UpdateSlottedSprite();
follower.SetHeldItem(null);
if (allowed.Contains(heldItem))
{
OnSuccess?.Invoke();
return true;
}
else
{
DebugUIMessage.Show("I'm not sure this works.");
OnFailure?.Invoke();
return true;
}
}
// CASE 4: No held item, slot empty -> show warning
if (heldItem == null && currentlySlottedItem == null)
{
DebugUIMessage.Show("This requires an item.");
return false;
}
return false;
}
/// <summary>
/// Updates the sprite and scale for the currently slotted item.
/// </summary>
private void UpdateSlottedSprite()
{
if (slottedItemRenderer != null && currentlySlottedItem != null && currentlySlottedItem.mapSprite != null)
{
slottedItemRenderer.sprite = currentlySlottedItem.mapSprite;
// Scale sprite to desired height, preserve aspect ratio, compensate for parent scale
float desiredHeight = GameManager.Instance.HeldIconDisplayHeight;
var sprite = currentlySlottedItem.mapSprite;
float spriteHeight = sprite.bounds.size.y;
float spriteWidth = sprite.bounds.size.x;
Vector3 parentScale = slottedItemRenderer.transform.parent != null
? slottedItemRenderer.transform.parent.localScale
: Vector3.one;
if (spriteHeight > 0f)
{
float uniformScale = desiredHeight / spriteHeight;
float scale = uniformScale / Mathf.Max(parentScale.x, parentScale.y);
slottedItemRenderer.transform.localScale = new Vector3(scale, scale, 1f);
}
}
else if (slottedItemRenderer != null)
{
slottedItemRenderer.sprite = null;
}
}
}