Gameplay.BarrelButton - robblofield/TomeboundDocs GitHub Wiki

Gameplay.BarrelButton

Author: Shahrzad Beevis
Last Updated: 14/05/2024

Overview

A script that controls the barrel dispenser, spawning and animating a barrel.

Dependencies

Prefab.Barrel

Contents

  • Use case
  • Breakdown of code
    • Using Statements
    • Class Declaration
    • Variables
    • Method/Awake()
    • Method/OnEnable()
    • Method/OnDisable()
    • Method/OnTriggerEnter(Collider other)
    • Method/OnTriggerExit(Collider other)
    • Method/PressButton()
    • Method/SpawnBarrel()
    • Method/DestroyCurrentBarrel()
    • Method/DeactivateAnimator()
  • Future Expansion
  • Full Code Reference

Use case

This script is used to facilitate the interactivity of the barrel dispenser button.

Breakdown of Code

Using Statements

using UnityEngine;

Class Declaration

public class BarrelButton : MonoBehaviour

Variables

public GameObject barrelPrefab;
private GameObject currentBarrel;

private PlayerInputActions playerActions;

private bool playerInRange = false;

public GameObject barrelPrefab:

This variable holds the template for the barrel that will be spawned, which assigned within the inspector.

private GameObject currentBarrel:

This variable stores the currently spawned barrel instance.

private PlayerInputActions playerActions:

This variable holds an instance of the PlayerInputActions class, which is responsible for managing player input actions within the script.

private bool playerInRange = false:

This variable, playerInRange, is a boolean flag used to track whether the player is within the interaction range of the button. It is initially set to false indicating that the player is not in range.

Awake()

private void Awake()
{
    playerActions = new PlayerInputActions();
    playerActions.Player.ObjectInteraction.performed += ctx => PressButton();
}

This method is called during the initialization of the script instance. It creates a new instance of PlayerInputActions and assigns it to the playerActions variable. Additionally, it subscribes to the "ObjectInteraction" action performed by the player, linking it to the PressButton() method.

OnEnable()

    private void OnEnable()
{
    playerActions.Player.Enable();
}

Invoked when the object becomes enabled and active. It enables the player input actions, allowing interaction with the button.

OnDisable()

private void OnDisable()
{
    playerActions.Player.Disable();
}

Executed when the object becomes disabled or inactive. It disables the player input actions, preventing further interaction with the button.

OnTriggerEnter(Collider other)

void OnTriggerEnter(Collider other)
{
    if (other.CompareTag("Player"))
    {
        playerInRange = true;
    }
}

This method is called when another collider enters the trigger collider attached to the button. It checks if the collider belongs to the player ("Player" tag) and sets the playerInRange flag to true if it does.

OnTriggerExit(Collider other)

void OnTriggerExit(Collider other)
{
    if (other.CompareTag("Player"))
    {
        playerInRange = false;
    }
}

Activated when another collider exits the trigger collider attached to the button. It verifies if the collider belongs to the player ("Player" tag) and sets the playerInRange flag to false accordingly.

PressButton()

void PressButton()
{
    if (currentBarrel == null && playerInRange)
    {
        SpawnBarrel();
    }
}

Called upon player interaction with the button. It confirms if there's no existing barrel (currentBarrel == null) and if the player is in range (playerInRange is true). If both conditions are met, it invokes the SpawnBarrel() method to create a new barrel.

SpawnBarrel()

void SpawnBarrel()
{
    currentBarrel = Instantiate(barrelPrefab, transform.position, Quaternion.Euler(-90f, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z));

    Animator barrelAnimator = currentBarrel.GetComponent<Animator>();

    if (barrelAnimator != null)
    {
        barrelAnimator.SetTrigger("buttonPressed");

        barrelAnimator.GetComponent<AnimationEventReceiver>().OnAnimationComplete += DeactivateAnimator;
    }
}

Instantiates a new barrel GameObject at the button's position. It sets up the barrel's initial position and rotation then retrieves the animator component from the spawned barrel and initiates the "buttonPressed" animation. It also subscribes to an animation event to trigger the DeactivateAnimator() method upon animation completion.

DestroyCurrentBarrel()

public void DestroyCurrentBarrel()
{
    if (currentBarrel != null)
    {
        Destroy(currentBarrel);
        currentBarrel = null;
    }
}

Destroys the currently spawned barrel GameObject if it exists and sets the currentBarrel reference to null after destruction.

DeactivateAnimator()

 void DeactivateAnimator()
 {
     Debug.Log("Animator deactivated");
     Animator barrelAnimator = currentBarrel.GetComponent<Animator>();
     if (barrelAnimator != null)
     {
         barrelAnimator.enabled = false;

         barrelAnimator.GetComponent<AnimationEventReceiver>().OnAnimationComplete -= DeactivateAnimator;
     }
 }

Deactivates the animator component of the current barrel. It logs a message indicating the deactivation of the animato and removes the event subscription to avoid memory leaks and ensure proper cleanup.

Future Expansion

N/A

Full Code Reference

using UnityEngine;

public class BarrelButton : MonoBehaviour
{
    public GameObject barrelPrefab;
    private GameObject currentBarrel;

    private PlayerInputActions playerActions;

    private bool playerInRange = false;

    private void Awake()
    {
        playerActions = new PlayerInputActions();
        playerActions.Player.ObjectInteraction.performed += ctx => PressButton();
    }
    
    private void OnEnable()
    {
        playerActions.Player.Enable();
    }

    private void OnDisable()
    {
        playerActions.Player.Disable();
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            playerInRange = true;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            playerInRange = false;
        }
    }

    void PressButton()
    {
        if (currentBarrel == null && playerInRange)
        {
            SpawnBarrel();
        }
    }

    void SpawnBarrel()
    {
        currentBarrel = Instantiate(barrelPrefab, transform.position, Quaternion.Euler(-90f, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z));

        Animator barrelAnimator = currentBarrel.GetComponent<Animator>();

        if (barrelAnimator != null)
        {
            barrelAnimator.SetTrigger("buttonPressed");

            barrelAnimator.GetComponent<AnimationEventReceiver>().OnAnimationComplete += DeactivateAnimator;
        }
    }

    public void DestroyCurrentBarrel()
    {
        if (currentBarrel != null)
        {
            Destroy(currentBarrel);
            currentBarrel = null;
        }
    }

    void DeactivateAnimator()
    {
        Debug.Log("Animator deactivated");
        Animator barrelAnimator = currentBarrel.GetComponent<Animator>();
        if (barrelAnimator != null)
        {
            barrelAnimator.enabled = false;

            barrelAnimator.GetComponent<AnimationEventReceiver>().OnAnimationComplete -= DeactivateAnimator;
        }
    }
  }
}
⚠️ **GitHub.com Fallback** ⚠️