2017-05-16 10:36:31 +02:00
|
|
|
|
|
|
|
|
|
using Cyber.Entities.SyncBases;
|
|
|
|
|
using Cyber.Networking.Clientside;
|
|
|
|
|
using Cyber.Networking.Messages;
|
|
|
|
|
|
|
|
|
|
namespace Cyber.Items {
|
|
|
|
|
|
2017-05-16 14:21:42 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles InventoryActions, building them on the client when needed and running them.
|
|
|
|
|
/// </summary>
|
2017-05-16 10:36:31 +02:00
|
|
|
|
public class InventoryActionHandler {
|
|
|
|
|
|
|
|
|
|
private Inventory Inventory;
|
|
|
|
|
private Character Character;
|
|
|
|
|
|
2017-05-16 14:21:42 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an <see cref="InventoryActionHandler"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="inventory"></param>
|
|
|
|
|
/// <param name="character"></param>
|
2017-05-16 10:36:31 +02:00
|
|
|
|
public InventoryActionHandler(Inventory inventory, Character character) {
|
|
|
|
|
Inventory = inventory;
|
|
|
|
|
Character = character;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 14:21:42 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Builds a <see cref="InventoryAction.Use"/> packet.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="slot">The equipped slot to use.</param>
|
|
|
|
|
/// <returns></returns>
|
2017-05-16 10:36:31 +02:00
|
|
|
|
public InventoryActionPkt BuildUseItem(EquipSlot slot) {
|
|
|
|
|
return new InventoryActionPkt(InventoryAction.Use, (int) slot);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 14:21:42 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Builds a <see cref="InventoryAction.Unequip"/> packet.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="slot">The equipped slot to clear.</param>
|
|
|
|
|
/// <returns></returns>
|
2017-05-16 10:36:31 +02:00
|
|
|
|
public InventoryActionPkt BuildClearSlot(EquipSlot slot) {
|
|
|
|
|
return new InventoryActionPkt(InventoryAction.Unequip, (int) slot);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 14:21:42 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Builds a <see cref="InventoryAction.Equip"/> packet.
|
|
|
|
|
/// </summary>
|
2017-05-16 19:35:38 +02:00
|
|
|
|
/// <param name="itemIdx">The item index to equip.</param>
|
2017-05-16 14:21:42 +02:00
|
|
|
|
/// <returns></returns>
|
2017-05-16 19:35:38 +02:00
|
|
|
|
public InventoryActionPkt BuildEquipItem(int itemIdx) {
|
|
|
|
|
return new InventoryActionPkt(InventoryAction.Equip, itemIdx);
|
2017-05-16 10:36:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-17 00:28:12 +02:00
|
|
|
|
public InventoryActionPkt BuildSlotSwitch(int switchFrom, int switchTo) {
|
|
|
|
|
return new InventoryActionPkt(InventoryAction.Switch, new int[]{ switchFrom, switchTo });
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 14:21:42 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles an <see cref="InventoryActionPkt"/> to handle. Ran on server and client.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="action">The <see cref="InventoryAction"/> to run</param>
|
|
|
|
|
/// <param name="relatedInt">The item related to the action.</param>
|
|
|
|
|
/// <returns>Weather the action failed or not.</returns>
|
2017-05-16 23:41:03 +02:00
|
|
|
|
public bool HandleAction(InventoryAction action, int[] intList) {
|
2017-05-16 10:36:31 +02:00
|
|
|
|
switch (action) {
|
|
|
|
|
case InventoryAction.Equip:
|
2017-05-16 23:41:03 +02:00
|
|
|
|
Inventory.Drive.EquipItem(intList[0]);
|
2017-05-16 10:36:31 +02:00
|
|
|
|
return true;
|
|
|
|
|
case InventoryAction.Unequip:
|
2017-05-16 23:41:03 +02:00
|
|
|
|
EquipSlot Slot = (EquipSlot) intList[0];
|
2017-05-16 19:35:38 +02:00
|
|
|
|
Inventory.Drive.UnequipSlot(Slot);
|
2017-05-16 10:36:31 +02:00
|
|
|
|
return true;
|
|
|
|
|
case InventoryAction.Use:
|
2017-05-16 23:41:03 +02:00
|
|
|
|
EquipSlot UseSlot = (EquipSlot) intList[0];
|
2017-05-16 19:35:38 +02:00
|
|
|
|
Item UseItem = Inventory.Drive.GetSlot(UseSlot);
|
2017-05-16 10:36:31 +02:00
|
|
|
|
if (UseItem != null && UseItem.Action != null && Character != null &&
|
|
|
|
|
(!Client.IsRunning() || Client.GetConnectedPlayer().Character != Character)) {
|
|
|
|
|
// Item exists, it has an action, and the character
|
|
|
|
|
// isn't controlled by the client (no double-actions).
|
|
|
|
|
UseItem.Action(Character);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2017-05-17 00:28:12 +02:00
|
|
|
|
case InventoryAction.Switch:
|
2017-05-17 00:44:28 +02:00
|
|
|
|
if (!Client.IsRunning() || Client.GetConnectedPlayer().Character != Character) {
|
|
|
|
|
Inventory.Drive.SwitchSlots(intList[0], intList[1]);
|
|
|
|
|
}
|
2017-05-17 00:28:12 +02:00
|
|
|
|
return true;
|
2017-05-16 10:36:31 +02:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|