powerfun-unity/Assets/Scripts/Scenes/VideoRide/NearVideoPlayerList.cs

144 lines
4.8 KiB
C#
Raw Normal View History

2022-03-25 09:57:30 +08:00
using Assets.Scenes.Ride.Scripts;
using PolyAndCode.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
2022-03-25 09:57:30 +08:00
namespace Assets.Scripts.Scenes.VideoRide
{
public class ContactInfo
2022-03-25 09:57:30 +08:00
{
public string Name;
public string Gender;
public string id;
}
class NearVideoPlayerList : MonoBehaviour, IRecyclableScrollRectDataSource
{
[SerializeField]
RecyclableScrollRect _recyclableScrollRect;
[SerializeField]
RectTransform _rectTransform;
2022-03-25 09:57:30 +08:00
[SerializeField]
private int _dataLength;
private bool _isInit;
2022-03-25 09:57:30 +08:00
//Dummy data List
private List<ContactInfo> _contactList = new List<ContactInfo>();
private VideoGameManager manager { get; set; }
2022-03-25 09:57:30 +08:00
//Recyclable scroll rect's data source must be assigned in Awake.
private void Awake()
{
InitData();
_recyclableScrollRect.DataSource = this;
manager = FindObjectOfType<VideoGameManager>();
2022-03-25 09:57:30 +08:00
}
private void InitData()
{
if (_contactList != null) _contactList.Clear();
var list = MapUDPService.GetOnlineUsers(App.RouteIdParam);
list = list.Where(c => c.Id != App.CurrentUser.Id).ToList();
2022-03-25 09:57:30 +08:00
foreach (var item in list)
{
_isInit = true;
2022-03-25 09:57:30 +08:00
ContactInfo obj = new ContactInfo();
obj.Name = item.Name;
obj.Gender = "";
2022-03-25 09:57:30 +08:00
obj.id = item.Id.ToString();
_contactList.Add(obj);
}
}
float timer = 1f;
2022-03-25 09:57:30 +08:00
private void Update()
{
timer -= Time.deltaTime;
while (timer < 0)
{
if (manager.CurrentPlayer == null)
return;
var totalDitance = manager.GetMapData().TotalDistance;
var currentDistance = manager.CurrentPlayer.totalDistance;
var currenList = GetComponentsInChildren<ListItem>(true);
2022-05-13 09:28:58 +08:00
var list = MapUDPService.GetOnlineUsers(App.RouteIdParam,manager.cyclingController.recorderData.RoomId);//最新列表
var currentPlayerInfo = list.Where(c => c.Id.ToString() == manager.CurrentPlayer.UserId.ToString()).FirstOrDefault();
list = list.Where(c => c.Id != App.CurrentUser.Id).ToList();
if (currenList.Count() == 0 && list.Count() > 0)
{
InitData();
if (_recyclableScrollRect.SelfInitialize)
{
_recyclableScrollRect.ReloadData();
}
}
2022-05-13 09:28:58 +08:00
var currentPlayer = FindObjectOfType<VideoPlayer>();
foreach (ListItem o in currenList)
{
if (currentPlayer.UserId.ToString() == o.Id)
continue;
var needUpdate = list.Where(c => c.Id.ToString() == o.Id).FirstOrDefault();
if (needUpdate != null && currentPlayerInfo != null)
{
var left = Math.Round((needUpdate.EndDistance - currentPlayerInfo.EndDistance) * 1000);
o.UpdateItem(needUpdate.Name, left.ToString() + "M", needUpdate.Id.ToString());
o.gameObject.SetActive(true);
}
else
{
o.gameObject.SetActive(false);
}
}
foreach (var item in list)
{
var newOne = _contactList.Where(c => c.id == item.Id.ToString()).FirstOrDefault();
if (newOne == null)
{
var one = new ContactInfo();
one.Name = item.Name;
one.Gender = "";
one.id = item.Id.ToString();
_contactList.Add(one);
_recyclableScrollRect.ReloadData();
}
}
timer +=1f;
2022-03-25 09:57:30 +08:00
}
}
#region DATA-SOURCE
/// <summary>
/// Data source method. return the list length.
/// </summary>
public int GetItemCount()
{
return _contactList.Count;
}
/// <summary>
/// Data source method. Called for a cell every time it is recycled.
/// Implement this method to do the necessary cell configuration.
/// </summary>
public void SetCell(ICell cell, int index)
{
//Casting to the implemented Cell
var item = cell as ListItem;
item.ConfigureCell(_contactList[index], index);
}
#endregion
}
}