86 lines
3.4 KiB
C#
86 lines
3.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using asap.core;
|
|
using System.Collections;
|
|
using UniRx;
|
|
|
|
namespace PinballUncountable
|
|
{
|
|
public class PinballUncountableSceneCanvasView : MonoBehaviour
|
|
{
|
|
[SerializeField] private Button[] buttons;
|
|
[SerializeField] private PinballUncountablePlaySystem replaySystem;
|
|
[SerializeField] private PinballUncountableDiscView[] discs;
|
|
[SerializeField] private float buttonCoolDown = 1f;
|
|
[SerializeField] private CanvasGroup canvasGroup;
|
|
|
|
public RectTransform TargetNode => buttons[0].transform as RectTransform;
|
|
private System.DateTime[] _lastClickTime;
|
|
|
|
private void Awake()
|
|
{
|
|
var manager = GContext.container.Resolve<Manager>();
|
|
|
|
for (int i = 0; i < buttons.Length; i++)
|
|
{
|
|
int j = i;
|
|
buttons[i].onClick.AddListener(() => OnClickButton(j));
|
|
}
|
|
var discMaxCount = manager.TableContext.GetMaxDiscCount();
|
|
|
|
if (discs.Length < 2 || discMaxCount.Length < 2)
|
|
Debug.LogError("[PinballUncountable] Invalid disc count.");
|
|
discs[0].Init(0, manager.HitCount1, discMaxCount[0]);
|
|
discs[1].Init(1, manager.HitCount2, discMaxCount[1]);
|
|
|
|
_lastClickTime = new System.DateTime[buttons.Length];
|
|
System.Array.Fill(_lastClickTime, System.DateTime.MinValue);
|
|
UnblockCanvasInteraction();
|
|
manager.EventAggregator.GetEvent<EventBlockCanvasInteraction>().Subscribe(BlockCanvasInteraction).AddTo(this);
|
|
manager.EventAggregator.GetEvent<EventUnblockCanvasInteraction>().Subscribe(UnblockCanvasInteraction).AddTo(this);
|
|
}
|
|
|
|
private void OnClickButton(int idx)
|
|
{
|
|
// Debug.Log("OnClickButton " + idx);
|
|
if (System.DateTime.Now - _lastClickTime[idx] < System.TimeSpan.FromSeconds(buttonCoolDown))
|
|
return;
|
|
var manager = GContext.container.Resolve<Manager>();
|
|
var multiplier = manager.GetMultiplier();
|
|
if (multiplier == -1)
|
|
multiplier = manager.GetMultiplierArray()[0];
|
|
var ticketRes = manager.AddTicket(-multiplier);
|
|
if (!ticketRes)
|
|
{
|
|
// Debug.Log("[PinballUncountable] Not enough tickets.");
|
|
manager.EventAggregator.Publish(new EventInsufficientTicket());
|
|
return;
|
|
}
|
|
manager.EventAggregator.Publish(new EventOnClickSceneButton { EntranceIdx = idx, Multiplier = multiplier });
|
|
_lastClickTime[idx] = System.DateTime.Now;
|
|
StartCoroutine(FlipButton(idx));
|
|
}
|
|
|
|
private IEnumerator FlipButton(int idx)
|
|
{
|
|
buttons[idx].gameObject.SetActive(false);
|
|
yield return new WaitForSeconds(buttonCoolDown);
|
|
buttons[idx].gameObject.SetActive(true);
|
|
}
|
|
|
|
private void BlockCanvasInteraction(EventBlockCanvasInteraction e = null)
|
|
{
|
|
for (int i = 1; i < buttons.Length; i++)
|
|
buttons[i].GetComponent<Image>().raycastTarget = false;
|
|
if (e != null && !e.DoLeaveFirstAlone)
|
|
buttons[0].GetComponent<Image>().raycastTarget = false;
|
|
}
|
|
|
|
private void UnblockCanvasInteraction(EventUnblockCanvasInteraction _ = null)
|
|
{
|
|
foreach (var button in buttons)
|
|
button.GetComponent<Image>().raycastTarget = true;
|
|
}
|
|
}
|
|
}
|