mirror of
https://github.com/DerTyp7/example-top-down-unity.git
synced 2026-07-31 15:29:02 +02:00
can interact if player has tool selected (in hand))
This commit is contained in:
8
Assets/Scripts/Interaction.meta
Normal file
8
Assets/Scripts/Interaction.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6acbaf264fa7d1f4eace556be331ac5a
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -10,7 +10,9 @@ public abstract class Harvestable : Interactable
|
||||
[Range(0.1f, 99.9f)]
|
||||
[SerializeField]
|
||||
float harvestDuration = 3f;
|
||||
|
||||
|
||||
public ToolType toolType;
|
||||
|
||||
float harvestTime = 0f;// time for how long the player already "harvested" the object
|
||||
|
||||
/// <summary>
|
||||
@@ -81,14 +81,20 @@ public class PlayerInteraction : MonoBehaviour
|
||||
break;
|
||||
case Interactable.InteractionType.Harvest:
|
||||
Harvestable harvestable = interactable.GetComponent<Harvestable>();
|
||||
ToolItem toolItem = (ToolItem)GetComponent<InventoryController>().selectedItem;
|
||||
|
||||
if (Input.GetButton("Interact") && interactable.isInRange())
|
||||
bool isCorrectTool = (toolItem != null && toolItem.toolType == harvestable.toolType);
|
||||
|
||||
|
||||
|
||||
if (Input.GetButton("Interact") && interactable.isInRange() && isCorrectTool)
|
||||
{
|
||||
harvestable.IncreaseHarvestTime();
|
||||
|
||||
if (harvestable.GetHarvestTime() >= harvestable.GetHarvestDuration())
|
||||
{
|
||||
harvestable.Interact();
|
||||
harvestable.ResetHarvestTime();
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -2,7 +2,14 @@ using UnityEngine;
|
||||
|
||||
public class TreeInteraction : Harvestable
|
||||
{
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
toolType = ToolType.AXE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override string GetDescription()
|
||||
{
|
||||
if (isInRange())
|
||||
@@ -7,4 +7,9 @@ public class FoodItem : Item
|
||||
public int health;
|
||||
public int water;
|
||||
public int regeneration;
|
||||
|
||||
public override void OnSelect()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ public class Inventory : MonoBehaviour
|
||||
public delegate void OnItemChanged();
|
||||
public OnItemChanged onItemChangedCallback; //
|
||||
|
||||
[SerializeField]
|
||||
List<Slot> inventory = new List<Slot>();
|
||||
|
||||
public List<Slot> getInventory { get => inventory;}
|
||||
@@ -33,7 +34,6 @@ public class Inventory : MonoBehaviour
|
||||
// Initializes the inventory with a specific number of slots and slotsize.
|
||||
// !!!Has to be called bevore adding any items!!!
|
||||
// Example createEmptyInventory(5, 10) : 5 Slots with space for 10 items each
|
||||
inventory = new List<Slot>();
|
||||
for (int i = 0; i < numberOfSlots; i++)
|
||||
{
|
||||
inventory.Add(new Slot(maxSpaceOfSlots));
|
||||
|
||||
@@ -6,15 +6,20 @@ public class InventoryController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] Item item1;// not needed
|
||||
[SerializeField] Item item2;// not needed
|
||||
public Item selectedItem;
|
||||
|
||||
Item testItem;
|
||||
public Inventory inventory;
|
||||
[SerializeField] UI_Inventory uiInventory;
|
||||
private void Awake()
|
||||
{
|
||||
inventory = transform.GetComponent<Inventory>();
|
||||
inventory.createEmptyInventory(8,10);
|
||||
|
||||
|
||||
testItem = Instantiate(item1);
|
||||
|
||||
inventory.addItemAt(0, item1, 8);
|
||||
inventory.addItemAt(0, item1, 1);
|
||||
inventory.addItemAt(0, testItem, 1);
|
||||
inventory.addItemAt(3, item2, 15);
|
||||
inventory.addItemAt(4, item1, 3);
|
||||
|
||||
@@ -24,7 +29,9 @@ public class InventoryController : MonoBehaviour
|
||||
Debug.Log(inventory.removeItemAt(0, 10));
|
||||
Debug.Log(inventory.getInventory[0].Count);
|
||||
*/
|
||||
uiInventory.setInventory(inventory);
|
||||
|
||||
|
||||
// uiInventory.setInventory(inventory);
|
||||
}
|
||||
void Start()
|
||||
{
|
||||
@@ -34,6 +41,14 @@ public class InventoryController : MonoBehaviour
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
// FOR DEBUG
|
||||
if (Input.GetKeyDown(KeyCode.X))
|
||||
{
|
||||
testItem.Select();
|
||||
if(testItem.isSelected)
|
||||
selectedItem = testItem;
|
||||
else
|
||||
selectedItem = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,19 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(fileName = "Item", menuName = "Items/Item", order = 1)]
|
||||
public class Item : ScriptableObject
|
||||
public abstract class Item : ScriptableObject
|
||||
{
|
||||
public new string name;
|
||||
public int id;
|
||||
public bool isStackable;
|
||||
public Sprite sprite;
|
||||
public bool isSelected;
|
||||
|
||||
public abstract void OnSelect();
|
||||
|
||||
public void Select()
|
||||
{
|
||||
isSelected = !isSelected;
|
||||
OnSelect();
|
||||
}
|
||||
}
|
||||
|
||||
8
Assets/Scripts/Inventory/Items/Tools.meta
Normal file
8
Assets/Scripts/Inventory/Items/Tools.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 07c440f0b142b6c43818e32e4a43686c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
20
Assets/Scripts/Inventory/Items/Tools/Stick.asset
Normal file
20
Assets/Scripts/Inventory/Items/Tools/Stick.asset
Normal 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: Stick
|
||||
m_EditorClassIdentifier:
|
||||
name: stick
|
||||
id: 10
|
||||
isStackable: 0
|
||||
sprite: {fileID: 21300000, guid: 8994265d0b3152b4cb238bdf03e8d440, type: 3}
|
||||
isSelected: 0
|
||||
toolType: 0
|
||||
8
Assets/Scripts/Inventory/Items/Tools/Stick.asset.meta
Normal file
8
Assets/Scripts/Inventory/Items/Tools/Stick.asset.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cada8a20c35f0bd4a93d92e1152e5625
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 11400000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
11
Assets/Scripts/Inventory/ResourceItem.cs
Normal file
11
Assets/Scripts/Inventory/ResourceItem.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
[CreateAssetMenu(fileName = "FoodItem", menuName = "Items/FoodItem", order = 2)]
|
||||
public class ResourceItem : Item
|
||||
{
|
||||
public override void OnSelect()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Inventory/ResourceItem.cs.meta
Normal file
11
Assets/Scripts/Inventory/ResourceItem.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0fae7bba50a98204883e78d8d6c96fc2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
26
Assets/Scripts/Inventory/ToolItem.cs
Normal file
26
Assets/Scripts/Inventory/ToolItem.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(fileName = "ToolItem", menuName = "Items/Tool", order = 2)]
|
||||
public class ToolItem : Item
|
||||
{
|
||||
|
||||
|
||||
public ToolType toolType;
|
||||
float durability = 1f;
|
||||
|
||||
SpriteRenderer playerHandSpriteRenderer;
|
||||
|
||||
public override void OnSelect()
|
||||
{
|
||||
playerHandSpriteRenderer = GameObject.Find("Player").transform.Find("Hand").gameObject.GetComponent<SpriteRenderer>();
|
||||
if (isSelected)
|
||||
{
|
||||
playerHandSpriteRenderer.GetComponent<SpriteRenderer>().sprite = sprite;
|
||||
}
|
||||
else
|
||||
{
|
||||
playerHandSpriteRenderer.GetComponent<SpriteRenderer>().sprite = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Inventory/ToolItem.cs.meta
Normal file
11
Assets/Scripts/Inventory/ToolItem.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c9a450f431b33414ab3a1f3b08250700
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
11
Assets/Scripts/Inventory/ToolType.cs
Normal file
11
Assets/Scripts/Inventory/ToolType.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public enum ToolType
|
||||
{
|
||||
AXE,
|
||||
PICKAXE,
|
||||
HOE,
|
||||
}
|
||||
|
||||
11
Assets/Scripts/Inventory/ToolType.cs.meta
Normal file
11
Assets/Scripts/Inventory/ToolType.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4f9fcb5700fbcfa4d93940e88f197200
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user