_xiaofang/xiaofang/Assets/Res/gsj/scripts/UseObjects.cs

387 lines
13 KiB
C#
Raw Normal View History

2024-12-23 14:46:56 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2024-12-26 22:26:18 +08:00
using static UnityEditor.Experimental.GraphView.GraphView;
using static UnityEngine.GraphicsBuffer;
2024-12-23 14:46:56 +08:00
public class UseObjects : MonoBehaviour
{
private Transform parentObject; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5A3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private GameObject player;//<2F><><EFBFBD><EFBFBD>
public JSONReader js;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒʹ<C6B7><CAB9>
public GameObject warningPrefab; // <20><><EFBFBD><EFBFBD>׶<EFBFBD><D7B6><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>
public GameObject polePrefab; // <20><><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>ģ<EFBFBD><C4A3>
public GameObject tapePrefab; // <20><><EFBFBD>ʾ<EBBEAF><CABE>ģ<EFBFBD><C4A3>
public float maxDistance = 10f; // <20><><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӿ<EFBFBD><D3BE><EFBFBD>10<31><30>
public bool hasTape = false; // <20>Ƿ<EFBFBD>Я<EFBFBD><D0AF><EFBFBD><EFBFBD><EFBFBD>ʾ<EBBEAF><CABE>
private List<GameObject> warningObjects = new List<GameObject>(); // <20><EFBFBD><E6B4A2><EFBFBD>о<EFBFBD><D0BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD>
public List<GameObject> potentialTargets; // <20><><EFBFBD>п<EFBFBD><D0BF>ܳ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>б<EFBFBD>
public Button deliverButton; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ť
public float deliveryRange = 1f; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ<EFBFBD>
private GameObject currentTarget; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD>Ŀ<EFBFBD><C4BF>
private bool islogistics;//<2F><>ɫ<EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD>;
private bool isdelivery;//<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
[System.Serializable]
public class EmergencyItem
{
public string itemName; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public bool isConsumable; // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>Ʒ (1=<3D>ǣ<EFBFBD>0=<3D><>)
public int validTime; // <20><>Чʱ<D0A7>䣺-1<><31><EFBFBD><EFBFBD><EFBFBD>ޣ<EFBFBD><DEA3><EFBFBD>0 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>
}
// ģ<><C4A3><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD>
public Dictionary<string, EmergencyItem> itemConfigs = new Dictionary<string, EmergencyItem>();
void Start()
{
parentObject = GameObject.Find("fireEquip").transform;
player=GameObject.FindGameObjectWithTag("Player");
// <20><>ʼ<EFBFBD><CABC>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD>
InitializeItemConfigs(3001);
// <20><><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD>ť
deliverButton.gameObject.SetActive(false);
deliverButton.onClick.AddListener(HandleDelivery);
// Ϊÿ<CEAA><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD>¼<EFBFBD>
foreach (Transform child in parentObject)
{
Button childButton = child.GetComponent<Button>();
if (childButton == null)
{
Debug.LogWarning($"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {child.name} ȱ<><C8B1> Button <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
continue;
}
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
Transform buttonPanel = child.Find("informationImage");
Transform highlight = child.Find("Image");
if (buttonPanel != null) buttonPanel.gameObject.SetActive(false);
if (highlight != null) highlight.gameObject.SetActive(false);
// <20>󶨵<EFBFBD><F3B6A8B5><EFBFBD><EFBFBD>¼<EFBFBD>
childButton.onClick.AddListener(() => OnChildClicked(child.gameObject));
}
2024-12-26 22:26:18 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>б<EFBFBD>ǩΪ "People" <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GameObject[] playNPC = GameObject.FindGameObjectsWithTag("People");
// <20><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
foreach (GameObject player in playNPC)
{
potentialTargets.Add(player.gameObject);
}
2024-12-23 14:46:56 +08:00
UseCautionaryitems(player.transform.position, polePrefab);
}
private void Update()
{
// <20>ڷ<EFBFBD>Χ<EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD>Ŀ<EFBFBD><C4BF>
currentTarget = FindNearestTarget();
if (currentTarget != null)
{
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>
HighlightTarget(currentTarget, true);
deliverButton.gameObject.SetActive(true);
}
else
{
// <20><><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD>ť<EFBFBD><C5A5>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (currentTarget != null)
{
HighlightTarget(currentTarget, false);
}
deliverButton.gameObject.SetActive(false);
}
}
// <20><>ʼ<EFBFBD><CABC>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD>
void InitializeItemConfigs(int id)
{
itemConfigs.Add("1", new EmergencyItem { itemName = "1", isConsumable = true, validTime = -1 });
itemConfigs.Add("2", new EmergencyItem { itemName = "2", isConsumable = true, validTime = 30 });
itemConfigs.Add("3", new EmergencyItem { itemName = "3", isConsumable = false, validTime = 15 });
itemConfigs.Add("4", new EmergencyItem { itemName = "4", isConsumable = true, validTime = -1 });
itemConfigs.Add("5", new EmergencyItem { itemName = "5", isConsumable = true, validTime = 30 });
itemConfigs.Add("6", new EmergencyItem { itemName = "6", isConsumable = false, validTime = 15 });
// <20><><EFBFBD>Ӹ<EFBFBD><D3B8><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD>...
foreach (var key in js.matialDictionary)
{
MatialData matialData = key.Value;
if (matialData.ID==id)
{
//<2F><>ȡ<EFBFBD><C8A1>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD>
2024-12-24 20:21:15 +08:00
2024-12-23 14:46:56 +08:00
}
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5B1BB><EFBFBD><EFBFBD>ʱ
void OnChildClicked(GameObject clickedChild)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>İ<EFBFBD>ť<EFBFBD><C5A5><EFBFBD>͸߹<CDB8><DFB9><EFBFBD>
foreach (Transform child in parentObject)
{
Transform buttonPanel = child.Find("informationImage");
Transform highlight = child.Find("Image");
if (buttonPanel != null) buttonPanel.gameObject.SetActive(false);
if (highlight != null) highlight.gameObject.SetActive(false);
}
// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
string itemName = clickedChild.name;
Debug.Log(itemName);
if (!itemConfigs.ContainsKey(itemName))
{
Debug.LogWarning($"δ<>ҵ<EFBFBD><D2B5><EFBFBD>Ʒ {itemName} <20><><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD>");
return;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>ʾ<EFBFBD><CABE>ť
EmergencyItem item = itemConfigs[itemName];
Transform clickedButtonPanel = clickedChild.transform.Find("informationImage");
Transform clickedHighlight = clickedChild.transform.Find("Image");
if (clickedButtonPanel != null)
{
//clickedButtonPanel.gameObject.SetActive(true);
// <20><>ʾ<EFBFBD><CABE>Ӧ<EFBFBD>İ<EFBFBD>ť
UpdateButtonPanel(clickedButtonPanel, item);
}
// <20><>ʾ<EFBFBD><CABE>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ߹<C4B8><DFB9><EFBFBD>
if (clickedHighlight != null)
{
clickedHighlight.gameObject.SetActive(true);
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD>ø<EFBFBD><C3B8>°<EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD>
void UpdateButtonPanel(Transform buttonPanel, EmergencyItem item)
{
// <20><>ȡ<EFBFBD><C8A1>ť
Button viewButton = buttonPanel.Find("LookButton").GetComponent<Button>();
Button useButton = buttonPanel.Find("UseButton").GetComponent<Button>();
Button discardButton = buttonPanel.Find("ThrowButton").GetComponent<Button>();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>ذ<EFBFBD>ť
if (item.isConsumable && item.validTime == -1)
{
// <20><><EFBFBD><EFBFBD>1<EFBFBD><31>ֱ<EFBFBD><D6B1>ʹ<EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ť
buttonPanel.gameObject.SetActive(false);
UseItem(item);
}
else if (item.isConsumable && item.validTime >= 0)
{
buttonPanel.gameObject.SetActive(true);
// <20><><EFBFBD><EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BFB4><EFBFBD><EFBFBD>ʹ<EFBFBD>á<EFBFBD><C3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
viewButton.gameObject.SetActive(true);
useButton.gameObject.SetActive(true);
discardButton.gameObject.SetActive(true);
// <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
viewButton.onClick.AddListener(() => ViewItem(item));
useButton.onClick.AddListener(() => UseItem(item));
discardButton.onClick.AddListener(() => DiscardItem(item));
}
else if (!item.isConsumable && item.validTime >= 0)
{
buttonPanel.gameObject.SetActive(true);
// <20><><EFBFBD><EFBFBD>3<EFBFBD><33><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
viewButton.gameObject.SetActive(true);
useButton.gameObject.SetActive(false);
discardButton.gameObject.SetActive(true);
// <20><><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
viewButton.onClick.AddListener(() => ViewItem(item));
discardButton.onClick.AddListener(() => DiscardItem(item));
}
}
// <20><EFBFBD><E9BFB4>Ʒ<EFBFBD>߼<EFBFBD>
void ViewItem(EmergencyItem item)
{
Debug.Log($"<22><EFBFBD><E9BFB4>Ʒ<EFBFBD><C6B7>{item.itemName}");
}
// ʹ<><CAB9><EFBFBD><EFBFBD>Ʒ<EFBFBD>߼<EFBFBD>
void UseItem(EmergencyItem item)
{
Debug.Log($"ʹ<><CAB9><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7>{item.itemName}");
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD>߼<EFBFBD>
void DiscardItem(EmergencyItem item)
{
Debug.Log($"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7>{item.itemName}");
}
//ʹ<>þ<EFBFBD><C3BE><EFBFBD><EFBFBD><EFBFBD>Ʒ
void UseCautionaryitems(Vector3 playerPosition,GameObject gameObject)
{
// <20><><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>׶)
GameObject newWarning = Instantiate(gameObject, playerPosition, Quaternion.identity);
warningObjects.Add(newWarning);
// <20>жϵ<D0B6>ǰλ<C7B0><CEBB><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>10<31><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GameObject nearestWarning = FindNearestWarning(newWarning);
if (nearestWarning != null)
{
if (hasTape)
{
// <20><><EFBFBD><EFBFBD>Я<EFBFBD><D0AF><EFBFBD><EFBFBD><EFBFBD>ʾ<EBBEAF><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
CreateTape(newWarning, nearestWarning);
}
else
{
// <20><>Я<EFBFBD><D0AF><EFBFBD><EFBFBD><EFBFBD>ʾ<EBBEAF><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD>Լ<EFBFBD>¼
Debug.Log("δЯ<CEB4><D0AF><EFBFBD><EFBFBD><EFBFBD>ʾ<EBBEAF><CABE><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD>Ӿ<EFBFBD><D3BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD>");
}
}
else
{
Debug.Log("<22><>ǰλ<C7B0><CEBB><EFBFBD><EFBFBD>Χ10<31>׷<EFBFBD>Χ<EFBFBD><CEA7>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD>");
}
}
// <20>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private GameObject FindNearestWarning(GameObject currentWarning)
{
GameObject nearest = null;
float minDistance = maxDistance;
foreach (GameObject warning in warningObjects)
{
if (warning == currentWarning) continue; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
float distance = Vector3.Distance(currentWarning.transform.position, warning.transform.position);
if (distance < minDistance)
{
minDistance = distance;
nearest = warning;
}
}
return nearest;
}
// <20><><EFBFBD>ɸ<EFBFBD><C9B8>ʾ<EBBEAF><CABE>
private void CreateTape(GameObject start, GameObject end)
{
// <20><><EFBFBD>ɸ<EFBFBD><C9B8>ʾ<EBBEAF><CABE>
GameObject tape = Instantiate(tapePrefab);
// <20><><EFBFBD>ø<EFBFBD><C3B8>ʾ<EBBEAF><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD>
LineRenderer lineRenderer = tape.GetComponent<LineRenderer>();
if (lineRenderer != null)
{
lineRenderer.positionCount = 2;
lineRenderer.SetPosition(0, start.transform.position);
lineRenderer.SetPosition(1, end.transform.position);
}
Debug.Log($"<22><><EFBFBD><EFBFBD><EFBFBD>Ӿ<EFBFBD><D3BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD>{start.name} -> {end.name}");
}
// <20><><EFBFBD><EFBFBD><EFBFBD>߼<EFBFBD><DFBC>в<EFBFBD><D0B2>ҷ<EFBFBD>Χ<EFBFBD><CEA7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>
GameObject FindNearestTarget()
{
GameObject nearestTarget = null;
float nearestDistance = deliveryRange;
foreach (var target in potentialTargets)
{
float distance = Vector3.Distance(player.transform.position, target.transform.position);
if (distance <= deliveryRange && IsInFront(target))
{
nearestTarget = target;
nearestDistance = distance;
}
}
return nearestTarget;
}
// <20>ж<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>Ƿ<EFBFBD><C7B7>ڽ<EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD> 180 <20><><EFBFBD><EFBFBD>
bool IsInFront(GameObject target)
{
Vector3 directionToTarget = (target.transform.position - player.transform.position).normalized;
float dotProduct = Vector3.Dot(player.transform.forward, directionToTarget);
return dotProduct > 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 180 <20><><EFBFBD><EFBFBD>
}
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EAA3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>Ե<EFBFBD><D4B5><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD>
void HighlightTarget(GameObject target, bool highlight)
{
Renderer renderer = target.GetComponent<Renderer>();
if (renderer != null)
{
// <20><><EFBFBD>ø<EFBFBD><C3B8><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2>ʻ<EFBFBD><CABB><EFBFBD>ɫ
renderer.material.SetColor("_EmissionColor", highlight ? Color.green : Color.black);
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߼<EFBFBD>
void HandleDelivery()
{
if (currentTarget == null)
{
Debug.LogWarning(<>пɽ<D0BF><C9BD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD>꣡");
return;
}
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>ְҵ<D6B0>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
TargetData targetData = currentTarget.GetComponent<TargetData>();
if (targetData == null || targetData.weightCapacity == -1)
{
Debug.LogWarning("Ŀ<><EFBFBD><EAB2BB><EFBFBD><EFBFBD><E3BDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
return;
}
// ִ<>н<EFBFBD><D0BD><EFBFBD><EFBFBD>߼<EFBFBD><DFBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
PlayerInventory playerInventory = player.GetComponent<PlayerInventory>();
if (playerInventory != null && playerInventory.UseItem("SelectedItem"))
{
targetData.ReceiveItem("SelectedItem");
Debug.Log($"<22>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߸<EFBFBD>Ŀ<EFBFBD>꣺{currentTarget.name}");
}
else
{
Debug.LogWarning("<22><><EFBFBD>ҵ<EFBFBD><D2B5>߲<EFBFBD><DFB2><EFBFBD>޷<EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
}
}
// Ŀ<><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public class TargetData : MonoBehaviour
{
public int weightCapacity = 10; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
private Dictionary<string, int> receivedItems = new Dictionary<string, int>();
public void ReceiveItem(string itemName)
{
if (receivedItems.ContainsKey(itemName))
{
receivedItems[itemName]++;
}
else
{
receivedItems[itemName] = 1;
}
Debug.Log($"{itemName} <20><><EFBFBD>ճɹ<D5B3><C9B9><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>{receivedItems[itemName]}");
}
}
// <20><><EFBFBD>ҵ<EFBFBD><D2B5>߹<EFBFBD><DFB9><EFBFBD><EFBFBD><EFBFBD>
public class PlayerInventory : MonoBehaviour
{
private Dictionary<string, int> items = new Dictionary<string, int>
{
{ "SelectedItem", 5 } // ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD>ҵĵ<D2B5><C4B5>߿<EFBFBD><DFBF><EFBFBD>
};
public bool UseItem(string itemName)
{
if (items.ContainsKey(itemName) && items[itemName] > 0)
{
items[itemName]--;
Debug.Log($"{itemName} ʹ<>óɹ<C3B3><C9B9><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>{items[itemName]}");
return true;
}
Debug.LogWarning($"{itemName} ʹ<><CAB9>ʧ<EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD>㣡");
return false;
}
}