InteractibleLever.cs 999 Bytes
Newer Older
Markus Schlösser's avatar
Markus Schlösser committed
1 2 3 4
using NaughtyAttributes;
using UnityEngine;
using UnityEngine.Events;

5
public class InteractibleLever : MonoBehaviour, IInteractible, IResetable {
Markus Schlösser's avatar
Markus Schlösser committed
6 7 8 9 10 11 12 13 14 15 16 17
	[System.Serializable]
	private class BoolEvent : UnityEvent<bool> { }

	[SerializeField] private Orientation orientation;

	[SerializeField] private bool isActive;

	[SerializeField] private SpriteRenderer render;
	[SerializeField] private Sprite active;
	[SerializeField] private Sprite inactive;

	[SerializeField] private BoolEvent onInteract;
Christopher Benes's avatar
Christopher Benes committed
18
	[SerializeField] private BoolEvent onInteractInversed;
Markus Schlösser's avatar
Markus Schlösser committed
19 20 21 22

	private bool state;

	private void Awake() {
23
		ResetToLevelBegin();
Markus Schlösser's avatar
Markus Schlösser committed
24 25
	}

Christopher Benes's avatar
Christopher Benes committed
26
    public void Interact(PlayerActor source) {
Markus Schlösser's avatar
Markus Schlösser committed
27 28 29
		if (source.Orientation == orientation) {
			state = !state;
			onInteract.Invoke(state);
Christopher Benes's avatar
Christopher Benes committed
30
            onInteractInversed.Invoke(!state);
Markus Schlösser's avatar
Markus Schlösser committed
31 32 33 34

			UpdateSprite();
		}
	}
35

Markus Schlösser's avatar
Markus Schlösser committed
36 37 38
	private void UpdateSprite() {
		render.sprite = state ? active : inactive;
	}
39 40 41 42 43

	public void ResetToLevelBegin() {
		state = isActive;
		UpdateSprite();
	}
Markus Schlösser's avatar
Markus Schlösser committed
44
}