quest resource check fix
This commit is contained in:
@@ -19,9 +19,9 @@ public class InventoryManager : MonoBehaviour
|
||||
{
|
||||
if (resetInventory)
|
||||
{
|
||||
resetInventory= false;
|
||||
resetInventory = false;
|
||||
|
||||
foreach(InventorySlot slot in inventorySlots)
|
||||
foreach (InventorySlot slot in inventorySlots)
|
||||
{
|
||||
slot.Clear();
|
||||
}
|
||||
@@ -30,40 +30,78 @@ public class InventoryManager : MonoBehaviour
|
||||
|
||||
public item GetItemByType(string type)
|
||||
{
|
||||
foreach(item i in lootsData.items)
|
||||
foreach (item i in lootsData.items)
|
||||
{
|
||||
if(i.type == type)
|
||||
if (i.type == type)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
Debug.Log("Could not find anything for " + type);
|
||||
Debug.Log("Could not find anything for " + type);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
void Start(){
|
||||
foreach(item i in startingItems){
|
||||
void Start()
|
||||
{
|
||||
foreach (item i in startingItems)
|
||||
{
|
||||
AddInvItem(i);
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<int, string> GetEntries(){
|
||||
public Dictionary<int, string> GetEntries()
|
||||
{
|
||||
Dictionary<int, string> entries = new Dictionary<int, string>();
|
||||
for(int i=0; i < inventorySlots.Length; i++){
|
||||
for (int i = 0; i < inventorySlots.Length; i++)
|
||||
{
|
||||
ItemInventory itemInSlot = inventorySlots[i].GetComponentInChildren<ItemInventory>();
|
||||
if(itemInSlot!= null && itemInSlot.item != null){
|
||||
if (itemInSlot != null && itemInSlot.item != null)
|
||||
{
|
||||
entries.Add(i, itemInSlot.item.type);
|
||||
}
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
public int GetStock(string type)
|
||||
{
|
||||
int count = 0;
|
||||
for (int i = 0; i < inventorySlots.Length; i++)
|
||||
{
|
||||
ItemInventory itemInSlot = inventorySlots[i].GetComponentInChildren<ItemInventory>();
|
||||
if (itemInSlot != null && itemInSlot.item != null)
|
||||
{
|
||||
if (itemInSlot.item.type == type)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public void RemoveItem(string type)
|
||||
{
|
||||
for (int i = 0; i < inventorySlots.Length; i++)
|
||||
{
|
||||
ItemInventory itemInSlot = inventorySlots[i].GetComponentInChildren<ItemInventory>();
|
||||
if (itemInSlot != null && itemInSlot.item != null)
|
||||
{
|
||||
if (itemInSlot.item.type == type)
|
||||
{
|
||||
Destroy(itemInSlot.gameObject);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetInventory(Dictionary<int, string> data)
|
||||
{
|
||||
Clear();
|
||||
|
||||
foreach(KeyValuePair<int, string> entry in data)
|
||||
foreach (KeyValuePair<int, string> entry in data)
|
||||
{
|
||||
SpawnNewItem(GetItemByType(entry.Value), inventorySlots[entry.Key]);
|
||||
}
|
||||
@@ -71,12 +109,12 @@ public class InventoryManager : MonoBehaviour
|
||||
|
||||
public bool UseItem(item i)
|
||||
{
|
||||
if(pnet.health >= 100)
|
||||
if (pnet.health >= 100)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
pnet.SetHealth(pnet.health+i.healthIncrease);
|
||||
pnet.SetHealth(pnet.health + i.healthIncrease);
|
||||
pnet.SavePlayerData();
|
||||
|
||||
return true;
|
||||
@@ -84,7 +122,8 @@ public class InventoryManager : MonoBehaviour
|
||||
|
||||
public item selectedItem;
|
||||
|
||||
public void SelectItem(item itemI){
|
||||
public void SelectItem(item itemI)
|
||||
{
|
||||
selectedItem = itemI;
|
||||
}
|
||||
|
||||
@@ -100,14 +139,17 @@ public class InventoryManager : MonoBehaviour
|
||||
AddInvItem(GetItemByType(type));
|
||||
}
|
||||
|
||||
public void AddInvItem (item item){
|
||||
public void AddInvItem(item item)
|
||||
{
|
||||
//find an empty slot
|
||||
for(int i = inventorySlots.Length-1; i >=0 ; i--){
|
||||
for (int i = inventorySlots.Length - 1; i >= 0; i--)
|
||||
{
|
||||
InventorySlot slot = inventorySlots[i];
|
||||
ItemInventory itemInSlot = slot.GetComponentInChildren<ItemInventory>();
|
||||
|
||||
if(itemInSlot == null){
|
||||
SpawnNewItem(item , slot);
|
||||
if (itemInSlot == null)
|
||||
{
|
||||
SpawnNewItem(item, slot);
|
||||
return;
|
||||
}
|
||||
//implement check for slot full
|
||||
@@ -116,30 +158,35 @@ public class InventoryManager : MonoBehaviour
|
||||
Debug.Log("Slots are full");
|
||||
}
|
||||
|
||||
public void Clear(){
|
||||
for(int i=0; i < inventorySlots.Length; i++){
|
||||
if(inventorySlots[i].transform.childCount > 0){
|
||||
public void Clear()
|
||||
{
|
||||
for (int i = 0; i < inventorySlots.Length; i++)
|
||||
{
|
||||
if (inventorySlots[i].transform.childCount > 0)
|
||||
{
|
||||
Destroy(inventorySlots[i].transform.GetChild(0).gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SpawnNewItem(item item , InventorySlot slot ){
|
||||
GameObject newItemAdd = Instantiate(ItemInventoryPrefab , slot.transform);
|
||||
|
||||
void SpawnNewItem(item item, InventorySlot slot)
|
||||
{
|
||||
GameObject newItemAdd = Instantiate(ItemInventoryPrefab, slot.transform);
|
||||
|
||||
ItemInventory inventoryItemm = newItemAdd.GetComponent<ItemInventory>();
|
||||
inventoryItemm.Set(item,this);
|
||||
inventoryItemm.Set(item, this);
|
||||
|
||||
pnet.SavePlayerData();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[System.Serializable]
|
||||
public class InventoryEntry{
|
||||
public class InventoryEntry
|
||||
{
|
||||
public int slot;
|
||||
public string item;
|
||||
}
|
||||
@@ -19,26 +19,33 @@ public class QuestAction : MonoBehaviour
|
||||
{
|
||||
if (other.CompareTag("Player") && other.transform == playerNetwork.localPlayerTransform)
|
||||
{
|
||||
foreach(QuestCompleteResourceCheckEntry entry in resourceCheckEntries) //Check if has all resources
|
||||
{
|
||||
int stockCount = playerNetwork.localPlayerTransform.GetComponent<Inventory>().inventoryManager.GetStock(entry.resource.type);
|
||||
if(stockCount < entry.amount)
|
||||
{
|
||||
Debug.Log($"Not enough {entry.resource.type}, need {entry.amount}, found {stockCount}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
foreach(QuestCompleteResourceCheckEntry entry in resourceCheckEntries){ //Remove resources
|
||||
if(playerNetwork.localPlayerTransform.GetComponent<Inventory>().inventoryManager.GetStock(entry.resource.type) < entry.amount){
|
||||
|
||||
}else{
|
||||
for(int i=0; i < entry.amount; i++)
|
||||
{
|
||||
playerNetwork.localPlayerTransform.GetComponent<Inventory>().inventoryManager.RemoveItem(entry.resource.type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OnComplete.Invoke();
|
||||
Debug.Log("QuestAction: QuestAction completed");
|
||||
if (isFinalAction)
|
||||
{
|
||||
|
||||
foreach(QuestCompleteResourceCheckEntry entry in resourceCheckEntries)
|
||||
{
|
||||
if(playerNetwork.localPlayerTransform.GetComponent<Inventory>().GetStock(entry.resourceName) < entry.amount)
|
||||
{
|
||||
Debug.Log("QuestAction: Resource check failed");
|
||||
return;
|
||||
}else{
|
||||
for(int i=0; i < entry.amount; i++)
|
||||
{
|
||||
playerNetwork.localPlayerTransform.GetComponent<Inventory>().RemoveItem(entry.resourceName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
playerNetwork.localPlayerTransform.GetComponent<playerNetwork>().CompleteQuest(questData);
|
||||
}
|
||||
|
||||
@@ -77,6 +84,6 @@ public class QuestAction : MonoBehaviour
|
||||
[Serializable ]
|
||||
public class QuestCompleteResourceCheckEntry
|
||||
{
|
||||
public string resourceName;
|
||||
public item resource;
|
||||
public int amount;
|
||||
}
|
||||
Reference in New Issue
Block a user