135 lines
3.3 KiB
C#
135 lines
3.3 KiB
C#
using UnityEngine;
|
|||
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
|
||
|
|
|
||
|
|
public class Inventory : MonoBehaviour {
|
||
|
|
|
||
|
|
private List<PickableItem> _items = new List<PickableItem>();
|
||
|
|
|
||
|
|
public List<PickableItem> Items {
|
||
|
|
get {
|
||
|
|
return _items;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public PickableItem QueryItem(string itemName, string className, ItemSocketType? socket) {
|
||
|
|
var id = QueryItemIndex(itemName, className, socket);
|
||
|
|
if (id < 0) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
return _items[id];
|
||
|
|
}
|
||
|
|
|
||
|
|
public int QueryItemIndex(string itemName, string className, ItemSocketType? socket) {
|
||
|
|
if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(className) && !socket.HasValue) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
for(int i = 0; i < _items.Count; ++i) {
|
||
|
|
var item = _items[i];
|
||
|
|
bool found = true;
|
||
|
|
|
||
|
|
if (!string.IsNullOrEmpty(itemName) && item.description.Name != itemName) {
|
||
|
|
found = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (found && !string.IsNullOrEmpty(className) && item.description.TypeName != className) {
|
||
|
|
found = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (found && socket.HasValue && item.description.TargetSocket != socket.Value) {
|
||
|
|
found = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (found) {
|
||
|
|
return i;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void AddItem(PickableItem item){
|
||
|
|
if (item == null || _items.Contains(item)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
_items.Add(item);
|
||
|
|
item.transform.parent = transform;
|
||
|
|
item.gameObject.SetActive(false);
|
||
|
|
}
|
||
|
|
|
||
|
|
public PickableItem QueryItem(int id) {
|
||
|
|
if (id < 0 || id >= _items.Count) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
return _items[id];
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool UnequipItem(PickableItem item) {
|
||
|
|
if (item != null && item.equipped && _items.Contains(item)) {
|
||
|
|
var player = GetComponent<HumanController>();
|
||
|
|
var socket = player.GetSocket(item.description.TargetSocket);
|
||
|
|
if (socket != null) {
|
||
|
|
socket.DetachItem(transform);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool EquipItem(PickableItem item) {
|
||
|
|
if (item == null || !_items.Contains(item)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return EquipItemInternal(item);
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool EquipItem(int id) {
|
||
|
|
var item = QueryItem(id);
|
||
|
|
return EquipItemInternal(item);
|
||
|
|
}
|
||
|
|
|
||
|
|
private bool EquipItemInternal(PickableItem item) {
|
||
|
|
if (item == null || !item.description.IsEquippable || item.equipped) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
var player = GetComponent<HumanController>();
|
||
|
|
var socket = player.GetSocket(item.description.TargetSocket);
|
||
|
|
if (socket != null) {
|
||
|
|
socket.DetachItem(transform);
|
||
|
|
socket.AttachItem(item);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public void DeleteAllItems() {
|
||
|
|
var deleteQueue = _items.ToArray();
|
||
|
|
foreach (var pickableItem in deleteQueue) {
|
||
|
|
UnequipItem(pickableItem);
|
||
|
|
Destroy(pickableItem.gameObject);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private AudioSource _audio;
|
||
|
|
|
||
|
|
public AudioSource Audio{
|
||
|
|
get{return _audio;}
|
||
|
|
}
|
||
|
|
// Use this for initialization
|
||
|
|
void Start () {
|
||
|
|
_audio = GetComponent<AudioSource>();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update is called once per frame
|
||
|
|
void Update () {
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnGUI(){
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|