Add Equipment system backend, close #24

This commit is contained in:
Sofia 2017-05-14 22:16:20 +03:00
parent 2b2ae8fc60
commit db95bb3640
6 changed files with 177 additions and 1 deletions

View File

@ -6,7 +6,25 @@ namespace Cyber.Entities {
/// </summary>
public enum InteractionType : byte {
Activate, Deactivate, Enter, Exit
/// <summary>
/// The <see cref="SyncBases.Interactable"/> is clicked or otherwise activated.
/// </summary>
Activate,
/// <summary>
/// The <see cref="SyncBases.Interactable"/> is called when for example the mouse is released of it.
/// </summary>
Deactivate,
/// <summary>
/// When the <see cref="SyncBases.Interactable"/> is hovered on.
/// </summary>
Enter,
/// <summary>
///
/// </summary>
Exit
}
}

View File

@ -3,6 +3,7 @@ using Cyber.Console;
using Cyber.Items;
using Cyber.Networking;
using Cyber.Networking.Serverside;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
@ -18,11 +19,17 @@ namespace Cyber.Entities.SyncBases {
/// </summary>
public Drive Drive;
/// <summary>
/// This entity's <see cref="Items.Equipped"/> <see cref="Item"/>s.
/// </summary>
public Equipped Equipped;
/// <summary>
/// Creates the Inventory-component for a game object.
/// </summary>
public Inventory() {
Drive = new Drive(10f);
Equipped = new Equipped();
if (Server.IsRunning()) {
Drive.AddItem(ItemDB.Singleton.Get(0));
Drive.AddItem(ItemDB.Singleton.Get(1));
@ -39,6 +46,10 @@ namespace Cyber.Entities.SyncBases {
for (int i = 0; i < Items.Length; i++) {
Checksum ^= Items[i].ID;
}
var EquippedItems = Equipped.GetEquippedList().ToArray();
for (int i = 0; i < EquippedItems.Length; i++) {
Checksum ^= EquippedItems[i].ID;
}
return Checksum;
}
@ -68,6 +79,20 @@ namespace Cyber.Entities.SyncBases {
foreach (int id in IDs) {
Drive.AddItem(ItemDB.Singleton.Get(id));
}
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]));
}
}
/// <summary>
@ -85,6 +110,24 @@ namespace Cyber.Entities.SyncBases {
writer.WriteBytesFull(ByteArray[1]);
writer.WriteBytesFull(ByteArray[2]);
writer.WriteBytesFull(ByteArray[3]);
var slotList = new List<EquipSlot>(Equipped.GetEquippedDict().Keys).ConvertAll(x => (byte) x);
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]);
}
}
}

View File

@ -0,0 +1,25 @@

namespace Cyber.Items {
/// <summary>
/// Represents an slot where an item can be equipped.
/// </summary>
public enum EquipSlot : byte {
/// <summary>
/// On top of the head
/// </summary>
Hat,
/// <summary>
/// Right hand
/// </summary>
RightHand,
/// <summary>
/// Left hand
/// </summary>
LeftHand
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9c224eeda321b6c4ab1362602de6217b
timeCreated: 1494785848
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,66 @@

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();
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 1bc32847ce18a3b4987872f04887b6de
timeCreated: 1494785490
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: