diff --git a/Assets/Prefabs/NPC.prefab b/Assets/Prefabs/NPC.prefab index ceb28ff..29247d6 100644 --- a/Assets/Prefabs/NPC.prefab +++ b/Assets/Prefabs/NPC.prefab @@ -541,6 +541,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: ID: 0 + Character: {fileID: 114052379458543858} --- !u!143 &143753897266899886 CharacterController: m_ObjectHideFlags: 1 diff --git a/Assets/Prefabs/PC.prefab b/Assets/Prefabs/PC.prefab index a199dee..504870d 100644 --- a/Assets/Prefabs/PC.prefab +++ b/Assets/Prefabs/PC.prefab @@ -4955,6 +4955,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: Character: {fileID: 114575501420754388} + InventoryInterface: {fileID: 114383476058023772} Camera: {fileID: 20678872378488080} --- !u!114 &114390919699494652 MonoBehaviour: @@ -5162,6 +5163,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: ID: 0 + Character: {fileID: 114575501420754388} --- !u!114 &114741357428691828 MonoBehaviour: m_ObjectHideFlags: 1 diff --git a/Assets/Scenes/TestMap.unity b/Assets/Scenes/TestMap.unity index e695e26..924c698 100644 --- a/Assets/Scenes/TestMap.unity +++ b/Assets/Scenes/TestMap.unity @@ -555,6 +555,7 @@ Transform: - {fileID: 375619906} - {fileID: 1610252930} - {fileID: 274219296} + - {fileID: 1785155275} m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -2353,6 +2354,46 @@ Transform: m_Father: {fileID: 310461192} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1785155274 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 5 + m_Component: + - component: {fileID: 1785155275} + - component: {fileID: 1785155276} + m_Layer: 0 + m_Name: Lerper + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1785155275 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1785155274} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 344272455} + m_RootOrder: 6 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1785155276 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1785155274} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 33c8d5915d5e847eb9bbe018c2fdb457, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &1810916631 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Controls/InventoryInterface.cs b/Assets/Scripts/Controls/InventoryInterface.cs index 28adc3d..9773911 100644 --- a/Assets/Scripts/Controls/InventoryInterface.cs +++ b/Assets/Scripts/Controls/InventoryInterface.cs @@ -98,12 +98,22 @@ namespace Cyber.Controls { private List ItemGridCells; private List ItemGridCellMeshes; private int ItemGridSelectedIndex; + private Transform GrabbedItem; + private int GrabbedItemIndex = -1; private Color IconInventoryColor; private Color IconStatusColor; private Color IconSocialColor; private Color IconMapColor; + /// + /// Whether the inventory is currently open and interactible. + /// + /// true if this instance is open; otherwise, false. + public bool IsOpen() { + return InventoryOpen; + } + private void Start() { int ItemGridSize = (int) ItemGridDimensions.x * (int) ItemGridDimensions.y; ItemGridCells = new List(ItemGridSize); @@ -138,22 +148,54 @@ namespace Cyber.Controls { if (ItemGridCells.Contains(LookedAt.collider.transform)) { // Interacting with the item list CurrentIndex = int.Parse(LookedAt.collider.name.Split(' ')[1]); - if (Input.GetButtonDown("Activate") || Input.GetButtonDown("Equip")) { - ItemGridSelectedIndex = CurrentIndex; - } - if (Input.GetButtonDown("Equip")) { - // Selected index was already this => equip (double-clicked) - Item SelectedItem = Inventory.Drive.GetItemAt(ItemGridSelectedIndex); - if (SelectedItem != null) { - Item Equipped = Inventory.Drive.GetSlot(SelectedItem.Slot); - if (Equipped != null && Equipped.ID == SelectedItem.ID) { - Inventory.Drive.UnequipSlot(SelectedItem.Slot); - Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildClearSlot(SelectedItem.Slot)); - } else { - Inventory.Drive.EquipItem(ItemGridSelectedIndex); - Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildEquipItem(ItemGridSelectedIndex)); + if (GrabbedItem == null) { + // Nothing is currently being dragged, continue as normal + if (Input.GetButton("Activate") && + (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0) && + ItemGridSelectedIndex == CurrentIndex) { + // This item has been selected for at least a frame, + // and the mouse is moving, this counts as dragging + Debug.Log("Grabbed!"); + GrabbedItem = LookedAt.collider.transform; + GrabbedItemIndex = CurrentIndex; + } + if (Input.GetButtonDown("Activate") || Input.GetButtonDown("Equip")) { + // Select things + ItemGridSelectedIndex = CurrentIndex; + } + if (Input.GetButtonDown("Equip")) { + // Equip things + Item SelectedItem = Inventory.Drive.GetItemAt(ItemGridSelectedIndex); + if (SelectedItem != null) { + Item Equipped = Inventory.Drive.GetSlot(SelectedItem.Slot); + if (Equipped != null && Equipped.ID == SelectedItem.ID) { + Inventory.Drive.UnequipSlot(SelectedItem.Slot); + Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildClearSlot(SelectedItem.Slot)); + } else { + Inventory.Drive.EquipItem(ItemGridSelectedIndex); + Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildEquipItem(ItemGridSelectedIndex)); + } } } + } else { + // Something is grabbed, make things react to this + if (Input.GetButtonUp("Activate")) { + // Grab was released, drop item here + // Lerp things + ItemGridCellMeshes[GrabbedItemIndex].transform.position = ItemGridCells[CurrentIndex].position; + ItemGridCellMeshes[CurrentIndex].transform.position = ItemGridCells[GrabbedItemIndex].position; + Lerper.LerpTransformPosition(ItemGridCellMeshes[GrabbedItemIndex].transform, new Vector3(), 10f); + Lerper.LerpTransformPosition(ItemGridCellMeshes[CurrentIndex].transform, new Vector3(), 10f); + + // Switch items + Inventory.Drive.SwitchSlots(GrabbedItemIndex, CurrentIndex); + + // Reset grabbing + GrabbedItem = null; + GrabbedItemIndex = -1; + } else { + + } } } else if (Mesh != null) { float InvBrightness = 1.1f; diff --git a/Assets/Scripts/Controls/PlayerController.cs b/Assets/Scripts/Controls/PlayerController.cs index ac6e6f0..fc1cc0b 100644 --- a/Assets/Scripts/Controls/PlayerController.cs +++ b/Assets/Scripts/Controls/PlayerController.cs @@ -21,6 +21,11 @@ namespace Cyber.Controls { /// public Character Character; + /// + /// The inventory interface. + /// + public InventoryInterface InventoryInterface; + /// /// The camera the player is seeing the world through. /// @@ -77,7 +82,7 @@ namespace Cyber.Controls { } // Equipment actions - if (!Interacted) { + if (!Interacted && !InventoryInterface.IsOpen()) { // Don't use equipment if you're interacting with something // (ie. don't shoot at the buttons) if (Input.GetButtonDown("Use Item (R)")) { diff --git a/Assets/Scripts/Items/Drive.cs b/Assets/Scripts/Items/Drive.cs index 5199100..e792813 100644 --- a/Assets/Scripts/Items/Drive.cs +++ b/Assets/Scripts/Items/Drive.cs @@ -104,6 +104,9 @@ namespace Cyber.Items { /// The first index. /// The second index. public void SwitchSlots(int idx1, int idx2) { + if (idx1 == idx2) { + return; + } Slot Slot1 = GetSlotAt(idx1); Slot Slot2 = GetSlotAt(idx2); SetSlotAt(idx1, Slot2); diff --git a/Assets/Scripts/Util/Lerper.cs b/Assets/Scripts/Util/Lerper.cs new file mode 100644 index 0000000..5091616 --- /dev/null +++ b/Assets/Scripts/Util/Lerper.cs @@ -0,0 +1,59 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace Cyber.Util { + + /// + /// Lerps stuff. + /// + public class Lerper : MonoBehaviour { + private struct PositionLerp { + public Vector3 Target; + public float Speed; + + public PositionLerp(Vector3 target, float speed) { + Target = target; + Speed = speed; + } + } + + private static Lerper Singleton; + + private Dictionary PositionLerps = new Dictionary(); + + /// + /// Sets the singleton. + /// + public Lerper() { + Singleton = this; + } + + /// + /// Lerps the transform local position. + /// + /// Transform. + /// To. + /// Speed. + public static void LerpTransformPosition(Transform transform, Vector3 to, float speed) { + if (Singleton != null) { + Singleton.PositionLerps[transform] = new PositionLerp(to, speed); + } + } + + private void Update() { + List RemoveThese = new List(); + foreach (Transform Transform in PositionLerps.Keys) { + Transform.localPosition = Vector3.Lerp(Transform.localPosition, + PositionLerps[Transform].Target, PositionLerps[Transform].Speed * Time.deltaTime); + if ((Transform.localPosition - PositionLerps[Transform].Target).magnitude < 0.001f) { + Transform.localPosition = PositionLerps[Transform].Target; + RemoveThese.Add(Transform); + } + } + foreach (Transform Transform in RemoveThese) { + PositionLerps.Remove(Transform); + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Util/Lerper.cs.meta b/Assets/Scripts/Util/Lerper.cs.meta new file mode 100644 index 0000000..ee05bda --- /dev/null +++ b/Assets/Scripts/Util/Lerper.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 33c8d5915d5e847eb9bbe018c2fdb457 +timeCreated: 1494978322 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: