Added Documentation

This commit is contained in:
janis
2022-02-15 16:02:22 +01:00
parent 93baa58c15
commit c485401c58
5 changed files with 172 additions and 16 deletions

View File

@@ -0,0 +1,44 @@
using UnityEngine;
/// <summary>
/// <c>Harvestable</c> represents the base class of all harvestable objects in the scene and inherits of "Interactable.cs"
/// </summary>
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
/// <summary>
/// <c>IncreaseHarvestTime</c> increases the harvestTime by Time.deltaTime.
/// <code>harvestTime += Time.deltaTime;</code>
/// </summary>
public void IncreaseHarvestTime() => harvestTime += Time.deltaTime;
/// <summary>
/// <c>ResetHarvestTime</c><strong> resets the harvestTime to 0f.</strong>
/// </summary>
public void ResetHarvestTime() => harvestTime = 0f;
/// <summary>
/// <c>GetHarvestTime</c> gets the time for how long the player has already "harvested".
/// </summary>
/// <returns><strong>A float of the time for how long the player has already "harvested"</strong></returns>
public float GetHarvestTime() => harvestTime;
/// <summary>
/// <c>GetHarvestTimeLeft</c> gets the time for how long a player still has to "harvest".
/// </summary>
/// <returns>A float containig the result of <strong>harvestDuration - harvestTime</strong></returns>
public float GetHarvestTimeLeft() => harvestDuration - harvestTime;
/// <summary>
/// <c>GetHarvestDuration</c> gets the time for how long the object needs to be harvested until the interact method triggers.
/// </summary>
/// <returns><strong>A float of the time for how long the object needs to be harvested until the interact method triggers</strong></returns>
public float GetHarvestDuration() => harvestDuration;
}

View File

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

View File

@@ -1,29 +1,112 @@
using UnityEngine; using UnityEngine;
// Represents the base class for all interactable objects in the scene /// <summary>
/// <c>Interactable</c> represents the base class of all interactable objects in the scene
/// </summary>
public abstract class Interactable : MonoBehaviour public abstract class Interactable : MonoBehaviour
{ {
/// <summary>
/// <c>InteractinType</c> is the type of an interaction. So the "PlayerInteractin.cs" can handle the interaction input.
///
/// <list type="bullet">
/// <item>Click: A single click which triggers the Interact Method</item>
/// <item>Hold: Holding a until time is reached an then triggers the Interact Method(Time resets if the Player stops holding)</item>
/// <item>Harvest: Same as Hold but time does not reset. So the time progress gets saved. Used for class "Harvastable.cs"</item>
/// </list>
/// </summary>
public enum InteractionType { public enum InteractionType {
Click, Click,
Hold Hold,
Harvest
} }
[Header("Interactable Properties")]
float holdTime; 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 playerTransform;
Transform interactableTransform; Transform interactableTransform;
public InteractionType interactionType; [Tooltip("The range in which the player can interact with an object")]
public float radius = 3f; [Range(1f, 50f)]
[SerializeField]
float radius = 3f;
#region GETTER
/// <summary>
/// <c>GetDescription</c> gets the description of an interactable object.
/// <list type="bullet">
/// <item>The description is usally used to display a help text for the player (e.g. "Turn Lights On")</item>
/// </list>
/// </summary>
/// <returns><strong>A string containing the description of the interactable object</strong></returns>
public abstract string GetDescription(); public abstract string GetDescription();
/// <summary>
/// <c>GetHoldTime</c> gets the time for how long the player has already pressed.
/// </summary>
/// <returns><strong>A float of the time for how long the player has already pressed</strong></returns>
public float GetHoldTime() => holdTime;
/// <summary>
/// <c>GetHoldDuration</c> gets the time the player has to hold until the Interact method triggers.
/// </summary>
/// <returns><strong>A float of the time the player has to hold until the Interact method triggers</strong></returns>
public float GetHoldDuration() => holdDuration;
/// <summary>
/// <c>GetHoldTimeLeft</c> gets the time for how long a player still has to hold.
/// </summary>
/// <returns>A float containig the result of <strong>holdDuration - holdTime</strong></returns>
public float GetHoldTimeLeft() => holdDuration - holdTime;
/// <summary>
/// <c>GetRadius</c> gets the maximum distance of a player is allowed to have in order to interact with an object.
/// </summary>
/// <returns><strong>A float containing the interaction distance</strong></returns>
public float GetRadius() => radius;
#endregion
/// <summary>
/// <c>Interact</c> is the method which gets called when a player start the interaction with an object.
///
/// <list type="bullet">
/// <item>If the player clicks on an object</item>
/// <item>If the holdTime is greater or equals the holdDuration</item>
/// <item>If the harvestTime is greater or equals the harvestDuration</item>
/// </list>
///
/// It usually gets called in the "PlayerInteraction.cs"
/// </summary>
public abstract void Interact(); public abstract void Interact();
/// <summary>
/// <c>IncreaseHoldTime</c> increases the holdTime by Time.deltaTime.
/// <code>holdTime += Time.deltaTime;</code>
/// </summary>
public void IncreaseHoldTime() => holdTime += Time.deltaTime; public void IncreaseHoldTime() => holdTime += Time.deltaTime;
/// <summary>
/// <c>ResetHoldTime</c><strong> resets the holdTime to 0f.</strong>
/// </summary>
public void ResetHoldTime() => holdTime = 0f; public void ResetHoldTime() => holdTime = 0f;
public float GetHoldTime() => holdTime; /// <summary>
/// <c>isInRange</c> checks if the player is in the interaction range of an interactable object.
///
/// <list type="bullet">
/// <item><strong>True</strong>: Player is in range</item>
/// <item><strong>False</strong>: Player is NOT in range</item>
/// </list>
/// </summary>
/// <returns>A bool which says if the player is in the interaction range of an interactable object</returns>
public bool isInRange() public bool isInRange()
{ {
playerTransform = GameObject.FindGameObjectWithTag("Player").gameObject.transform; // Maybe singleton later? 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() private void OnDrawGizmosSelected()
{ {
// Gizmos are only visible in the scene view -> NOT visible IN-GAME (DEBUG Reasons) // Gizmos are only visible in the scene view -> NOT visible IN-GAME (DEBUG Reasons)

View File

@@ -30,12 +30,17 @@ public class PlayerInteraction : MonoBehaviour
} }
} }
void HandleInteractionText(Interactable interactable) void HandleInteractionText(Interactable interactable)
{ {
interactionText.text = interactable.GetDescription(); 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); interactionText.transform.position = new Vector3(Input.mousePosition.x + interactionText.rectTransform.sizeDelta.x / 2 + 20, Input.mousePosition.y - 5, Input.mousePosition.z);
} }
/// <summary>
/// <c>HandleInteraction</c> <strong>handles the player interaction based on the Interactable.InteractionType.</strong>
/// </summary>
/// <param name="interactable"></param>
void HandleInteraction(Interactable interactable) void HandleInteraction(Interactable interactable)
{ {
switch (interactable.interactionType) switch (interactable.interactionType)
@@ -51,7 +56,7 @@ public class PlayerInteraction : MonoBehaviour
{ {
interactable.IncreaseHoldTime(); interactable.IncreaseHoldTime();
if(interactable.GetHoldTime() > 1f) if(interactable.GetHoldTime() > interactable.GetHoldDuration())
{ {
interactable.Interact(); interactable.Interact();
interactable.ResetHoldTime(); interactable.ResetHoldTime();
@@ -62,6 +67,19 @@ public class PlayerInteraction : MonoBehaviour
interactable.ResetHoldTime(); interactable.ResetHoldTime();
} }
break; break;
case Interactable.InteractionType.Harvest:
Harvestable harvestable = interactable.GetComponent<Harvestable>();
Debug.Log(harvestable.GetHarvestTimeLeft());
if (Input.GetButton("Interact"))
{
harvestable.IncreaseHarvestTime();
if (harvestable.GetHarvestTime() >= harvestable.GetHarvestDuration())
{
harvestable.Interact();
}
}
break;
default: default:
throw new System.Exception("Unsupported type of interactable"); throw new System.Exception("Unsupported type of interactable");
} }

View File

@@ -2,8 +2,9 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class TreeInteraction : Interactable public class TreeInteraction : Harvestable
{ {
public override string GetDescription() public override string GetDescription()
{ {
if (isInRange()) if (isInRange())
@@ -13,9 +14,6 @@ public class TreeInteraction : Interactable
} }
public override void Interact() public override void Interact()
{ {
if (isInRange()) Debug.Log("Harvest BAUM");
Debug.Log("AaaaaaaaaaAaAAaAaAAaAaAaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
else
Debug.Log("Tree is not in range");
} }
} }