505 lines
20 KiB
C#
505 lines
20 KiB
C#
using Assets.Scenes.Ride.Scripts;
|
||
using Assets.Scenes.Ride.Scripts.Model;
|
||
using Assets.Scripts;
|
||
using Assets.Scripts.Devices.Ant;
|
||
using Assets.Scripts.Scenes;
|
||
using Assets.Scripts.UI.Prefab.Device;
|
||
using DG.Tweening;
|
||
using Mapbox.Examples;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using UnityEngine;
|
||
using UnityEngine.SceneManagement;
|
||
using UnityEngine.UI;
|
||
|
||
public class MainController : BaseScene
|
||
{
|
||
[SerializeField]GameObject root;
|
||
private Text Version;
|
||
protected override void Awake()
|
||
{
|
||
base.Awake();
|
||
Debug.Log("执行25");
|
||
Version = this.transform.Find("GameObject").Find("Version").GetComponent<Text>();
|
||
Version.text = "V"+App.AppVersion;
|
||
DeviceCache.Init(PFConstants.DeviceCacheFolder);
|
||
Loom.Initialize();
|
||
msg = transform.Find("GameObject/Message").GetComponent<CanvasGroup>();
|
||
InitialMessage();
|
||
#if UNITY_ANDROID || UNITY_IOS
|
||
transform.GetComponent<CanvasScaler>().referenceResolution = new Vector2(844, 390);
|
||
//transform.Find("Panel").GetComponent<RectTransform>().sizeDelta = new Vector2(844, 390);
|
||
var rectTransform = transform.Find("Panel").GetComponent<RectTransform>();
|
||
rectTransform.anchorMin = Vector2.zero;
|
||
rectTransform.anchorMax = Vector2.one;
|
||
rectTransform.offsetMin = Vector2.zero;
|
||
rectTransform.offsetMax = Vector2.zero;
|
||
transform.Find("GameObject/Version").gameObject.SetActive(false);
|
||
//var rect1 = transform.Find("GameObject").GetComponent<RectTransform>();
|
||
//rect1.anchorMin = Vector2.zero;
|
||
//rect1.anchorMax = Vector2.one;
|
||
//rect1.offsetMin = Vector2.zero;
|
||
//rect1.offsetMax = Vector2.zero;
|
||
//transform.Find("ModalPanel").GetComponent<RectTransform>().sizeDelta = new Vector2(844, 390);
|
||
#else
|
||
var modalRect = transform.Find("ModalPanel").GetComponent<RectTransform>();
|
||
modalRect.anchorMin = new Vector2(0.5f, 0.5f);
|
||
modalRect.anchorMax = new Vector2(0.5f, 0.5f);
|
||
modalRect.offsetMin = Vector2.zero;
|
||
modalRect.offsetMax = Vector2.zero;
|
||
modalRect.sizeDelta = new Vector2(1600, 900);
|
||
App.MainDeviceAdapter.StartScan();
|
||
#endif
|
||
EventQueueSystem.AddListener<LinkedMessageEvent>(LinkedMessageHandler);
|
||
App.isHomeChanged -= OnIsHomeChanged;
|
||
App.isHomeChanged += OnIsHomeChanged;
|
||
}
|
||
#region 显示消息有关
|
||
//切换页面后
|
||
private void OnIsHomeChanged(object sender, EventArgs e)
|
||
{
|
||
//DOTween.KillAll();
|
||
FinishMessage(sender);
|
||
}
|
||
|
||
List<CanvasGroup> msgs;
|
||
Vector3 msgLocation;
|
||
int msgIndex = 0;
|
||
CanvasGroup rightMessage;
|
||
private void InitialMessage()
|
||
{
|
||
Debug.Log("初始化组件");
|
||
#if UNITY_STANDALONE_WIN
|
||
msgLocation = 1 * msg.transform.localPosition;
|
||
var go = transform.Find("GameObject");
|
||
rightMessage = Instantiate<CanvasGroup>(msg);
|
||
DestroyImmediate(transform.Find("GameObject/Message").gameObject);
|
||
rightMessage.transform.SetParent(go);
|
||
rightMessage.transform.localScale = Vector3.one;
|
||
rightMessage.transform.localPosition = 1 * msgLocation;
|
||
rightMessage.alpha = 0;
|
||
msgs = new List<CanvasGroup> { rightMessage };
|
||
for (int i = 0; i < 4; i++)
|
||
{
|
||
var tmpm = Instantiate<CanvasGroup>(rightMessage);
|
||
tmpm.transform.SetParent(rightMessage.transform.parent);
|
||
tmpm.transform.localPosition = 1 * msgLocation;
|
||
tmpm.transform.localScale = Vector3.one;
|
||
tmpm.alpha = 0;
|
||
msgs.Add(tmpm);
|
||
}
|
||
#else
|
||
msgLocation = new Vector3(168, 37-46, 0);
|
||
var go = transform.Find("GameObject");
|
||
go.GetComponent<RectTransform>().anchorMin = Vector2.zero;
|
||
go.GetComponent<RectTransform>().anchorMax = Vector2.one;
|
||
go.GetComponent<RectTransform>().offsetMax = Vector2.zero;
|
||
go.GetComponent<RectTransform>().offsetMin = Vector2.zero;
|
||
DestroyImmediate(transform.Find("GameObject/Message").gameObject);
|
||
var m = Resources.Load<GameObject>("UI/Prefab/NewRoute/Mobile/Message");
|
||
rightMessage = Instantiate<GameObject>(m).GetComponent<CanvasGroup>();
|
||
rightMessage.transform.SetParent(go);
|
||
rightMessage.transform.localScale = Vector3.one;
|
||
rightMessage.transform.localPosition = 1 * msgLocation;
|
||
rightMessage.alpha = 0;
|
||
msgs = new List<CanvasGroup> { rightMessage };
|
||
for (int i = 0; i < 4; i++)
|
||
{
|
||
var tmpm = Instantiate<CanvasGroup>(rightMessage);
|
||
tmpm.transform.SetParent(rightMessage.transform.parent);
|
||
tmpm.transform.localPosition = 1 * msgLocation;
|
||
tmpm.transform.localScale = Vector3.one;
|
||
tmpm.alpha = 0;
|
||
msgs.Add(tmpm);
|
||
}
|
||
|
||
Debug.Log(rightMessage.GetComponent<RectTransform>().localPosition);
|
||
//rightMessage.transform.SetParent(transform.Find("GameObject"));
|
||
#endif
|
||
}
|
||
|
||
private void ShowQueueMessage()
|
||
{
|
||
int i = 0,count = App.globalMessageQueue.Count;
|
||
while (App.globalMessageQueue.Count != 0)
|
||
{
|
||
//SetMessage(msgs[i], App.globalMessageQueue.Dequeue());
|
||
var v = 1 * msgLocation;
|
||
// 0 1 2
|
||
// 2 1 0
|
||
msgs[i].transform.localPosition = new Vector3(v.x, v.y - (count - 1 - i) * moveOffset, v.z);
|
||
msgs[i].alpha = 1;
|
||
msgs[i].gameObject.SetActive(true);
|
||
SetMessage(msgs[i], App.globalMessageQueue.Dequeue());
|
||
i++;
|
||
}
|
||
msgIndex = i;
|
||
}
|
||
|
||
private void LinkedMessageHandler(LinkedMessageEvent e)
|
||
{
|
||
if (App.globalMessageQueue.Count == 5)
|
||
{
|
||
App.globalMessageQueue.Dequeue();
|
||
}
|
||
App.globalMessageQueue.Enqueue(e);
|
||
emptyt = 0;
|
||
if (App.currentPageIsHome)
|
||
{
|
||
DoMessage6(e);
|
||
}
|
||
}
|
||
|
||
#if UNITY_STANDALONE_WIN
|
||
float moveOffset = 74;
|
||
#else
|
||
float moveOffset = 38;
|
||
#endif
|
||
private void DoMessage6(LinkedMessageEvent e)
|
||
{
|
||
//Resources.UnloadUnusedAssets();
|
||
//GC.Collect();
|
||
Debug.Log(msgIndex);
|
||
var selectIndex = msgIndex % 5;
|
||
var m = msgs[selectIndex];
|
||
if (msgIndex >= 5) //
|
||
{
|
||
//msgIndex = 0;
|
||
Sequence se = DOTween.Sequence();
|
||
//m.transform.DOLocalMoveY(m.transform.localPosition.y - 38, 0.5f);
|
||
foreach (var item in msgs)
|
||
{
|
||
se.Join(item.transform.DOLocalMoveY(item.transform.localPosition.y - moveOffset, 0.5f));
|
||
}
|
||
se.Join(m.DOFade(0, 0.5f));
|
||
se.Play().onComplete = () =>
|
||
{
|
||
SetMessage(m, e);
|
||
m.transform.localPosition = 1 * msgLocation;
|
||
m.DOFade(1, 0.5f);
|
||
};
|
||
}
|
||
else
|
||
{
|
||
SetMessage(m, e);
|
||
Sequence se = DOTween.Sequence();
|
||
for (int i = 0; i < msgIndex; i++)
|
||
{
|
||
var rect = msgs[i].GetComponent<RectTransform>();
|
||
se.Join(rect.DOLocalMoveY(rect.localPosition.y - moveOffset, 0.5f));
|
||
}
|
||
se.Join(m.DOFade(1, 0.5f));
|
||
se.Play();
|
||
}
|
||
m.transform.SetAsLastSibling();
|
||
msgIndex++;
|
||
}
|
||
|
||
void FinishMessage(object sender)
|
||
{
|
||
if (!(bool)sender)
|
||
{
|
||
//FinishMessage6();
|
||
//DOTween.CompleteAll(true);
|
||
DOTween.CompleteAll(true);
|
||
transform.Find("GameObject").gameObject.SetActive(false);
|
||
}
|
||
else
|
||
{
|
||
transform.Find("GameObject").gameObject.SetActive(true);
|
||
}
|
||
//#if UNITY_STANDALONE_WIN
|
||
// if ((bool)sender)
|
||
// {
|
||
// FinishMessageRight();
|
||
// }
|
||
// else
|
||
// {
|
||
// FinishMessageLeft();
|
||
// }
|
||
//#else
|
||
|
||
|
||
//#endif
|
||
}
|
||
void FinishMessageLeft()
|
||
{
|
||
//#if UNITY_STANDALONE_WIN
|
||
// msgIndex = 0;
|
||
// if (msgs != null)
|
||
// {
|
||
// foreach (var item in msgs)
|
||
// {
|
||
// item.DOFade(0, 0.3f);
|
||
// }
|
||
// }
|
||
//#endif
|
||
}
|
||
|
||
private void SetMessage(CanvasGroup m,LinkedMessageEvent e)
|
||
{
|
||
m.GetComponent<Button>().onClick.RemoveAllListeners();
|
||
m.GetComponent<Button>().onClick.AddListener(()=>
|
||
{
|
||
if (e.routeId > 0)
|
||
{
|
||
var res = ConfigHelper.mapApi.GetById(e.routeId);
|
||
if (res.result)
|
||
{
|
||
UIManager.ShowStartRideModal(res.data);
|
||
}
|
||
//UIManager.ShowConfirm(App.GetLocalString("RIDE NOW"), App.GetLocalLanguage() == "zh"? $"<color=#fff>是否要加入</color><color=#f93086>{e.routeName}</color><color=#fff>骑行?</color>" :$"<color=#fff>Join</color> <color=#f93086>{e.routeName}</color><color=#fff>?</color>", () =>
|
||
// {
|
||
// App.RouteIdParam = e.routeId;
|
||
// SceneManager.LoadScene("Ride");
|
||
// });
|
||
}
|
||
//Debug.Log(e.routeId);
|
||
});
|
||
m.transform.Find("Avatar").GetComponent<RawImage>().texture = null;
|
||
Utils.DisplayHead(m.transform.Find("Avatar").GetComponent<RawImage>(), e.avatar);
|
||
m.transform.Find("Nickname").GetComponent<Text>().text = e.nickname;
|
||
var txt = m.transform.Find("TextContainer/Content").GetComponent<RectTransform>();
|
||
txt.GetComponent<Text>().text = e.content;
|
||
LayoutRebuilder.ForceRebuildLayoutImmediate(txt);
|
||
//Debug.Log(e.content + txt.sizeDelta.x);
|
||
m.GetComponent<HomeMessageController>().Initial(txt.sizeDelta.x);
|
||
}
|
||
CanvasGroup msg;
|
||
void fade0()
|
||
{
|
||
rightMessage.DOFade(0, 0.5f);
|
||
}
|
||
#endregion
|
||
// Start is called before the first frame update
|
||
async void Start()
|
||
{
|
||
UIManager.Instance.Root = root;
|
||
UIManager.Instance.MainPanel = this.transform.Find("Panel").GetComponent<PFUIPanel>();
|
||
UIManager.Instance.ModalsPanel = this.transform.Find("ModalPanel").GetComponent<PFUIPanel>();
|
||
transform.Find("MobileInfo/BatteryText").GetComponent<Text>().text =
|
||
$"{Math.Round(SystemInfo.batteryLevel * 100, 0)}%";
|
||
transform.Find("MobileInfo/TimeText").GetComponent<Text>().text =
|
||
DateTime.Now.ToString("HH:mm");
|
||
ShowQueueMessage();
|
||
#if UNITY_EDITOR
|
||
if (App.CurrentUser == null) //App.CurrentUser == null
|
||
{
|
||
await Login();
|
||
}
|
||
#endif
|
||
//MainMenu.transform.Find("Home").GetComponent<Button>().onClick.AddListener(() =>
|
||
//{
|
||
// UIManager.ShowHomePanel();
|
||
//});
|
||
#if UNITY_ANDROID || UNITY_IOS
|
||
if (!App.IsRowerMode.HasValue)
|
||
{
|
||
UIManager.ShowRowerSelector();
|
||
return;
|
||
}
|
||
if (App.IsRowerMode == true)
|
||
{
|
||
UIManager.ShowRowerPanel();
|
||
return;
|
||
}
|
||
#endif
|
||
LastRide();//检查本地异常中断的骑行并弹窗提示继续
|
||
if (App.MainSceneParam.ContainsKey("Name") && !string.IsNullOrEmpty(App.MainSceneParam["Name"]))
|
||
{
|
||
UIManager.ShowHomePanel();
|
||
UIManager.ShowNewRouteOverviewPanel();
|
||
if (App.MainSceneParam["Name"] == "MapListPanel")
|
||
{
|
||
UIManager.ShowMapListPanel();
|
||
}
|
||
if (App.MainSceneParam["Name"] == "NewRouteDetail" && App.currentArea != null)
|
||
{
|
||
UIManager.ShowNewRouteDetailPanel(App.currentArea.Id);
|
||
}
|
||
if (App.MainSceneParam["Name"] == "UserInfoPanel")
|
||
{
|
||
UIManager.ShowUserInfoPanel();
|
||
#if UNITY_ANDROID || UNITY_IOS
|
||
UIManager.ShowResultListPanel();
|
||
#endif
|
||
}
|
||
|
||
if (App.MainSceneParam["Name"] == "RaceHomePanel")
|
||
{
|
||
UIManager.ShowRaceHomePanel();
|
||
}
|
||
App.MainSceneParam["Name"] = string.Empty;
|
||
}
|
||
else
|
||
{
|
||
UIManager.ShowHomePanel();
|
||
//UIManager.ShowNewRouteOverviewPanel();
|
||
//UIManager.ShowUserInfoPanel();
|
||
//UIManager.ShowEditUserPanel();
|
||
//UIManager.ShowBigMapPanel();
|
||
//UIManager.ShowEarthPanel();
|
||
}
|
||
|
||
UIManager.UpdateJoinCompetition();//查询当前我参加的赛事
|
||
}
|
||
|
||
|
||
float emptyt = 0;//用于无消息计时
|
||
float t = 1f;
|
||
float scanTicks = 0;
|
||
float clearcachet = 30;
|
||
// Update is called once per frame
|
||
protected override void Update()
|
||
{
|
||
base.Update();
|
||
|
||
clearcachet -= Time.deltaTime;
|
||
if (clearcachet <= 0)
|
||
{
|
||
Debug.Log("清一次");
|
||
Resources.UnloadUnusedAssets();
|
||
GC.Collect();
|
||
clearcachet = 30;
|
||
}
|
||
if (App.canvasWidth != transform.GetComponent<RectTransform>().sizeDelta.x)
|
||
{
|
||
App.canvasWidth = transform.GetComponent<RectTransform>().sizeDelta.x;
|
||
//Debug.Log(App.canvasWidth);
|
||
}
|
||
//Debug.Log(transform.GetComponent<RectTransform>().sizeDelta.x);
|
||
t -= Time.deltaTime;
|
||
while (t <= 0)
|
||
{
|
||
App.CurrentScene = "Main";
|
||
UIManager.SendCompetitionStartMessage("Main");
|
||
t = 1;
|
||
scanTicks++;
|
||
emptyt++;
|
||
if (emptyt >= 10)
|
||
{
|
||
emptyt = 0;
|
||
if (App.currentPageIsHome)
|
||
{
|
||
FinishMessageLeft();
|
||
}
|
||
}
|
||
if (scanTicks == 10)
|
||
{
|
||
//App.MainDeviceAdapter.StopScan();
|
||
//Debug.Log("StopScan");
|
||
}
|
||
MapUDPService.Send(0, App.CurrentUser.Id, new double[]{ 0d,0d}, competitionId: 0);
|
||
//发送消息
|
||
|
||
var list = MapUDPService.GetAllOnlineUserList();
|
||
var lang = App.GetLocalLanguage();
|
||
foreach (var item in list)
|
||
{
|
||
if (item.Point != null && item.Point.Length > 0 && item.Point[0] == -1d)
|
||
{
|
||
var message = lang == "zh" ? $"{item.Name}进入了 运动地球" : "entered PowerFun";
|
||
EventQueueSystem.QueueEventOnce(new LinkedMessageEvent(-1, message, item.HeadImage, item.Name, item.RouteName), $"{item.Id}{item.LastActiveTime}{item.Point}");
|
||
}
|
||
if (string.IsNullOrEmpty(item.RouteName))
|
||
continue;
|
||
if (item.RouteId > 0)
|
||
{
|
||
var message = "";
|
||
if (item.TotalTicks == 0 && item.PreDistance == 0)
|
||
{
|
||
var routeName = string.IsNullOrEmpty(item.RouteName) ? $"#{item.RouteId}" : item.RouteName;
|
||
message = lang=="zh"?$"发起了对<color=#f93086>{routeName}</color>的挑战!" : $"started riding <color=#f93086>{item.RouteName}</color>!";
|
||
}
|
||
if (item.IsCompleted)
|
||
{
|
||
message = lang == "zh" ? $"完成了<color=#f93086>{item.RouteName}</color>的挑战!" : $"completed <color=#f93086>{item.RouteName}</color> ride!";
|
||
}
|
||
if (!string.IsNullOrEmpty(message))
|
||
{
|
||
EventQueueSystem.QueueEventOnce(new LinkedMessageEvent(item.RouteId, message, item.HeadImage, item.Name,item.RouteName), $"{item.RouteId}{item.Name}");
|
||
}
|
||
}
|
||
//距离50
|
||
if (item.EndDistance >= 50 && item.EndDistance < 100)
|
||
{
|
||
var message = lang == "zh" ? $"在<color=#f93086>{item.RouteName}</color>中骑行里程达到<color=#e3d427>{"50KM"}</color>!" : $"riding distance reaches <color=#e3d427>{"50KM"}</color> in <color=#f93086>{item.RouteName}</color>!";
|
||
EventQueueSystem.QueueEventOnce(new LinkedMessageEvent(item.RouteId, message, item.HeadImage, item.Name, item.RouteName), $"{item.RouteId}{item.Name}50KM");
|
||
}
|
||
//距离100
|
||
if (item.EndDistance >= 100)
|
||
{
|
||
var message = lang == "zh" ? $"在<color=#f93086>{item.RouteName}</color>中骑行里程达到<color=#e3d427>{"100KM"}</color>!" : $"riding distance reaches <color=#e3d427>{"100KM"}</color> in <color=#f93086>{item.RouteName}</color>!";
|
||
EventQueueSystem.QueueEventOnce(new LinkedMessageEvent(item.RouteId, message, item.HeadImage, item.Name, item.RouteName), $"{item.RouteId}{item.Name}100KM");
|
||
}
|
||
////速度35
|
||
//if (item.Speed >= 35)
|
||
//{
|
||
// var message = lang == "zh" ? $"骑行速度突破<color=#e3d427>{Math.Round(item.Speed, 2)}KM/H</color>!" : $"riding speed reaches <color=#e3d427>{Math.Round(item.Speed, 2)}KM/H</color>!";
|
||
// EventQueueSystem.QueueEventOnce(new LinkedMessageEvent(item.RouteId, message, item.HeadImage, item.Name, item.RouteName), $"{item.RouteId}{item.Name}35KM/H");
|
||
//}
|
||
//速度50
|
||
if (item.Speed >= 50)
|
||
{
|
||
var message = lang == "zh" ? $"在<color=#f93086>{item.RouteName}</color>中骑行速度达到<color=#e3d427>{Math.Round(item.Speed,2)}KM/H</color>!" : $"riding speed reaches <color=#e3d427>{Math.Round(item.Speed, 2)}KM/H</color> in <color=#f93086>{item.RouteName}</color>!";
|
||
EventQueueSystem.QueueEventOnce(new LinkedMessageEvent(item.RouteId, message, item.HeadImage, item.Name, item.RouteName), $"{item.RouteId}{item.Name}50KM/H");
|
||
}
|
||
//功体比
|
||
if (item.WeightKg >= 4)
|
||
{
|
||
var message = lang == "zh" ? $"在<color=#f93086>{item.RouteName}</color>中功体比达到<color=#e3d427>{Math.Round(item.WeightKg, 2)}w/kg</color>!" : $" riding PWR reaches <color=#e3d427>{Math.Round(item.WeightKg, 2)}w/kg</color> in <color=#f93086>{item.RouteName}</color>!";
|
||
EventQueueSystem.QueueEventOnce(new LinkedMessageEvent(item.RouteId, message, item.HeadImage, item.Name, item.RouteName), $"{item.RouteId}{item.Name}3WeightKg");
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
|
||
private async Task Login()
|
||
{
|
||
var result = await ConfigHelper.userApi.Login("13115011550", "laozhong", "");
|
||
App.CurrentUser = result.data;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 检查上次异常中断的骑行
|
||
/// </summary>
|
||
private void LastRide()
|
||
{
|
||
var tempFileList = System.IO.Directory.GetFiles(PFConstants.MapWorkoutRecordTempFolder);
|
||
if (tempFileList.Length > 0)
|
||
{
|
||
var tempFile = tempFileList[0];
|
||
var content = System.IO.File.ReadAllText(tempFile);
|
||
if (!string.IsNullOrEmpty(content))
|
||
{
|
||
var tempRecordData = Newtonsoft.Json.JsonConvert.DeserializeObject<TempRecordData>(content);
|
||
App.tempRecordData = tempRecordData;
|
||
if (App.tempRecordData.UserId == App.CurrentUser.Id)
|
||
{
|
||
UIManager.ShowConfirm(App.GetLocalString("Reconnect"), App.GetLocalString("Did you want to reconnect the trip that was interrupted abnormally?"), () =>
|
||
{
|
||
App.RouteIdParam = App.tempRecordData.RouteId;
|
||
App.CompetionId = App.tempRecordData.CompetitionId;
|
||
SceneManager.LoadScene("Ride");
|
||
}, 2, () =>
|
||
{
|
||
Helper.DelectDir(PFConstants.MapWorkoutRecordTempFolder);
|
||
App.tempRecordData = null;
|
||
});
|
||
}
|
||
}
|
||
}
|
||
}
|
||
private void OnDestroy()
|
||
{
|
||
EventQueueSystem.RemoveListener<LinkedMessageEvent>(LinkedMessageHandler);
|
||
App.isHomeChanged -= OnIsHomeChanged;
|
||
}
|
||
}
|