285 lines
7.9 KiB
C#
285 lines
7.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting.Antlr3.Runtime;
|
|
using UnityEngine;
|
|
using UnityEngine.U2D;
|
|
using UnityEngine.UI;
|
|
using Random = UnityEngine.Random;
|
|
|
|
public class OnlinePanel : BasePanel
|
|
{
|
|
//时间条
|
|
public RectTransform imgBK;
|
|
//时间条长度
|
|
public float maxWidth = 310f;
|
|
//关闭
|
|
public Button btnClose;
|
|
//帮助
|
|
public Button btnHelp;
|
|
//礼物按钮
|
|
public Button btnGift1;
|
|
private bool isOpen1=false;
|
|
public Button btnGift5;
|
|
private bool isOpen5 = false;
|
|
public Button btnGift10;
|
|
private bool isOpen10 = false;
|
|
public Button btnGift20;
|
|
private bool isOpen20 = false;
|
|
public Button btnGift30;
|
|
private bool isOpen30 = false;
|
|
public Button btnGift60;
|
|
private bool isOpen60 = false;
|
|
|
|
//时间文字
|
|
public Text txtTime;
|
|
//计时更新
|
|
private float timeUp=0;
|
|
|
|
private PlayerData player;
|
|
//开启函数
|
|
private bool isRunning = true;
|
|
private SpriteAtlas sa;
|
|
//随机领取的金币
|
|
private int gold;
|
|
|
|
public override void Init()
|
|
{
|
|
sa = Resources.Load<SpriteAtlas>("Atlas/OnlinePanel");
|
|
//获取一次服务器数据
|
|
player = GameDataMgr.Instance.player;
|
|
|
|
StartCoroutine(UpdateTime(1f));
|
|
//打开面板,更新面板上的礼图案
|
|
UpdateGift();
|
|
|
|
btnClose.onClick.AddListener(() =>
|
|
{
|
|
UIManager.Instance.HidePanel<OnlinePanel>(false);
|
|
StopCorotine();
|
|
});
|
|
btnGift1.onClick.AddListener(() =>
|
|
{
|
|
if (isOpen1)
|
|
{
|
|
//关闭按钮
|
|
OpenGift(btnGift1);
|
|
player.onlineGiftOpen[0] = true;
|
|
GetGold();
|
|
}
|
|
});
|
|
btnGift5.onClick.AddListener(() =>
|
|
{
|
|
if (isOpen5)
|
|
{
|
|
//关闭按钮
|
|
OpenGift(btnGift5);
|
|
player.onlineGiftOpen[1] = true;
|
|
GetGold();
|
|
}
|
|
});
|
|
btnGift10.onClick.AddListener(() =>
|
|
{
|
|
if (isOpen10)
|
|
{
|
|
//关闭按钮
|
|
OpenGift(btnGift10);
|
|
player.onlineGiftOpen[2] = true;
|
|
GetGold();
|
|
}
|
|
});
|
|
btnGift20.onClick.AddListener(() =>
|
|
{
|
|
if (isOpen20)
|
|
{
|
|
//关闭按钮
|
|
OpenGift(btnGift20);
|
|
player.onlineGiftOpen[3] = true;
|
|
GetGold();
|
|
}
|
|
});
|
|
btnGift30.onClick.AddListener(() =>
|
|
{
|
|
if (isOpen30)
|
|
{
|
|
//关闭按钮
|
|
OpenGift(btnGift30);
|
|
player.onlineGiftOpen[4] = true;
|
|
GetGold();
|
|
}
|
|
});
|
|
btnGift60.onClick.AddListener(() =>
|
|
{
|
|
if (isOpen60)
|
|
{
|
|
//关闭按钮
|
|
OpenGift(btnGift60);
|
|
player.onlineGiftOpen[5] = true;
|
|
GetGold();
|
|
}
|
|
});
|
|
}
|
|
|
|
//获得随机金币
|
|
private void GetGold()
|
|
{
|
|
int gold = 0;
|
|
int valueRan = Random.Range(0, 10); // 生成0到1之间的随机数
|
|
|
|
if (valueRan < 6)
|
|
{
|
|
// 60% 概率获得 1-100 金币
|
|
gold = Random.Range(1, 101);
|
|
}
|
|
else if (valueRan>=6&&valueRan < 9f)
|
|
{
|
|
// 30% 概率获得 101-500 金币
|
|
gold = Random.Range(101, 501);
|
|
}
|
|
else
|
|
{
|
|
// 10% 概率获得 501-1000 金币
|
|
gold = Random.Range(501, 1001);
|
|
}
|
|
//获取奖励,更新游戏面板金币数据,通知服务器,哪一个关卡奖励已领取
|
|
player.gold += gold;
|
|
StartCoroutine(NetMgr.Instance.ChangeDataPost(player));
|
|
}
|
|
|
|
// 无限延时的协程
|
|
private IEnumerator UpdateTime(float delayTime)
|
|
{
|
|
while (isRunning)
|
|
{
|
|
// 执行延时后的操作
|
|
int minutes = Mathf.FloorToInt(GameMgr.Instance.timeAll / 60); // 计算分钟
|
|
int seconds = Mathf.FloorToInt(GameMgr.Instance.timeAll % 60); // 计算秒数
|
|
// 格式化时间为 "00:00" 格式
|
|
txtTime.text = string.Format("{0:00}:{1:00}", minutes, seconds);
|
|
//更新时间条长度
|
|
UpdateImg();
|
|
// 延时指定的时间
|
|
yield return new WaitForSeconds(delayTime);
|
|
}
|
|
}
|
|
|
|
// 停止无限延时的操作
|
|
public void StopCorotine()
|
|
{
|
|
isRunning = false;
|
|
}
|
|
|
|
private void UpdateImg()
|
|
{
|
|
// 更新 Image 的宽度
|
|
float newWidth = maxWidth * (GameMgr.Instance.timeAll /3600f);
|
|
imgBK.sizeDelta = new Vector2(newWidth, imgBK.sizeDelta.y);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断是否达到开启礼包条件
|
|
/// </summary>
|
|
private void UpdateGift()
|
|
{
|
|
|
|
if (GameMgr.Instance.timeAll >= 60 && !isOpen1)
|
|
{
|
|
if (!player.onlineGiftOpen[0])
|
|
{
|
|
btnGift1.image.sprite = sa.GetSprite("icn_giftrand");
|
|
isOpen1 = true;
|
|
}
|
|
else
|
|
{
|
|
OpenGift(btnGift1);
|
|
}
|
|
}
|
|
if (GameMgr.Instance.timeAll >= 300 && !isOpen5)
|
|
{
|
|
if (!player.onlineGiftOpen[1])
|
|
{
|
|
btnGift5.image.sprite = sa.GetSprite("icn_giftrand");
|
|
isOpen5 = true;
|
|
}
|
|
else
|
|
{
|
|
OpenGift(btnGift5);
|
|
}
|
|
}
|
|
if (GameMgr.Instance.timeAll >= 600 && !isOpen10)
|
|
{
|
|
if (!player.onlineGiftOpen[2])
|
|
{
|
|
btnGift10.image.sprite = sa.GetSprite("icn_giftrand");
|
|
isOpen10 = true;
|
|
}
|
|
else
|
|
{
|
|
OpenGift(btnGift10);
|
|
}
|
|
}
|
|
if (GameMgr.Instance.timeAll >= 1200 && !isOpen20)
|
|
{
|
|
if (!player.onlineGiftOpen[3])
|
|
{
|
|
btnGift20.image.sprite = sa.GetSprite("icn_giftrand");
|
|
isOpen20 = true;
|
|
}
|
|
else
|
|
{
|
|
OpenGift(btnGift20);
|
|
}
|
|
}
|
|
if (GameMgr.Instance.timeAll >= 1800 && !isOpen30)
|
|
{
|
|
if (!player.onlineGiftOpen[4])
|
|
{
|
|
btnGift30.image.sprite = sa.GetSprite("icn_giftrand");
|
|
isOpen30 = true;
|
|
}
|
|
else
|
|
{
|
|
OpenGift(btnGift30);
|
|
}
|
|
}
|
|
if (GameMgr.Instance.timeAll >= 3600 && !isOpen60)
|
|
{
|
|
if (!player.onlineGiftOpen[5])
|
|
{
|
|
btnGift60.image.sprite = sa.GetSprite("icn_giftrand");
|
|
isOpen60 = true;
|
|
}
|
|
else
|
|
{
|
|
OpenGift(btnGift60);
|
|
}
|
|
}
|
|
|
|
if (GameMgr.Instance.timeAll<60)
|
|
{
|
|
btnGift1.image.sprite = sa.GetSprite("icn_giftrandgray");
|
|
btnGift1.GetComponent<RectTransform>().sizeDelta = new Vector2(108f, 112f);
|
|
btnGift5.image.sprite = sa.GetSprite("icn_giftrandgray");
|
|
btnGift5.GetComponent<RectTransform>().sizeDelta = new Vector2(108f, 112f);
|
|
btnGift10.image.sprite = sa.GetSprite("icn_giftrandgray");
|
|
btnGift10.GetComponent<RectTransform>().sizeDelta = new Vector2(108f, 112f);
|
|
btnGift20.image.sprite = sa.GetSprite("icn_giftrandgray");
|
|
btnGift20.GetComponent<RectTransform>().sizeDelta = new Vector2(108f, 112f);
|
|
btnGift30.image.sprite = sa.GetSprite("icn_giftrandgray");
|
|
btnGift30.GetComponent<RectTransform>().sizeDelta = new Vector2(108f, 112f);
|
|
btnGift60.image.sprite = sa.GetSprite("icn_giftrandgray");
|
|
btnGift60.GetComponent<RectTransform>().sizeDelta = new Vector2(108f, 112f);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开礼包之后的显示图片
|
|
/// </summary>
|
|
/// <param name="btn"></param>
|
|
private void OpenGift( Button btn)
|
|
{
|
|
btn.image.sprite = sa.GetSprite("icn_giftrandopen");
|
|
btn.GetComponent<RectTransform>().sizeDelta = new Vector2(134f, 112f);
|
|
}
|
|
}
|