using System.Collections.Generic; using WSMGameStudio.Splines; using UnityEngine.Events; using UnityEngine; using System.Linq; namespace WSMGameStudio.RailroadSystem { public class RailroadSwitch_v3 : MonoBehaviour { private bool _activated = false; //Variables [SerializeField] private List _railsColliders; [SerializeField] private List _railsSplines; //Not being used, but kept anyway (hidden from inspector) [SerializeField] private List _affectedRoutes; //Events [SerializeField] private UnityEvent _onActivate; [SerializeField] private UnityEvent _onDeactivate; [SerializeField] private UnityEvent _onSwitch; public bool Activated { get { return _activated; } } public List RailsColliders { get { return _railsColliders; } set { _railsColliders = value; } } public List RailsSplines { get { return _railsSplines; } set { _railsSplines = value; } } public List AffectedRoutes { get { ValidateAffectedRoutes(); return _affectedRoutes; } set { _affectedRoutes = value; } } //Events public UnityEvent OnActivate { get { return _onActivate; } set { _onActivate = value; } } public UnityEvent OnDeactivate { get { return _onDeactivate; } set { _onDeactivate = value; } } public UnityEvent OnSwitch { get { return _onSwitch; } set { _onSwitch = value; } } /// /// Physics based rail switching /// OnActivate, OnDeactivate and OnSwitch events are triggered /// public void SwitchRails() { if (_railsColliders != null) { foreach (var collider in _railsColliders) collider.SetActive(!collider.activeInHierarchy); } UpdateActivationStatus(); _onSwitch.Invoke(); } private void UpdateActivationStatus() { _activated = !_activated; if (_activated) _onActivate.Invoke(); else _onDeactivate.Invoke(); } /// /// Physics based rail switching /// OnSwitch event is triggered /// /// /// public void SwitchRailsByIndex(int[] indexes, bool activation) { for (int i = 0; i < _railsColliders.Count; i++) { _railsColliders[i].SetActive(indexes.Contains(i) ? activation : !activation); } _onSwitch.Invoke(); } /// /// Spline based rail switching /// /// /// /// public void SplineBasedSwitchRails(ILocomotive locomotive, int leftRouteIndex, int rightRouteIndex) { if (locomotive.GetType() != typeof(SplineBasedLocomotive)) return; if (!ValidateAffectedRoutes()) return; if (leftRouteIndex >= _affectedRoutes.Count || rightRouteIndex >= _affectedRoutes.Count) return; if (_activated) RouteManager.Instance.ApplyRoute((SplineBasedLocomotive)locomotive, _affectedRoutes[leftRouteIndex]); else RouteManager.Instance.ApplyRoute((SplineBasedLocomotive)locomotive, _affectedRoutes[rightRouteIndex]); UpdateActivationStatus(); _onSwitch.Invoke(); } /// /// Validate if routes exists, if not, remove /// /// public bool ValidateAffectedRoutes() { RouteManager routeManager = RouteManager.Instance; #if UNITY_EDITOR routeManager = FindObjectOfType(); #endif if (routeManager == null) return false; if (_affectedRoutes == null || _affectedRoutes.Count == 0) return false; if (routeManager.Routes == null) { _affectedRoutes = new List(); } else { for (int i = _affectedRoutes.Count - 1; i >= 0; i--) { if (_affectedRoutes[i] > routeManager.Routes.Count - 1) _affectedRoutes.RemoveAt(i); } } return true; } } }