Diamond Pickup


Added pickup feature with diamond pickup.

Implementation Overview

This script defines an abstract Pickup class for collectible items in Unity. It detects when the player collects the item. The Diamond class extends Pickup and implements specific behavior upon collection.

Class Structure

1. Pickup (Abstract Base Class)

Fields:

  • const string playerTagString: Tag string to identify the player object (default: "Player").

Methods:

  • void OnTriggerEnter(Collider other)

    • Checks if the colliding object has the "Player" tag.

    • Calls OnPickup() when collected.

    • Destroys the pickup object after collection.

    void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag(playerTagString))
         {
             OnPickup();
             Destroy(gameObject);
         }
     } 
  • protected abstract void OnPickup()

    • Abstract method to be implemented by subclasses to define unique behavior upon collection.

public abstract class Pickup : MonoBehaviour
 {
     const string playerTagString = "Player";
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag(playerTagString))
         {
             OnPickup();
             Destroy(gameObject);
         }
     }
     protected abstract void OnPickup();
 } 

2. Diamond (Derived Class)

Inherits from: Pickup

Overrides:

  • protected override void OnPickup()

    • Logs a message to the Unity console.

    • Calls MazeGenerator.instance.CreateDoors(DoorType.Exit); to create exit doors.

    • Placeholder for additional functionality.

    protected override void OnPickup() 
    {     
        Debug.Log("Diamond Pickup!");
        MazeGenerator.instance.CreateDoors(DoorType.Exit);
    } 
public class Diamond : Pickup 
{     
    protected override void OnPickup()     
    {         
       Debug.Log("Diamond Pickup!");         
       MazeGenerator.instance.CreateDoors(DoorType.Exit);
    }
 } 

Summary

  • The Pickup class provides a template for collectible objects with player detection.

  • The Diamond class implements specific functionality for its collection event.

  • Other pickups can be created by extending Pickup and implementing OnPickup() with unique behavior.

Leave a comment

Log in with itch.io to leave a comment.