2017-05-12 10:53:57 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Cyber.Util;
|
2017-05-13 21:56:06 +02:00
|
|
|
|
using Cyber.Console;
|
2017-05-13 22:40:08 +02:00
|
|
|
|
using Cyber.Entities.SyncBases;
|
|
|
|
|
using Cyber.Items;
|
2017-05-14 23:35:44 +02:00
|
|
|
|
using Cyber.Networking.Clientside;
|
|
|
|
|
using Cyber.Networking;
|
|
|
|
|
using Cyber.Networking.Messages;
|
2017-05-12 10:53:57 +02:00
|
|
|
|
|
|
|
|
|
namespace Cyber.Controls {
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles displaying and interacting with the inventory.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class InventoryInterface : MonoBehaviour {
|
|
|
|
|
|
2017-05-13 22:40:08 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The inventory of the player, will be displayed here.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Inventory Inventory;
|
|
|
|
|
|
2017-05-12 10:53:57 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The camera that is displaying this inventory interface.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Camera Camera;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The hologram that acts as the root for the inventory.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Hologram Hologram;
|
|
|
|
|
|
2017-05-14 00:39:10 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The text texture that shows the selected item's name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public TextTextureApplier ItemNameText;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The text texture that shows the selected item's description.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public TextTextureApplier ItemDescriptionText;
|
|
|
|
|
|
2017-05-13 21:56:06 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The item preview mesh.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public MeshFilter ItemPreviewMesh;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The item preview spinner.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Spinner ItemPreviewSpinner;
|
|
|
|
|
|
2017-05-12 20:27:01 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The icon for the inventory.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public MeshRenderer IconInventory;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The icon for the status.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public MeshRenderer IconStatus;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The icon for the social.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public MeshRenderer IconSocial;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The icon for the map.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public MeshRenderer IconMap;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The text that describes the selected icon.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public TextTextureApplier IconExplainerText;
|
|
|
|
|
|
2017-05-12 10:53:57 +02:00
|
|
|
|
/// <summary>
|
2017-05-13 21:56:06 +02:00
|
|
|
|
/// The selector mesh. It'll move to the position of the selected item
|
|
|
|
|
/// in the grid.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Transform ItemGridSelector;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The parent of all the gameobjects that create the grid.
|
2017-05-12 10:53:57 +02:00
|
|
|
|
/// </summary>
|
2017-05-13 21:56:06 +02:00
|
|
|
|
public Transform ItemGridParent;
|
2017-05-12 10:53:57 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-05-13 21:56:06 +02:00
|
|
|
|
/// The item grid dimensions.
|
2017-05-12 10:53:57 +02:00
|
|
|
|
/// </summary>
|
2017-05-13 21:56:06 +02:00
|
|
|
|
public Vector2 ItemGridDimensions;
|
2017-05-12 10:53:57 +02:00
|
|
|
|
|
|
|
|
|
private bool InventoryOpen = false;
|
2017-05-14 01:12:47 +02:00
|
|
|
|
private float PreviewVisibility = 0f;
|
2017-05-13 21:56:06 +02:00
|
|
|
|
|
|
|
|
|
private List<Transform> ItemGridCells;
|
|
|
|
|
private List<MeshFilter> ItemGridCellMeshes;
|
|
|
|
|
private int ItemGridSelectedIndex;
|
2017-05-14 22:28:55 +02:00
|
|
|
|
private Item SelectedItem = null;
|
2017-05-12 10:53:57 +02:00
|
|
|
|
|
2017-05-12 20:27:01 +02:00
|
|
|
|
private Color IconInventoryColor;
|
|
|
|
|
private Color IconStatusColor;
|
|
|
|
|
private Color IconSocialColor;
|
|
|
|
|
private Color IconMapColor;
|
|
|
|
|
|
2017-05-12 10:53:57 +02:00
|
|
|
|
private void Start() {
|
2017-05-13 21:56:06 +02:00
|
|
|
|
int ItemGridSize = (int) ItemGridDimensions.x * (int) ItemGridDimensions.y;
|
|
|
|
|
ItemGridCells = new List<Transform>(ItemGridSize);
|
|
|
|
|
ItemGridCellMeshes = new List<MeshFilter>(ItemGridSize);
|
|
|
|
|
for (int i = 0; i < ItemGridSize; i++) {
|
|
|
|
|
Transform Cell = ItemGridParent.GetChild(i).transform;
|
|
|
|
|
ItemGridCells.Add(Cell);
|
|
|
|
|
ItemGridCellMeshes.Add(Cell.GetComponentInChildren<MeshFilter>());
|
|
|
|
|
}
|
2017-05-12 20:27:01 +02:00
|
|
|
|
|
|
|
|
|
IconInventoryColor = IconInventory.material.GetColor("_EmissionColor");
|
|
|
|
|
IconStatusColor = IconStatus.material.GetColor("_EmissionColor");
|
|
|
|
|
IconSocialColor = IconSocial.material.GetColor("_EmissionColor");
|
|
|
|
|
IconMapColor = IconMap.material.GetColor("_EmissionColor");
|
2017-05-12 10:53:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update() {
|
2017-05-13 21:56:06 +02:00
|
|
|
|
if (Term.IsVisible()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-12 10:53:57 +02:00
|
|
|
|
if (Input.GetButtonDown("Inventory")) {
|
|
|
|
|
InventoryOpen = !InventoryOpen;
|
|
|
|
|
Hologram.Visible = InventoryOpen;
|
|
|
|
|
CursorHandler.RequestLockState(!InventoryOpen);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-13 21:56:06 +02:00
|
|
|
|
int CurrentIndex = -1;
|
2017-05-12 10:53:57 +02:00
|
|
|
|
RaycastHit LookedAt = CameraUtil.GetLookedAtHit(Camera, 1f, true);
|
|
|
|
|
if (LookedAt.collider != null) {
|
2017-05-12 20:27:01 +02:00
|
|
|
|
MeshRenderer Mesh = LookedAt.collider.GetComponent<MeshRenderer>();
|
2017-05-13 21:56:06 +02:00
|
|
|
|
if (ItemGridCells.Contains(LookedAt.collider.transform)) {
|
2017-05-12 10:53:57 +02:00
|
|
|
|
// Interacting with the item list
|
2017-05-13 21:56:06 +02:00
|
|
|
|
CurrentIndex = int.Parse(LookedAt.collider.name.Split(' ')[1]);
|
2017-05-12 10:53:57 +02:00
|
|
|
|
if (Input.GetButtonDown("Activate")) {
|
2017-05-14 22:28:55 +02:00
|
|
|
|
if (ItemGridSelectedIndex == CurrentIndex && SelectedItem != null) {
|
|
|
|
|
// Selected index was already this => equip (double-clicked)
|
|
|
|
|
Item Equipped = Inventory.Equipped.GetItem(SelectedItem.Slot);
|
|
|
|
|
if (Equipped != null && Equipped.ID == SelectedItem.ID) {
|
|
|
|
|
Inventory.Equipped.ClearSlot(SelectedItem.Slot);
|
2017-05-14 23:35:44 +02:00
|
|
|
|
Client.Send(PktType.InventoryAction, new InventoryActionPkt(InventoryAction.Unequip, (int) SelectedItem.Slot));
|
2017-05-14 22:28:55 +02:00
|
|
|
|
} else {
|
|
|
|
|
Inventory.Equipped.SetSlot(SelectedItem.Slot, SelectedItem);
|
2017-05-14 23:35:44 +02:00
|
|
|
|
Client.Send(PktType.InventoryAction, new InventoryActionPkt(InventoryAction.Equip, SelectedItem.ID));
|
2017-05-14 22:28:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-13 21:56:06 +02:00
|
|
|
|
ItemGridSelectedIndex = CurrentIndex;
|
2017-05-12 10:53:57 +02:00
|
|
|
|
}
|
2017-05-12 20:27:01 +02:00
|
|
|
|
} else if (Mesh != null) {
|
2017-05-14 01:12:47 +02:00
|
|
|
|
float InvBrightness = 1.1f;
|
2017-05-12 20:27:01 +02:00
|
|
|
|
float StsBrightness = 1f;
|
|
|
|
|
float SclBrightness = 1f;
|
|
|
|
|
float MapBrightness = 1f;
|
|
|
|
|
string SelectedIcon = "";
|
|
|
|
|
|
|
|
|
|
if (Mesh == IconInventory) {
|
|
|
|
|
InvBrightness = 1.2f;
|
2017-05-13 21:56:06 +02:00
|
|
|
|
SelectedIcon = "Storage";
|
2017-05-12 20:27:01 +02:00
|
|
|
|
} else if (Mesh == IconStatus) {
|
|
|
|
|
StsBrightness = 1.2f;
|
|
|
|
|
SelectedIcon = "Status";
|
|
|
|
|
} else if (Mesh == IconSocial) {
|
|
|
|
|
SclBrightness = 1.2f;
|
|
|
|
|
SelectedIcon = "Social";
|
|
|
|
|
} else if (Mesh == IconMap) {
|
|
|
|
|
MapBrightness = 1.2f;
|
|
|
|
|
SelectedIcon = "Map";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextTextureProperties Props = IconExplainerText.TextProperties;
|
|
|
|
|
Props.Text = SelectedIcon;
|
|
|
|
|
IconExplainerText.SetTextProperties(Props);
|
|
|
|
|
|
2017-05-13 21:56:06 +02:00
|
|
|
|
|
|
|
|
|
IconInventory.material.SetColor("_EmissionColor", new Color(IconInventoryColor.r * InvBrightness,
|
|
|
|
|
IconInventoryColor.g * InvBrightness, IconInventoryColor.b * InvBrightness));
|
|
|
|
|
IconStatus.material.SetColor("_EmissionColor", new Color(IconStatusColor.r * StsBrightness,
|
|
|
|
|
IconStatusColor.g * StsBrightness, IconStatusColor.b * StsBrightness));
|
|
|
|
|
IconSocial.material.SetColor("_EmissionColor", new Color(IconSocialColor.r * SclBrightness,
|
|
|
|
|
IconSocialColor.g * SclBrightness, IconSocialColor.b * SclBrightness));
|
|
|
|
|
IconMap.material.SetColor("_EmissionColor", new Color(IconMapColor.r * MapBrightness,
|
|
|
|
|
IconMapColor.g * MapBrightness, IconMapColor.b * MapBrightness));
|
2017-05-12 10:53:57 +02:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Outside of the inventory, clicking will unselect
|
|
|
|
|
if (Input.GetButtonDown("Activate")) {
|
2017-05-13 21:56:06 +02:00
|
|
|
|
ItemGridSelectedIndex = -1;
|
2017-05-12 10:53:57 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-13 21:56:06 +02:00
|
|
|
|
RebuildItemGrid(CurrentIndex);
|
2017-05-12 10:53:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-13 21:56:06 +02:00
|
|
|
|
private void RebuildItemGrid(int focused) {
|
2017-05-14 01:12:47 +02:00
|
|
|
|
bool ItemFound = false;
|
2017-05-13 22:40:08 +02:00
|
|
|
|
for (int y = 0; y < ItemGridDimensions.y; y++) {
|
|
|
|
|
for (int x = 0; x < ItemGridDimensions.x; x++) {
|
2017-05-14 01:12:47 +02:00
|
|
|
|
// Find the item and mesh
|
2017-05-13 22:40:08 +02:00
|
|
|
|
int i = x + y * (int) ItemGridDimensions.x;
|
|
|
|
|
Item Item = Inventory.Drive.Interface.GetItemAt(x, y);
|
|
|
|
|
Mesh Mesh = null;
|
|
|
|
|
if (Item != null) {
|
2017-05-14 20:45:56 +02:00
|
|
|
|
Mesh = MeshDB.GetMesh(Item.ModelID);
|
2017-05-13 21:56:06 +02:00
|
|
|
|
}
|
2017-05-13 22:40:08 +02:00
|
|
|
|
ItemGridCellMeshes[i].mesh = Mesh;
|
|
|
|
|
|
2017-05-14 01:12:47 +02:00
|
|
|
|
// Set the base scale and spin status
|
2017-05-13 22:40:08 +02:00
|
|
|
|
float Scale = 0.08f;
|
|
|
|
|
bool Spinning = false;
|
2017-05-14 01:12:47 +02:00
|
|
|
|
|
2017-05-13 22:40:08 +02:00
|
|
|
|
if (focused == i || ItemGridSelectedIndex == i) {
|
2017-05-14 01:12:47 +02:00
|
|
|
|
// Item is selected or hovered, animate
|
2017-05-13 22:40:08 +02:00
|
|
|
|
Scale = 0.1f;
|
|
|
|
|
Spinning = true;
|
|
|
|
|
}
|
2017-05-14 01:12:47 +02:00
|
|
|
|
|
2017-05-13 22:40:08 +02:00
|
|
|
|
if (ItemGridSelectedIndex == i) {
|
2017-05-14 22:28:55 +02:00
|
|
|
|
// Update the selected item
|
|
|
|
|
SelectedItem = Item;
|
|
|
|
|
|
2017-05-14 00:39:10 +02:00
|
|
|
|
// Set preview information
|
|
|
|
|
SetPreviewMesh(Mesh);
|
|
|
|
|
TextTextureProperties NameProps = ItemNameText.TextProperties;
|
|
|
|
|
TextTextureProperties DescriptionProps = ItemDescriptionText.TextProperties;
|
|
|
|
|
if (Item != null) {
|
|
|
|
|
NameProps.Text = Item.Name;
|
|
|
|
|
DescriptionProps.Text = Item.Description;
|
2017-05-14 01:12:47 +02:00
|
|
|
|
ItemFound = true;
|
2017-05-14 00:39:10 +02:00
|
|
|
|
} else {
|
2017-05-14 01:12:47 +02:00
|
|
|
|
NameProps.Text = "";
|
|
|
|
|
DescriptionProps.Text = "";
|
2017-05-14 00:39:10 +02:00
|
|
|
|
}
|
|
|
|
|
ItemNameText.SetTextProperties(NameProps);
|
|
|
|
|
ItemDescriptionText.SetTextProperties(DescriptionProps);
|
|
|
|
|
|
|
|
|
|
// Move selector
|
2017-05-13 22:40:08 +02:00
|
|
|
|
if ((ItemGridSelector.position - ItemGridCells[i].position).magnitude < 0.01f) {
|
|
|
|
|
ItemGridSelector.position = ItemGridCells[i].position;
|
|
|
|
|
} else {
|
|
|
|
|
ItemGridSelector.position =
|
|
|
|
|
Vector3.Lerp(ItemGridSelector.position,
|
|
|
|
|
ItemGridCells[i].position, 20f * Time.deltaTime);
|
|
|
|
|
}
|
|
|
|
|
Vector3 NewRot = ItemGridSelector.localEulerAngles;
|
|
|
|
|
NewRot.z = 0;
|
|
|
|
|
ItemGridSelector.localEulerAngles = NewRot;
|
|
|
|
|
}
|
2017-05-14 01:12:47 +02:00
|
|
|
|
|
2017-05-13 22:40:08 +02:00
|
|
|
|
if (!Spinning) {
|
2017-05-14 01:12:47 +02:00
|
|
|
|
// Not selected, reset rotation
|
2017-05-13 22:40:08 +02:00
|
|
|
|
ItemGridCellMeshes[i].transform.LookAt(Camera.transform);
|
|
|
|
|
Vector3 NewRot = ItemGridCellMeshes[i].transform.localEulerAngles;
|
|
|
|
|
NewRot.z = 0;
|
|
|
|
|
ItemGridCellMeshes[i].transform.localEulerAngles = NewRot;
|
|
|
|
|
}
|
2017-05-14 01:12:47 +02:00
|
|
|
|
|
|
|
|
|
// Update spinning status and scaling
|
2017-05-13 22:40:08 +02:00
|
|
|
|
ItemGridCells[i].GetComponent<Spinner>().Spinning = Spinning;
|
|
|
|
|
FixMeshScaling(ItemGridCellMeshes[i], Scale);
|
2017-05-12 10:53:57 +02:00
|
|
|
|
}
|
2017-05-13 21:56:06 +02:00
|
|
|
|
}
|
2017-05-14 01:12:47 +02:00
|
|
|
|
|
|
|
|
|
// Hide the selector if nothing is selected
|
|
|
|
|
ItemGridSelector.gameObject.SetActive(ItemGridSelectedIndex >= 0);
|
|
|
|
|
|
|
|
|
|
// Clean up the preview screen if there was no item
|
|
|
|
|
if (!ItemFound) {
|
|
|
|
|
SetPreviewMesh(null);
|
|
|
|
|
TextTextureProperties NameProps = ItemNameText.TextProperties;
|
|
|
|
|
TextTextureProperties DescriptionProps = ItemDescriptionText.TextProperties;
|
|
|
|
|
NameProps.Text = "";
|
|
|
|
|
DescriptionProps.Text = "";
|
|
|
|
|
ItemNameText.SetTextProperties(NameProps);
|
|
|
|
|
ItemDescriptionText.SetTextProperties(DescriptionProps);
|
|
|
|
|
PreviewVisibility = Mathf.Lerp(PreviewVisibility, 0f, 10f * Time.deltaTime);
|
|
|
|
|
} else {
|
|
|
|
|
PreviewVisibility = Mathf.Lerp(PreviewVisibility, 1f, 10f * Time.deltaTime);
|
|
|
|
|
}
|
2017-05-13 21:56:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetPreviewMesh(Mesh mesh) {
|
|
|
|
|
ItemPreviewSpinner.Spinning = mesh != null;
|
|
|
|
|
if (mesh != null) {
|
2017-05-14 01:12:47 +02:00
|
|
|
|
ItemPreviewMesh.mesh = mesh;
|
2017-05-12 10:53:57 +02:00
|
|
|
|
}
|
2017-05-14 01:12:47 +02:00
|
|
|
|
FixMeshScaling(ItemPreviewMesh, 0.175f * PreviewVisibility);
|
2017-05-13 21:56:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FixMeshScaling(MeshFilter toFix, float scale) {
|
2017-05-13 22:40:08 +02:00
|
|
|
|
if (toFix.mesh == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float HighestExtent = 0.1f;
|
2017-05-13 21:56:06 +02:00
|
|
|
|
float Height = toFix.mesh.bounds.extents.y * 2f;
|
|
|
|
|
float Width = Mathf.Sqrt(Mathf.Pow(toFix.mesh.bounds.extents.x * 2f, 2) +
|
|
|
|
|
Mathf.Pow(toFix.mesh.bounds.extents.y * 2f, 2));
|
|
|
|
|
float Depth = Mathf.Sqrt(Mathf.Pow(toFix.mesh.bounds.extents.z * 2f, 2) +
|
|
|
|
|
Mathf.Pow(toFix.mesh.bounds.extents.y * 2f, 2));
|
|
|
|
|
|
|
|
|
|
if (Height > HighestExtent) {
|
|
|
|
|
HighestExtent = Height;
|
|
|
|
|
}
|
|
|
|
|
if (Width > HighestExtent) {
|
|
|
|
|
HighestExtent = Width;
|
|
|
|
|
}
|
|
|
|
|
if (Depth > HighestExtent) {
|
|
|
|
|
HighestExtent = Depth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float Scale = scale * 1f / HighestExtent;
|
|
|
|
|
toFix.transform.localScale = new Vector3(Scale, Scale, Scale);
|
2017-05-12 10:53:57 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|