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

127 lines
4.0 KiB
C#

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;
namespace Assets.Scripts.Scenes.VideoRide
{
public struct ContactInfo
{
public string Name;
public string Gender;
public string id;
}
class NearVideoPlayerList : MonoBehaviour, IRecyclableScrollRectDataSource
{
[SerializeField]
RecyclableScrollRect _recyclableScrollRect;
[SerializeField]
RectTransform _rectTransform;
[SerializeField]
private int _dataLength;
private bool _isInit;
//Dummy data List
private List<ContactInfo> _contactList = new List<ContactInfo>();
private VideoGameManager manager { get; set; }
//Recyclable scroll rect's data source must be assigned in Awake.
private void Awake()
{
InitData();
_recyclableScrollRect.DataSource = this;
manager = FindObjectOfType<VideoGameManager>();
}
private void InitData()
{
if (_contactList != null) _contactList.Clear();
var list = MapUDPService.GetOnlineUsers(App.RouteIdParam);
int index = 0;
foreach (var item in list)
{
_isInit = true;
ContactInfo obj = new ContactInfo();
obj.Name = item.Name;
obj.Gender = DateTime.Now.Millisecond.ToString();
obj.id = item.Id.ToString();
_contactList.Add(obj);
}
}
float timer = 0.5f;
private void Update()
{
timer -= Time.deltaTime;
while (timer < 0)
{
if (manager.CurrentPlayer == null)
return;
var totalDitance = manager.GetMapData().TotalDistance;
var currentDistance = manager.CurrentPlayer.totalDistance % totalDitance;
var currenList = FindObjectsOfType<ListItem>();//当前列表
var list = MapUDPService.GetOnlineUsers(App.RouteIdParam);//最新列表
foreach (ListItem o in currenList)
{
var needUpdate = list.Where(c => c.Id.ToString() == o.Id).FirstOrDefault();
if (needUpdate != null)
{
var left = (needUpdate.EndDistance % totalDitance - currentDistance) * 1000;
var right = (needUpdate.EndDistance % totalDitance - totalDitance - currentDistance) * 1000;
var diff = Math.Abs(left) > Math.Abs(right) ? right : left;
o.gameObject.SetActive(true);
o.UpdateItem(needUpdate.Name,diff.ToString("f0")+"M", needUpdate.Id.ToString());
}
else
{
o.gameObject.SetActive(false);
}
}
if (currenList.Count() == 0 && list.Count() > 0)
{
InitData();
if (_recyclableScrollRect.SelfInitialize)
{
_recyclableScrollRect.ReloadData();
}
}
timer += 0.5f;
}
}
#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
}
}