This commit is contained in:
j.mei7
2022-02-22 21:31:04 +01:00
parent 7d055be8d1
commit f5f3154c95
12 changed files with 231 additions and 134 deletions

View File

@@ -920,6 +920,7 @@ GameObject:
- component: {fileID: 310196958}
- component: {fileID: 310196957}
- component: {fileID: 310196963}
- component: {fileID: 310196964}
m_Layer: 3
m_Name: Player
m_TagString: Player
@@ -1114,9 +1115,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: ee2fcb6d299019740bbede78bbc6a1d7, type: 3}
m_Name:
m_EditorClassIdentifier:
testItemType: {fileID: 11400000, guid: e04039a6cbed5e5439b605aeee90485f, type: 2}
numberOfSlots: 20
maxSpaceOfSlots: 60
isPlayerInventory: 1
inventory: []
--- !u!95 &310196963
Animator:
@@ -1138,6 +1139,19 @@ Animator:
m_HasTransformHierarchy: 1
m_AllowConstantClipSamplingOptimization: 1
m_KeepAnimatorControllerStateOnDisable: 0
--- !u!114 &310196964
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 310196950}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: cff3b5ce3863a7442a86b400700ff131, type: 3}
m_Name:
m_EditorClassIdentifier:
selectedSlotIndex: 0
--- !u!1 &360525550
GameObject:
m_ObjectHideFlags: 0
@@ -6335,8 +6349,11 @@ MonoBehaviour:
interactionType: 2
holdDuration: 1
radius: 3
centerPoint: {fileID: 0}
harvestDuration: 3
centerPoint: {fileID: 1335747188}
harvestDuration: 1
toolType: 0
isHarvested: 0
dropItem: {fileID: 11400000, guid: e6de4c0f524fecd418672a51c89d547e, type: 2}
--- !u!1 &1357469793
GameObject:
m_ObjectHideFlags: 0

View File

@@ -13,6 +13,9 @@ public abstract class Harvestable : Interactable
float harvestTime = 0f;// time for how long the player already "harvested" the object
public ToolType toolType = ToolType.AXE;
public bool isHarvested = false;
/// <summary>
/// <c>IncreaseHarvestTime</c> increases the harvestTime by Time.deltaTime.
/// <code>harvestTime += Time.deltaTime;</code>

View File

@@ -30,6 +30,8 @@ public class Inventory : MonoBehaviour
[SerializeField]
List<Slot> inventory = new List<Slot>();
public static Inventory PlayerInstance { get; private set; } // instance of PlayerInventory
public List<Slot> GetInventory() => inventory;
/// <summary>
@@ -119,7 +121,7 @@ public class Inventory : MonoBehaviour
/// </summary>
/// <param name="index"></param>
/// <returns>True: Is in range. False: Is not in range.</returns>
bool IndexIsInRange(int index)
public bool IndexIsInRange(int index)
{
// Returns true if a given index is in the bounds of the inventory.
// Example (maxSize = 10) index = -10 : false , index = 100: false, index = 7 : true
@@ -260,9 +262,12 @@ public class Inventory : MonoBehaviour
}
void Start()
void Awake()
{
// Initialize Inventory Slots
AddSlots(numberOfSlots);
AddSlots(numberOfSlots);
if (isPlayerInventory)
PlayerInstance = this;
}
}

View File

@@ -4,35 +4,39 @@ using UnityEngine;
public class InventoryController : MonoBehaviour
{
/*[SerializeField] Item item1;// not needed
[SerializeField] Item item2;// not needed
public Inventory inventory;
[SerializeField] UI_Inventory uiInventory;
private void Awake()
{
inventory = transform.GetComponent<Inventory>();
inventory.createEmptyInventory(8,10);
inventory.addItemAt(0, item1, 8);
inventory.addItemAt(0, item1, 1);
inventory.addItemAt(3, item2, 15);
inventory.addItemAt(4, item1, 3);
[SerializeField]
int selectedSlotIndex;
Debug.Log(inventory.addItemAt(0, item2, 15));
Debug.Log(inventory.getInventory[0].Count);
Debug.Log(inventory.removeItemAt(0, 10));
Debug.Log(inventory.getInventory[0].Count);
uiInventory.setInventory(inventory);
Inventory inventory;
public static InventoryController PlayerInstance { get; private set; }
public Slot GetSelectedSlot()
{
return inventory.GetInventory()[selectedSlotIndex];
}
void Start()
{
PlayerInstance = this;
inventory = Inventory.PlayerInstance;
selectedSlotIndex = 0;
}
// Update is called once per frame
void Update()
{
}*/
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (inventory.IndexIsInRange(selectedSlotIndex - 1))
{
selectedSlotIndex -= 1;
}
}
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (inventory.IndexIsInRange(selectedSlotIndex + 1))
{
selectedSlotIndex += 1;
}
}
}
}

View File

@@ -0,0 +1,20 @@
%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: c9a450f431b33414ab3a1f3b08250700, type: 3}
m_Name: Axe
m_EditorClassIdentifier:
name: Axe
id: 3
isStackable: 0
sprite: {fileID: 21300000, guid: 475f050511847c0499c1ca4f47fcaefa, type: 3}
isSelected: 0
toolType: 0

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 83d7c41047fbb504bb67f024912542e6
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,19 @@
%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: 0fae7bba50a98204883e78d8d6c96fc2, type: 3}
m_Name: Wood
m_EditorClassIdentifier:
name: Wood
id: 2
isStackable: 1
sprite: {fileID: 0}
isSelected: 0

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e6de4c0f524fecd418672a51c89d547e
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -81,14 +81,17 @@ public class PlayerInteraction : MonoBehaviour
break;
case Interactable.InteractionType.Harvest:
Harvestable harvestable = interactable.GetComponent<Harvestable>();
ToolItem toolItem = (ToolItem)InventoryController.PlayerInstance.GetSelectedSlot().GetItem();
bool isCorrectTool = (toolItem != null && toolItem.toolType == harvestable.toolType);
if (Input.GetButton("Interact") && interactable.isInRange())
if (Input.GetButton("Interact") && interactable.isInRange() && isCorrectTool)
{
harvestable.IncreaseHarvestTime();
if (harvestable.GetHarvestTime() >= harvestable.GetHarvestDuration())
{
harvestable.Interact();
harvestable.ResetHarvestTime();
}
}
break;

View File

@@ -1,7 +1,11 @@
using UnityEngine;
public class TreeInteraction : Harvestable
{
[SerializeField]
Item dropItem;
public override string GetDescription()
{
@@ -12,6 +16,12 @@ public class TreeInteraction : Harvestable
}
public override void Interact()
{
Debug.Log("Harvest BAUM");
if (!isHarvested)
{
Debug.Log("Harvest BAUM");
Inventory.PlayerInstance.Add(dropItem, Random.Range(1, 10));
isHarvested = true;
}
}
}