Files
HacknSlash/Assets/Scripts/PlayerBuilder.cs

65 lines
1.9 KiB
C#

using UnityEngine;
using Synty.AnimationBaseLocomotion.Samples.InputSystem; // Required to see the InputReader
public class PlayerBuilder : MonoBehaviour
{
[Header("References")]
public InputReader inputReader;
[Header("Repair Settings")]
public float repairRange = 4.0f;
public float repairHealthPerSecond = 100f;
public LayerMask fortificationLayer;
private bool isHoldingRepair = false;
void OnEnable()
{
// Subscribe to the broadcasts when the script turns on
if (inputReader != null)
{
inputReader.onRepairActivated += StartRepairing;
inputReader.onRepairDeactivated += StopRepairing;
}
}
void OnDisable()
{
// Unsubscribe when turned off to prevent memory leaks
if (inputReader != null)
{
inputReader.onRepairActivated -= StartRepairing;
inputReader.onRepairDeactivated -= StopRepairing;
}
}
// These trigger the moment the InputReader hears a button press
private void StartRepairing() { isHoldingRepair = true; }
private void StopRepairing() { isHoldingRepair = false; }
void Update()
{
// If the switch is ON, continuously repair every frame
if (isHoldingRepair)
{
RepairNearbyFortification();
}
}
private void RepairNearbyFortification()
{
Collider[] hits = Physics.OverlapSphere(transform.position, repairRange, fortificationLayer);
foreach (Collider hit in hits)
{
Fortification fort = hit.GetComponentInParent<Fortification>();
if (fort != null && fort.currentHealth < fort.maxHealth)
{
fort.Repair(repairHealthPerSecond * Time.deltaTime);
//add in repair audio and VFX
break; // Only repair one barrier at a time
}
}
}
}