2017-05-13 21:54:14 +02:00
|
|
|
|
|
2017-05-14 00:59:30 +02:00
|
|
|
|
using Cyber.Console;
|
2017-05-13 21:54:14 +02:00
|
|
|
|
using Cyber.Items;
|
|
|
|
|
using Cyber.Networking;
|
2017-05-14 00:59:30 +02:00
|
|
|
|
using Cyber.Networking.Serverside;
|
2017-05-14 21:16:20 +02:00
|
|
|
|
using System.Collections.Generic;
|
2017-05-16 19:35:38 +02:00
|
|
|
|
using UnityEngine;
|
2017-05-13 21:54:14 +02:00
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
namespace Cyber.Entities.SyncBases {
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The Inventory component, used for managing the inventory of a <see cref="Character"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Inventory : SyncBase {
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Refrence of the actual <see cref="Drive"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Drive Drive;
|
|
|
|
|
|
2017-05-16 10:36:31 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The possible <see cref="Character"/> component associated with this Inventory. Used in <see cref="UseItemInSlot(EquipSlot)"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Character Character;
|
|
|
|
|
|
2017-05-16 14:21:42 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The <see cref="InventoryActionHandler"/> which handles inventory actions properly.
|
|
|
|
|
/// </summary>
|
2017-05-16 10:36:31 +02:00
|
|
|
|
public InventoryActionHandler ActionHandler;
|
|
|
|
|
|
2017-05-13 21:54:14 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the Inventory-component for a game object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Inventory() {
|
|
|
|
|
Drive = new Drive(10f);
|
2017-05-14 00:59:30 +02:00
|
|
|
|
if (Server.IsRunning()) {
|
|
|
|
|
Drive.AddItem(ItemDB.Singleton.Get(0));
|
|
|
|
|
Drive.AddItem(ItemDB.Singleton.Get(1));
|
2017-05-15 21:30:12 +02:00
|
|
|
|
Drive.AddItem(ItemDB.Singleton.Get(2));
|
2017-05-14 00:59:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-05-14 23:17:42 +02:00
|
|
|
|
/// Generates a checksum for the inventory.
|
2017-05-14 00:59:30 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A checksum of the IDs of the items</returns>
|
|
|
|
|
public override int GenerateChecksum() {
|
2017-05-16 19:35:38 +02:00
|
|
|
|
var Slots = Drive.GetSlots();
|
2017-05-14 00:59:30 +02:00
|
|
|
|
int Checksum = 0;
|
2017-05-16 19:35:38 +02:00
|
|
|
|
for (int i = 0; i < Slots.Length; i++) {
|
2017-05-14 23:17:42 +02:00
|
|
|
|
// Times with primes and sprinkle some i to spice up the stew
|
2017-05-16 19:35:38 +02:00
|
|
|
|
Checksum += (((Slots[i].Item != null) ? Slots[i].Item.ID : i) + 1) * 509 * (i + 1) * 53 + (Slots[i].Equipped ? 1789 : 431);
|
2017-05-14 21:16:20 +02:00
|
|
|
|
}
|
2017-05-14 00:59:30 +02:00
|
|
|
|
return Checksum;
|
2017-05-13 21:54:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the sync handletype indicating how the inventory should be synced.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override SyncHandletype GetSyncHandletype() {
|
|
|
|
|
return new SyncHandletype(true, 10);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 10:36:31 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Uses the item in the left hand if something is equipped.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void UseItemInSlot(EquipSlot slot) {
|
2017-05-16 19:35:38 +02:00
|
|
|
|
Item Item = Drive.GetSlot(slot);
|
2017-05-16 10:36:31 +02:00
|
|
|
|
if (Item != null && Item.Action != null && Character != null) {
|
|
|
|
|
Item.Action(Character);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-13 21:54:14 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deserializes the ID's and creates them in the <see cref="Drive"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="reader"></param>
|
|
|
|
|
public override void Deserialize(NetworkReader reader) {
|
2017-05-14 00:59:30 +02:00
|
|
|
|
|
2017-05-13 21:54:14 +02:00
|
|
|
|
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);
|
|
|
|
|
|
2017-05-16 19:35:38 +02:00
|
|
|
|
byte[] Equippeds = reader.ReadBytesAndSize();
|
2017-05-14 21:16:20 +02:00
|
|
|
|
|
2017-05-16 19:35:38 +02:00
|
|
|
|
Drive.Clear();
|
|
|
|
|
for (int i = 0; i < IDs.Length; i++) {
|
|
|
|
|
int ID = IDs[i];
|
|
|
|
|
if (ID >= 0) {
|
|
|
|
|
Drive.AddItemToIndex(ItemDB.Singleton.Get(ID), i);
|
|
|
|
|
Drive.GetSlots()[i].Equipped = (Equippeds[i] == 1 ? true : false);
|
|
|
|
|
}
|
2017-05-14 21:16:20 +02:00
|
|
|
|
}
|
2017-05-13 21:54:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializes only the <see cref="Drive"/>'s item IDs.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="writer"></param>
|
|
|
|
|
public override void Serialize(NetworkWriter writer) {
|
2017-05-16 19:35:38 +02:00
|
|
|
|
Slot[] Slots = Drive.GetSlots();
|
|
|
|
|
|
|
|
|
|
int[] IDs = new int[Slots.Length];
|
|
|
|
|
byte[] Equippeds = new byte[Slots.Length];
|
|
|
|
|
for (int i = 0; i < Slots.Length; i++) {
|
|
|
|
|
if (Slots[i].Item == null) {
|
|
|
|
|
IDs[i] = -1;
|
|
|
|
|
} else {
|
|
|
|
|
IDs[i] = Slots[i].Item.ID;
|
|
|
|
|
}
|
|
|
|
|
Equippeds[i] = (byte) ((Slots[i].Equipped) ? 1 : 0);
|
2017-05-13 21:54:14 +02:00
|
|
|
|
}
|
2017-05-16 19:35:38 +02:00
|
|
|
|
|
2017-05-13 21:54:14 +02:00
|
|
|
|
byte[][] ByteArray = NetworkHelper.SerializeIntArray(IDs);
|
2017-05-14 22:47:45 +02:00
|
|
|
|
|
2017-05-13 21:54:14 +02:00
|
|
|
|
writer.WriteBytesFull(ByteArray[0]);
|
|
|
|
|
writer.WriteBytesFull(ByteArray[1]);
|
|
|
|
|
writer.WriteBytesFull(ByteArray[2]);
|
|
|
|
|
writer.WriteBytesFull(ByteArray[3]);
|
2017-05-14 21:16:20 +02:00
|
|
|
|
|
2017-05-16 19:35:38 +02:00
|
|
|
|
writer.WriteBytesFull(Equippeds);
|
2017-05-13 21:54:14 +02:00
|
|
|
|
}
|
2017-05-16 10:36:31 +02:00
|
|
|
|
|
|
|
|
|
private void Start() {
|
|
|
|
|
Character = GetComponent<Character>();
|
|
|
|
|
ActionHandler = new InventoryActionHandler(this, Character);
|
|
|
|
|
}
|
2017-05-13 21:54:14 +02:00
|
|
|
|
}
|
|
|
|
|
}
|