Add items and itemsystems, possibly a broken build

This commit is contained in:
Sofia 2017-05-13 22:54:14 +03:00
parent 5eb424bea0
commit 966b3e1826
14 changed files with 451 additions and 0 deletions

View File

@ -38,6 +38,7 @@ GameObject:
- component: {fileID: 4844777526459442} - component: {fileID: 4844777526459442}
- component: {fileID: 143753897266899886} - component: {fileID: 143753897266899886}
- component: {fileID: 114052379458543858} - component: {fileID: 114052379458543858}
- component: {fileID: 114934786006823366}
m_Layer: 0 m_Layer: 0
m_Name: NPC m_Name: NPC
m_TagString: Untagged m_TagString: Untagged
@ -293,8 +294,21 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
ID: 0 ID: 0
MovementSpeed: 5 MovementSpeed: 5
InteractionDistance: 2
CharacterController: {fileID: 143753897266899886} CharacterController: {fileID: 143753897266899886}
Head: {fileID: 4900355877646882} 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 --- !u!143 &143753897266899886
CharacterController: CharacterController:
m_ObjectHideFlags: 1 m_ObjectHideFlags: 1

View File

@ -59,6 +59,7 @@ GameObject:
- component: {fileID: 143869468979164672} - component: {fileID: 143869468979164672}
- component: {fileID: 114575501420754388} - component: {fileID: 114575501420754388}
- component: {fileID: 114385213279389382} - component: {fileID: 114385213279389382}
- component: {fileID: 114620180746352054}
m_Layer: 0 m_Layer: 0
m_Name: PC m_Name: PC
m_TagString: Untagged m_TagString: Untagged
@ -1129,6 +1130,18 @@ MonoBehaviour:
InteractionDistance: 2 InteractionDistance: 2
CharacterController: {fileID: 143869468979164672} CharacterController: {fileID: 143869468979164672}
Head: {fileID: 4883048021777868} 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 --- !u!114 &114633016952828368
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 1 m_ObjectHideFlags: 1

View File

@ -0,0 +1,69 @@

using Cyber.Items;
using Cyber.Networking;
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;
/// <summary>
/// Creates the Inventory-component for a game object.
/// </summary>
public Inventory() {
Drive = new Drive(10f);
Drive.AddItem(ItemDB.Singleton.Get(0));
}
/// <summary>
/// Returns the sync handletype indicating how the inventory should be synced.
/// </summary>
/// <returns></returns>
public override SyncHandletype GetSyncHandletype() {
return new SyncHandletype(true, 10);
}
/// <summary>
/// Deserializes the ID's and creates them in the <see cref="Drive"/>.
/// </summary>
/// <param name="reader"></param>
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));
}
}
/// <summary>
/// Serializes only the <see cref="Drive"/>'s item IDs.
/// </summary>
/// <param name="writer"></param>
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]);
}
}
}

View File

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

View File

@ -15,6 +15,7 @@ namespace Cyber.Entities {
private static readonly Type[] SyncableClasses = new Type[] { private static readonly Type[] SyncableClasses = new Type[] {
typeof(Character), typeof(Character),
typeof(Inventory),
typeof(Button), typeof(Button),
typeof(Door), typeof(Door),
typeof(Computer) typeof(Computer)

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: bbdd5f62728d08e43b717724c9412411
folderAsset: yes
timeCreated: 1494693625
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,94 @@

using System.Collections.Generic;
namespace Cyber.Items {
/// <summary>
/// A drive containing items, and has a limited capacity that cannot be changed.
/// </summary>
public class Drive {
private List<Item> Items = new List<Item>();
/// <summary>
/// The capacity of the drive, meaning how much stuff can this drive contain.
/// </summary>
public readonly float Capacity;
/// <summary>
/// The interface-class of this Drive.
/// </summary>
public DriveInterface Interface;
/// <summary>
/// Creates a drive with given capacity. Capacity cannot be changed after this.
/// </summary>
/// <param name="capacity">Capacity of the drive</param>
public Drive(float capacity) {
Capacity = capacity;
Interface = new DriveInterface(this);
}
/// <summary>
/// Current total weight of all the items combined.
/// </summary>
/// <returns>The totam weight</returns>
public float TotalWeight() {
float sum = 0;
foreach (Item item in Items) {
sum += item.Weight;
}
return sum;
}
/// <summary>
/// Current free space in the drive. Literally is Capacity - TotalWeight()
/// </summary>
/// <returns>Current Free space.</returns>
public float FreeSpace() {
return Capacity - TotalWeight();
}
/// <summary>
/// Clears the drive, completely wiping it empty. Mainly used for
/// <see cref="Inventory.Deserialize(NetworkReader)"/>
/// </summary>
public void Clear() {
Items.Clear();
}
/// <summary>
/// Adds the item in the drive. If the addition was not successful, returns false, true otherwise.
/// </summary>
/// <returns>Weather the drive had enough space.</returns>
public bool AddItem(Item item) {
if (item.Weight > FreeSpace()) {
return false;
}
Interface.AddNewItem(Items.Count);
Items.Add(item);
return true;
}
/// <summary>
/// Gets the item at the given index, or null if there is nothing.
/// </summary>
/// <param name="idx">The index of the desired item</param>
/// <returns>The item or null if nothing was found.</returns>
public Item GetItem(int idx) {
if (idx < 0 || idx > Items.Count) {
return null;
}
return Items[idx];
}
/// <summary>
/// Returns the list of all items currently in the drive.
/// </summary>
/// <returns></returns>
public List<Item> GetItems() {
return Items;
}
}
}

View File

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

View File

@ -0,0 +1,101 @@

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 = 8;
/// <summary>
/// Minimun height of the interface.
/// </summary>
public const int MinHeight = 4;
private int[,] ItemGrid = new int[4, Width];
private Drive Drive;
/// <summary>
/// Creates a Drive interface for a <see cref="Drive"/>.
/// </summary>
/// <param name="drive"></param>
public DriveInterface(Drive drive) {
Drive = drive;
}
/// <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 (ItemGrid[y, x] == 0) {
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(1);
}
/// <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 = 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;
}
/// <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;
}
}
}
}
}
}

View File

@ -0,0 +1,59 @@

namespace Cyber.Items {
/// <summary>
/// An item, containing itemmy information.
/// </summary>
public class Item {
/// <summary>
/// ID of the item, used in <see cref="ItemDB"/>.
/// </summary>
public readonly int ID;
/// <summary>
/// Model ID of this item.
/// </summary>
public int ModelID;
/// <summary>
/// Name of the item.
/// </summary>
public string Name;
/// <summary>
/// Description of the item.
/// </summary>
public string Description;
/// <summary>
/// The weight of the item (in kg).
/// </summary>
public float Weight;
/// <summary>
/// Creates an item. This should technically be only called by ItemDB, but it's public because of "reasons".
/// </summary>
/// <param name="id">ID of the item</param>
/// <param name="modelId">ModelID of the item, see ModelDB.</param>
/// <param name="name">The name if the item</param>
/// <param name="weight">The Weight of the item</param>
/// <param name="description">The description of the item.</param>
public Item(int id, int modelId, string name, float weight, string description) {
ID = id;
ModelID = modelId;
Name = name;
Weight = weight;
Description = description;
}
/// <summary>
/// Clones the item, used mostly for <see cref="ItemDB.Get(int)"/>.
/// </summary>
/// <returns></returns>
public Item Clone() {
return new Item(ID, ModelID, Name, Weight, Description);
}
}
}

View File

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

View File

@ -0,0 +1,42 @@
using System.Collections.Generic;
namespace Cyber.Items {
/// <summary>
/// ItemDB containing 'templates' for all items.
/// </summary>
public class ItemDB {
private Dictionary<int, Item> Items = new Dictionary<int, Item>();
private int Counter = 0;
/// <summary>
///
/// </summary>
public static ItemDB Singleton = new ItemDB();
/// <summary>
/// Creates the ItemDB. Should not be externally called. See <see cref="Singleton"/>.
/// </summary>
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."));
}
/// <summary>
/// If there is an item at the given ID, return a clone of it. Otherwise return null.
/// </summary>
/// <param name="itemId">The id of the desired item.</param>
/// <returns></returns>
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);
}
}
}

View File

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

View File

@ -1,4 +1,5 @@
 
using Cyber.Console;
using Cyber.Entities; using Cyber.Entities;
using UnityEngine.Networking; using UnityEngine.Networking;