148 lines
5.0 KiB
C#
148 lines
5.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 class 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);
|
|
list = list.Where(c => c.Id != App.CurrentUser.Id).ToList();
|
|
foreach (var item in list)
|
|
{
|
|
_isInit = true;
|
|
ContactInfo obj = new ContactInfo();
|
|
obj.Name = item.Name;
|
|
obj.Gender = "";
|
|
obj.id = item.Id.ToString();
|
|
_contactList.Add(obj);
|
|
}
|
|
}
|
|
float timer = 1f;
|
|
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 = FindObjectsOfType<ListItem>();//当前列表
|
|
var currenList = GetComponentsInChildren<ListItem>(true);
|
|
var list = MapUDPService.GetOnlineUsers(App.RouteIdParam);//最新列表
|
|
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();
|
|
}
|
|
}
|
|
//var currenList = FindObjectsOfType<ListItem>();//当前列表
|
|
//var list = MapUDPService.GetOnlineUsers(App.RouteIdParam);//最新列表
|
|
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;
|
|
}
|
|
}
|
|
|
|
#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
|
|
}
|
|
}
|