CCCPaste

ActionFlashlightDrain

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC
{
[System.Serializable]
public class ActionFlashlightDrain : Action
{
// Declare properties
public override ActionCategory Category { get { return ActionCategory.Custom; } }
public override string Title { get { return "Flashlight: Drain Battery"; } }
public override string Description { get { return "Drains battery power over time while the flashlight is on."; } }

// Variables
public int batteryVariableID = 0; // Global Variable ID (Float)
public float drainAmount = 5f; // Battery drain amount per cycle
public float drainInterval = 30f; // Time before each drain
public bool flashlightOn = true; // Is flashlight active?

private bool firstRun = true; // Check if it's the first time running

public override float Run()
{
if (!isRunning)
{
isRunning = true;
return drainInterval; // Wait first before draining
}
else
{
// Get the battery variable
GVar batteryVar = GlobalVariables.GetVariable(batteryVariableID);
if (batteryVar != null && batteryVar.type == VariableType.Float)
{
if (flashlightOn && batteryVar.FloatValue > 0)
{
batteryVar.FloatValue = Mathf.Max(0, batteryVar.FloatValue - drainAmount);
return drainInterval; // Wait again before next drain
}
}

isRunning = false;
return 0f; // Stop if flashlight is off or battery is empty
}
}

#if UNITY_EDITOR
public override void ShowGUI()
{
// Custom UI in AC editor
batteryVariableID = EditorGUILayout.IntField("Battery Variable ID:", batteryVariableID);
drainAmount = EditorGUILayout.FloatField("Drain Amount:", drainAmount);
drainInterval = EditorGUILayout.FloatField("Drain Interval (sec):", drainInterval);
flashlightOn = EditorGUILayout.Toggle("Is Flashlight On?", flashlightOn);
}

public override string SetLabel()
{
return "Drains " + drainAmount + " every " + drainInterval + "s from Battery ID " + batteryVariableID;
}
#endif
}
}