Semi-working Interactables rework
This commit is contained in:
3
Assets/Scripts/Characters.meta
Normal file
3
Assets/Scripts/Characters.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 01ee82c489314d60bf27aa9a405f2633
|
||||
timeCreated: 1757513074
|
||||
11
Assets/Scripts/Characters/Character.cs
Normal file
11
Assets/Scripts/Characters/Character.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for all characters that can interact in the world (e.g., player, follower).
|
||||
/// </summary>
|
||||
public abstract class Character : MonoBehaviour
|
||||
{
|
||||
// Placeholder for shared character logic or properties.
|
||||
// For now, this is intentionally minimal.
|
||||
}
|
||||
|
||||
3
Assets/Scripts/Characters/Character.cs.meta
Normal file
3
Assets/Scripts/Characters/Character.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9253c7c4ca6946b1b31196c4bf8d685e
|
||||
timeCreated: 1757513074
|
||||
@@ -157,11 +157,28 @@ public class InputManager : MonoBehaviour
|
||||
Collider2D hit = Physics2D.OverlapPoint(worldPos, mask);
|
||||
if (hit != null)
|
||||
{
|
||||
var interactable = hit.GetComponent<ITouchInputConsumer>();
|
||||
var interactable = hit.GetComponent<Interactable>();
|
||||
if (interactable != null)
|
||||
{
|
||||
Debug.unityLogger.Log("Interactable", $"[InputManager] Delegating tap to interactable at {worldPos} (GameObject: {hit.gameObject.name})");
|
||||
interactable.OnTap(worldPos);
|
||||
// Find the player Character (by tag)
|
||||
var playerObj = GameObject.FindGameObjectWithTag("Player");
|
||||
var playerCharacter = playerObj != null ? playerObj.GetComponent<Character>() : null;
|
||||
if (playerCharacter != null)
|
||||
{
|
||||
InteractionOrchestrator.Instance.RequestInteraction(interactable, playerCharacter);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("[InputManager] Player Character not found for interaction delegation.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Fallback: support other ITouchInputConsumer implementations
|
||||
var consumer = hit.GetComponent<ITouchInputConsumer>();
|
||||
if (consumer != null)
|
||||
{
|
||||
consumer.OnTap(worldPos);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace Input
|
||||
/// Handles player movement in response to tap and hold input events.
|
||||
/// Supports both direct and pathfinding movement modes, and provides event/callbacks for arrival/cancellation.
|
||||
/// </summary>
|
||||
public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
|
||||
public class PlayerTouchController : Character, ITouchInputConsumer
|
||||
{
|
||||
// --- Movement State ---
|
||||
private Vector3 targetPosition;
|
||||
|
||||
@@ -9,13 +9,6 @@ public class Interactable : MonoBehaviour, ITouchInputConsumer
|
||||
public event Action StartedInteraction;
|
||||
public event Action<bool> InteractionComplete;
|
||||
|
||||
private ObjectiveStepBehaviour stepBehaviour;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
stepBehaviour = GetComponent<ObjectiveStepBehaviour>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles tap input. Triggers interaction logic.
|
||||
/// </summary>
|
||||
@@ -25,48 +18,20 @@ public class Interactable : MonoBehaviour, ITouchInputConsumer
|
||||
StartedInteraction?.Invoke();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// No hold behavior for interactables.
|
||||
/// </summary>
|
||||
// No hold behavior for interactables.
|
||||
public void OnHoldStart(Vector2 worldPosition) { }
|
||||
public void OnHoldMove(Vector2 worldPosition) { }
|
||||
public void OnHoldEnd(Vector2 worldPosition) { }
|
||||
|
||||
/// <summary>
|
||||
/// Called when the follower arrives at this interactable.
|
||||
/// Called to interact with this object by a character (player, follower, etc).
|
||||
/// </summary>
|
||||
public bool OnFollowerArrived(FollowerController follower)
|
||||
public virtual void OnInteract(Character character)
|
||||
{
|
||||
// Check if step is locked here
|
||||
if (stepBehaviour != null && !stepBehaviour.IsStepUnlocked())
|
||||
{
|
||||
DebugUIMessage.Show("Item is not unlocked yet");
|
||||
Debug.Log("[Puzzles] Tried to interact with locked step: " + gameObject.name);
|
||||
InteractionComplete?.Invoke(false);
|
||||
return false;
|
||||
}
|
||||
var requirements = GetComponents<InteractionRequirementBase>();
|
||||
if (requirements.Length == 0)
|
||||
{
|
||||
InteractionComplete?.Invoke(true);
|
||||
return true;
|
||||
}
|
||||
bool anySuccess = false;
|
||||
foreach (var req in requirements)
|
||||
{
|
||||
if (req.TryInteract(follower))
|
||||
{
|
||||
anySuccess = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
InteractionComplete?.Invoke(anySuccess);
|
||||
if (!anySuccess)
|
||||
{
|
||||
Debug.Log($"[Interactable] No interaction requirements succeeded for {gameObject.name}");
|
||||
// Optionally trigger a default failure event or feedback here
|
||||
}
|
||||
return anySuccess;
|
||||
// In the new architecture, requirements and step checks will be handled by orchestrator.
|
||||
StartedInteraction?.Invoke();
|
||||
// For now, immediately complete interaction as success (can be extended later).
|
||||
InteractionComplete?.Invoke(true);
|
||||
}
|
||||
|
||||
public void CompleteInteraction(bool success)
|
||||
|
||||
154
Assets/Scripts/Interactions/InteractionOrchestrator.cs
Normal file
154
Assets/Scripts/Interactions/InteractionOrchestrator.cs
Normal file
@@ -0,0 +1,154 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Handles the process of moving characters to interactables and orchestrating interactions.
|
||||
/// </summary>
|
||||
public class InteractionOrchestrator : MonoBehaviour
|
||||
{
|
||||
private static InteractionOrchestrator _instance;
|
||||
private static bool _isQuitting = false;
|
||||
|
||||
// Singleton for easy access (optional, can be replaced with DI or scene reference)
|
||||
public static InteractionOrchestrator Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null && Application.isPlaying && !_isQuitting)
|
||||
{
|
||||
_instance = FindAnyObjectByType<InteractionOrchestrator>();
|
||||
if (_instance == null)
|
||||
{
|
||||
var go = new GameObject("InteractionOrchestrator");
|
||||
_instance = go.AddComponent<InteractionOrchestrator>();
|
||||
// DontDestroyOnLoad(go);
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_instance = this;
|
||||
// DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
_isQuitting = true;
|
||||
}
|
||||
|
||||
// Store pending interaction state
|
||||
private Interactable _pendingInteractable;
|
||||
private Input.PlayerTouchController _pendingPlayer;
|
||||
private FollowerController _pendingFollower;
|
||||
private bool _interactionInProgress;
|
||||
|
||||
/// <summary>
|
||||
/// Request an interaction between a character and an interactable.
|
||||
/// </summary>
|
||||
public void RequestInteraction(Interactable interactable, Character character)
|
||||
{
|
||||
// Only support player-initiated interactions for now
|
||||
if (character is Input.PlayerTouchController player)
|
||||
{
|
||||
// Compute closest point on the interaction radius
|
||||
Vector3 interactablePos = interactable.transform.position;
|
||||
Vector3 playerPos = player.transform.position;
|
||||
float stopDistance = GameManager.Instance.PlayerStopDistance;
|
||||
Vector3 toPlayer = (playerPos - interactablePos).normalized;
|
||||
Vector3 stopPoint = interactablePos + toPlayer * stopDistance;
|
||||
|
||||
// Unsubscribe previous to avoid duplicate calls
|
||||
player.OnArrivedAtTarget -= OnPlayerArrived;
|
||||
player.OnArrivedAtTarget += OnPlayerArrived;
|
||||
_pendingInteractable = interactable;
|
||||
_pendingPlayer = player;
|
||||
_pendingFollower = FindFollower();
|
||||
_interactionInProgress = true;
|
||||
player.MoveToAndNotify(stopPoint);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Fallback: immediately interact
|
||||
OnCharacterArrived(interactable, character);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper to find the follower in the scene
|
||||
private FollowerController FindFollower()
|
||||
{
|
||||
// Use the recommended Unity API for finding objects
|
||||
return GameObject.FindFirstObjectByType<FollowerController>();
|
||||
}
|
||||
|
||||
private void OnPlayerArrived()
|
||||
{
|
||||
if (!_interactionInProgress || _pendingInteractable == null || _pendingPlayer == null)
|
||||
return;
|
||||
// Unsubscribe to avoid memory leaks
|
||||
_pendingPlayer.OnArrivedAtTarget -= OnPlayerArrived;
|
||||
// Now dispatch the follower to the interactable
|
||||
if (_pendingFollower != null)
|
||||
{
|
||||
_pendingFollower.OnPickupArrived -= OnFollowerArrived;
|
||||
_pendingFollower.OnPickupArrived += OnFollowerArrived;
|
||||
_pendingFollower.GoToPointAndReturn(_pendingInteractable.transform.position, _pendingPlayer.transform);
|
||||
}
|
||||
else
|
||||
{
|
||||
// No follower found, just interact as player
|
||||
OnCharacterArrived(_pendingInteractable, _pendingPlayer);
|
||||
_interactionInProgress = false;
|
||||
_pendingInteractable = null;
|
||||
_pendingPlayer = null;
|
||||
_pendingFollower = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnFollowerArrived()
|
||||
{
|
||||
if (!_interactionInProgress || _pendingInteractable == null || _pendingFollower == null)
|
||||
return;
|
||||
_pendingFollower.OnPickupArrived -= OnFollowerArrived;
|
||||
// Now check requirements and interact as follower
|
||||
OnCharacterArrived(_pendingInteractable, _pendingFollower);
|
||||
_interactionInProgress = false;
|
||||
_pendingInteractable = null;
|
||||
_pendingPlayer = null;
|
||||
_pendingFollower = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the character has arrived at the interactable.
|
||||
/// </summary>
|
||||
private void OnCharacterArrived(Interactable interactable, Character character)
|
||||
{
|
||||
// Check all requirements
|
||||
var requirements = interactable.GetComponents<InteractionRequirementBase>();
|
||||
bool allMet = true;
|
||||
foreach (var req in requirements)
|
||||
{
|
||||
// For now, only FollowerController is supported for requirements
|
||||
// (can be extended to support Player, etc.)
|
||||
if (character is FollowerController follower)
|
||||
{
|
||||
if (!req.TryInteract(follower))
|
||||
{
|
||||
allMet = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Add more character types as needed
|
||||
}
|
||||
if (allMet)
|
||||
{
|
||||
interactable.OnInteract(character);
|
||||
}
|
||||
else
|
||||
{
|
||||
interactable.CompleteInteraction(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 705c4ee7f8204cc68aacd79e2a4a506d
|
||||
timeCreated: 1757513080
|
||||
@@ -13,8 +13,6 @@ public class Pickup : MonoBehaviour
|
||||
public SpriteRenderer iconRenderer;
|
||||
private Interactable interactable;
|
||||
|
||||
private bool pickupInProgress = false;
|
||||
|
||||
/// <summary>
|
||||
/// Unity Awake callback. Sets up icon, interactable, and event handlers.
|
||||
/// </summary>
|
||||
@@ -98,76 +96,11 @@ public class Pickup : MonoBehaviour
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the start of an interaction (player approaches, then follower picks up).
|
||||
/// Handles the start of an interaction (for feedback/UI only).
|
||||
/// </summary>
|
||||
private void OnStartedInteraction()
|
||||
{
|
||||
if (pickupInProgress) return;
|
||||
var playerObj = GameObject.FindGameObjectWithTag("Player");
|
||||
var followerObj = GameObject.FindGameObjectWithTag("Pulver");
|
||||
if (playerObj == null || followerObj == null)
|
||||
{
|
||||
Debug.LogWarning("Pickup: Player or Follower not found.");
|
||||
return;
|
||||
}
|
||||
var playerController = playerObj.GetComponent<PlayerTouchController>();
|
||||
var followerController = followerObj.GetComponent<FollowerController>();
|
||||
if (playerController == null || followerController == null)
|
||||
{
|
||||
Debug.LogWarning("Pickup: PlayerTouchController or FollowerController missing.");
|
||||
return;
|
||||
}
|
||||
float playerStopDistance = GameManager.Instance.PlayerStopDistance;
|
||||
float followerPickupDelay = GameManager.Instance.FollowerPickupDelay;
|
||||
// --- Local event/coroutine handlers ---
|
||||
void OnPlayerArrived()
|
||||
{
|
||||
playerController.OnArrivedAtTarget -= OnPlayerArrived;
|
||||
playerController.OnMoveToCancelled -= OnPlayerMoveCancelled;
|
||||
pickupInProgress = true;
|
||||
StartCoroutine(DispatchFollower());
|
||||
}
|
||||
void OnPlayerMoveCancelled()
|
||||
{
|
||||
playerController.OnArrivedAtTarget -= OnPlayerArrived;
|
||||
playerController.OnMoveToCancelled -= OnPlayerMoveCancelled;
|
||||
pickupInProgress = false;
|
||||
}
|
||||
System.Collections.IEnumerator DispatchFollower()
|
||||
{
|
||||
yield return new WaitForSeconds(followerPickupDelay);
|
||||
followerController.OnPickupArrived += OnFollowerArrived;
|
||||
followerController.OnPickupReturned += OnFollowerReturned;
|
||||
followerController.GoToPointAndReturn(transform.position, playerObj.transform);
|
||||
}
|
||||
void OnFollowerArrived()
|
||||
{
|
||||
followerController.OnPickupArrived -= OnFollowerArrived;
|
||||
bool interactionSuccess = true;
|
||||
if (interactable != null)
|
||||
{
|
||||
interactionSuccess = interactable.OnFollowerArrived(followerController);
|
||||
}
|
||||
followerController.SetInteractionResult(interactionSuccess);
|
||||
}
|
||||
void OnFollowerReturned()
|
||||
{
|
||||
followerController.OnPickupReturned -= OnFollowerReturned;
|
||||
pickupInProgress = false;
|
||||
}
|
||||
playerController.OnArrivedAtTarget += OnPlayerArrived;
|
||||
playerController.OnMoveToCancelled += OnPlayerMoveCancelled;
|
||||
Vector3 stopPoint = transform.position + (playerObj.transform.position - transform.position).normalized * playerStopDistance;
|
||||
float distToPickup = Vector2.Distance(new Vector2(playerObj.transform.position.x, playerObj.transform.position.y), new Vector2(transform.position.x, transform.position.y));
|
||||
float dist = Vector2.Distance(new Vector2(playerObj.transform.position.x, playerObj.transform.position.y), new Vector2(stopPoint.x, stopPoint.y));
|
||||
if (distToPickup <= playerStopDistance || dist <= 0.2f)
|
||||
{
|
||||
OnPlayerArrived();
|
||||
}
|
||||
else
|
||||
{
|
||||
playerController.MoveToAndNotify(stopPoint);
|
||||
}
|
||||
// Optionally, add pickup-specific feedback here (e.g., highlight, sound).
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -47,14 +47,13 @@ public class SlotItemBehavior : InteractionRequirementBase
|
||||
SetSlottedObject(obj);
|
||||
}
|
||||
|
||||
private void RestoreSlottedObject(Vector3 position)
|
||||
private void RestoreSlottedObject()
|
||||
{
|
||||
if (_cachedSlottedObject != null)
|
||||
{
|
||||
_cachedSlottedObject.transform.position = position;
|
||||
_cachedSlottedObject.transform.SetParent(null);
|
||||
_cachedSlottedObject.SetActive(true);
|
||||
_cachedSlottedObject = null;
|
||||
// Do NOT activate or move the object here; it stays hidden until dropped
|
||||
// Activation is handled by the drop logic elsewhere
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,8 +75,10 @@ public class SlotItemBehavior : InteractionRequirementBase
|
||||
// CASE 1: No held item, slot has item -> pick up slotted item
|
||||
if (heldItem == null && _cachedSlottedObject != null)
|
||||
{
|
||||
// Give the hidden slotted object to the follower (do NOT activate or move it)
|
||||
RestoreSlottedObject();
|
||||
follower.SetHeldItemFromObject(_cachedSlottedObject);
|
||||
RemoveSlottedObject();
|
||||
_cachedSlottedObject = null;
|
||||
currentlySlottedItem = null;
|
||||
UpdateSlottedSprite();
|
||||
return true;
|
||||
@@ -95,7 +96,8 @@ public class SlotItemBehavior : InteractionRequirementBase
|
||||
currentlySlottedItem = followerHeldItem;
|
||||
UpdateSlottedSprite();
|
||||
|
||||
// 2. Give the slot's object to the follower
|
||||
// 2. Give the slot's object to the follower (do NOT activate or move it)
|
||||
RestoreSlottedObject();
|
||||
follower.SetHeldItemFromObject(slotObj);
|
||||
|
||||
return true;
|
||||
|
||||
@@ -6,7 +6,7 @@ using Utils;
|
||||
/// <summary>
|
||||
/// Controls the follower character, including following the player, handling pickups, and managing held items.
|
||||
/// </summary>
|
||||
public class FollowerController : MonoBehaviour
|
||||
public class FollowerController : Character
|
||||
{
|
||||
[Header("Follower Settings")]
|
||||
public bool debugDrawTarget = true;
|
||||
@@ -457,5 +457,3 @@ public class FollowerController : MonoBehaviour
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user