118 lines
3.6 KiB
C#
Executable File
118 lines
3.6 KiB
C#
Executable File
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Mirror;
|
|
|
|
public class NetworkTrail : NetworkBehaviour
|
|
{
|
|
public TrailRenderer trail;
|
|
public LineRenderer line;
|
|
public bool enableValidation = true;
|
|
public int MaxDetourPoints = 5;
|
|
int maxDetourPoints => (int)( MaxDetourPoints * player.speed);
|
|
public float DetourThreshold = 1.5f;
|
|
float detourThreshold => player.speed * DetourThreshold;
|
|
public int Detours;
|
|
|
|
public Vector3[] positions;
|
|
public SpaceshipController player;
|
|
|
|
void Start()
|
|
{
|
|
//trail.gameObject.SetActive(isServer);
|
|
line.gameObject.SetActive(!isServer);
|
|
line.transform.parent=null;
|
|
line.transform.position =Vector3.zero;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
public float trailLength;
|
|
void Update()
|
|
{
|
|
if(isServer){
|
|
positions = new Vector3[trail.positionCount];
|
|
trail.GetPositions(positions);
|
|
RpcUpdatePositions(positions);
|
|
}else{
|
|
|
|
}
|
|
// trailLength = GetTrailLength();
|
|
// Debug.Log(GetTrailLength());
|
|
}
|
|
|
|
public float GetTrailLength(){
|
|
Vector3[] m_positions = new Vector3[trail.positionCount];
|
|
trail.GetPositions(m_positions);
|
|
if(m_positions.Length < 2){
|
|
Debug.Log("Trail is empty?");
|
|
return 0;
|
|
}
|
|
|
|
float distance = 0;
|
|
for(int i = 1; i < m_positions.Length;i++){
|
|
distance += Vector2.Distance(m_positions[i-1], m_positions[i]);
|
|
}
|
|
|
|
return distance;
|
|
}
|
|
float timer = 0;
|
|
[ClientRpc]
|
|
void RpcUpdatePositions(Vector3[] Positions){
|
|
if(!enableValidation){return;}
|
|
if(timer < 5){
|
|
timer+=Time.deltaTime;
|
|
return;
|
|
}
|
|
timer=0;
|
|
if(SceneData.localPlayer == null){return;}
|
|
if(Vector3.Distance(transform.position, SceneData.localPlayer.transform.position) > MinigameManager.instance.mapRadius/3f){
|
|
return;
|
|
}
|
|
|
|
positions = Positions;
|
|
line.positionCount = positions.Length;
|
|
line.SetPositions(positions);
|
|
|
|
|
|
|
|
//Validate With Trail
|
|
Vector3[] localPositions = new Vector3[trail.positionCount];
|
|
trail.GetPositions(localPositions);
|
|
Detours = 0;
|
|
for(int i=0; i < Positions.Length; i++){
|
|
bool pointValidated = false;
|
|
for(int j=1; j < 3; j++){
|
|
try{
|
|
DebugHelpers.DrawSphere(Positions[i], 1f, Color.blue);
|
|
DebugHelpers.DrawSphere(localPositions[i+j], 1f, Color.green);
|
|
if(isCloseEnough(Positions[i], localPositions[i+j])){
|
|
pointValidated = true;
|
|
|
|
|
|
break;
|
|
}
|
|
if(isCloseEnough(Positions[i], localPositions[i-j])){
|
|
pointValidated = true;
|
|
break;
|
|
}
|
|
|
|
}catch{
|
|
//No local position, Hence, no validation
|
|
}
|
|
}
|
|
if(!pointValidated){
|
|
Detours++;
|
|
}
|
|
}
|
|
// if(isLocalPlayer){if(timer < 0.2f){timer+=Time.deltaTime;return;}else{timer=0;}}
|
|
if(Detours > maxDetourPoints){
|
|
//Too much detours, Set back to servers data
|
|
trail.SetPositions(Positions);
|
|
}
|
|
}
|
|
|
|
bool isCloseEnough(Vector3 a, Vector3 b){
|
|
return Vector3.Distance(a,b) < detourThreshold;
|
|
}
|
|
}
|