Files
AppleHillsProduction/Assets/Scripts/FX/TrafalgarFXController.cs
2025-09-23 11:05:35 +02:00

92 lines
3.0 KiB
C#

using UnityEngine;
using Input;
namespace FX
{
/// <summary>
/// Controls particle effects based on player movement.
/// Automatically enables/disables particle emission when the player starts/stops moving.
/// Attach this to the same GameObject as the PlayerTouchController.
/// </summary>
public class TrafalgarFXController : MonoBehaviour
{
private PlayerTouchController _playerController;
private ParticleSystem _particleSystem;
private void Awake()
{
// Get the PlayerTouchController on this game object
_playerController = GetComponent<PlayerTouchController>();
if (_playerController == null)
{
Debug.LogError("TrafalgarFXController: No PlayerTouchController found on this GameObject! This component should be attached to the same GameObject as PlayerTouchController.");
enabled = false;
return;
}
// Find the particle system in children
_particleSystem = GetComponentInChildren<ParticleSystem>();
if (_particleSystem == null)
{
Debug.LogError("TrafalgarFXController: No ParticleSystem found in children! This component requires a ParticleSystem in the hierarchy below it.");
enabled = false;
return;
}
// Make sure emission is disabled at start
var emission = _particleSystem.emission;
emission.enabled = false;
}
private void Start()
{
// Subscribe to movement events
_playerController.OnMovementStarted += EnableParticles;
_playerController.OnMovementStopped += DisableParticles;
// Set initial state based on current movement status
SetParticlesState(_playerController.IsMoving);
}
private void OnDestroy()
{
// Unsubscribe from events when this component is destroyed
if (_playerController != null)
{
_playerController.OnMovementStarted -= EnableParticles;
_playerController.OnMovementStopped -= DisableParticles;
}
}
private void EnableParticles()
{
SetParticlesState(true);
}
private void DisableParticles()
{
SetParticlesState(false);
}
private void SetParticlesState(bool enabled)
{
if (_particleSystem != null)
{
var emission = _particleSystem.emission;
emission.enabled = enabled;
if (enabled)
{
// Optionally play the system when enabling (useful if it's not in looping mode)
if (!_particleSystem.isPlaying)
{
_particleSystem.Play();
}
}
}
}
}
}