diff --git a/Assets/Prefabs/NPC.prefab b/Assets/Prefabs/NPC.prefab index c057721..153c57b 100644 --- a/Assets/Prefabs/NPC.prefab +++ b/Assets/Prefabs/NPC.prefab @@ -38,6 +38,7 @@ GameObject: - component: {fileID: 4844777526459442} - component: {fileID: 143753897266899886} - component: {fileID: 114052379458543858} + - component: {fileID: 114934786006823366} m_Layer: 0 m_Name: NPC m_TagString: Untagged @@ -293,8 +294,21 @@ MonoBehaviour: m_EditorClassIdentifier: ID: 0 MovementSpeed: 5 + InteractionDistance: 2 CharacterController: {fileID: 143753897266899886} Head: {fileID: 4900355877646882} +--- !u!114 &114934786006823366 +MonoBehaviour: + m_ObjectHideFlags: 1 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 100100000} + m_GameObject: {fileID: 1045762529817142} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 28f60457b196bf144a8b332b7327db1f, type: 3} + m_Name: + m_EditorClassIdentifier: + ID: 0 --- !u!143 &143753897266899886 CharacterController: m_ObjectHideFlags: 1 diff --git a/Assets/Prefabs/PC.prefab b/Assets/Prefabs/PC.prefab index 8ac1750..35714fb 100644 --- a/Assets/Prefabs/PC.prefab +++ b/Assets/Prefabs/PC.prefab @@ -59,6 +59,7 @@ GameObject: - component: {fileID: 143869468979164672} - component: {fileID: 114575501420754388} - component: {fileID: 114385213279389382} + - component: {fileID: 114620180746352054} m_Layer: 0 m_Name: PC m_TagString: Untagged @@ -1129,6 +1130,18 @@ MonoBehaviour: InteractionDistance: 2 CharacterController: {fileID: 143869468979164672} Head: {fileID: 4883048021777868} +--- !u!114 &114620180746352054 +MonoBehaviour: + m_ObjectHideFlags: 1 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 100100000} + m_GameObject: {fileID: 1297568499365208} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 28f60457b196bf144a8b332b7327db1f, type: 3} + m_Name: + m_EditorClassIdentifier: + ID: 0 --- !u!114 &114633016952828368 MonoBehaviour: m_ObjectHideFlags: 1 diff --git a/Assets/Scripts/Entities/SyncBases/Inventory.cs b/Assets/Scripts/Entities/SyncBases/Inventory.cs new file mode 100644 index 0000000..239d28e --- /dev/null +++ b/Assets/Scripts/Entities/SyncBases/Inventory.cs @@ -0,0 +1,69 @@ + +using Cyber.Items; +using Cyber.Networking; +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); + Drive.AddItem(ItemDB.Singleton.Get(0)); + } + + /// + /// 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) { + 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]); + } + } +} diff --git a/Assets/Scripts/Entities/SyncBases/Inventory.cs.meta b/Assets/Scripts/Entities/SyncBases/Inventory.cs.meta new file mode 100644 index 0000000..f2c1611 --- /dev/null +++ b/Assets/Scripts/Entities/SyncBases/Inventory.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 28f60457b196bf144a8b332b7327db1f +timeCreated: 1494693625 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Entities/SyncDB.cs b/Assets/Scripts/Entities/SyncDB.cs index ee5b800..d159904 100644 --- a/Assets/Scripts/Entities/SyncDB.cs +++ b/Assets/Scripts/Entities/SyncDB.cs @@ -15,6 +15,7 @@ namespace Cyber.Entities { private static readonly Type[] SyncableClasses = new Type[] { typeof(Character), + typeof(Inventory), typeof(Button), typeof(Door), typeof(Computer) diff --git a/Assets/Scripts/Items.meta b/Assets/Scripts/Items.meta new file mode 100644 index 0000000..3f05a95 --- /dev/null +++ b/Assets/Scripts/Items.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: bbdd5f62728d08e43b717724c9412411 +folderAsset: yes +timeCreated: 1494693625 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Items/Drive.cs b/Assets/Scripts/Items/Drive.cs new file mode 100644 index 0000000..0ede809 --- /dev/null +++ b/Assets/Scripts/Items/Drive.cs @@ -0,0 +1,94 @@ + +using System.Collections.Generic; + +namespace Cyber.Items { + + /// + /// A drive containing items, and has a limited capacity that cannot be changed. + /// + public class Drive { + + private List Items = new List(); + + /// + /// The capacity of the drive, meaning how much stuff can this drive contain. + /// + public readonly float Capacity; + + /// + /// The interface-class of this Drive. + /// + public DriveInterface Interface; + + /// + /// Creates a drive with given capacity. Capacity cannot be changed after this. + /// + /// Capacity of the drive + public Drive(float capacity) { + Capacity = capacity; + Interface = new DriveInterface(this); + } + + /// + /// Current total weight of all the items combined. + /// + /// The totam weight + public float TotalWeight() { + float sum = 0; + foreach (Item item in Items) { + sum += item.Weight; + } + return sum; + } + + /// + /// Current free space in the drive. Literally is Capacity - TotalWeight() + /// + /// Current Free space. + public float FreeSpace() { + return Capacity - TotalWeight(); + } + + /// + /// Clears the drive, completely wiping it empty. Mainly used for + /// + /// + public void Clear() { + Items.Clear(); + } + + /// + /// Adds the item in the drive. If the addition was not successful, returns false, true otherwise. + /// + /// Weather the drive had enough space. + public bool AddItem(Item item) { + if (item.Weight > FreeSpace()) { + return false; + } + Interface.AddNewItem(Items.Count); + Items.Add(item); + return true; + } + + /// + /// Gets the item at the given index, or null if there is nothing. + /// + /// The index of the desired item + /// The item or null if nothing was found. + public Item GetItem(int idx) { + if (idx < 0 || idx > Items.Count) { + return null; + } + return Items[idx]; + } + + /// + /// Returns the list of all items currently in the drive. + /// + /// + public List GetItems() { + return Items; + } + + } +} diff --git a/Assets/Scripts/Items/Drive.cs.meta b/Assets/Scripts/Items/Drive.cs.meta new file mode 100644 index 0000000..dfbe162 --- /dev/null +++ b/Assets/Scripts/Items/Drive.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 492df702a2300a447ae72a26f82c7f57 +timeCreated: 1494693625 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Items/DriveInterface.cs b/Assets/Scripts/Items/DriveInterface.cs new file mode 100644 index 0000000..cac9446 --- /dev/null +++ b/Assets/Scripts/Items/DriveInterface.cs @@ -0,0 +1,101 @@ + +namespace Cyber.Items { + + /// + /// The interface, which contains a grid of indices of items in the . Use to get items in the interface. + /// + public class DriveInterface { + + /// + /// Width of the interface. + /// + public const int Width = 8; + + /// + /// Minimun height of the interface. + /// + public const int MinHeight = 4; + + private int[,] ItemGrid = new int[4, Width]; + + private Drive Drive; + + /// + /// Creates a Drive interface for a . + /// + /// + public DriveInterface(Drive drive) { + Drive = drive; + } + + /// + /// Returns the item at the specified coordinate on the interface. Returns null if invalid or empty coordinate. + /// + /// The x-coordinate + /// The y-coordinate + /// The item or null + public Item GetItemAt(int x, int y) { + if (ItemGrid[y, x] == 0) { + return null; + } else { + return Drive.GetItem(ItemGrid[y, x]); + } + } + + /// + /// Gets the Width of the interface, or simply . + /// + /// + public int GetWidth() { + return Width; + } + + /// + /// Gets the current height of the interface + /// + /// + public int GetHeight() { + return ItemGrid.GetLength(1); + } + + /// + /// Updates the height of the interface, adding new rows or deleting old useless ones. + /// + 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 = new int[RequiredHeight + 1, Width]; + 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; + } + + /// + /// Adds a new item to the grid. The idx in the parameter is the idx of the item in the drive. + /// + /// + 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; + } + } + } + } + } +} diff --git a/Assets/Scripts/Items/Item.cs b/Assets/Scripts/Items/Item.cs new file mode 100644 index 0000000..2653aa1 --- /dev/null +++ b/Assets/Scripts/Items/Item.cs @@ -0,0 +1,59 @@ + +namespace Cyber.Items { + + /// + /// An item, containing itemmy information. + /// + public class Item { + + /// + /// ID of the item, used in . + /// + public readonly int ID; + + /// + /// Model ID of this item. + /// + public int ModelID; + + /// + /// Name of the item. + /// + public string Name; + + /// + /// Description of the item. + /// + public string Description; + + /// + /// The weight of the item (in kg). + /// + public float Weight; + + /// + /// Creates an item. This should technically be only called by ItemDB, but it's public because of "reasons". + /// + /// ID of the item + /// ModelID of the item, see ModelDB. + /// The name if the item + /// The Weight of the item + /// The description of the item. + public Item(int id, int modelId, string name, float weight, string description) { + ID = id; + ModelID = modelId; + Name = name; + Weight = weight; + Description = description; + } + + /// + /// Clones the item, used mostly for . + /// + /// + public Item Clone() { + return new Item(ID, ModelID, Name, Weight, Description); + } + + } +} diff --git a/Assets/Scripts/Items/Item.cs.meta b/Assets/Scripts/Items/Item.cs.meta new file mode 100644 index 0000000..faa35a3 --- /dev/null +++ b/Assets/Scripts/Items/Item.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: dc68513dcc5b31840b1a758b687eb2e0 +timeCreated: 1494693626 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Items/ItemDB.cs b/Assets/Scripts/Items/ItemDB.cs new file mode 100644 index 0000000..403d3e2 --- /dev/null +++ b/Assets/Scripts/Items/ItemDB.cs @@ -0,0 +1,42 @@ +using System.Collections.Generic; + +namespace Cyber.Items { + + /// + /// ItemDB containing 'templates' for all items. + /// + public class ItemDB { + + private Dictionary Items = new Dictionary(); + private int Counter = 0; + + /// + /// + /// + public static ItemDB Singleton = new ItemDB(); + + /// + /// Creates the ItemDB. Should not be externally called. See . + /// + public ItemDB() { + AddItem(new Item(Counter++, 0, "Very Long Item Name", 1.5f, "This item is a rare piece of the \"way too long of a name\" technology, invented by space goblins in ancient times.")); + } + + /// + /// If there is an item at the given ID, return a clone of it. Otherwise return null. + /// + /// The id of the desired item. + /// + public Item Get(int itemId) { + if (Items.ContainsKey(itemId)) { + return Items[itemId].Clone(); + } + return null; + } + + private void AddItem(Item item) { + Items.Add(item.ID, item); + } + + } +} diff --git a/Assets/Scripts/Items/ItemDB.cs.meta b/Assets/Scripts/Items/ItemDB.cs.meta new file mode 100644 index 0000000..ae11042 --- /dev/null +++ b/Assets/Scripts/Items/ItemDB.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: bd2b5984cb9102345bf98feeac875057 +timeCreated: 1494693625 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Networking/Messages/SyncPkt.cs b/Assets/Scripts/Networking/Messages/SyncPkt.cs index 9e41fe3..2eb9150 100644 --- a/Assets/Scripts/Networking/Messages/SyncPkt.cs +++ b/Assets/Scripts/Networking/Messages/SyncPkt.cs @@ -1,4 +1,5 @@  +using Cyber.Console; using Cyber.Entities; using UnityEngine.Networking;