Files
HacknSlash/Assets/Scripts/Pickup.cs

159 lines
4.6 KiB
C#
Raw Normal View History

2026-06-18 16:37:02 +01:00
using Synty.AnimationBaseLocomotion.Samples;
using UnityEngine;
public class Pickup : MonoBehaviour
{
public enum PickupType { Health, Strength, StunBomb }
[Header("Pickup Settings")]
2026-09-02 21:11:50 +01:00
[SerializeField] private PickupType type;
[SerializeField] private float rotationSpeed = 90f; // Gives it that classic video game spin
2026-06-18 16:37:02 +01:00
[Header("Health Settings")]
2026-09-02 21:11:50 +01:00
[SerializeField] private int healAmount = 50;
[SerializeField] private GameObject healthVFX;
2026-06-18 16:37:02 +01:00
[Header("Strength Settings")]
2026-09-02 21:11:50 +01:00
[SerializeField] private float strengthMultiplier = 2.0f; // Doubles damage and knockback
[SerializeField] private float strengthDuration = 10f;
[SerializeField] private GameObject strengthVFX;
2026-06-18 16:37:02 +01:00
[Header("Stun Settings")]
2026-09-02 21:11:50 +01:00
[SerializeField] private float stunRadius = 8f;
[SerializeField] private float stunDuration = 5f;
[SerializeField] private LayerMask enemyLayer;
[SerializeField] private GameObject stunVFX;
// Cached components for performance
private PlayerHealth playerHealth;
private SyntyPlayerCombat syntyPlayerCombat;
private EnemyAI enemyAI;
2026-06-18 16:37:02 +01:00
void Update()
{
// Slowly rotate the pickup to make it visually obvious
transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
// Make sure your Player Prefab has the "Player" Tag assigned at the very top of its Inspector!
if (other.CompareTag("Player"))
{
ApplyPickupEffect(other.gameObject);
// Optional: Play a sound or spawn a particle effect here before destroying
Destroy(gameObject); // Remove the pickup from the world
}
}
private void ApplyPickupEffect(GameObject player)
{
switch (type)
{
case PickupType.Health:
2026-09-02 21:11:50 +01:00
ApplyHealthPickup(player);
2026-06-18 16:37:02 +01:00
break;
case PickupType.Strength:
2026-09-02 21:11:50 +01:00
ApplyStrengthPickup(player);
2026-06-18 16:37:02 +01:00
break;
case PickupType.StunBomb:
ExecuteStunBomb(transform.position);
break;
}
}
2026-09-02 21:11:50 +01:00
private void ApplyHealthPickup(GameObject player)
{
// Cache the component for performance
if (playerHealth == null)
{
playerHealth = player.GetComponent<PlayerHealth>();
if (playerHealth == null)
playerHealth = player.GetComponentInParent<PlayerHealth>();
}
if (playerHealth != null)
{
playerHealth.Heal(healAmount);
}
else
{
Debug.LogWarning("PlayerHealth component not found on player or its parent");
}
// Spawn VFX at pickup location
if (healthVFX != null)
{
Instantiate(healthVFX, transform.position, Quaternion.identity);
}
// Spawn VFX at player location
if (healthVFX != null && player != null)
{
Instantiate(healthVFX, player.transform.position + Vector3.up * 0.1f,
player.transform.rotation, player.transform);
}
}
private void ApplyStrengthPickup(GameObject player)
{
// Cache the component for performance
if (syntyPlayerCombat == null)
{
syntyPlayerCombat = player.GetComponent<SyntyPlayerCombat>();
}
if (syntyPlayerCombat != null)
{
syntyPlayerCombat.ApplyStrengthBuff(strengthMultiplier, strengthDuration);
}
else
{
Debug.LogWarning("SyntyPlayerCombat component not found on player");
}
// Spawn VFX at pickup location
if (strengthVFX != null)
{
Instantiate(strengthVFX, transform.position, transform.rotation);
}
}
2026-06-18 16:37:02 +01:00
private void ExecuteStunBomb(Vector3 center)
{
Collider[] hitEnemies = Physics.OverlapSphere(center, stunRadius, enemyLayer);
foreach (Collider enemyCollider in hitEnemies)
{
2026-09-02 21:11:50 +01:00
// Cache the component for performance
if (enemyAI == null)
2026-06-18 16:37:02 +01:00
{
2026-09-02 21:11:50 +01:00
enemyAI = enemyCollider.GetComponentInParent<EnemyAI>();
2026-06-18 16:37:02 +01:00
}
2026-09-02 21:11:50 +01:00
if (enemyAI != null)
{
enemyAI.ApplyStun(stunDuration);
}
}
// Spawn VFX at pickup location
if (stunVFX != null)
{
Instantiate(stunVFX, transform.position, transform.rotation);
2026-06-18 16:37:02 +01:00
}
}
void OnDrawGizmosSelected()
{
if (type == PickupType.StunBomb)
{
Gizmos.color = Color.cyan;
Gizmos.DrawWireSphere(transform.position, stunRadius);
}
}
2026-09-02 21:11:50 +01:00
}