86 lines
2.4 KiB
C#
Raw Normal View History

2024-08-17 14:27:18 +08:00
using AppGame;
using AppGame.Sync;
using Cinemachine;
using DG.Tweening;
using Game.JNGState.Logic.Data;
using JNGame.Sync.System;
using JNGame.Sync.View;
using UnityEngine;
namespace Game.JNGFrame.View.Entity
{
2024-09-22 03:27:16 +08:00
public class VDPlayers : VDEntityBasis<EDPlayerData>
2024-08-17 14:27:18 +08:00
{
public GameObject VWorld => App.Resource.VWorld;
2024-08-31 15:35:12 +08:00
public GameObject Player => App.Resource.Player;
2024-08-17 14:27:18 +08:00
public CinemachineFreeLook FreeLook => App.Resource.FreeLook;
public VDPlayers(SViewSystem root) : base(root)
{
2024-09-22 03:27:16 +08:00
Register((int)GDataValueCode.Position, OnUpdatePosition);
Register((int)EDPlayerValueCode.Auth, OnUpdateAuth);
2024-08-17 14:27:18 +08:00
}
2024-09-22 03:27:16 +08:00
/// <summary>
/// 更新坐标
/// </summary>
/// <param name="tuple"></param>
private void OnUpdatePosition((GameObject View, EDPlayerData Data) tuple)
2024-08-17 14:27:18 +08:00
{
2024-09-22 03:27:16 +08:00
var (view, data) = tuple;
2024-08-17 14:27:18 +08:00
//更新位置
if (data.Value.Position != null)
{
view.transform.DOMove(data.Value.Position.ToVector3(), 0.5f);
}
2024-09-22 03:27:16 +08:00
if (data.Value.Auth == App.ClientID)
{
//更新本地玩家位置
if (data.Value.Position != null) App.Game.GetClient<JNGTileClientSystem>()?.SetPlayerPosition(data.Value.Position.ToLVector3());
}
}
/// <summary>
/// 更新权限
/// </summary>
private void OnUpdateAuth((GameObject View, EDPlayerData Data) tuple)
{
var (view, data) = tuple;
2024-08-17 14:27:18 +08:00
//权限操作
if (App.IsClient() && data.Value.Auth == App.ClientID)
{
//绑定相机
FreeLook.LookAt = view.transform;
FreeLook.Follow = view.transform;
}
2024-09-22 03:27:16 +08:00
2024-08-17 14:27:18 +08:00
}
public override GameObject NewView(EDPlayerData data)
{
2024-08-31 15:35:12 +08:00
var gameObject = Object.Instantiate(Player, VWorld.transform);
2024-09-22 03:27:16 +08:00
gameObject.name = $"Player_{data.Id}";
2024-08-17 14:27:18 +08:00
return gameObject;
}
public override EDPlayerData[] GetData()
{
return GetService<EDPlayerDataSystem>().Datas;
}
public override void ViewRemove(GameObject view)
{
2024-09-13 04:06:25 +08:00
view.transform.DOKill();
2024-08-17 14:27:18 +08:00
Object.Destroy(view);
}
}
}