using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using ZXing;
public class StandaloneAPI
{
public static string url;//支付二维码对应的URL
//记录一个投币数量
public static int CoinCount = 0;
public static Texture2D GetQR()
{
if (UserSettings.ins.PerRoundCoin <= 0)
{
Debug.LogWarning("PerRoundCoin 小于等于 0,不生成二维码。");
return null;
}
return ShowQRCode(url, 256, 256);
}
///
/// 根据二维码图片信息绘制制定字符串信息的二维码到指定区域
///
/// 字符串信息
/// 二维码的宽度
/// 二维码的高度
///
public static Texture2D ShowQRCode(string str, int width, int height)
{
if (string.IsNullOrEmpty(str))
return null;
Texture2D texture = new Texture2D(width, height);
Color32[] colors = GeneQRCode(str, width, height);
texture.SetPixels32(colors);
texture.Apply();
return texture;
}
///
/// 将制定字符串信息转换成二维码图片信息
///
/// 要产生二维码的字符串信息
/// 二维码的宽度
/// 二维码的高度
///
static Color32[] GeneQRCode(string formatStr, int width, int height)
{
ZXing.QrCode.QrCodeEncodingOptions options = new ZXing.QrCode.QrCodeEncodingOptions();//绘制二维码之前 进行设置
options.CharacterSet = "UTF-8";//设置字符编码,确保字符串信息保持正确
options.Width = width;//设置二维码宽
options.Height = height;//设置二维码高
options.Margin = 1;//设置二维码留白 (值越大,留白越大,二维码越小)
BarcodeWriter a = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = options };//实例化字符串绘制二维码工具
return a.Write(formatStr);
}
public static void ForceBackHome()
{
SceneManager.LoadScene("Home", LoadSceneMode.Single);
if (PersistenHandler.ins)
{
var views = PersistenHandler.ins.menuBackCtr.views;
while (views.Count > 0)
{
var view = views[views.Count - 1];
views.RemoveAt(views.Count - 1);
view.OnMenuBack();
}
}
}
private static Object _GameLocker = new();
public static void PauseGame()
{
string sceneName = SceneManager.GetActiveScene().name;
if (sceneName == "Game" && GameMgr.ins)
{
GameMgr.ins.addLockerForGamePause(_GameLocker);
}
else if (IsGameScene(sceneName))
{
Time.timeScale = 0;
}
}
public static void ResumeGame()
{
string sceneName = SceneManager.GetActiveScene().name;
if (sceneName == "Game" && GameMgr.ins)
{
GameMgr.ins.removeLockerForGamePause(_GameLocker);
}
else if (IsGameScene(sceneName))
{
Time.timeScale = 1;
}
}
private static long _GameTimeCountDownMS = 0;
private static void AddGameTimeCountDown(long second)
{
_GameTimeCountDownMS += second * 1000;
}
public static long GetGameTimeCountDown()
{
return _GameTimeCountDownMS;
}
private static long _LastTimeMS;
public static void StartGameTimeCountDown()
{
_LastTimeMS = JCUnityLib.TimeUtils.GetTimestamp();
//刷新一次
InsertCoinForAddTime();
}
public static void DoGameTimeCountDown()
{
long t = JCUnityLib.TimeUtils.GetTimestamp();
long dt = t - _LastTimeMS;
_LastTimeMS = t;
if (UserSettings.ins.PerRoundCoin == 0) return;
if (_GameTimeCountDownMS <= 0)
{
_GameTimeCountDownMS = 0;
//延迟一分钟关闭激光
if (UserSettings.ins.lightState)
SerialPortHelper.ins.GetPort()?.DelayCloseLight();
}
else if (_GameTimeCountDownMS > 0)
_GameTimeCountDownMS -= dt;
}
private static bool _TimeCounterInited;
public static void InitTimeCounter()
{
if (_TimeCounterInited) return;
_TimeCounterInited = true;
SceneManager.sceneLoaded += (scene, mode) =>
{
string sceneName = scene.name;
if (IsGameScene(sceneName))
{
//操作引导场景不用投币
if (!GameMgr.bShowDistance && GameMgr.turnOffTimer) return;
if (UserSettings.ins.PerRoundCoin == 0) return;
Object.Instantiate(Resources.Load("GameTimeCounterSA"));
}
};
}
private static bool IsGameScene(string sceneName)
{
if (sceneName.StartsWith("Game") || sceneName.StartsWith("DuckHunter") || sceneName.StartsWith("WildAttack") ||
sceneName.StartsWith("FruitMaster") || sceneName.StartsWith("Hyperspace") ||
sceneName.StartsWith("GameChallenge") || sceneName.StartsWith("GameDouble"))
return true;
return false;
}
///
/// 投币加时
///
//public static void InsertCoinForAddTime1()
//{
// //如果符合设置数量。增加时间
// if (CoinCount >= UserSettings.ins.PerRoundCoin)
// {
// //打开激光
// var port = SerialPortHelper.ins.GetPort();
// port?.CancelDelayCloseLight();
// if (!UserSettings.ins.lightState)
// port?.RequestLightState(true, true);
// CoinCount -= UserSettings.ins.PerRoundCoin;
// AddGameTimeCountDown(UserSettings.ins.PerRoundSeconds);
// Debug.Log("UserSettings.ins.PerRoundCoin:" + UserSettings.ins.PerRoundCoin);
// Debug.Log("UserSettings.ins.PerRoundSeconds:" + UserSettings.ins.PerRoundSeconds);
// }
//}
///
/// 投币循环加时
///
public static void InsertCoinForAddTime()
{
int perRoundCoin = UserSettings.ins.PerRoundCoin;
int perRoundSeconds = UserSettings.ins.PerRoundSeconds;
if (perRoundCoin <= 0)
{
Debug.LogWarning("PerRoundCoin 设置小于0,不用投币!");
return;
}
// 如果符合设置数量。增加时间
if (CoinCount >= perRoundCoin)
{
// 打开激光
var port = SerialPortHelper.ins.GetPort();
port?.CancelDelayCloseLight();
if (!UserSettings.ins.lightState)
port?.RequestLightState(true, true);
while (CoinCount >= perRoundCoin)
{
CoinCount -= perRoundCoin;
AddGameTimeCountDown(perRoundSeconds);
}
Debug.Log("剩余CoinCount:" + CoinCount);
Debug.Log("每轮消耗:" + perRoundCoin);
Debug.Log("每轮增加时间:" + perRoundSeconds);
}
}
public static void InsertCoint(byte num)
{
Debug.Log("确认投币数量:" + num);
CoinCount += num;
StandaloneAPI.InsertCoinForAddTime();
//for (int i = 0; i < num; i++)
//{
// StandaloneAPI.InsertCoinForAddTime();
//}
}
}