Net_Ease_Dome/Assets/Scripts/SYNC/MyPlayer.cs

460 lines
10 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
public class MyPlayer : MonoBehaviour
{
public static Dictionary<ushort, MyPlayer> players = new Dictionary<ushort, MyPlayer>();
public static MyPlayer Me;
public ushort syncid;
public bool isme;
float posx, posy, posz, roty;
//发送
byte[] data;
//轴判断
byte zhouPanduandate;
public bool ReadySend = false;
private Animator animator;
/// <summary>
/// 格子编号
/// </summary>
public byte[] cellNum = new byte[2];
public void Init(ushort id, bool isMe,byte[] myGrid)
{
this.syncid = id;
this.isme = isMe;
if (myGrid != null)
{
this.cellNum = myGrid;
}
data = new byte[23];
//type
Array.Copy(BitConverter.GetBytes(1000), 0, data, 0, 4);
//id
Array.Copy(BitConverter.GetBytes(syncid), 0, data, 4, 2);
players.Add(syncid, this);
//初始化
posx = transform.position.x;
posy = transform.position.y;
posz = transform.position.z;
roty = transform.eulerAngles.y;
animator = GetComponent<Animator>();
TextMesh textMesh = transform.Find("MyId").GetComponent<TextMesh>();
textMesh.text = syncid.ToString();
if(isMe)
textMesh.color = Color.green;
}
float time = 0;
float gridTimer = 0; //根据格子更新周围玩家
void LateUpdate()
{
#region
//if (isme)
//{
// if (ReadySend)
// {
// CheckTransfrom();
// //发送格子数据
// gridTimer += Time.deltaTime;
// if (gridTimer > 1)
// {
// gridTimer = 0;
// if (IsArrayEqual(GameManager.Instance.JiugongGridSegmentation.CenterIndex, cellNum))
// {
// CopyArray(GameManager.Instance.JiugongGridSegmentation.CenterIndex, cellNum);
// SendCellChange(cellNum);
// }
// foreach (var item in players)
// {
// int xsub = Mathf.Abs(Me.cellNum[0] - item.Value.cellNum[0]);
// int zsub = Mathf.Abs(Me.cellNum[1] - item.Value.cellNum[1]);
// GameManager.Instance.DisplayPlayer(item.Value, xsub <= 1 && zsub <= 1);
// }
// }
// //if (sendTimeTTTTT.Equals(0))
// //{
// // //如果未在提交数据
// // fixedUpdateTime += Time.deltaTime;
// // if (fixedUpdateTime > 5)
// // {
// // fixedUpdateTime = 0;
// // dataUpdated = true;
// // //提交数据
// // }
// //}
// }
//}
//else
//{
// SetMove();
//}
////记录一秒发送次数
//time += Time.deltaTime;
//if (time >= 1)
//{
// time = 0;
// fixedUpdateTime = 0;
// Debug.Log("1秒发送位置次数" + sendTimeTTTTT);
// sendTimeTTTTT = 0;
//}
#endregion
}
void FixedUpdate()
{
if (isme)
{
if (ReadySend&&!GameManager.GodMode)
{
CheckTransfrom();
//发送格子数据
gridTimer += Time.fixedDeltaTime;
if (gridTimer > 1)
{
gridTimer = 0;
//判断格子并发送格子数据
if (IsArrayEqual(GameManager.Instance.JiugongGridSegmentation.CurrentIndex, cellNum))
{
CopyArray(GameManager.Instance.JiugongGridSegmentation.CurrentIndex, cellNum);
SendCellChange(cellNum);
}
//根据九宫格显示隐藏其他玩家
foreach (var item in players)
{
int xsub = Mathf.Abs(Me.cellNum[0] - item.Value.cellNum[0]);
int zsub = Mathf.Abs(Me.cellNum[1] - item.Value.cellNum[1]);
GameManager.Instance.DisplayPlayer(item.Value, xsub <= 1 && zsub <= 1);
}
}
}
}
else
{
SetMove();
}
//记录一秒发送次数
time += Time.fixedDeltaTime;
if (time >= 1)
{
time = 0;
Debug.Log("1秒发送位置次数" + sendTimeTTTTT);
sendTimeTTTTT = 0;
}
}
int sendTimeTTTTT = 0;
private void SendSync()
{
//类型
data[6] = zhouPanduandate;
int index = 7;
//
if (zhouPanduandate<<7 !=0)
{
//pos
Array.Copy(BitConverter.GetBytes(lastPosx), 0, data, index, 4);
index += 4;
}
var a=zhouPanduandate >> 1;
if (a<<7 !=0)
{
Array.Copy(BitConverter.GetBytes(lastPosy), 0, data, index, 4);
index += 4;
}
var b = zhouPanduandate >> 2;
if (b<<7 != 0)
{
Array.Copy(BitConverter.GetBytes(lastPosz), 0, data, index, 4);
index += 4;
}
var c = zhouPanduandate >> 3;
if (c<<7 != 0)
{
//roation
Array.Copy(BitConverter.GetBytes(lastRoty), 0, data, index, 4);
index += 4;
}
WebSocketDemo.instance.SendByTrans(data);
Debug.Log("发给trans");
sendTimeTTTTT++;
}
/// <summary>
/// 发送全数据
/// </summary>
public void SendSyncAnyWay()
{
zhouPanduandate = 15;
SendSync();
Debug.Log("立即发送!!!!!!!");
}
//记录上一次发送位置信息
float lastPosx;
float lastPosy;
float lastPosz;
float lastRoty;
float SyncRate = 0.2f;
float timer = 0;
/// <summary>
/// me执行
/// </summary>
private void CheckTransfrom()
{
timer += Time.fixedDeltaTime;
if (timer > SyncRate)
{
timer = 0;
Vector3 tmppos = transform.position;
Vector3 tmprot = transform.localEulerAngles;
if (Mathf.Abs(lastPosx - tmppos.x) > 0.05f)
{
//发送x
lastPosx = tmppos.x;
//0b00000001
zhouPanduandate |=1;
}
else
{
byte a= (byte)(~zhouPanduandate);
a |= 1;
zhouPanduandate = (byte)(~a);
}
if (Mathf.Abs(lastPosy - tmppos.y) > 0.05f)
{
//发送y
lastPosy = tmppos.y;
//0b00000010
zhouPanduandate |=2 ;
}
else
{
byte a = (byte)(~zhouPanduandate);
a |= 2;
zhouPanduandate = (byte)(~a);
}
if (Mathf.Abs(lastPosz - tmppos.z) > 0.05f)
{
//发送z
lastPosz = tmppos.z;
//0b00000100
zhouPanduandate |= 4;
}
else
{
byte a = (byte)(~zhouPanduandate);
a |= 4;
zhouPanduandate = (byte)(~a);
}
if (Mathf.Abs(lastRoty - tmprot.y) > 1)
{
//发送y
lastRoty = tmprot.y;
//0b00001000
zhouPanduandate |= 8;
}
else
{
byte a = (byte)(~zhouPanduandate);
a |= 8;
zhouPanduandate = (byte)(~a);
}
if (zhouPanduandate != 0)
{
this.SendSync();
}
}
}
/// <summary>
/// 他人执行
/// </summary>
private void SetMove()
{
UpdateAni();
}
public float dis;
//int count = 0;
//float localMoveSpeed;
/// <summary>
/// 步数系数
/// </summary>
public static float MoveStepRatio = 0.25f;
/// <summary>
/// 动画系数
/// </summary>
public static float AnimStepRatio = 1500;
float once;
Vector3 disPos = new Vector3();
Vector3 lastAnipos = new Vector3();
public void ChangeOnce()
{
disPos = new Vector3(posx, posy, posz);
once = Vector3.Distance(disPos , transform.position)/ MoveStepRatio*Time.fixedDeltaTime;
}
public void UpdateAni()
{
//Vector3 targetPos = new Vector3(posx, posy, posz);
Vector3 tmpangle = transform.localEulerAngles;
tmpangle.y = roty;
transform.localEulerAngles = tmpangle;
transform.position = Vector3.MoveTowards(transform.position, disPos, once);
#region
// float distance = dis = Vector3.Distance(transform.position, targetPos);
//float multi = 0.7f;
//if (distance > 0.5f)
//{
// multi = 1.5f;
//}
//if (count >= 5)
//{
// transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime / distance * (GameManager.Instance.playerSyncSpeed + distance * 0.25f) * multi);
//}
//else
//{
// transform.position = targetPos;
//}
//if (distance > 0)
//{
// if (localMoveSpeed < 0.5f)
// localMoveSpeed += Time.deltaTime * 0.5f;
//}
//else
//{
// if (localMoveSpeed > 0)
// localMoveSpeed -= Time.deltaTime * 0.5f;
//}
//float lms = localMoveSpeed + distance * 0.5f;
//lms = Mathf.Clamp(lms, 0, 1);
#endregion
//float speed = Vector3.Distance(transform.position, lastAnipos) * AnimStepRatio * Time.deltaTime * GameManager.FrameRate / 90;
float speed = Vector3.Distance(transform.position, lastAnipos) * AnimStepRatio * Time.fixedDeltaTime;
//speed = Mathf.Clamp(speed, 0f, 0.5f); //限制只播放走的动画
lastAnipos = transform.position;
animator.SetFloat("InputMagnitude", speed);
}
public void SetPx(float x)
{
posx = x;
}
public void SetPy(float y)
{
posy = y;
}
public void SetPz(float z)
{
posz = z;
}
public void SetRy(float ry)
{
roty = ry;
}
/// <summary>
/// 发送格子更改
/// </summary>
private void SendCellChange(byte[] cellNum)
{
//this.cellNum = cellNum;
byte[] data = new byte[8];
Array.Copy(BitConverter.GetBytes(103), 0, data, 0, 4);
Array.Copy(BitConverter.GetBytes(syncid), 0, data, 4, 2);
data[6] = cellNum[0];
data[7] = cellNum[1];
WebSocketDemo.instance.SendByMain(data);
Debug.Log("发送格子改变:" + cellNum[0] + "," + cellNum[1]);
}
/// <summary>
/// 接收格子更改
/// </summary>
/// <param name="cellNum"></param>
public void OnCellChange(ushort id,byte[] cellNum)
{
if (players.ContainsKey(id))
{
players[id].cellNum = cellNum;
Debug.Log("别人格子改变:" + id + "----" + cellNum[0] + "," + cellNum[1]);
}
}
/// <summary>
/// 判断两个数组是否相等
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
private bool IsArrayEqual(byte[] a, byte[] b)
{
if (a.Length != b.Length)
{
return false;
}
for (int i = 0; i < a.Length; i++)
{
if (a[i] != b[i])
return true;
}
return false;
}
/// <summary>
/// 复制数组
/// </summary>
/// <param name="source"> </param>
/// <param name="target"> 被赋值的数组</param>
private void CopyArray(byte[] source, byte[] target)
{
for (int i = 0; i < target.Length; i++)
{
target[i] = source[i];
}
}
/// <summary>
/// 本地立即执行位置更新
/// </summary>
public void SyncImmediately()
{
transform.position = new Vector3(posx, posy, posz);
ChangeOnce();
}
}