2021-06-29 18:03:20 +08:00
|
|
|
|
using Assets.Scenes.Ride.Scripts.Model;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using UnityEngine;
|
2021-08-25 11:20:59 +08:00
|
|
|
|
using UnityEngine.EventSystems;
|
2021-07-06 18:24:15 +08:00
|
|
|
|
using UnityEngine.UI;
|
2021-06-29 18:03:20 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Assets.Scenes.Ride.Scripts
|
|
|
|
|
|
{
|
|
|
|
|
|
public class NearByFactory : BaseListFactory
|
|
|
|
|
|
{
|
2021-07-06 18:24:15 +08:00
|
|
|
|
private Text Title { get; set; }
|
|
|
|
|
|
private Text OnlineUserNum { get; set; }
|
2021-06-29 18:03:20 +08:00
|
|
|
|
protected override void Awake()
|
|
|
|
|
|
{
|
2021-07-06 18:24:15 +08:00
|
|
|
|
scroll = transform.Find("NearByScrollView").gameObject;
|
|
|
|
|
|
if (scroll != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
UIManager.AddEvent(scroll, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag);
|
|
|
|
|
|
}
|
|
|
|
|
|
parent = scroll.transform.Find("Viewport/Content");
|
2021-06-29 18:03:20 +08:00
|
|
|
|
cyclingController = FindObjectOfType<CyclingController>();
|
|
|
|
|
|
playerController = FindObjectOfType<PlayerController>();
|
2021-09-02 10:08:46 +08:00
|
|
|
|
#if UNITY_IOS || UNITY_ANDROID
|
|
|
|
|
|
nearByItem = Resources.Load<GameObject>("UI/Prefab/Ride/Mobile/NearbyItem");
|
|
|
|
|
|
nearByMajorItem = Resources.Load<GameObject>("UI/Prefab/Ride/Mobile/NearbyMajorItem");
|
|
|
|
|
|
#else
|
2021-06-29 18:03:20 +08:00
|
|
|
|
nearByItem = Resources.Load<GameObject>("UI/Prefab/Ride/NearbyItem");
|
|
|
|
|
|
nearByMajorItem = Resources.Load<GameObject>("UI/Prefab/Ride/NearbyMajorItem");
|
2021-09-02 10:08:46 +08:00
|
|
|
|
#endif
|
2021-07-06 18:24:15 +08:00
|
|
|
|
Title = transform.Find("Title").GetComponent<Text>();
|
|
|
|
|
|
OnlineUserNum = transform.Find("NearByView/OnlineUserNum").GetComponent<Text>();
|
2021-09-07 16:11:15 +08:00
|
|
|
|
pageSize = 7;//开启无线下拉
|
2021-06-29 18:03:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
protected override void CreateList()
|
|
|
|
|
|
{
|
2021-09-09 16:29:22 +08:00
|
|
|
|
OnlineUserNum.text = " "+ MapUDPService.GetNearRiderCount().ToString();
|
2021-08-17 18:43:41 +08:00
|
|
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(OnlineUserNum.rectTransform);
|
2021-06-29 18:03:20 +08:00
|
|
|
|
var nearList = MapUDPService.GetNearRiderData(pageIndex, pageSize, new double[] { playerController.Currentlatlong.x, playerController.Currentlatlong.y });
|
2021-08-31 14:24:08 +08:00
|
|
|
|
var mod = MapUDPService.GetNearRiderCount() % pageSize;
|
|
|
|
|
|
var pages = MapUDPService.GetNearRiderCount() / pageSize;
|
2021-11-14 16:27:00 +08:00
|
|
|
|
totalPages = mod > 0 ? pages + 1 : pages;
|
2021-08-19 14:09:42 +08:00
|
|
|
|
//删除
|
|
|
|
|
|
var currentNearList = FindObjectsOfType<NearByItemScript>();
|
|
|
|
|
|
foreach (var item in currentNearList)
|
|
|
|
|
|
{
|
|
|
|
|
|
var e = nearList.Where(c => c.Id == item.UserId).FirstOrDefault();
|
|
|
|
|
|
if (e == null)
|
|
|
|
|
|
{
|
2021-08-31 14:24:08 +08:00
|
|
|
|
//item.transform.gameObject.SetActive(false);
|
|
|
|
|
|
item.Dispose();
|
2021-08-19 14:09:42 +08:00
|
|
|
|
bufferSize--;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-29 18:03:20 +08:00
|
|
|
|
for (int i = 0; i < nearList.Count(); i++)
|
|
|
|
|
|
{
|
2021-11-14 16:27:00 +08:00
|
|
|
|
Create(nearList[i], currentNearList);
|
2021-06-29 18:03:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-08-31 14:24:08 +08:00
|
|
|
|
|
2021-08-25 11:20:59 +08:00
|
|
|
|
|
2021-11-14 16:27:00 +08:00
|
|
|
|
private void Create(NearRiderModel item, NearByItemScript[] rankingList)
|
2021-06-29 18:03:20 +08:00
|
|
|
|
{
|
2021-11-14 16:27:00 +08:00
|
|
|
|
//var rankingList = FindObjectsOfType<NearByItemScript>();
|
2021-07-06 18:24:15 +08:00
|
|
|
|
var nearByItemscript = rankingList.Where(c => c.UserId == item.Id).FirstOrDefault();
|
|
|
|
|
|
//如果当前有这个用户则更新没有则创建并setslibingindex
|
|
|
|
|
|
if (nearByItemscript == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
//创建
|
|
|
|
|
|
if (bufferSize < pageSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
//情况一:还有足够的buffer,直接创建
|
|
|
|
|
|
GameObject prefabItem = item.IsSelf ? nearByMajorItem : nearByItem;
|
|
|
|
|
|
GameObject newObj = (GameObject)Instantiate(prefabItem, parent);
|
|
|
|
|
|
nearByItemscript = newObj.GetComponent<NearByItemScript>();
|
|
|
|
|
|
bufferSize++;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//情况二:没有足够的buffer,找到要被淘汰的buffer()最后一名
|
|
|
|
|
|
nearByItemscript = rankingList.OrderByDescending(c => c.distance).FirstOrDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-29 18:03:20 +08:00
|
|
|
|
nearByItemscript.setUserId(item.Id);
|
|
|
|
|
|
nearByItemscript.setName(item.Name);
|
2021-07-06 18:24:15 +08:00
|
|
|
|
nearByItemscript.setRatio(item.KGWeight + "W/KG");
|
2021-06-29 18:03:20 +08:00
|
|
|
|
nearByItemscript.setSpeed(item.Speed.ToString() + "KM/H");
|
2021-11-15 15:21:22 +08:00
|
|
|
|
if (cyclingController.currentPlayer.UserId == item.Id)
|
|
|
|
|
|
{
|
|
|
|
|
|
nearByItemscript.setHead(item.Headimage);
|
|
|
|
|
|
}
|
|
|
|
|
|
#if !(UNITY_IOS || UNITY_ANDROID)
|
2021-06-29 18:03:20 +08:00
|
|
|
|
nearByItemscript.setCountry(cyclingController.GetCountryImageByName(item.Country));
|
2021-11-15 15:21:22 +08:00
|
|
|
|
#endif
|
2021-06-29 18:03:20 +08:00
|
|
|
|
nearByItemscript.setDistance(item.Near.ToString() + "KM");
|
2021-07-06 18:24:15 +08:00
|
|
|
|
nearByItemscript.transform.SetSiblingIndex((int)item.Near);
|
2021-06-29 18:03:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|