122 lines
2.6 KiB
C#
122 lines
2.6 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public enum SwitchState{
|
|
On,
|
|
Off,
|
|
Error
|
|
}
|
|
|
|
public class Switch : Predicate {
|
|
protected override bool CheckInternal ()
|
|
{
|
|
return state == SwitchState.On;
|
|
}
|
|
|
|
protected override bool CheckInternal (object param)
|
|
{
|
|
return Check ();
|
|
}
|
|
|
|
|
|
public Color errorColor;
|
|
public Color offColor;
|
|
public Color onColor;
|
|
public SwitchState state;
|
|
private PlayerSensor _sensor;
|
|
private Notification _notify;
|
|
private GameAction _action;
|
|
public string actionOn = "Switch ON";
|
|
public string actionOff = "Switch OFF";
|
|
public string errorNotify = "Switch not working";
|
|
public AudioClip soundOn;
|
|
public AudioClip soundOff;
|
|
private AudioSource _audio;
|
|
public bool interactive = true;
|
|
public WaveController ledIntensityController;
|
|
public Predicate interactivePredicate;
|
|
private Led _led;
|
|
|
|
// Use this for initialization
|
|
void Start () {
|
|
_action = new GameAction();
|
|
_action.target = gameObject;
|
|
_notify = new Notification();
|
|
_notify.source = gameObject;
|
|
_led = GetComponentInChildren<Led>();
|
|
_led.color = errorColor;
|
|
|
|
_sensor = GetComponentInChildren<PlayerSensor>();
|
|
_sensor.PlayerEnter += HandlePlayerEnter;
|
|
_sensor.PlayerExit += HandlePlayerExit;
|
|
|
|
_audio = GetComponent<AudioSource>();
|
|
}
|
|
|
|
void SetState(SwitchState s){
|
|
state = s;
|
|
if(state == SwitchState.Off){
|
|
_audio.PlayOneShot(soundOff);
|
|
}else if(state == SwitchState.On){
|
|
_audio.PlayOneShot(soundOn);
|
|
}
|
|
}
|
|
|
|
void OnAction(ActionArgs a){
|
|
if(state == SwitchState.On){
|
|
SetState(SwitchState.Off);
|
|
}else if(state == SwitchState.Off){
|
|
SetState(SwitchState.On);
|
|
}
|
|
}
|
|
|
|
void HandlePlayerEnter (PlayerSensor sensor, GameObject player)
|
|
{
|
|
if(interactive){
|
|
NotificationsController.AddNotification(_notify);
|
|
ActionController.AddAction(_action);
|
|
}
|
|
}
|
|
|
|
void HandlePlayerExit (PlayerSensor sensor, GameObject player)
|
|
{
|
|
if(interactive){
|
|
NotificationsController.RemoveNotification(_notify);
|
|
ActionController.RemoveAction(_action);
|
|
}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update () {
|
|
if(interactivePredicate != null){
|
|
interactive = interactivePredicate.Check();
|
|
}
|
|
|
|
if(state == SwitchState.Error){
|
|
_notify.text = errorNotify;
|
|
_action.text = "";
|
|
}else{
|
|
if(state == SwitchState.On){
|
|
_action.text = actionOff;
|
|
}else{
|
|
_action.text = actionOn;
|
|
}
|
|
_notify.text = "";
|
|
}
|
|
|
|
Color c;
|
|
if(state == SwitchState.On){
|
|
c = onColor;
|
|
}else if(state == SwitchState.Off){
|
|
c = offColor;
|
|
}else{
|
|
c = errorColor;
|
|
}
|
|
_led.color = c;
|
|
|
|
float intensity = ledIntensityController == null ? 0 : ledIntensityController.Get();
|
|
|
|
_led.intensity = Mathf.Clamp(intensity, 0.5f, 1.0f);
|
|
}
|
|
}
|