using Cyber.Console;
using Cyber.Items;
using Cyber.Networking;
using Cyber.Networking.Serverside;
using UnityEngine;
using UnityEngine.Networking;
namespace Cyber.Entities.SyncBases {
///
/// The Inventory component, used for managing the inventory of a .
///
public class Inventory : SyncBase {
///
/// Refrence of the actual .
///
public Drive Drive;
///
/// Creates the Inventory-component for a game object.
///
public Inventory() {
Drive = new Drive(10f);
if (Server.IsRunning()) {
Drive.AddItem(ItemDB.Singleton.Get(0));
Drive.AddItem(ItemDB.Singleton.Get(1));
}
}
///
/// Generates a checksum for the inventory
///
/// A checksum of the IDs of the items
public override int GenerateChecksum() {
var Items = Drive.GetItems().ToArray();
int Checksum = 0;
for (int i = 0; i < Items.Length; i++) {
Checksum ^= Items[i].ID;
}
return Checksum;
}
///
/// Returns the sync handletype indicating how the inventory should be synced.
///
///
public override SyncHandletype GetSyncHandletype() {
return new SyncHandletype(true, 10);
}
///
/// Deserializes the ID's and creates them in the .
///
///
public override void Deserialize(NetworkReader reader) {
Debug.Log("Deserializing inventory!");
byte[][] ByteArray = new byte[4][];
ByteArray[0] = reader.ReadBytesAndSize();
ByteArray[1] = reader.ReadBytesAndSize();
ByteArray[2] = reader.ReadBytesAndSize();
ByteArray[3] = reader.ReadBytesAndSize();
int[] IDs = NetworkHelper.DeserializeIntArray(ByteArray);
Drive.Clear();
foreach (int id in IDs) {
Drive.AddItem(ItemDB.Singleton.Get(id));
}
}
///
/// Serializes only the 's item IDs.
///
///
public override void Serialize(NetworkWriter writer) {
var Items = Drive.GetItems();
int[] IDs = new int[Items.Count];
for (int i = 0; i < Items.Count; i++) {
IDs[i] = Items[i].ID;
}
byte[][] ByteArray = NetworkHelper.SerializeIntArray(IDs);
writer.WriteBytesFull(ByteArray[0]);
writer.WriteBytesFull(ByteArray[1]);
writer.WriteBytesFull(ByteArray[2]);
writer.WriteBytesFull(ByteArray[3]);
}
}
}