196 lines
5.7 KiB
C#
196 lines
5.7 KiB
C#
// using System.Collections.Generic;
|
|
// using FishNet.Object.Synchronizing;
|
|
// using FishNet.Object.Synchronizing.Internal;
|
|
// using FishNet.Serializing;
|
|
// using UnityEngine;
|
|
//
|
|
// public class SyncPlayerData<T> : SyncBase, ICustomSync where T : MonoBehaviour
|
|
// {
|
|
// private PlayerData _playerData;
|
|
//
|
|
// public object GetSerializedType()
|
|
// {
|
|
// return typeof(PlayerData);
|
|
// }
|
|
//
|
|
// public void Initialize(PlayerData playerData)
|
|
// {
|
|
// _playerData = playerData;
|
|
// }
|
|
//
|
|
// public PlayerData GetData()
|
|
// {
|
|
// return _playerData;
|
|
// }
|
|
//
|
|
// public void SetData(PlayerData newData, bool force = false)
|
|
// {
|
|
// if (!IsInitialized)
|
|
// return;
|
|
//
|
|
// if (!IsNetworkInitialized)
|
|
// {
|
|
// Debug.LogWarning("Cannot update PlayerData when server is not active.");
|
|
// return;
|
|
// }
|
|
//
|
|
// var changed = false;
|
|
//
|
|
// // Compare and update each field
|
|
// if (force || newData.playerName != _playerData.playerName)
|
|
// {
|
|
// _playerData.playerName = newData.playerName;
|
|
// changed = true;
|
|
// }
|
|
//
|
|
// if (force || newData.level != _playerData.level)
|
|
// {
|
|
// _playerData.level = newData.level;
|
|
// changed = true;
|
|
// }
|
|
//
|
|
// if (force || newData.health != _playerData.health)
|
|
// {
|
|
// _playerData.health = newData.health;
|
|
// changed = true;
|
|
// }
|
|
//
|
|
// if (force || newData.InventorySlots != _playerData.InventorySlots)
|
|
// {
|
|
// _playerData.InventorySlots = newData.InventorySlots;
|
|
// changed = true;
|
|
// }
|
|
//
|
|
// if (force || newData.ToolbarSlots != _playerData.ToolbarSlots)
|
|
// {
|
|
// _playerData.ToolbarSlots = newData.ToolbarSlots;
|
|
// changed = true;
|
|
// }
|
|
//
|
|
// if (force || newData.EquipmentSlots != _playerData.EquipmentSlots)
|
|
// {
|
|
// _playerData.EquipmentSlots = newData.EquipmentSlots;
|
|
// changed = true;
|
|
// }
|
|
//
|
|
// if (changed) Dirty();
|
|
// }
|
|
//
|
|
// protected override void WriteDelta(PooledWriter writer, bool resetSyncTick = true)
|
|
// {
|
|
// base.WriteDelta(writer, resetSyncTick);
|
|
// WritePlayerData(writer, _playerData);
|
|
// }
|
|
//
|
|
// protected override void WriteFull(PooledWriter writer)
|
|
// {
|
|
// WriteDelta(writer, false);
|
|
// }
|
|
//
|
|
// protected override void Read(PooledReader reader, bool asServer)
|
|
// {
|
|
// var newData = ReadPlayerData(reader);
|
|
//
|
|
// // Update the _playerData with the new values
|
|
// SetData(newData, true);
|
|
//
|
|
// // Invoke any necessary callbacks or events
|
|
// OnDataChanged();
|
|
// }
|
|
//
|
|
// private void WritePlayerData(PooledWriter writer, PlayerData data)
|
|
// {
|
|
// writer.WriteString(data.playerName);
|
|
// writer.WriteInt32(data.level);
|
|
// writer.WriteInt32(data.health);
|
|
// // Write other fields...
|
|
//
|
|
// // Write dictionaries
|
|
// WriteSlotDictionary(writer, data.InventorySlots);
|
|
// WriteSlotDictionary(writer, data.ToolbarSlots);
|
|
// WriteSlotDictionary(writer, data.EquipmentSlots);
|
|
// }
|
|
//
|
|
// private PlayerData ReadPlayerData(PooledReader reader)
|
|
// {
|
|
// var data = ScriptableObject.CreateInstance<PlayerData>();
|
|
// data.playerName = reader.ReadString();
|
|
// data.level = reader.ReadInt32();
|
|
// data.health = reader.ReadInt32();
|
|
// // Read other fields...
|
|
//
|
|
// // Read dictionaries
|
|
// data.InventorySlots = ReadSlotDictionary(reader);
|
|
// data.ToolbarSlots = ReadSlotDictionary(reader);
|
|
// data.EquipmentSlots = ReadSlotDictionary(reader);
|
|
//
|
|
// return data;
|
|
// }
|
|
//
|
|
// private void WriteSlotDictionary(PooledWriter writer, Dictionary<int, Slot> dict)
|
|
// {
|
|
// writer.WriteInt32(dict.Count);
|
|
// foreach (var kvp in dict)
|
|
// {
|
|
// writer.WriteInt32(kvp.Key);
|
|
// WriteSlot(writer, kvp.Value);
|
|
// }
|
|
// }
|
|
//
|
|
// private Dictionary<int, Slot> ReadSlotDictionary(PooledReader reader)
|
|
// {
|
|
// var count = reader.ReadInt32();
|
|
// var dict = new Dictionary<int, Slot>();
|
|
// for (var i = 0; i < count; i++)
|
|
// {
|
|
// var key = reader.ReadInt32();
|
|
// var slot = ReadSlot(reader);
|
|
// dict[key] = slot;
|
|
// }
|
|
//
|
|
// return dict;
|
|
// }
|
|
//
|
|
// private void WriteSlot(PooledWriter writer, Slot slot)
|
|
// {
|
|
// // Implement slot writing logic
|
|
//
|
|
// writer.WriteInt32((int)slot.GetSlotType());
|
|
// writer.WriteInt32(slot.GetId());
|
|
// writer.WriteInt32(slot.GetPositionId());
|
|
//
|
|
// writer.WriteInt32(slot.GetItem().GetId());
|
|
// writer.WriteInt32(slot.GetItem().GetQuantity());
|
|
// }
|
|
//
|
|
// private Slot ReadSlot(PooledReader reader)
|
|
// {
|
|
// var slotType = (Slot.SlotType)reader.ReadInt32();
|
|
// var slotId = reader.ReadInt32();
|
|
// var position = reader.ReadInt32();
|
|
// var itemId = reader.ReadInt32();
|
|
// var quantity = reader.ReadInt32();
|
|
//
|
|
// // Implement slot reading logic
|
|
// var slot = ScriptableObject.CreateInstance<Slot>();
|
|
// slot.SetSlotId(slotId);
|
|
// slot.SetPosition(position);
|
|
// var item = ItemDatabase.Instance.GetItemById(itemId);
|
|
// item.quantity = quantity;
|
|
//
|
|
//
|
|
// slot.SetItem(item);
|
|
//
|
|
//
|
|
// return slot;
|
|
//
|
|
// // Set item properties
|
|
// }
|
|
//
|
|
// private void OnDataChanged()
|
|
// {
|
|
// // Implement any necessary callbacks or events when data changes
|
|
// }
|
|
// }
|
|
|