using UnityEngine; using System.Collections; public enum IgnitionState{ Lock, Acc, On, Start } public interface IIgnitionStateController{ void SetButtonState (IgnitionSystem sys, int button, bool press); void Update (IgnitionSystem sys); } public delegate void IgnitionStateCallback(IgnitionSystem caller, IgnitionState oldState, IgnitionState newState); public class IgnitionSystem : MonoBehaviour { private int _ignitionState = 0; public bool buttonMode = false; private bool _hold = false; private IgnitionState _state; public event IgnitionStateCallback StateChanged; private IgnitionButtonController _buttonController = new IgnitionButtonController(); private IgnitionSwitchController _switchController = new IgnitionSwitchController(); private Car _car; public Starter starter; private PowerBus powerBus; public PowerBus PowerBus { get { return powerBus; } set { powerBus = value; starter.SetPowerBus(powerBus); } } public Car Car{ get{ return _car;} } public void Attach(Car car){ _car = car; } public void Detach(){ _car = null; } public void SetButtonState(int button, bool press){ if (buttonMode) { if (_buttonController != null) { _buttonController.SetButtonState (this, button, press); } else { Debug.LogWarning ("Ignition system: button is not installed! Can't press!"); } } else { if (_switchController != null) { _switchController.SetButtonState (this, button, press); } else { Debug.LogWarning ("Ignition system: ignition switch is not installed! Can't press!"); } } } public IgnitionState State{ get{ return _state; } set{ if (_state == value) { return; } var oldState = _state; _state = value; Debug.LogFormat("IgnitionStystem: state changed from {0} to {1}", oldState, _state); if (StateChanged != null) { StateChanged(this, oldState, _state); } starter.On = _state == IgnitionState.Start; } } // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (buttonMode) { _buttonController.Update (this); } else { _switchController.Update (this); } } }