Successful sync test
This commit is contained in:
107
Assets/Test stuff/TestBall.cs
Normal file
107
Assets/Test stuff/TestBall.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using librsync.net;
|
||||
using Marro.PacManUdon;
|
||||
using System;
|
||||
using System.Drawing.Text;
|
||||
using UnityEngine;
|
||||
|
||||
public class TestBall : SyncedObject
|
||||
{
|
||||
[SerializeField] private NetworkManager networkManager;
|
||||
[SerializeField] private Transform start;
|
||||
[SerializeField] private Transform end;
|
||||
|
||||
private const int LoopTimeMs = 1000;
|
||||
private const float MaxUp = 0.7f;
|
||||
private const float UpPerPress = 0.4f;
|
||||
private const float DownPerSecond = 1;
|
||||
|
||||
private float amountUp = 0;
|
||||
private int loopOffset = 0;
|
||||
private uint lastUpdate;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
networkManager.Initialize();
|
||||
lastUpdate = networkManager.CurrentTime;
|
||||
}
|
||||
|
||||
public void FixedUpdate()
|
||||
{
|
||||
var currentTime = networkManager.CurrentTime;
|
||||
float dt = networkManager.GetDtInSeconds(currentTime, lastUpdate);
|
||||
DeltaUp(-DownPerSecond * dt);
|
||||
|
||||
float progress = GetProgress(currentTime);
|
||||
transform.position = Vector3.Lerp(start.position, end.position, progress) + amountUp * MaxUp * Vector3.up;
|
||||
|
||||
lastUpdate = currentTime;
|
||||
}
|
||||
|
||||
private void DeltaUp(float delta)
|
||||
{
|
||||
amountUp = Mathf.Clamp(amountUp + delta, 0, 1);
|
||||
//Debug.Log($"delta: {delta}, amountUp: {amountUp}");
|
||||
}
|
||||
|
||||
private void SetProgress(float progress, uint currentTime)
|
||||
{
|
||||
//loopOffset = (int)(progress * LoopTimeMs % (LoopTimeMs + currentTime));
|
||||
loopOffset = (int)(currentTime - progress * LoopTimeMs);
|
||||
//loopOffset = (int)currentTime % LoopTimeMs + (int)(progress * LoopTimeMs);
|
||||
}
|
||||
|
||||
private float GetProgress(uint currentTime)
|
||||
{
|
||||
return ((int)currentTime - loopOffset) % LoopTimeMs / (float)LoopTimeMs; // "uint % int" is not exposed, I love working in Udon
|
||||
//return loopOffset - ((int)currentTime % LoopTimeMs) / (float)LoopTimeMs; // "uint % int" is not exposed, I love working in Udon
|
||||
}
|
||||
|
||||
public void UpButtonPressed()
|
||||
{
|
||||
DeltaUp(UpPerPress);
|
||||
Debug.Log($"({nameof(TestBall)}) Up button pressed, jumped up at {GetProgress(networkManager.CurrentTime)} to {amountUp}.");
|
||||
networkManager.SendEvent((NetworkEventType)1);
|
||||
}
|
||||
|
||||
public void SyncButtonPressed()
|
||||
{
|
||||
networkManager.SendEvent((NetworkEventType)0);
|
||||
Debug.Log($"({nameof(TestBall)}) Sync button pressed, synced at progress {GetProgress(networkManager.CurrentTime)} and amountUp {amountUp}.");
|
||||
}
|
||||
|
||||
public override void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType, uint eventTime)
|
||||
{
|
||||
var currentTime = networkManager.CurrentTime;
|
||||
|
||||
if (eventType == 0)
|
||||
{
|
||||
data[index++] = BitConverter.GetBytes(amountUp);
|
||||
data[index++] = BitConverter.GetBytes(GetProgress(currentTime));
|
||||
}
|
||||
}
|
||||
|
||||
public override bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType, uint eventTime)
|
||||
{
|
||||
var currentTime = networkManager.CurrentTime;
|
||||
|
||||
if (eventType == 0)
|
||||
{
|
||||
amountUp = BitConverter.ToSingle(data, index);
|
||||
SetProgress(BitConverter.ToSingle(data, index + 4), currentTime);
|
||||
Debug.Log($"({nameof(TestBall)}) Received sync event, synced to progress {GetProgress(currentTime)} and amountUp {amountUp}.");
|
||||
index += 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
DeltaUp(UpPerPress);
|
||||
Debug.Log($"({nameof(TestBall)}) Received up event, jumped up at {GetProgress(networkManager.CurrentTime)} to {amountUp}.");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void SyncedToNewTime(uint oldTime, uint newTime)
|
||||
{
|
||||
lastUpdate += newTime - oldTime;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user