91 lines
2.8 KiB
C#
91 lines
2.8 KiB
C#
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 void Start()
|
|
{
|
|
networkManager.Initialize();
|
|
}
|
|
|
|
public override void FixedUpdate()
|
|
{
|
|
DeltaUp(-DownPerSecond * Dt);
|
|
|
|
float progress = GetProgress();
|
|
transform.position = Vector3.Lerp(start.position, end.position, progress) + amountUp * MaxUp * Vector3.up;;
|
|
}
|
|
|
|
private void DeltaUp(float delta)
|
|
{
|
|
amountUp = Mathf.Clamp(amountUp + delta, 0, 1);
|
|
}
|
|
|
|
private void SetProgress(float progress)
|
|
{
|
|
var currentTimestamp = networkManager.GetTimestamp(networkManager.CurrentTimeTicks);
|
|
loopOffset = (int)(currentTimestamp - progress * LoopTimeMs);
|
|
}
|
|
|
|
private float GetProgress()
|
|
{
|
|
var currentTimestamp = networkManager.GetTimestamp(networkManager.CurrentTimeTicks);
|
|
return ((int)currentTimestamp - loopOffset) % 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()} to {amountUp}.");
|
|
networkManager.SendEvent((NetworkEventType)1);
|
|
}
|
|
|
|
public void SyncButtonPressed()
|
|
{
|
|
networkManager.SendEvent((NetworkEventType)0);
|
|
Debug.Log($"({nameof(TestBall)}) Sync button pressed, synced at progress {GetProgress()} and amountUp {amountUp}.");
|
|
}
|
|
|
|
public override void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType)
|
|
{
|
|
if (eventType == 0)
|
|
{
|
|
data[index++] = BitConverter.GetBytes(amountUp);
|
|
data[index++] = BitConverter.GetBytes(GetProgress());
|
|
}
|
|
}
|
|
|
|
public override bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType)
|
|
{
|
|
if (eventType == 0)
|
|
{
|
|
amountUp = BitConverter.ToSingle(data, index);
|
|
SetProgress(BitConverter.ToSingle(data, index + 4));
|
|
Debug.Log($"({nameof(TestBall)}) Received sync event, synced to progress {GetProgress()} and amountUp {amountUp}.");
|
|
index += 8;
|
|
}
|
|
else
|
|
{
|
|
DeltaUp(UpPerPress);
|
|
Debug.Log($"({nameof(TestBall)}) Received up event, jumped up at {GetProgress()} to {amountUp}.");
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|