Property drawer for circular buffer

This commit is contained in:
Sveske_Juice 2024-03-17 18:17:12 +01:00
parent 35117a5d8b
commit c9b874667e
5 changed files with 73 additions and 8 deletions

View File

@ -137,6 +137,7 @@ public class Rope
} */
}
[System.Serializable]
public struct NetworkRope : INetworkSerializable
{
// For rope points

View File

@ -73,6 +73,8 @@ public class RopeSimulator : NetworkBehaviour
private System.Random rng = new System.Random(k_rngSeed);
private CountdownTimer ropeSendTimer;
private int currentTick => NetworkManager.Singleton.NetworkTickSystem.LocalTime.Tick;
[SerializeField] private CircularBuffer<GameState> stateBuffer;
private const int k_bufferSize = 512;
private int[] order;
@ -98,6 +100,8 @@ public class RopeSimulator : NetworkBehaviour
Destroy(instance);
}
stateBuffer = new(k_bufferSize);
ropeSendTimer = new(k_sendRopeDataDelay);
// ropeSendTimer.OnTimerStop += SendRopeData;
@ -108,13 +112,13 @@ public class RopeSimulator : NetworkBehaviour
private void OnEnable()
{
GameManager.OnPlayersReady += PlayersReady;
NetworkManager.Singleton.NetworkTickSystem.Tick += SendGameState;
NetworkManager.Singleton.NetworkTickSystem.Tick += NetworkTick;
}
private void OnDisable()
{
GameManager.OnPlayersReady -= PlayersReady;
NetworkManager.Singleton.NetworkTickSystem.Tick -= SendGameState;
NetworkManager.Singleton.NetworkTickSystem.Tick -= NetworkTick;
}
public void PlayersReady(GameObject[] players)
@ -305,16 +309,23 @@ public class RopeSimulator : NetworkBehaviour
Debug.Log($"Received server state. Server tick: {serverState.tick}, client: {currentTick}");
}
private void SendGameState()
private void NetworkTick()
{
if (!IsServer) return;
stateBuffer.Add(ProcessGame(), currentTick);
GameState serverState = new() {
// Send to clients if is server
if (IsServer)
ServerToClientGameStateRpc(stateBuffer.Get(currentTick));
}
private GameState ProcessGame()
{
GameState localState = new() {
tick = currentTick,
nrope = Rope.ToNetworkRope(this.rope)
};
ServerToClientGameStateRpc(serverState);
return localState;
}
private void SendRopeData()

View File

@ -1,6 +1,7 @@
[System.Serializable]
public class CircularBuffer<T> {
T[] buffer;
int bufferSize;
public T[] buffer;
public int bufferSize;
public CircularBuffer(int bufferSize) {
this.bufferSize = bufferSize;

View File

@ -0,0 +1,50 @@
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(CircularBuffer<>))]
public class CircularBufferDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
EditorGUI.indentLevel++;
SerializedProperty bufferSizeProp = property.FindPropertyRelative("bufferSize");
int bufferSize = bufferSizeProp.intValue;
EditorGUI.PropertyField(
new Rect(position.x, position.y + EditorGUIUtility.singleLineHeight, position.width, EditorGUIUtility.singleLineHeight),
bufferSizeProp);
SerializedProperty bufferProp = property.FindPropertyRelative("buffer");
EditorGUI.LabelField(
new Rect(position.x, position.y + EditorGUIUtility.singleLineHeight * 2, position.width, EditorGUIUtility.singleLineHeight),
"Buffer Elements");
float lineHeight = EditorGUIUtility.singleLineHeight;
float bufferHeight = lineHeight * bufferSize;
Rect bufferRect = new Rect(position.x, position.y + EditorGUIUtility.singleLineHeight * 3, position.width, bufferHeight);
EditorGUI.PropertyField(bufferRect, bufferProp, GUIContent.none, true);
EditorGUI.indentLevel--;
EditorGUI.EndProperty();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
SerializedProperty bufferSizeProp = property.FindPropertyRelative("bufferSize");
int bufferSize = bufferSizeProp.intValue;
return EditorGUIUtility.singleLineHeight * (bufferSize + 3); // 1 for buffer size, 1 for label, and bufferSize for buffer elements
}
}
#endif

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 53ed82cd9cfbdb1868c0d12e415dd33f