144 lines
5.0 KiB
C#

using UnityEngine;
namespace BulletHellTemplate
{
/// <summary>
/// Represents a drop entity that can contain gold, experience, health, or act as a collector for specific types or all types of drops.
/// </summary>
public class DropEntity : MonoBehaviour
{
public enum DropType
{
Gold,
Experience,
Health,
Shield,
CollectAll,
CollectGold,
CollectExperience,
CollectHealth
}
[Header("Drop Settings")]
public DropType type; // Type of the drop or collector
[SerializeField]
private int value; // The value of the drop
public bool canAutoCollect; // Can this item be auto-collected when within range?
private Transform target; // Reference to the character for auto-collection
private float collectRange; // Range within which auto-collection is triggered
private bool isPaused = false; // Tracks if the game is paused
private void Start()
{
if (canAutoCollect)
{
target = GameObject.FindGameObjectWithTag("Character").transform;
if (target != null)
{
CharacterEntity character = target.GetComponent<CharacterEntity>();
if (character != null)
{
collectRange = character.GetCurrentCollectRange(); // Get the current collect range from the character
}
}
}
}
private void Update()
{
if (GameplayManager.Singleton.IsPaused())
{
if (!isPaused)
{
isPaused = true;
}
}
else
{
if (isPaused)
{
isPaused = false;
}
if (canAutoCollect && !isPaused && target != null)
{
float distance = Vector3.Distance(transform.position, target.position);
if (distance <= collectRange)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * 5f);
}
}
}
}
public void SetValue(int amount)
{
value = amount;
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Character") && !isPaused)
{
CharacterEntity character = other.GetComponent<CharacterEntity>();
if (character != null)
{
if (type == DropType.CollectAll || type == DropType.CollectGold || type == DropType.CollectExperience || type == DropType.CollectHealth)
{
AttractDrops(character, type);
Destroy(gameObject);
}
else
{
ApplyDrop(character);
Destroy(gameObject); // Destroy the drop after it has been collected
}
}
}
}
private void ApplyDrop(CharacterEntity character)
{
switch (type)
{
case DropType.Gold:
GameplayManager.Singleton.IncrementGainGold(value);
break;
case DropType.Experience:
character.AddXP(value);
break;
case DropType.Health:
character.Heal(value);
break;
case DropType.Shield:
character.GainShield(value);
break;
}
}
private void AttractDrops(CharacterEntity character, DropType collectorType)
{
if (!isPaused)
{
foreach (var drop in FindObjectsOfType<DropEntity>())
{
if (ShouldAttractDrop(collectorType, drop.type))
{
drop.canAutoCollect = true; // Enable auto collection to make it move towards the character
drop.target = character.transform; // Set target to the character for movement
drop.collectRange = float.MaxValue; // Set the collect range to max to ensure it moves towards the character
}
}
}
}
private bool ShouldAttractDrop(DropType collectorType, DropType dropType)
{
return (collectorType == DropType.CollectAll && dropType != DropType.CollectAll) ||
(collectorType == DropType.CollectGold && dropType == DropType.Gold) ||
(collectorType == DropType.CollectExperience && dropType == DropType.Experience) ||
(collectorType == DropType.CollectHealth && dropType == DropType.Health);
}
}
}