[Input][Interaction] Add interactable items

This commit is contained in:
Michal Pikulski
2025-09-01 16:14:21 +02:00
parent 8b0f6b9376
commit 8e1174d4bf
11 changed files with 725 additions and 79 deletions

View File

@@ -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;
}
}

View 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
}
}

View 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);
}
}

View 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
View 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);
}
}

View 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;
}