Interactable items - slotting working correctly and calling the dispatchers each time

This commit is contained in:
Michal Pikulski
2025-09-12 12:26:44 +02:00
parent ef96d80d51
commit 445e36975d
5 changed files with 67 additions and 107 deletions

View File

@@ -50,7 +50,7 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 7447346505753002421, guid: bf4b9d7045397f946b2125b1ad4a3fbd, type: 3} - target: {fileID: 7447346505753002421, guid: bf4b9d7045397f946b2125b1ad4a3fbd, type: 3}
propertyPath: m_Name propertyPath: m_Name
value: BasePuzzlePickup value: BurgerBuns
objectReference: {fileID: 0} objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_RemovedGameObjects: [] m_RemovedGameObjects: []

View File

@@ -14,7 +14,8 @@ public class DebugUIMessage : MonoBehaviour
/// </summary> /// </summary>
/// <param name="message">The message to display.</param> /// <param name="message">The message to display.</param>
/// <param name="duration">How long to display the message (seconds).</param> /// <param name="duration">How long to display the message (seconds).</param>
public static void Show(string message, float duration = 2f) /// <param name="displayColor"></param>
public static void Show(string message, Color displayColor, float duration = 2f)
{ {
if (instance == null) if (instance == null)
{ {
@@ -23,7 +24,7 @@ public class DebugUIMessage : MonoBehaviour
instance.SetupUI(); instance.SetupUI();
DontDestroyOnLoad(go); DontDestroyOnLoad(go);
} }
instance.ShowMessage(message, duration); instance.ShowMessage(message, duration, displayColor);
} }
/// <summary> /// <summary>
@@ -45,7 +46,7 @@ public class DebugUIMessage : MonoBehaviour
messageText.font = customFont; messageText.font = customFont;
else else
messageText.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf"); messageText.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
messageText.fontSize = 16; messageText.fontSize = 32;
messageText.color = Color.yellow; messageText.color = Color.yellow;
var outline = textGO.AddComponent<Outline>(); var outline = textGO.AddComponent<Outline>();
outline.effectColor = Color.black; outline.effectColor = Color.black;
@@ -55,16 +56,17 @@ public class DebugUIMessage : MonoBehaviour
rect.anchorMax = new Vector2(0.5f, 0.5f); rect.anchorMax = new Vector2(0.5f, 0.5f);
rect.pivot = new Vector2(0.5f, 0.5f); rect.pivot = new Vector2(0.5f, 0.5f);
rect.anchoredPosition = Vector2.zero; rect.anchoredPosition = Vector2.zero;
rect.sizeDelta = new Vector2(400, 40); rect.sizeDelta = new Vector2(800, 40);
messageText.text = ""; messageText.text = "";
} }
/// <summary> /// <summary>
/// Internal method to show a message and start the hide coroutine. /// Internal method to show a message and start the hide coroutine.
/// </summary> /// </summary>
private void ShowMessage(string message, float duration) private void ShowMessage(string message, float duration, Color fontColor)
{ {
messageText.text = message; messageText.text = message;
messageText.color = fontColor;
messageText.enabled = true; messageText.enabled = true;
if (hideCoroutine != null) if (hideCoroutine != null)
StopCoroutine(hideCoroutine); StopCoroutine(hideCoroutine);

View File

@@ -130,7 +130,7 @@ namespace Interactions
var step = GetComponent<PuzzleS.ObjectiveStepBehaviour>(); var step = GetComponent<PuzzleS.ObjectiveStepBehaviour>();
if (step != null && !step.IsStepUnlocked()) if (step != null && !step.IsStepUnlocked())
{ {
DebugUIMessage.Show("This step is locked!", 2f); DebugUIMessage.Show("This step is locked!", Color.yellow);
BroadcastInteractionComplete(false); BroadcastInteractionComplete(false);
// Reset variables for next time // Reset variables for next time
_interactionInProgress = false; _interactionInProgress = false;

View File

@@ -19,7 +19,6 @@ namespace Interactions
public SpriteRenderer slottedItemRenderer; public SpriteRenderer slottedItemRenderer;
private GameObject _currentlySlottedItemObject = null; private GameObject _currentlySlottedItemObject = null;
public GameObject GetSlottedObject() public GameObject GetSlottedObject()
{ {
return _currentlySlottedItemObject; return _currentlySlottedItemObject;
@@ -38,107 +37,41 @@ namespace Interactions
{ {
var heldItemData = FollowerController.CurrentlyHeldItemData; var heldItemData = FollowerController.CurrentlyHeldItemData;
var heldItemObj = FollowerController.GetHeldPickupObject(); var heldItemObj = FollowerController.GetHeldPickupObject();
var pickup = GetComponent<Pickup>(); var config = GameManager.Instance.GetSlotItemConfig(itemData);
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>(); var forbidden = config?.forbiddenItems ?? new List<PickupItemData>();
if (heldItemData != null && _currentlySlottedItemObject != null) // Held item, slot empty -> try to slot item
{
// Remove the currently slotted item
FollowerController.TryPickupItem(_currentlySlottedItemObject, _currentlySlottedItemData);
_currentlySlottedItemObject = null;
_currentlySlottedItemData = null;
onItemSlotRemoved?.Invoke();
UpdateSlottedSprite();
// Now slot the held item and check correctness
if (forbidden.Contains(heldItemData))
{
DebugUIMessage.Show("Can't place that here.");
onForbiddenItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(false);
return;
}
SlotItem(heldItemObj, heldItemData);
if (allowed.Contains(heldItemData))
{
onCorrectItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(true);
return;
}
else
{
DebugUIMessage.Show("I'm not sure this works.");
onIncorrectItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(false);
return;
}
}
else if (heldItemData == null && _currentlySlottedItemObject != null)
{
FollowerController.TryPickupItem(_currentlySlottedItemObject, _currentlySlottedItemData);
_currentlySlottedItemObject = null;
_currentlySlottedItemData = null;
onItemSlotRemoved?.Invoke();
UpdateSlottedSprite();
return;
}
// // CASE 1: No held item, slot has item -> pick up slotted item
// if (heldItemData == null && _cachedSlottedObject != null)
// {
// InteractionOrchestrator.Instance.PickupItem(FollowerController, _cachedSlottedObject);
// _cachedSlottedObject = null;
// currentlySlottedItem = null;
// UpdateSlottedSprite();
// Interactable.BroadcastInteractionComplete(false);
// return;
// }
// // CASE 2: Held item, slot has item -> swap
// if
// {
// InteractionOrchestrator.Instance.SwapItems(FollowerController, this);
// currentlySlottedItem = heldItemData;
// UpdateSlottedSprite();
// return;
// }
// CASE 3: Held item, slot empty -> slot the held item
if (heldItemData != null && _currentlySlottedItemObject == null) if (heldItemData != null && _currentlySlottedItemObject == null)
{ {
// First check for forbidden items at the very start so we don't continue unnecessarily
if (forbidden.Contains(heldItemData)) if (forbidden.Contains(heldItemData))
{ {
DebugUIMessage.Show("Can't place that here."); DebugUIMessage.Show("Can't place that here.", Color.red);
onForbiddenItemSlotted?.Invoke(); onForbiddenItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(false); Interactable.BroadcastInteractionComplete(false);
return; return;
} }
SlotItem(heldItemObj, heldItemData); SlotItem(heldItemObj, heldItemData, true);
if (allowed.Contains(heldItemData)) return;
{ }
onCorrectItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(true); // Either pickup or swap items
return; if ((heldItemData == null && _currentlySlottedItemObject != null)
} || (heldItemData != null && _currentlySlottedItemObject != null))
else {
{ FollowerController.TryPickupItem(_currentlySlottedItemObject, _currentlySlottedItemData, false);
DebugUIMessage.Show("I'm not sure this works."); onItemSlotRemoved?.Invoke();
onIncorrectItemSlotted?.Invoke(); SlotItem(heldItemObj, heldItemData, _currentlySlottedItemObject == null);
Interactable.BroadcastInteractionComplete(false); return;
return; }
}
} // No held item, slot empty -> show warning
if (heldItemData == null && _currentlySlottedItemObject == null)
// CASE 4: No held item, slot empty -> show warning {
if (heldItemData == null && _currentlySlottedItemObject == null) DebugUIMessage.Show("This requires an item.", Color.red);
{
DebugUIMessage.Show("This requires an item.");
return; return;
} }
return;
} }
/// <summary> /// <summary>
@@ -170,18 +103,43 @@ namespace Interactions
} }
} }
public void SlotItem(GameObject itemToSlot, PickupItemData itemToSlotData) public void SlotItem(GameObject itemToSlot, PickupItemData itemToSlotData, bool clearFollowerHeldItem = true)
{ {
if (itemToSlot == null) if (itemToSlot == null)
return; {
_currentlySlottedItemData = null;
_currentlySlottedItemData = null;
}
else
{
itemToSlot.SetActive(false);
itemToSlot.transform.SetParent(null);
SetSlottedObject(itemToSlot);
itemToSlot.SetActive(false); _currentlySlottedItemData = itemToSlotData;
itemToSlot.transform.SetParent(null); if (clearFollowerHeldItem)
SetSlottedObject(itemToSlot); {
FollowerController.ClearHeldItem();
_currentlySlottedItemData = itemToSlotData; }
}
UpdateSlottedSprite(); UpdateSlottedSprite();
FollowerController.ClearHeldItem();
// Once an item is slotted, we know it is not forbidden, so we can skip that check, but now check if it was
// the correct item we're looking for
var config = GameManager.Instance.GetSlotItemConfig(itemData);
var allowed = config?.allowedItems ?? new List<PickupItemData>();
if (allowed.Contains(itemToSlotData))
{
DebugUIMessage.Show("You correctly slotted " + itemToSlotData.itemName + " into: " + itemData.itemName, Color.green);
onCorrectItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(true);
}
else
{
DebugUIMessage.Show("I'm not sure this works.", Color.yellow);
onIncorrectItemSlotted?.Invoke();
Interactable.BroadcastInteractionComplete(false);
}
} }
} }
} }

View File

@@ -311,9 +311,9 @@ public class FollowerController: MonoBehaviour
#endregion Movement #endregion Movement
#region ItemInteractions #region ItemInteractions
public void TryPickupItem(GameObject itemObject, PickupItemData itemData) public void TryPickupItem(GameObject itemObject, PickupItemData itemData, bool dropItem = true)
{ {
if (_currentlyHeldItemData != null && _cachedPickupObject != null) if (_currentlyHeldItemData != null && _cachedPickupObject != null && dropItem)
{ {
// Drop the currently held item at the current position // Drop the currently held item at the current position
DropHeldItemAt(transform.position); DropHeldItemAt(transform.position);