Fix slot items to fully work with the stashing prefab flow

This commit is contained in:
Michal Pikulski
2025-09-08 16:13:13 +02:00
committed by Michal Pikulski
parent a8d398e097
commit 5804b170d0
7 changed files with 278 additions and 35 deletions

View File

@@ -60,11 +60,20 @@ public class FollowerController : MonoBehaviour
/// Cache for the currently picked-up GameObject (hidden while held).
/// </summary>
private GameObject _cachedPickupObject = null;
public bool justCombined = false;
/// <summary>
/// Set to true if the follower just combined items.
/// Caches the given pickup object as the currently held item, hides it, and parents it to the follower.
/// </summary>
public bool justCombined = false;
public void CacheHeldPickupObject(GameObject obj)
{
// Do not destroy the previous object; just replace and hide
_cachedPickupObject = obj;
if (_cachedPickupObject != null)
{
_cachedPickupObject.SetActive(false);
}
}
void Awake()
{
@@ -281,6 +290,45 @@ public class FollowerController : MonoBehaviour
_lastInteractionSuccess = success;
}
public GameObject GetHeldPickupObject()
{
return _cachedPickupObject;
}
public void SetHeldItemFromObject(GameObject obj)
{
if (obj == null)
{
ClearHeldItem();
return;
}
var pickup = obj.GetComponent<Pickup>();
if (pickup != null)
{
SetHeldItem(pickup.itemData, pickup.iconRenderer);
CacheHeldPickupObject(obj);
}
else
{
ClearHeldItem();
}
}
public void ClearHeldItem()
{
if (_cachedPickupObject != null)
{
// Destroy(_cachedPickupObject);
_cachedPickupObject = null;
}
_currentlyHeldItem = null;
if (heldObjectRenderer != null)
{
heldObjectRenderer.sprite = null;
heldObjectRenderer.enabled = false;
}
}
private System.Collections.IEnumerator PickupSequence(Vector2 itemPosition, Transform playerTransform)
{
_isManualFollowing = false;
@@ -314,12 +362,6 @@ public class FollowerController : MonoBehaviour
}
if (justCombined)
{
// Combination: just destroy the pickup, don't spawn anything
if (_cachedPickupObject != null)
{
Destroy(_cachedPickupObject);
_cachedPickupObject = null;
}
GameObject.Destroy(pickup.gameObject);
justCombined = false;
break;
@@ -334,10 +376,7 @@ public class FollowerController : MonoBehaviour
_cachedPickupObject = null;
}
SetHeldItem(pickup.itemData, pickup.iconRenderer);
// Cache and hide the picked up object
_cachedPickupObject = pickup.gameObject;
_cachedPickupObject.SetActive(false);
_cachedPickupObject.transform.SetParent(this.transform);
CacheHeldPickupObject(pickup.gameObject);
break;
}
}
@@ -367,6 +406,27 @@ public class FollowerController : MonoBehaviour
_pickupCoroutine = null;
}
/// <summary>
/// Drop the held item at the specified position, unparenting and activating it.
/// </summary>
/// <param name="position">The world position to drop the item at.</param>
public void DropHeldItemAt(Vector3 position)
{
if (_cachedPickupObject != null)
{
_cachedPickupObject.transform.position = position;
_cachedPickupObject.transform.SetParent(null);
_cachedPickupObject.SetActive(true);
_cachedPickupObject = null;
_currentlyHeldItem = null;
if (heldObjectRenderer != null)
{
heldObjectRenderer.sprite = null;
heldObjectRenderer.enabled = false;
}
}
}
void OnDrawGizmos()
{
if (debugDrawTarget && Application.isPlaying)