Added Basic ResourceManagement

This commit is contained in:
DerTyp187
2021-09-30 20:44:50 +02:00
parent 4136dec646
commit 15203c52ad
22 changed files with 347 additions and 66 deletions

View File

@@ -1,8 +1,8 @@
fileFormatVersion: 2
guid: 9309bb620f211124297c6d9da498fca1
NativeFormatImporter:
guid: 67b1c241d94696c409ad579573961232
folderAsset: yes
DefaultImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,8 +1,8 @@
fileFormatVersion: 2
guid: 89329e8fd5243144e83e179fb10df382
NativeFormatImporter:
guid: 03d0b1e434e28de4f9f37f90abf6d095
folderAsset: yes
DefaultImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WarehouseBuilding : StorageBuilding
{
private void Start()
{
inventorySpace = 500;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8a48bc1459390b4459fbf54ad16b50f9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -4,5 +4,96 @@ using UnityEngine;
public class StorageBuilding : Building
{
[SerializeField] private List<Item> inventory = new List<Item>();
public int inventorySpace;
public void Add(Item item)
{
if(GetFreeSpace() >= item.count)
{
bool added = false;
//Check if the Item can get stacked
foreach (Item i in inventory)
{
if (i.uuid == item.uuid)
{
i.count += item.count;
added = true;
return;
}
added = false;
}
//If foreach does not work just ADD (List is empty)
if (!added)
{
inventory.Add(item);
}
}
else
{
Debug.Log("Inventory Full");
}
//TODO mach wenn nicht ganz voll, dass dann so viele items added werden wie platz ist
//Sonst wird bei 20 Holz KOMPLETT nein gesagt weil/obowhl 19 Space noch da ist
}
public void Remove(Item item)
{
//Check if the Item can get stacked
foreach (Item i in inventory)
{
if (i.uuid == item.uuid)
{
if(i.count > item.count)
{
i.count -= item.count;
}else if(i.count <= item.count)
{
//!!!Muss eventuell sp<73>ter anders gehandelt werden!!!
inventory.Remove(i); //Wenn du mehr entfernst als im Inventar ist, dann wird das Item einfach komplett removed
}
}
}
}
public int GetCountOfItem(Item item)
{
int count = 0;
foreach(Item i in inventory)
{
if(i.uuid == item.uuid)
{
count += i.count;
}
}
return count;
}
public int GetUsedSpace()
{
int usedSpace = 0;
foreach(Item item in inventory)
{
usedSpace += item.count;
}
return usedSpace;
}
public int GetFreeSpace()
{
return inventorySpace - GetUsedSpace();
}
public List<Item> Getinventory()
{
return inventory;
}
}

View File

@@ -1,18 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 232c60ba7df6f074d9c92fa97215e412, type: 3}
m_Name: Stone
m_EditorClassIdentifier:
name: New Item
uuid: new_item
icon: {fileID: 0}
isDefaultItem: 0

View File

@@ -0,0 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StoneItem : Item
{
public StoneItem(int c = 1)
{
count = c;
name = "Stone";
uuid = "item_stone";
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 437c0f4d48ae7a441beeaf0c3b7376a6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,17 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 232c60ba7df6f074d9c92fa97215e412, type: 3}
m_Name: Wood
m_EditorClassIdentifier:
name: Wood
icon: {fileID: 10915, guid: 0000000000000000f000000000000000, type: 0}
isDefaultItem: 0

View File

@@ -0,0 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WoodItem : Item
{
public WoodItem(int c = 1)
{
count = c;
name = "Wood";
uuid = "item_wood";
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7e095dfc35783fe418bcfddbdf69924f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -2,13 +2,16 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Item", menuName = "Resources/Item")]
public class Item : ScriptableObject
[System.Serializable]
public class Item
{
new public string name = "New Item";
public string name = "New Item";
public string uuid = "new_item";
public Sprite icon = null;
public bool isDefaultItem = false;
public int count = 1;
public Item(int c = 1)
{
count = c;
}
}

View File

@@ -4,33 +4,69 @@ using UnityEngine;
public class ResourceManager: MonoBehaviour
{
[SerializeField] private List<Item> itemList;
[SerializeField] private GameObject[] storageBuildings;
// Count All Resources of all storage buildings
/*
public void Remove(Item item)
private void Start()
{
itemList.Remove(item);
storageBuildings = GameObject.FindGameObjectsWithTag("StorageBuilding");
}
public void Add(Item item)
/*
private void Update()
{
itemList.Add(item);
if (Input.GetKeyDown(KeyCode.K))
{
Item wood = new WoodItem(10);
storageBuildings[0].GetComponent<StorageBuilding>().Add(wood);
}
if (Input.GetKeyDown(KeyCode.I))
{
Item stone = new StoneItem(12);
storageBuildings[0].GetComponent<StorageBuilding>().Add(stone);
}
if (Input.GetKeyDown(KeyCode.L))
{
GetAllResources();
}
}*/
public int Count(Item item)
{
int count = 0;
foreach(Item i in itemList)
public List<Item> GetAllResources()
{
List<Item> inventory = new List<Item>();
//F<>r jedes StorageBuilding
foreach(GameObject b in storageBuildings)
{
if(i == item)
List<Item> buildingInv = b.GetComponent<StorageBuilding>().Getinventory();
//Add items to already existing item +=
foreach (Item item in buildingInv)
{
foreach(Item i in inventory)
{
if(i.uuid == item.uuid)
{
i.count += item.count;
buildingInv.Remove(item);
}
}
}
//Add den Rest
foreach(Item item in buildingInv)
{
count += 1;
inventory.Add(item);
}
}
return count;
/*
Debug.Log(inventory);
Debug.Log(inventory[0].count);
Debug.Log(inventory[1].count);*/
return inventory;
}
}