Revamp inventory, make it into a 1D array
This commit is contained in:
parent
8e533220f0
commit
6c1669d1cc
@ -48,7 +48,7 @@ namespace Cyber.Controls {
|
|||||||
|
|
||||||
private void Update() {
|
private void Update() {
|
||||||
if (Time.time - LastUpdateTime >= 1f / UpdateFrequency) {
|
if (Time.time - LastUpdateTime >= 1f / UpdateFrequency) {
|
||||||
Dictionary<EquipSlot, Item> Equips = Inventory.Equipped.GetEquippedDict();
|
Dictionary<EquipSlot, Item> Equips = Inventory.Drive.GetEquippedItems();
|
||||||
// Empty all slots
|
// Empty all slots
|
||||||
for (int i = 0; i < Slots.Length; i++) {
|
for (int i = 0; i < Slots.Length; i++) {
|
||||||
Slots[i].Mesh.mesh = null;
|
Slots[i].Mesh.mesh = null;
|
||||||
|
@ -143,16 +143,15 @@ namespace Cyber.Controls {
|
|||||||
}
|
}
|
||||||
if (Input.GetButtonDown("Equip")) {
|
if (Input.GetButtonDown("Equip")) {
|
||||||
// Selected index was already this => equip (double-clicked)
|
// Selected index was already this => equip (double-clicked)
|
||||||
Item SelectedItem = Inventory.Drive.Interface.GetItemAt(ItemGridSelectedIndex % (int) ItemGridDimensions.x,
|
Item SelectedItem = Inventory.Drive.GetItemAt(ItemGridSelectedIndex);
|
||||||
ItemGridSelectedIndex / (int) ItemGridDimensions.y);
|
|
||||||
if (SelectedItem != null) {
|
if (SelectedItem != null) {
|
||||||
Item Equipped = Inventory.Equipped.GetItem(SelectedItem.Slot);
|
Item Equipped = Inventory.Drive.GetSlot(SelectedItem.Slot);
|
||||||
if (Equipped != null && Equipped.ID == SelectedItem.ID) {
|
if (Equipped != null && Equipped.ID == SelectedItem.ID) {
|
||||||
Inventory.Equipped.ClearSlot(SelectedItem.Slot);
|
Inventory.Drive.UnequipSlot(SelectedItem.Slot);
|
||||||
Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildClearSlot(SelectedItem.Slot));
|
Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildClearSlot(SelectedItem.Slot));
|
||||||
} else {
|
} else {
|
||||||
Inventory.Equipped.SetSlot(SelectedItem.Slot, SelectedItem);
|
Inventory.Drive.EquipItem(ItemGridSelectedIndex);
|
||||||
Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildEquipItem(SelectedItem.ID));
|
Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildEquipItem(ItemGridSelectedIndex));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -206,7 +205,7 @@ namespace Cyber.Controls {
|
|||||||
for (int x = 0; x < ItemGridDimensions.x; x++) {
|
for (int x = 0; x < ItemGridDimensions.x; x++) {
|
||||||
// Find the item and mesh
|
// Find the item and mesh
|
||||||
int i = x + y * (int) ItemGridDimensions.x;
|
int i = x + y * (int) ItemGridDimensions.x;
|
||||||
Item Item = Inventory.Drive.Interface.GetItemAt(x, y);
|
Item Item = Inventory.Drive.GetItemAt(x + y * (int) ItemGridDimensions.y);
|
||||||
Mesh Mesh = null;
|
Mesh Mesh = null;
|
||||||
if (Item != null) {
|
if (Item != null) {
|
||||||
Mesh = MeshDB.GetMesh(Item.ModelID);
|
Mesh = MeshDB.GetMesh(Item.ModelID);
|
||||||
|
@ -4,6 +4,7 @@ using Cyber.Items;
|
|||||||
using Cyber.Networking;
|
using Cyber.Networking;
|
||||||
using Cyber.Networking.Serverside;
|
using Cyber.Networking.Serverside;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
using UnityEngine.Networking;
|
using UnityEngine.Networking;
|
||||||
|
|
||||||
namespace Cyber.Entities.SyncBases {
|
namespace Cyber.Entities.SyncBases {
|
||||||
@ -18,11 +19,6 @@ namespace Cyber.Entities.SyncBases {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public Drive Drive;
|
public Drive Drive;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// This entity's <see cref="Items.Equipped"/> <see cref="Item"/>s.
|
|
||||||
/// </summary>
|
|
||||||
public Equipped Equipped;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The possible <see cref="Character"/> component associated with this Inventory. Used in <see cref="UseItemInSlot(EquipSlot)"/>.
|
/// The possible <see cref="Character"/> component associated with this Inventory. Used in <see cref="UseItemInSlot(EquipSlot)"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -38,7 +34,6 @@ namespace Cyber.Entities.SyncBases {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public Inventory() {
|
public Inventory() {
|
||||||
Drive = new Drive(10f);
|
Drive = new Drive(10f);
|
||||||
Equipped = new Equipped();
|
|
||||||
if (Server.IsRunning()) {
|
if (Server.IsRunning()) {
|
||||||
Drive.AddItem(ItemDB.Singleton.Get(0));
|
Drive.AddItem(ItemDB.Singleton.Get(0));
|
||||||
Drive.AddItem(ItemDB.Singleton.Get(1));
|
Drive.AddItem(ItemDB.Singleton.Get(1));
|
||||||
@ -51,15 +46,11 @@ namespace Cyber.Entities.SyncBases {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>A checksum of the IDs of the items</returns>
|
/// <returns>A checksum of the IDs of the items</returns>
|
||||||
public override int GenerateChecksum() {
|
public override int GenerateChecksum() {
|
||||||
var Items = Drive.GetItems().ToArray();
|
var Slots = Drive.GetSlots();
|
||||||
int Checksum = 0;
|
int Checksum = 0;
|
||||||
for (int i = 0; i < Items.Length; i++) {
|
for (int i = 0; i < Slots.Length; i++) {
|
||||||
// Times with primes and sprinkle some i to spice up the stew
|
// Times with primes and sprinkle some i to spice up the stew
|
||||||
Checksum += (Items[i].ID + 1) * 509 * (i + 1) * 53;
|
Checksum += (((Slots[i].Item != null) ? Slots[i].Item.ID : i) + 1) * 509 * (i + 1) * 53 + (Slots[i].Equipped ? 1789 : 431);
|
||||||
}
|
|
||||||
var EquippedItems = Equipped.GetEquippedList().ToArray();
|
|
||||||
for (int i = 0; i < EquippedItems.Length; i++) {
|
|
||||||
Checksum += (EquippedItems[i].ID + 1) * 859 * (i + 1) * 97;
|
|
||||||
}
|
}
|
||||||
return Checksum;
|
return Checksum;
|
||||||
}
|
}
|
||||||
@ -76,7 +67,7 @@ namespace Cyber.Entities.SyncBases {
|
|||||||
/// Uses the item in the left hand if something is equipped.
|
/// Uses the item in the left hand if something is equipped.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void UseItemInSlot(EquipSlot slot) {
|
public void UseItemInSlot(EquipSlot slot) {
|
||||||
Item Item = Equipped.GetItem(slot);
|
Item Item = Drive.GetSlot(slot);
|
||||||
if (Item != null && Item.Action != null && Character != null) {
|
if (Item != null && Item.Action != null && Character != null) {
|
||||||
Item.Action(Character);
|
Item.Action(Character);
|
||||||
}
|
}
|
||||||
@ -95,29 +86,15 @@ namespace Cyber.Entities.SyncBases {
|
|||||||
ByteArray[3] = reader.ReadBytesAndSize();
|
ByteArray[3] = reader.ReadBytesAndSize();
|
||||||
int[] IDs = NetworkHelper.DeserializeIntArray(ByteArray);
|
int[] IDs = NetworkHelper.DeserializeIntArray(ByteArray);
|
||||||
|
|
||||||
|
byte[] Equippeds = reader.ReadBytesAndSize();
|
||||||
|
|
||||||
Drive.Clear();
|
Drive.Clear();
|
||||||
foreach (int id in IDs) {
|
for (int i = 0; i < IDs.Length; i++) {
|
||||||
Drive.AddItem(ItemDB.Singleton.Get(id));
|
int ID = IDs[i];
|
||||||
|
if (ID >= 0) {
|
||||||
|
Drive.AddItemToIndex(ItemDB.Singleton.Get(ID), i);
|
||||||
|
Drive.GetSlots()[i].Equipped = (Equippeds[i] == 1 ? true : false);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ReceivedSlots = reader.ReadBoolean();
|
|
||||||
if (!ReceivedSlots) {
|
|
||||||
Equipped.ClearAllEquipped();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] Slots = reader.ReadBytesAndSize();
|
|
||||||
|
|
||||||
byte[][] EquippedIdsBytes = new byte[4][];
|
|
||||||
EquippedIdsBytes[0] = reader.ReadBytesAndSize();
|
|
||||||
EquippedIdsBytes[1] = reader.ReadBytesAndSize();
|
|
||||||
EquippedIdsBytes[2] = reader.ReadBytesAndSize();
|
|
||||||
EquippedIdsBytes[3] = reader.ReadBytesAndSize();
|
|
||||||
int[] EquippedIds = NetworkHelper.DeserializeIntArray(EquippedIdsBytes);
|
|
||||||
|
|
||||||
Equipped.ClearAllEquipped();
|
|
||||||
for (int i = 0; i < Slots.Length; i++) {
|
|
||||||
Equipped.SetSlot((EquipSlot) Slots[i], ItemDB.Singleton.Get(EquippedIds[i]));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,11 +103,19 @@ namespace Cyber.Entities.SyncBases {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="writer"></param>
|
/// <param name="writer"></param>
|
||||||
public override void Serialize(NetworkWriter writer) {
|
public override void Serialize(NetworkWriter writer) {
|
||||||
var Items = Drive.GetItems();
|
Slot[] Slots = Drive.GetSlots();
|
||||||
int[] IDs = new int[Items.Count];
|
|
||||||
for (int i = 0; i < Items.Count; i++) {
|
int[] IDs = new int[Slots.Length];
|
||||||
IDs[i] = Items[i].ID;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
byte[][] ByteArray = NetworkHelper.SerializeIntArray(IDs);
|
byte[][] ByteArray = NetworkHelper.SerializeIntArray(IDs);
|
||||||
|
|
||||||
writer.WriteBytesFull(ByteArray[0]);
|
writer.WriteBytesFull(ByteArray[0]);
|
||||||
@ -138,30 +123,7 @@ namespace Cyber.Entities.SyncBases {
|
|||||||
writer.WriteBytesFull(ByteArray[2]);
|
writer.WriteBytesFull(ByteArray[2]);
|
||||||
writer.WriteBytesFull(ByteArray[3]);
|
writer.WriteBytesFull(ByteArray[3]);
|
||||||
|
|
||||||
var slotList = new List<EquipSlot>(Equipped.GetEquippedDict().Keys).ConvertAll(x => (byte) x);
|
writer.WriteBytesFull(Equippeds);
|
||||||
|
|
||||||
if (slotList.Count > 0) {
|
|
||||||
writer.Write(true);
|
|
||||||
} else {
|
|
||||||
writer.Write(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
slotList.Sort((a, b) => {
|
|
||||||
return b - a;
|
|
||||||
});
|
|
||||||
|
|
||||||
var idList = new List<int>();
|
|
||||||
slotList.ForEach(x => {
|
|
||||||
idList.Add(Equipped.GetItem((EquipSlot) x).ID);
|
|
||||||
});
|
|
||||||
|
|
||||||
writer.WriteBytesFull(slotList.ToArray());
|
|
||||||
|
|
||||||
byte[][] EquippedByteArray = NetworkHelper.SerializeIntArray(idList.ToArray());
|
|
||||||
writer.WriteBytesFull(EquippedByteArray[0]);
|
|
||||||
writer.WriteBytesFull(EquippedByteArray[1]);
|
|
||||||
writer.WriteBytesFull(EquippedByteArray[2]);
|
|
||||||
writer.WriteBytesFull(EquippedByteArray[3]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Start() {
|
private void Start() {
|
||||||
|
@ -8,25 +8,19 @@ namespace Cyber.Items {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class Drive {
|
public class Drive {
|
||||||
|
|
||||||
private List<Item> Items = new List<Item>();
|
private Slot[] Slots = new Slot[0];
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The capacity of the drive, meaning how much stuff can this drive contain.
|
/// The capacity of the drive, meaning how much stuff can this drive contain.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly float Capacity;
|
public readonly float Capacity;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The interface-class of this Drive.
|
|
||||||
/// </summary>
|
|
||||||
public DriveInterface Interface;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a drive with given capacity. Capacity cannot be changed after this.
|
/// Creates a drive with given capacity. Capacity cannot be changed after this.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="capacity">Capacity of the drive</param>
|
/// <param name="capacity">Capacity of the drive</param>
|
||||||
public Drive(float capacity) {
|
public Drive(float capacity) {
|
||||||
Capacity = capacity;
|
Capacity = capacity;
|
||||||
Interface = new DriveInterface(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -35,8 +29,10 @@ namespace Cyber.Items {
|
|||||||
/// <returns>The totam weight</returns>
|
/// <returns>The totam weight</returns>
|
||||||
public float TotalWeight() {
|
public float TotalWeight() {
|
||||||
float sum = 0;
|
float sum = 0;
|
||||||
foreach (Item item in Items) {
|
foreach (Slot slot in Slots) {
|
||||||
sum += item.Weight;
|
if (slot.Item != null) {
|
||||||
|
sum += slot.Item.Weight;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
@ -54,7 +50,7 @@ namespace Cyber.Items {
|
|||||||
/// <see cref="Inventory.Deserialize(NetworkReader)"/>
|
/// <see cref="Inventory.Deserialize(NetworkReader)"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Clear() {
|
public void Clear() {
|
||||||
Items.Clear();
|
Slots = new Slot[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -65,21 +61,50 @@ namespace Cyber.Items {
|
|||||||
if (item.Weight > FreeSpace()) {
|
if (item.Weight > FreeSpace()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Interface.AddNewItem(Items.Count);
|
bool foundEmpty = false;
|
||||||
Items.Add(item);
|
for (int i = 0; i < Slots.Length; i++) {
|
||||||
|
if (Slots[i].Item == null) {
|
||||||
|
Slots[i] = new Slot(item, false);
|
||||||
|
foundEmpty = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!foundEmpty) {
|
||||||
|
// Add space and set the item there
|
||||||
|
IncreaseCapacity(1);
|
||||||
|
Slots[Slots.Length - 1] = new Slot(item, false);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to add an item to a specific slot. Returns false if slot already occupied.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item">Item to add.</param>
|
||||||
|
/// <param name="idx">Index in the inventory.</param>
|
||||||
|
/// <returns>Weather the slot was empty or not.</returns>
|
||||||
|
public bool AddItemToIndex(Item item, int idx) {
|
||||||
|
if (idx < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (GetItemAt(idx) == null) {
|
||||||
|
if (Slots.Length > idx) {
|
||||||
|
Slots[idx].Item = item;
|
||||||
|
} else {
|
||||||
|
IncreaseCapacity(idx - Slots.Length + 1);
|
||||||
|
Slots[idx].Item = item;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the item at the given index, or null if there is nothing.
|
/// Gets the item at the given index, or null if there is nothing.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="idx">The index of the desired item</param>
|
/// <param name="idx">The index of the desired item</param>
|
||||||
/// <returns>The item or null if nothing was found.</returns>
|
/// <returns>The item or null if nothing was found.</returns>
|
||||||
public Item GetItem(int idx) {
|
public Item GetItemAt(int idx) {
|
||||||
if (idx < 0 || idx >= Items.Count) {
|
return GetSlotAt(idx).Item;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return Items[idx];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -87,8 +112,104 @@ namespace Cyber.Items {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public List<Item> GetItems() {
|
public List<Item> GetItems() {
|
||||||
|
List<Item> Items = new List<Item>();
|
||||||
|
foreach (Slot slot in Slots) {
|
||||||
|
if (slot.Item != null) {
|
||||||
|
Items.Add(slot.Item);
|
||||||
|
}
|
||||||
|
}
|
||||||
return Items;
|
return Items;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the item at the given slot. Loops through all items and checks if there is an item equipped at the given slot and returns it if there is.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="slot">The desired slot</param>
|
||||||
|
/// <returns>The item at the slot, or null if no items</returns>
|
||||||
|
public Item GetSlot(EquipSlot slot) {
|
||||||
|
Item Item = null;
|
||||||
|
foreach (Slot s in Slots) {
|
||||||
|
if (s.Item != null && s.Item.Slot == slot && s.Equipped) {
|
||||||
|
Item = s.Item;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Item;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns all the equipped items in a dictionary.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The dictionary of items.</returns>
|
||||||
|
public Dictionary<EquipSlot, Item> GetEquippedItems() {
|
||||||
|
Dictionary<EquipSlot, Item> Items = new Dictionary<EquipSlot, Item>();
|
||||||
|
foreach (Slot s in Slots) {
|
||||||
|
if (s.Item != null && s.Equipped) {
|
||||||
|
Items.Add(s.Item.Slot, s.Item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Items;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attempt to equip the item at the given index.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="idx">Equip the iten at the given slot</param>
|
||||||
|
/// <returns>Weather the item could be equipped or if there already exists something on that equip slot.</returns>
|
||||||
|
public bool EquipItem(int idx) {
|
||||||
|
Slot Slot = GetSlotAt(idx);
|
||||||
|
if (Slot.Item != null && GetSlot(Slot.Item.Slot) == null) {
|
||||||
|
Slots[idx].Equipped = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Unequips the item at the given inventory index.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="idx">The index to unequip.</param>
|
||||||
|
public void UnequipItem(int idx) {
|
||||||
|
Slot Slot = GetSlotAt(idx);
|
||||||
|
if (Slot.Item != null) {
|
||||||
|
Slots[idx].Equipped = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Unequip all items that are in this slot.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="equipSlot">The slot of the desired item to be unequipped.</param>
|
||||||
|
public void UnequipSlot(EquipSlot equipSlot) {
|
||||||
|
for (int i = 0; i < Slots.Length; i++) {
|
||||||
|
if (Slots[i].Item != null && Slots[i].Item.Slot == equipSlot) {
|
||||||
|
Slots[i].Equipped = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Simply returns the slots-array. The fastest way to get all of inventory if needed.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The Slot-structs</returns>
|
||||||
|
public Slot[] GetSlots() {
|
||||||
|
return Slots;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void IncreaseCapacity(int moreCapacity) {
|
||||||
|
Slot[] NewSlots = new Slot[Slots.Length + moreCapacity];
|
||||||
|
for (int i = 0; i < Slots.Length; i++) {
|
||||||
|
NewSlots[i] = Slots[i];
|
||||||
|
}
|
||||||
|
Slots = NewSlots;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Slot GetSlotAt(int idx) {
|
||||||
|
if (idx < 0 || idx >= Slots.Length) {
|
||||||
|
return new Slot(null, false);
|
||||||
|
}
|
||||||
|
return Slots[idx];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,116 +0,0 @@
|
|||||||
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace Cyber.Items {
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The <see cref="Drive"/> interface, which contains a grid of indices of items in the <see cref="Drive"/>. Use <see cref="GetItemAt(int, int)"/> to get items in the interface.
|
|
||||||
/// </summary>
|
|
||||||
public class DriveInterface {
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Width of the interface.
|
|
||||||
/// </summary>
|
|
||||||
public const int Width = 7;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Minimun height of the interface.
|
|
||||||
/// </summary>
|
|
||||||
public const int MinHeight = 4;
|
|
||||||
|
|
||||||
private int[,] ItemGrid;
|
|
||||||
|
|
||||||
private Drive Drive;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a Drive interface for a <see cref="Drive"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="drive"></param>
|
|
||||||
public DriveInterface(Drive drive) {
|
|
||||||
Drive = drive;
|
|
||||||
ItemGrid = CreateEmptyGrid(Width, 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the item at the specified coordinate on the interface. Returns null if invalid or empty coordinate.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="x">The x-coordinate</param>
|
|
||||||
/// <param name="y">The y-coordinate</param>
|
|
||||||
/// <returns>The item or null</returns>
|
|
||||||
public Item GetItemAt(int x, int y) {
|
|
||||||
if (y < 0 || x < 0 || y >= GetHeight() || x >= GetWidth() ||
|
|
||||||
ItemGrid[y, x] == -1) {
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
return Drive.GetItem(ItemGrid[y, x]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the Width of the interface, or simply <see cref="Width"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public int GetWidth() {
|
|
||||||
return Width;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the current height of the interface
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public int GetHeight() {
|
|
||||||
return ItemGrid.GetLength(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Updates the height of the interface, adding new rows or deleting old useless ones.
|
|
||||||
/// </summary>
|
|
||||||
public void UpdateHeight() {
|
|
||||||
int RequiredHeight = MinHeight;
|
|
||||||
for (int y = MinHeight; y < GetHeight(); y++) {
|
|
||||||
for (int x = 0; x < Width; x++) {
|
|
||||||
if (GetItemAt(x, y) == null || (x == Width - 1 && y == GetHeight() - 1)) {
|
|
||||||
RequiredHeight = y;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int[,] Temp = CreateEmptyGrid(Width, RequiredHeight + 1);
|
|
||||||
for (int y = 0; y < RequiredHeight - 1; y++) {
|
|
||||||
for (int x = 0; x < Width; x++) {
|
|
||||||
if (GetItemAt(x, y) != null) {
|
|
||||||
Temp[y, x] = Drive.GetItems().IndexOf(GetItemAt(x, y));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ItemGrid = Temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a new item to the grid. The idx in the parameter is the idx of the item in the drive.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="idx"></param>
|
|
||||||
public void AddNewItem(int idx) {
|
|
||||||
UpdateHeight();
|
|
||||||
for (int y = 0; y < GetHeight(); y++) {
|
|
||||||
for (int x = 0; x < Width; x++) {
|
|
||||||
if (GetItemAt(x, y) == null) {
|
|
||||||
ItemGrid[y, x] = idx;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private int[,] CreateEmptyGrid(int width, int height) {
|
|
||||||
int[,] Grid = new int[height, width];
|
|
||||||
for (int y = 0; y < height; y++) {
|
|
||||||
for (int x = 0; x < width; x++) {
|
|
||||||
Grid[y, x] = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Grid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,66 +0,0 @@
|
|||||||
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Cyber.Items {
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Represents the equipped items at given slots.
|
|
||||||
/// </summary>
|
|
||||||
public class Equipped {
|
|
||||||
|
|
||||||
Dictionary<EquipSlot, Item> EquippedItems = new Dictionary<EquipSlot, Item>();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Inserts an item here, marking it as 'equipped'.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="slot">The slot to equip the item to.</param>
|
|
||||||
/// <param name="item">The item to equip.</param>
|
|
||||||
public void SetSlot(EquipSlot slot, Item item) {
|
|
||||||
EquippedItems[slot] = item;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Empties the desired slot of any items.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="slot">The slot to empty.</param>
|
|
||||||
public void ClearSlot(EquipSlot slot) {
|
|
||||||
EquippedItems.Remove(slot);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the item at the given slot, or null if no item at the slot was found.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="slot"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public Item GetItem(EquipSlot slot) {
|
|
||||||
if (EquippedItems.ContainsKey(slot)) {
|
|
||||||
return EquippedItems[slot];
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a dictionary of all equipped items.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>Dictionary of equipped items.</returns>
|
|
||||||
public Dictionary<EquipSlot, Item> GetEquippedDict() {
|
|
||||||
return EquippedItems;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a list of all items that are generally equipped.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>List of equipped items.</returns>
|
|
||||||
public List<Item> GetEquippedList() {
|
|
||||||
return new List<Item>(EquippedItems.Values);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Clears all equipped items, removing them from their slots.
|
|
||||||
/// </summary>
|
|
||||||
public void ClearAllEquipped() {
|
|
||||||
EquippedItems.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 1bc32847ce18a3b4987872f04887b6de
|
|
||||||
timeCreated: 1494785490
|
|
||||||
licenseType: Free
|
|
||||||
MonoImporter:
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
@ -44,10 +44,10 @@ namespace Cyber.Items {
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Builds a <see cref="InventoryAction.Equip"/> packet.
|
/// Builds a <see cref="InventoryAction.Equip"/> packet.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="itemID">The item ID to equip.</param>
|
/// <param name="itemIdx">The item index to equip.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public InventoryActionPkt BuildEquipItem(int itemID) {
|
public InventoryActionPkt BuildEquipItem(int itemIdx) {
|
||||||
return new InventoryActionPkt(InventoryAction.Equip, itemID);
|
return new InventoryActionPkt(InventoryAction.Equip, itemIdx);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -59,16 +59,15 @@ namespace Cyber.Items {
|
|||||||
public bool HandleAction(InventoryAction action, int relatedInt) {
|
public bool HandleAction(InventoryAction action, int relatedInt) {
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case InventoryAction.Equip:
|
case InventoryAction.Equip:
|
||||||
Item Item = ItemDB.Singleton.Get(relatedInt);
|
Inventory.Drive.EquipItem(relatedInt);
|
||||||
Inventory.Equipped.SetSlot(Item.Slot, Item);
|
|
||||||
return true;
|
return true;
|
||||||
case InventoryAction.Unequip:
|
case InventoryAction.Unequip:
|
||||||
EquipSlot Slot = (EquipSlot) relatedInt;
|
EquipSlot Slot = (EquipSlot) relatedInt;
|
||||||
Inventory.Equipped.ClearSlot(Slot);
|
Inventory.Drive.UnequipSlot(Slot);
|
||||||
return true;
|
return true;
|
||||||
case InventoryAction.Use:
|
case InventoryAction.Use:
|
||||||
EquipSlot UseSlot = (EquipSlot) relatedInt;
|
EquipSlot UseSlot = (EquipSlot) relatedInt;
|
||||||
Item UseItem = Inventory.Equipped.GetItem(UseSlot);
|
Item UseItem = Inventory.Drive.GetSlot(UseSlot);
|
||||||
if (UseItem != null && UseItem.Action != null && Character != null &&
|
if (UseItem != null && UseItem.Action != null && Character != null &&
|
||||||
(!Client.IsRunning() || Client.GetConnectedPlayer().Character != Character)) {
|
(!Client.IsRunning() || Client.GetConnectedPlayer().Character != Character)) {
|
||||||
// Item exists, it has an action, and the character
|
// Item exists, it has an action, and the character
|
||||||
|
30
Assets/Scripts/Items/Slot.cs
Normal file
30
Assets/Scripts/Items/Slot.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
|
||||||
|
namespace Cyber.Items {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a slot which contains an item and a bool weather the item is equipped or not.
|
||||||
|
/// </summary>
|
||||||
|
public struct Slot {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The item that this slot holds. If this is null, the slot is empty.
|
||||||
|
/// </summary>
|
||||||
|
public Item Item;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Weather this item is equipped or not.
|
||||||
|
/// </summary>
|
||||||
|
public bool Equipped;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a slot.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item">Item in the slot, or null if the slot is empty.</param>
|
||||||
|
/// <param name="equipped">Weather this slot is equipped or not.</param>
|
||||||
|
public Slot(Item item, bool equipped) {
|
||||||
|
Item = item;
|
||||||
|
Equipped = equipped;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: fc91d98b617f041da8ff205f4252ed06
|
guid: ca2662d3bcd2ddf4d94a410f907d028d
|
||||||
timeCreated: 1494705607
|
timeCreated: 1494949326
|
||||||
licenseType: Free
|
licenseType: Free
|
||||||
MonoImporter:
|
MonoImporter:
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
Loading…
Reference in New Issue
Block a user