94 lines
2.7 KiB
C#
94 lines
2.7 KiB
C#
using System;
|
|
using Cysharp.Threading.Tasks;
|
|
using HighlightPlus;
|
|
using RebootKit.Engine.Simulation;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using Logger = RebootKit.Engine.Foundation.Logger;
|
|
|
|
namespace RebootReality.jelycho {
|
|
public class ButtonActor : Actor, IInteractable {
|
|
static readonly Logger s_Logger = new Logger(nameof(ButtonActor));
|
|
|
|
[SerializeField] Transform m_Graphics;
|
|
[SerializeField] float m_CooldownDuration = 0.5f;
|
|
[SerializeField] HighlightEffect m_HighlightEffect;
|
|
|
|
DateTime m_LastPressTime;
|
|
|
|
public UnityEvent pressed = new UnityEvent();
|
|
|
|
//
|
|
// @MARK: Unity callbacks
|
|
//
|
|
void Awake() {
|
|
SetHighlight(false);
|
|
m_LastPressTime = DateTime.MinValue;
|
|
}
|
|
|
|
//
|
|
// @MARK: Actor
|
|
//
|
|
protected override IActorData CreateActorData() {
|
|
return new NoActorData();
|
|
}
|
|
|
|
protected override void OnActorCommandServer(ulong senderID, ActorCommand actorCommand) {
|
|
if (actorCommand.CommandID == (ushort) ButtonActorCommands.Press) {
|
|
if (DateTime.UtcNow - m_LastPressTime < TimeSpan.FromSeconds(m_CooldownDuration)) {
|
|
return;
|
|
}
|
|
|
|
m_LastPressTime = DateTime.UtcNow;
|
|
pressed?.Invoke();
|
|
|
|
SendActorEvent((byte) ButtonActorEvents.Pressed);
|
|
}
|
|
}
|
|
|
|
protected override void OnActorEventClient(ActorEvent actorEvent) {
|
|
ButtonActorEvents buttonEvent = (ButtonActorEvents) actorEvent.EventID;
|
|
|
|
switch (buttonEvent) {
|
|
case ButtonActorEvents.Pressed: {
|
|
PlayPressAnimation();
|
|
break;
|
|
}
|
|
default:
|
|
s_Logger.Error($"Unknown actor event: {actorEvent.EventID}");
|
|
break;
|
|
}
|
|
}
|
|
|
|
void PlayPressAnimation() {
|
|
PressAsync().Forget();
|
|
}
|
|
|
|
async UniTask PressAsync() {
|
|
m_Graphics.localScale = new Vector3(1, 0.1f, 1);
|
|
await UniTask.Delay(TimeSpan.FromSeconds(m_CooldownDuration), cancellationToken: destroyCancellationToken);
|
|
m_Graphics.localScale = new Vector3(1, 1, 1);
|
|
}
|
|
|
|
//
|
|
// @MARK: IInteractable
|
|
//
|
|
public void SetHighlight(bool highlight) {
|
|
m_HighlightEffect.highlighted = highlight;
|
|
}
|
|
|
|
public void Interact() {
|
|
SendActorCommand((byte) ButtonActorCommands.Press);
|
|
}
|
|
}
|
|
|
|
public enum ButtonActorCommands : byte {
|
|
None = 0x00,
|
|
Press = 0x01,
|
|
}
|
|
|
|
public enum ButtonActorEvents : byte {
|
|
None = 0x00,
|
|
Pressed = 0x01,
|
|
}
|
|
} |