[Input][Interaction] Add interactable items
This commit is contained in:
@@ -67,10 +67,19 @@ public class InputManager : MonoBehaviour
|
||||
defaultConsumer?.OnTouchPosition(pos);
|
||||
}
|
||||
|
||||
private bool TryDelegateToInteractable(Vector2 screenPos)
|
||||
private bool TryDelegateToInteractable(Vector2 worldPos)
|
||||
{
|
||||
// TODO: Raycast logic to find ITouchInputConsumer at screenPos
|
||||
// For now, always return false (no interactable found)
|
||||
// Raycast at the world position to find an Interactable
|
||||
Collider2D hit = Physics2D.OverlapPoint(worldPos);
|
||||
if (hit != null)
|
||||
{
|
||||
var interactable = hit.GetComponent<Interactable>();
|
||||
if (interactable != null)
|
||||
{
|
||||
interactable.OnTouchPress(worldPos);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
20
Assets/Scripts/Interactable.cs
Normal file
20
Assets/Scripts/Interactable.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
public class Interactable : MonoBehaviour, ITouchInputConsumer
|
||||
{
|
||||
public event Action Interacted;
|
||||
|
||||
// Called by InputManager when this interactable is clicked/touched
|
||||
public void OnTouchPress(Vector2 worldPosition)
|
||||
{
|
||||
Debug.Log($"Interactable.OnTouchPress at {worldPosition} on {gameObject.name}");
|
||||
Interacted?.Invoke();
|
||||
}
|
||||
|
||||
public void OnTouchPosition(Vector2 screenPosition)
|
||||
{
|
||||
// Optionally handle drag/move here
|
||||
}
|
||||
}
|
||||
|
||||
50
Assets/Scripts/LevelSwitch.cs
Normal file
50
Assets/Scripts/LevelSwitch.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class LevelSwitch : MonoBehaviour
|
||||
{
|
||||
public LevelSwitchData switchData;
|
||||
public SpriteRenderer iconRenderer;
|
||||
private Interactable interactable;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
interactable = GetComponent<Interactable>();
|
||||
if (interactable != null)
|
||||
{
|
||||
interactable.Interacted += OnInteracted;
|
||||
}
|
||||
ApplySwitchData();
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (interactable != null)
|
||||
{
|
||||
interactable.Interacted -= OnInteracted;
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void OnValidate()
|
||||
{
|
||||
ApplySwitchData();
|
||||
}
|
||||
#endif
|
||||
|
||||
public void ApplySwitchData()
|
||||
{
|
||||
if (switchData != null)
|
||||
{
|
||||
if (iconRenderer != null)
|
||||
iconRenderer.sprite = switchData.mapSprite;
|
||||
gameObject.name = switchData.targetLevelSceneName;
|
||||
// Optionally update other fields, e.g. description
|
||||
}
|
||||
}
|
||||
|
||||
private void OnInteracted()
|
||||
{
|
||||
Debug.Log($"LevelSwitch.OnInteracted: Switching to level {switchData?.targetLevelSceneName}");
|
||||
// TODO: Add scene loading logic here, e.g. UnityEngine.SceneManagement.SceneManager.LoadScene(switchData.targetLevelSceneName);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/LevelSwitchData.cs
Normal file
11
Assets/Scripts/LevelSwitchData.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(fileName = "LevelSwitchData", menuName = "Game/Level Switch Data")]
|
||||
public class LevelSwitchData : ScriptableObject
|
||||
{
|
||||
public string targetLevelSceneName;
|
||||
[TextArea]
|
||||
public string description;
|
||||
public Sprite mapSprite;
|
||||
}
|
||||
|
||||
55
Assets/Scripts/Pickup.cs
Normal file
55
Assets/Scripts/Pickup.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Pickup : MonoBehaviour
|
||||
{
|
||||
public PickupItemData itemData;
|
||||
public SpriteRenderer iconRenderer;
|
||||
private Interactable interactable;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (iconRenderer == null)
|
||||
iconRenderer = GetComponent<SpriteRenderer>();
|
||||
interactable = GetComponent<Interactable>();
|
||||
if (interactable != null)
|
||||
{
|
||||
interactable.Interacted += OnInteracted;
|
||||
}
|
||||
ApplyItemData();
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (interactable != null)
|
||||
{
|
||||
interactable.Interacted -= OnInteracted;
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void OnValidate()
|
||||
{
|
||||
if (iconRenderer == null)
|
||||
iconRenderer = GetComponent<SpriteRenderer>();
|
||||
ApplyItemData();
|
||||
}
|
||||
#endif
|
||||
|
||||
public void ApplyItemData()
|
||||
{
|
||||
if (itemData != null)
|
||||
{
|
||||
if (iconRenderer != null)
|
||||
iconRenderer.sprite = itemData.mapSprite;
|
||||
gameObject.name = itemData.itemName;
|
||||
// Optionally update other fields, e.g. description
|
||||
}
|
||||
}
|
||||
|
||||
private void OnInteracted()
|
||||
{
|
||||
Debug.Log($"Pickup.OnInteracted: Picked up {itemData?.itemName}");
|
||||
// TODO: Add item to inventory manager here
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
10
Assets/Scripts/PickupItemData.cs
Normal file
10
Assets/Scripts/PickupItemData.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(fileName = "PickupItemData", menuName = "Game/Pickup Item Data")]
|
||||
public class PickupItemData : ScriptableObject
|
||||
{
|
||||
public string itemName;
|
||||
[TextArea]
|
||||
public string description;
|
||||
public Sprite mapSprite;
|
||||
}
|
||||
Reference in New Issue
Block a user