Homebound / Scripts / InteractionsAndObstacles / Interaction.cs
Interaction.cs
Raw
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

/// <summary>
/// Interactble fields and objects functionality
/// </summary>
public class Interaction : MonoBehaviour
{
    public enum InteractionTypes
    {
        Launcher,                   // Launches player from a point
        Placeholder_One,            // N/A, TBD
        Placeholder_Two,            // N/A, TBD
        Placeholder_Three           // N/A, TBD
    }

    public InteractionTypes interactionType = InteractionTypes.Launcher;    // Initialized type to Launcher

    // Launcher Properties
    [SerializeField] GameObject launchPoint;                                // Launch point for the player
    [SerializeField] float power = 1000.0f;                                 // Force behind the launch   
    [SerializeField] Vector3 direction = Vector3.forward;                   // Direction the launch will occur

    /// <summary>
    /// When the player touches the interacting area, their rigibody is passed and depending on the type Enum, will determine the functionality
    /// </summary>
    /// <param name="rb">Rigidbody that is being interacted with</param>
    public void Interact(Rigidbody rb)
    {
        switch (interactionType)
        {
            case InteractionTypes.Launcher:
                Launch(rb); 
                break;
            default:
                break;
        }
    }

    /// <summary>
    /// Rockets the player from the launch origin at a fast speed
    /// </summary>
    /// <param name="rb">Player's rigidbody</param>
    void Launch(Rigidbody rb)
    {
        rb.transform.position = launchPoint.transform.position;

        rb.AddForce(direction * power);
    }

    /// <summary>
    /// Draws arrow in the direction of the launch
    /// </summary>
    void OnDrawGizmos()
    {
        if (interactionType == InteractionTypes.Launcher && direction.magnitude != 0)
            DrawArrow.ForGizmo(launchPoint.transform.position, direction, Color.yellow);
    }
}

/// <summary>
/// Edits the inspector properties for the Interaction Script, however since only one type of interaction is available only the Launcher is really seen
/// </summary>
#if UNITY_EDITOR
[CustomEditor(typeof(Interaction))]
public class InteractionEditor : Editor
{
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        Interaction interaction = (Interaction)target;

        interaction.interactionType = (Interaction.InteractionTypes)EditorGUILayout.EnumPopup("Type of Interaction", interaction.interactionType);

        switch (interaction.interactionType)
        {
            case Interaction.InteractionTypes.Launcher:
                {
                    EditorGUILayout.LabelField("Launcher Parameters", EditorStyles.boldLabel);
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("launchPoint"));
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("power"));
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("direction"));
                    break;
                }
            default:
                break;
        }

        serializedObject.ApplyModifiedProperties();
    }
}
#endif