using UnityEngine; using System.Collections; public enum ButtonState{ Error, Locked, Ready } public delegate void ButtonEventHandler(); public class Button : Trigger { public ButtonState state; public PlayerSensor sensor; private GameAction _pressAction; public bool interactive = true; public Predicate interactivePredicate; public event ButtonEventHandler Pressed; public bool locked = false; public Predicate lockedPredicate; public Predicate errorPredicate; public bool error; public string pressActionText = "Press the button"; protected virtual void OnPressed () { var handler = Pressed; if (handler != null) handler (); } void Start () { if(sensor != null){ sensor.PlayerEnter += HandlePlayerEnter; sensor.PlayerExit += HandlePlayerExit; }else{ Debug.LogWarningFormat("Button {0} sensor is null", name ); } _pressAction = new GameAction(gameObject, pressActionText); } void HandlePlayerEnter (PlayerSensor sensor, GameObject player) { if(interactive){ ActionController.AddAction(_pressAction); } } void HandlePlayerExit (PlayerSensor sensor, GameObject player) { ActionController.RemoveAction(_pressAction); } void SetInteractive(bool val){ if(interactive == val){ return; } if(val){ if(sensor != null && sensor.Check()){ ActionController.AddAction(_pressAction); } }else{ ActionController.RemoveAction(_pressAction); } interactive = val; } void OnAction(ActionArgs a){ if(interactive){ if(state == ButtonState.Ready){ OnPressed(); OnTrig(); } } } void Update () { if(interactivePredicate != null){ SetInteractive(interactivePredicate.Check()); } if(errorPredicate.Check()){ state = ButtonState.Error; }else if(lockedPredicate.Check()){ state = ButtonState.Locked; }else{ state = ButtonState.Ready; } } }