68 lines
1.6 KiB
C#
Raw Permalink Normal View History

2025-05-08 17:21:26 +08:00
using QFramework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XMLTool;
public class PlayerController : MonoSingleton<PlayerController>
{
private PlayerController() { }
public class HasDeviceData
2025-05-08 17:21:26 +08:00
{
public Device device;
public int count;
}
public Dictionary<string, HasDeviceData> hasDevicesDict = new Dictionary<string, HasDeviceData>();
2025-05-08 17:21:26 +08:00
public int DropDevice(string key, int count = 1)
2025-05-08 17:21:26 +08:00
{
if (hasDevicesDict.ContainsKey(key))
2025-05-08 17:21:26 +08:00
{
var dev = hasDevicesDict[key];
dev.count -= count;
if (dev.count <= 0)
2025-05-08 17:21:26 +08:00
{
hasDevicesDict.Remove(key);
return 0;
2025-05-08 17:21:26 +08:00
}
return dev.count;
2025-05-08 17:21:26 +08:00
}
return -1;
2025-05-08 17:21:26 +08:00
}
public void PickDevice(string key, int count = 1)
{
Device device = null;
if (hasDevicesDict.ContainsKey(key))
2025-05-08 17:21:26 +08:00
{
device = hasDevicesDict[key].device;
hasDevicesDict[key].count += count;
2025-05-08 17:21:26 +08:00
}
else
{
device = DeviceController.Instance.GetDevice(key);
hasDevicesDict.Add(key, new HasDeviceData() { count = count, device = device });
2025-05-08 17:21:26 +08:00
}
TypeEventSystem.Global.Send<OnPickDevice>(new OnPickDevice() { device = device, count = count });
}
2025-05-08 17:21:26 +08:00
public HasDeviceData HasDevice(string key)
{
if (hasDevicesDict.ContainsKey(key))
{
var dev = hasDevicesDict[key];
if (dev.count > 0)
{
return dev;
}
}
return null;
2025-05-08 17:21:26 +08:00
}
}