Drop System - AlexisBliesener/Bewitched GitHub Wiki
The Drop System is an upgrade system that allows enemies to drop collectible items when defeated. Players can pick up these drops to receive random upgrade choices. Refer to upgrades details for more details.
-
Add Drop System to Scene:
- Add the
DropSystemprefab to the scene-
DropSystemprefab can be found in/Assets/Prefabs/DropSystem/DropSystem.prefab
-
- The DropSystem is a singleton - only one should exist per scene
- Configure the drop chance percentage
- Drop Chance % -> chance an enemy drops an item.
- Salvage Amount -> amount of health restored if the player salvages a drop.
- Assign the drop pickup prefab (the spinning 3D model)
- Add the
-
Creating New Rarities:
- Open the
DropSystemin the inspector. - Add a new entry under Available Rarities.
- Set display name ("Common", "Rare", "Ultra Rare", etc...)
- Set drop chance percentage (1-100%)
- Higher percentages = more likely to drop
- Open the
-
Creating New Drop Items:
- Add a new entry to Available Drops in the
DropSystem. - Fill in:
- Drop Name
- Description
- Icon
- Rarity -> chosen from a dropdown (shown from the available rarities)
-
Drop Script -> a prefab of GameObject that implements
IDrop.
- Add a new entry to Available Drops in the
This code runs when an enemy is defeated:
DropSystem.Instance.TryDropItem(transform.position);The system will:
- Check the drop chance percentage
- If successful, spawn a pickup at the enemy position
- The pickup will spin and wait for player interaction
- Metadata for each drop:
- Name
- Description
- Icon
- Rarity (via index lookup in DropSystem)
- Drop Script (GameObject implementing
IDrop)
- Defines rarity tiers.
- Display Name
- Drop Chance %
- Handle the spinning 3D model behavior
- Detect player collision
- Trigger drop selection event
- Every drop must implement this interface:
public interface IDrop
{
void Activate();
}- Create a new script and implement
IDrop. - Override the
Activate()method to apply the effect.
using UnityEngine;
/// <summary>
/// HealthBoost drop implementation
/// </summary>
public class HealthBoost : MonoBehaviour, IDrop
{
/// <summary>
/// Implement the drop's functionality
/// </summary>
public void Activate()
{
base.Activate();
}
}The Drop System use events to notify the UI when a drop is picked up:
public class UIDropHandler : MonoBehaviour
{
private void OnEnable()
{
// Subscribe to drop pickup events
DropSystem.Instance.OnDropRandomDrop += HandleDropPickup;
}
private void OnDisable()
{
// Unsubscribe from events
DropSystem.Instance.OnDropRandomDrop -= HandleDropPickup;
}
private void HandleDropPickup(DropData option1, DropData option2)
{
// Show UI with both options
ShowDropSelectionUI(option1, option2);
}
private void ShowDropSelectionUI(DropData option1, DropData option2)
{
// Display UI showing both drop options
// Call DropSystem.Instance.SelectDropsOption(chosenOption);
// Or call DropSystem.Instance.SalvageDrop(); to skip both
// Name of the drop? option1.GetDropName();
// Description of the drop? option1.GetDescription();
// etc..
}
}- Enemy is defeated ->
TryDropItem()called - System checks drop chance -> spawns pickup if successful
- Player touch pickup ->
ShowDropSelection()triggered - System selects 2 random drops based on rarity
-
OnDropRandomDropevent fired with both options - UI displays options to player
- Player chooses -> UI calls
SelectDropsOption()orSalvageDrop() - Finally, the chosen drop's
Activate()method is called