chip-game/Assets/Scripts/GameModeAvailableActualizator.cs
2026-01-12 09:36:17 +03:00

53 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameModeAvailableActualizator : MonoBehaviour
{
public string PriceKey;
public bool readyToBlink = false;
public float frequency = 5;
public Gradient gradient;
public Graphic graphic;
private void OnEnable()
{
RefreshState();
ComWrapper.onCoinAdded.AddListener(RefreshState);
ComWrapper.onCoinRemoved.AddListener(RefreshState);
SettingsManager.OnSettingsChanged.AddListener(RefreshState);
}
private void OnDisable()
{
ComWrapper.onCoinAdded.RemoveListener(RefreshState);
ComWrapper.onCoinRemoved.RemoveListener(RefreshState);
SettingsManager.OnSettingsChanged.RemoveListener(RefreshState);
}
void Update()
{
if (readyToBlink) Blink();
}
private void Blink()
{
float sin = 0.5f + 0.5f * Mathf.Sin(Time.time * frequency);
graphic.color = gradient.Evaluate(sin);
}
private bool GameModeAvailable()
{
if (SettingsManager.GetBool("Freeplay", false)) return true;
return ComWrapper.Balance >= PlayerPrefs.GetInt(PriceKey, 10);
}
private void RefreshState(int a = 0) => RefreshState();
private void RefreshState()
{
readyToBlink = GameModeAvailable();
if (!readyToBlink)
graphic.color = Vector4.zero;
}
}