diff --git a/Assets/Scripts/Harvestable.cs b/Assets/Scripts/Harvestable.cs new file mode 100644 index 0000000..a001d35 --- /dev/null +++ b/Assets/Scripts/Harvestable.cs @@ -0,0 +1,44 @@ +using UnityEngine; + +/// +/// Harvestable represents the base class of all harvestable objects in the scene and inherits of "Interactable.cs" +/// +public abstract class Harvestable : Interactable +{ + [Header("Harvestable Properties")] + [Tooltip("The time for how long the object needs to be harvested.")] + [Range(0.1f, 99.9f)] + [SerializeField] + float harvestDuration = 3f; + + float harvestTime = 0f;// time for how long the player already "harvested" the object + + /// + /// IncreaseHarvestTime increases the harvestTime by Time.deltaTime. + /// harvestTime += Time.deltaTime; + /// + public void IncreaseHarvestTime() => harvestTime += Time.deltaTime; + + /// + /// ResetHarvestTime resets the harvestTime to 0f. + /// + public void ResetHarvestTime() => harvestTime = 0f; + + /// + /// GetHarvestTime gets the time for how long the player has already "harvested". + /// + /// A float of the time for how long the player has already "harvested" + public float GetHarvestTime() => harvestTime; + + /// + /// GetHarvestTimeLeft gets the time for how long a player still has to "harvest". + /// + /// A float containig the result of harvestDuration - harvestTime + public float GetHarvestTimeLeft() => harvestDuration - harvestTime; + + /// + /// GetHarvestDuration gets the time for how long the object needs to be harvested until the interact method triggers. + /// + /// A float of the time for how long the object needs to be harvested until the interact method triggers + public float GetHarvestDuration() => harvestDuration; +} diff --git a/Assets/Scripts/Harvestable.cs.meta b/Assets/Scripts/Harvestable.cs.meta new file mode 100644 index 0000000..df0ec58 --- /dev/null +++ b/Assets/Scripts/Harvestable.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 05a54d359a6f37f4096b2163dcfeaaea +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Interactable.cs b/Assets/Scripts/Interactable.cs index 31eb045..071f3b7 100644 --- a/Assets/Scripts/Interactable.cs +++ b/Assets/Scripts/Interactable.cs @@ -1,29 +1,112 @@ using UnityEngine; -// Represents the base class for all interactable objects in the scene +/// +/// Interactable represents the base class of all interactable objects in the scene +/// public abstract class Interactable : MonoBehaviour { - + + /// + /// InteractinType is the type of an interaction. So the "PlayerInteractin.cs" can handle the interaction input. + /// + /// + /// Click: A single click which triggers the Interact Method + /// Hold: Holding a until time is reached an then triggers the Interact Method(Time resets if the Player stops holding) + /// Harvest: Same as Hold but time does not reset. So the time progress gets saved. Used for class "Harvastable.cs" + /// + /// public enum InteractionType { Click, - Hold + Hold, + Harvest } - - float holdTime; + [Header("Interactable Properties")] + public InteractionType interactionType; + + [Tooltip("The time the player has to hold until the Interact method triggers.")] + [Range(0.1f, 99.9f)] + [SerializeField] + float holdDuration = 1f; // The time the player has to hold until the Interact method triggers + float holdTime; // The time for how long the player has already pressed + + // Used to measure the distance between the player and the object Transform playerTransform; Transform interactableTransform; - public InteractionType interactionType; - public float radius = 3f; + [Tooltip("The range in which the player can interact with an object")] + [Range(1f, 50f)] + [SerializeField] + float radius = 3f; + #region GETTER + /// + /// GetDescription gets the description of an interactable object. + /// + /// The description is usally used to display a help text for the player (e.g. "Turn Lights On") + /// + /// + /// A string containing the description of the interactable object public abstract string GetDescription(); + + /// + /// GetHoldTime gets the time for how long the player has already pressed. + /// + /// A float of the time for how long the player has already pressed + public float GetHoldTime() => holdTime; + + /// + /// GetHoldDuration gets the time the player has to hold until the Interact method triggers. + /// + /// A float of the time the player has to hold until the Interact method triggers + public float GetHoldDuration() => holdDuration; + + /// + /// GetHoldTimeLeft gets the time for how long a player still has to hold. + /// + /// A float containig the result of holdDuration - holdTime + public float GetHoldTimeLeft() => holdDuration - holdTime; + + /// + /// GetRadius gets the maximum distance of a player is allowed to have in order to interact with an object. + /// + /// A float containing the interaction distance + public float GetRadius() => radius; + #endregion + + /// + /// Interact is the method which gets called when a player start the interaction with an object. + /// + /// + /// If the player clicks on an object + /// If the holdTime is greater or equals the holdDuration + /// If the harvestTime is greater or equals the harvestDuration + /// + /// + /// It usually gets called in the "PlayerInteraction.cs" + /// public abstract void Interact(); + /// + /// IncreaseHoldTime increases the holdTime by Time.deltaTime. + /// holdTime += Time.deltaTime; + /// public void IncreaseHoldTime() => holdTime += Time.deltaTime; + + /// + /// ResetHoldTime resets the holdTime to 0f. + /// public void ResetHoldTime() => holdTime = 0f; - public float GetHoldTime() => holdTime; - + /// + /// isInRange checks if the player is in the interaction range of an interactable object. + /// + /// + /// True: Player is in range + /// False: Player is NOT in range + /// + /// + + /// A bool which says if the player is in the interaction range of an interactable object public bool isInRange() { playerTransform = GameObject.FindGameObjectWithTag("Player").gameObject.transform; // Maybe singleton later? @@ -41,6 +124,8 @@ public abstract class Interactable : MonoBehaviour } + + // Show interactable range in editor but NOT IN-GAME private void OnDrawGizmosSelected() { // Gizmos are only visible in the scene view -> NOT visible IN-GAME (DEBUG Reasons) diff --git a/Assets/Scripts/PlayerInteraction.cs b/Assets/Scripts/PlayerInteraction.cs index be2112f..aed2b0b 100644 --- a/Assets/Scripts/PlayerInteraction.cs +++ b/Assets/Scripts/PlayerInteraction.cs @@ -30,12 +30,17 @@ public class PlayerInteraction : MonoBehaviour } } + void HandleInteractionText(Interactable interactable) { interactionText.text = interactable.GetDescription(); interactionText.transform.position = new Vector3(Input.mousePosition.x + interactionText.rectTransform.sizeDelta.x / 2 + 20, Input.mousePosition.y - 5, Input.mousePosition.z); - } + + /// + /// HandleInteraction handles the player interaction based on the Interactable.InteractionType. + /// + /// void HandleInteraction(Interactable interactable) { switch (interactable.interactionType) @@ -51,7 +56,7 @@ public class PlayerInteraction : MonoBehaviour { interactable.IncreaseHoldTime(); - if(interactable.GetHoldTime() > 1f) + if(interactable.GetHoldTime() > interactable.GetHoldDuration()) { interactable.Interact(); interactable.ResetHoldTime(); @@ -62,6 +67,19 @@ public class PlayerInteraction : MonoBehaviour interactable.ResetHoldTime(); } break; + case Interactable.InteractionType.Harvest: + Harvestable harvestable = interactable.GetComponent(); + Debug.Log(harvestable.GetHarvestTimeLeft()); + if (Input.GetButton("Interact")) + { + harvestable.IncreaseHarvestTime(); + + if (harvestable.GetHarvestTime() >= harvestable.GetHarvestDuration()) + { + harvestable.Interact(); + } + } + break; default: throw new System.Exception("Unsupported type of interactable"); } diff --git a/Assets/Scripts/TreeInteraction.cs b/Assets/Scripts/TreeInteraction.cs index 134b13b..62b19d1 100644 --- a/Assets/Scripts/TreeInteraction.cs +++ b/Assets/Scripts/TreeInteraction.cs @@ -2,8 +2,9 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; -public class TreeInteraction : Interactable +public class TreeInteraction : Harvestable { + public override string GetDescription() { if (isInRange()) @@ -13,9 +14,6 @@ public class TreeInteraction : Interactable } public override void Interact() { - if (isInRange()) - Debug.Log("AaaaaaaaaaAaAAaAaAAaAaAaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"); - else - Debug.Log("Tree is not in range"); + Debug.Log("Harvest BAUM"); } } \ No newline at end of file