Fix slot items to fully work with the stashing prefab flow
This commit is contained in:
committed by
Michal Pikulski
parent
a8d398e097
commit
5804b170d0
@@ -56,7 +56,7 @@ public class GameSettings : ScriptableObject
|
||||
public class CombinationRule {
|
||||
public PickupItemData itemA;
|
||||
public PickupItemData itemB;
|
||||
public PickupItemData result;
|
||||
public GameObject resultPrefab; // The prefab to spawn as the result
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
|
||||
@@ -28,19 +28,35 @@ public class CombineWithBehavior : InteractionRequirementBase
|
||||
return false;
|
||||
}
|
||||
var rule = GameManager.Instance.GetCombinationRule(heldItem, pickup.itemData);
|
||||
if (rule != null && rule.result != null)
|
||||
if (rule != null && rule.resultPrefab != null)
|
||||
{
|
||||
// Remove both items and add result to follower's inventory
|
||||
follower.SetHeldItem(rule.result);
|
||||
follower.justCombined = true;
|
||||
OnSuccess?.Invoke();
|
||||
return true;
|
||||
// Instantiate the result prefab at the pickup's position
|
||||
var resultObj = GameObject.Instantiate(rule.resultPrefab, pickup.transform.position, Quaternion.identity);
|
||||
var resultPickup = resultObj.GetComponent<Pickup>();
|
||||
if (resultPickup != null)
|
||||
{
|
||||
// Hide and parent to follower
|
||||
resultObj.SetActive(false);
|
||||
resultObj.transform.SetParent(follower.transform);
|
||||
// Set held item and icon from the spawned prefab
|
||||
follower.SetHeldItem(resultPickup.itemData, resultPickup.iconRenderer);
|
||||
// Cache the spawned object as the held item
|
||||
follower.CacheHeldPickupObject(resultObj);
|
||||
follower.justCombined = true;
|
||||
OnSuccess?.Invoke();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("Result prefab does not have a Pickup component.");
|
||||
GameObject.Destroy(resultObj);
|
||||
OnFailure?.Invoke();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string heldName = heldItem.itemName ?? "an item";
|
||||
string targetName = pickup.itemData.itemName ?? "target item";
|
||||
// DebugUIMessage.Show($"Cannot combine {heldName} with {targetName}.");
|
||||
// DebugUIMessage.Show($"Cannot combine {heldItem.itemName ?? \"an item\"} with {pickup.itemData.itemName ?? \"target item\"}.");
|
||||
OnFailure?.Invoke();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,45 @@ public class SlotItemBehavior : InteractionRequirementBase
|
||||
/// </summary>
|
||||
public SpriteRenderer slottedItemRenderer;
|
||||
|
||||
private GameObject _cachedSlottedObject = null;
|
||||
|
||||
// Helper for slotting an object, with option to skip destruction (for swap)
|
||||
private void SetSlottedObject(GameObject obj)
|
||||
{
|
||||
_cachedSlottedObject = obj;
|
||||
if (_cachedSlottedObject != null)
|
||||
{
|
||||
_cachedSlottedObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveSlottedObject()
|
||||
{
|
||||
if (_cachedSlottedObject != null)
|
||||
{
|
||||
Destroy(_cachedSlottedObject);
|
||||
_cachedSlottedObject = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void CacheSlottedObject(GameObject obj)
|
||||
{
|
||||
// Only destroy if not swapping
|
||||
RemoveSlottedObject();
|
||||
SetSlottedObject(obj);
|
||||
}
|
||||
|
||||
private void RestoreSlottedObject(Vector3 position)
|
||||
{
|
||||
if (_cachedSlottedObject != null)
|
||||
{
|
||||
_cachedSlottedObject.transform.position = position;
|
||||
_cachedSlottedObject.transform.SetParent(null);
|
||||
_cachedSlottedObject.SetActive(true);
|
||||
_cachedSlottedObject = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to interact with the slot, handling slotting, swapping, or picking up items.
|
||||
/// </summary>
|
||||
@@ -27,6 +66,7 @@ public class SlotItemBehavior : InteractionRequirementBase
|
||||
public override bool TryInteract(FollowerController follower)
|
||||
{
|
||||
var heldItem = follower.CurrentlyHeldItem;
|
||||
var heldObj = follower.GetHeldPickupObject();
|
||||
var pickup = GetComponent<Pickup>();
|
||||
var slotItem = pickup != null ? pickup.itemData : null;
|
||||
var config = GameManager.Instance.GetSlotItemConfig(slotItem);
|
||||
@@ -34,33 +74,44 @@ public class SlotItemBehavior : InteractionRequirementBase
|
||||
var forbidden = config?.forbiddenItems ?? new List<PickupItemData>();
|
||||
|
||||
// CASE 1: No held item, slot has item -> pick up slotted item
|
||||
if (heldItem == null && currentlySlottedItem != null)
|
||||
if (heldItem == null && _cachedSlottedObject != null)
|
||||
{
|
||||
follower.SetHeldItem(currentlySlottedItem);
|
||||
follower.SetHeldItemFromObject(_cachedSlottedObject);
|
||||
RemoveSlottedObject();
|
||||
currentlySlottedItem = null;
|
||||
UpdateSlottedSprite();
|
||||
return true;
|
||||
}
|
||||
// CASE 2: Held item, slot has item -> swap
|
||||
if (heldItem != null && currentlySlottedItem != null)
|
||||
if (heldItem != null && _cachedSlottedObject != null)
|
||||
{
|
||||
var temp = currentlySlottedItem;
|
||||
currentlySlottedItem = heldItem;
|
||||
var followerHeldObj = heldObj;
|
||||
var followerHeldItem = heldItem;
|
||||
var slotObj = _cachedSlottedObject;
|
||||
var slotItemData = currentlySlottedItem;
|
||||
|
||||
// 1. Slot the follower's held object (do NOT destroy the old one)
|
||||
SetSlottedObject(followerHeldObj);
|
||||
currentlySlottedItem = followerHeldItem;
|
||||
UpdateSlottedSprite();
|
||||
follower.SetHeldItem(temp);
|
||||
|
||||
// 2. Give the slot's object to the follower
|
||||
follower.SetHeldItemFromObject(slotObj);
|
||||
|
||||
return true;
|
||||
}
|
||||
// CASE 3: Held item, slot empty -> slot the held item
|
||||
if (heldItem != null && currentlySlottedItem == null)
|
||||
if (heldItem != null && _cachedSlottedObject == null)
|
||||
{
|
||||
if (forbidden.Contains(heldItem))
|
||||
{
|
||||
DebugUIMessage.Show("Can't place that here.");
|
||||
return false;
|
||||
}
|
||||
CacheSlottedObject(heldObj);
|
||||
currentlySlottedItem = heldItem;
|
||||
UpdateSlottedSprite();
|
||||
follower.SetHeldItem(null);
|
||||
follower.ClearHeldItem();
|
||||
if (allowed.Contains(heldItem))
|
||||
{
|
||||
OnSuccess?.Invoke();
|
||||
@@ -74,7 +125,7 @@ public class SlotItemBehavior : InteractionRequirementBase
|
||||
}
|
||||
}
|
||||
// CASE 4: No held item, slot empty -> show warning
|
||||
if (heldItem == null && currentlySlottedItem == null)
|
||||
if (heldItem == null && _cachedSlottedObject == null)
|
||||
{
|
||||
DebugUIMessage.Show("This requires an item.");
|
||||
return false;
|
||||
@@ -110,4 +161,9 @@ public class SlotItemBehavior : InteractionRequirementBase
|
||||
slottedItemRenderer.sprite = null;
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
Debug.Log($"[SlotItemBehavior] _cachedSlottedObject: {_cachedSlottedObject} (GameObject name: {(_cachedSlottedObject != null ? _cachedSlottedObject.name : "null")})", this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user