Implement inventory slot switching in the inventory interface

This commit is contained in:
excitedneon 2017-05-17 03:27:49 +03:00
parent 7feff52f69
commit c892218560
8 changed files with 180 additions and 15 deletions

View File

@ -541,6 +541,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
ID: 0
Character: {fileID: 114052379458543858}
--- !u!143 &143753897266899886
CharacterController:
m_ObjectHideFlags: 1

View File

@ -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

View File

@ -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

View File

@ -98,12 +98,22 @@ namespace Cyber.Controls {
private List<Transform> ItemGridCells;
private List<MeshFilter> ItemGridCellMeshes;
private int ItemGridSelectedIndex;
private Transform GrabbedItem;
private int GrabbedItemIndex = -1;
private Color IconInventoryColor;
private Color IconStatusColor;
private Color IconSocialColor;
private Color IconMapColor;
/// <summary>
/// Whether the inventory is currently open and interactible.
/// </summary>
/// <returns><c>true</c> if this instance is open; otherwise, <c>false</c>.</returns>
public bool IsOpen() {
return InventoryOpen;
}
private void Start() {
int ItemGridSize = (int) ItemGridDimensions.x * (int) ItemGridDimensions.y;
ItemGridCells = new List<Transform>(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;

View File

@ -21,6 +21,11 @@ namespace Cyber.Controls {
/// </summary>
public Character Character;
/// <summary>
/// The inventory interface.
/// </summary>
public InventoryInterface InventoryInterface;
/// <summary>
/// The camera the player is seeing the world through.
/// </summary>
@ -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)")) {

View File

@ -104,6 +104,9 @@ namespace Cyber.Items {
/// <param name="idx1">The first index.</param>
/// <param name="idx2">The second index.</param>
public void SwitchSlots(int idx1, int idx2) {
if (idx1 == idx2) {
return;
}
Slot Slot1 = GetSlotAt(idx1);
Slot Slot2 = GetSlotAt(idx2);
SetSlotAt(idx1, Slot2);

View File

@ -0,0 +1,59 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Cyber.Util {
/// <summary>
/// Lerps stuff.
/// </summary>
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<Transform, PositionLerp> PositionLerps = new Dictionary<Transform, PositionLerp>();
/// <summary>
/// Sets the singleton.
/// </summary>
public Lerper() {
Singleton = this;
}
/// <summary>
/// Lerps the transform local position.
/// </summary>
/// <param name="transform">Transform.</param>
/// <param name="to">To.</param>
/// <param name="speed">Speed.</param>
public static void LerpTransformPosition(Transform transform, Vector3 to, float speed) {
if (Singleton != null) {
Singleton.PositionLerps[transform] = new PositionLerp(to, speed);
}
}
private void Update() {
List<Transform> RemoveThese = new List<Transform>();
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);
}
}
}
}

View File

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