Clean up InventoryActions a bit, close ##26

This commit is contained in:
Sofia 2017-05-16 11:36:31 +03:00
parent 427071c1d3
commit 6ac11277d6
9 changed files with 107 additions and 59 deletions

View File

@ -149,10 +149,10 @@ namespace Cyber.Controls {
Item Equipped = Inventory.Equipped.GetItem(SelectedItem.Slot);
if (Equipped != null && Equipped.ID == SelectedItem.ID) {
Inventory.Equipped.ClearSlot(SelectedItem.Slot);
Client.Send(PktType.InventoryAction, new InventoryActionPkt(InventoryAction.Unequip, (int) SelectedItem.Slot));
Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildClearSlot(SelectedItem.Slot));
} else {
Inventory.Equipped.SetSlot(SelectedItem.Slot, SelectedItem);
Client.Send(PktType.InventoryAction, new InventoryActionPkt(InventoryAction.Equip, SelectedItem.ID));
Client.Send(PktType.InventoryAction, Inventory.ActionHandler.BuildEquipItem(SelectedItem.ID));
}
}
}

View File

@ -81,12 +81,12 @@ namespace Cyber.Controls {
// Don't use equipment if you're interacting with something
// (ie. don't shoot at the buttons)
if (Input.GetButtonDown("Use Item (R)")) {
Character.UseItemInSlot(EquipSlot.RightHand);
Client.Send(PktType.InventoryAction, new InventoryActionPkt(InventoryAction.Use, (int) EquipSlot.RightHand));
Character.Inventory.UseItemInSlot(EquipSlot.RightHand);
Client.Send(PktType.InventoryAction, Character.Inventory.ActionHandler.BuildUseItem(EquipSlot.RightHand));
}
if (Input.GetButtonDown("Use Item (L)")) {
Character.UseItemInSlot(EquipSlot.LeftHand);
Client.Send(PktType.InventoryAction, new InventoryActionPkt(InventoryAction.Use, (int) EquipSlot.LeftHand));
Character.Inventory.UseItemInSlot(EquipSlot.LeftHand);
Client.Send(PktType.InventoryAction, Character.Inventory.ActionHandler.BuildUseItem(EquipSlot.LeftHand));
}
}
} else if (Character.Moving()) {

View File

@ -61,16 +61,6 @@ namespace Cyber.Entities.SyncBases {
}
}
/// <summary>
/// Uses the item in the left hand if something is equipped.
/// </summary>
public void UseItemInSlot(EquipSlot slot) {
Item Equipped = Inventory.Equipped.GetItem(slot);
if (Equipped != null && Equipped.Action != null) {
Equipped.Action(this);
}
}
/// <summary>
/// Sets the character's rotation.
/// </summary>

View File

@ -4,7 +4,6 @@ using Cyber.Items;
using Cyber.Networking;
using Cyber.Networking.Serverside;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace Cyber.Entities.SyncBases {
@ -24,6 +23,13 @@ namespace Cyber.Entities.SyncBases {
/// </summary>
public Equipped Equipped;
/// <summary>
/// The possible <see cref="Character"/> component associated with this Inventory. Used in <see cref="UseItemInSlot(EquipSlot)"/>.
/// </summary>
public Character Character;
public InventoryActionHandler ActionHandler;
/// <summary>
/// Creates the Inventory-component for a game object.
/// </summary>
@ -63,6 +69,16 @@ namespace Cyber.Entities.SyncBases {
return new SyncHandletype(true, 10);
}
/// <summary>
/// Uses the item in the left hand if something is equipped.
/// </summary>
public void UseItemInSlot(EquipSlot slot) {
Item Item = Equipped.GetItem(slot);
if (Item != null && Item.Action != null && Character != null) {
Item.Action(Character);
}
}
/// <summary>
/// Deserializes the ID's and creates them in the <see cref="Drive"/>.
/// </summary>
@ -144,5 +160,10 @@ namespace Cyber.Entities.SyncBases {
writer.WriteBytesFull(EquippedByteArray[2]);
writer.WriteBytesFull(EquippedByteArray[3]);
}
private void Start() {
Character = GetComponent<Character>();
ActionHandler = new InventoryActionHandler(this, Character);
}
}
}

View File

@ -20,5 +20,6 @@ namespace Cyber.Items {
/// Use the item in the given slot.
/// </summary>
Use
}
}

View File

@ -0,0 +1,57 @@

using Cyber.Entities.SyncBases;
using Cyber.Networking.Clientside;
using Cyber.Networking.Messages;
using UnityEngine;
namespace Cyber.Items {
public class InventoryActionHandler {
private Inventory Inventory;
private Character Character;
public InventoryActionHandler(Inventory inventory, Character character) {
Inventory = inventory;
Character = character;
}
public InventoryActionPkt BuildUseItem(EquipSlot slot) {
return new InventoryActionPkt(InventoryAction.Use, (int) slot);
}
public InventoryActionPkt BuildClearSlot(EquipSlot slot) {
return new InventoryActionPkt(InventoryAction.Unequip, (int) slot);
}
public InventoryActionPkt BuildEquipItem(int itemID) {
return new InventoryActionPkt(InventoryAction.Equip, itemID);
}
public bool HandleAction(InventoryAction action, int relatedInt) {
switch (action) {
case InventoryAction.Equip:
Item Item = ItemDB.Singleton.Get(relatedInt);
Inventory.Equipped.SetSlot(Item.Slot, Item);
return true;
case InventoryAction.Unequip:
EquipSlot Slot = (EquipSlot) relatedInt;
Inventory.Equipped.ClearSlot(Slot);
return true;
case InventoryAction.Use:
EquipSlot UseSlot = (EquipSlot) relatedInt;
Item UseItem = Inventory.Equipped.GetItem(UseSlot);
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;
}
return false;
}
}
}

View File

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

View File

@ -111,12 +111,15 @@ namespace Cyber.Networking.Clientside {
}
/// <summary>
/// Returns the connected player.
/// Returns the connected player, or null if no client is active.
/// </summary>
/// <returns>The connected player.</returns>
public static CConnectedPlayer GetConnectedPlayer() {
if (IsRunning()) {
return Singleton.Player;
}
return null;
}
/// <summary>
/// Properly shut down the client.
@ -279,27 +282,8 @@ namespace Cyber.Networking.Clientside {
break;
}
switch (InventoryActionPkt.Action) {
case InventoryAction.Equip:
Item Item = ItemDB.Singleton.Get(InventoryActionPkt.RelatedInt);
Inventory.Equipped.SetSlot(Item.Slot, Item);
break;
case InventoryAction.Unequip:
EquipSlot Slot = (EquipSlot) InventoryActionPkt.RelatedInt;
Inventory.Equipped.ClearSlot(Slot);
break;
case InventoryAction.Use:
EquipSlot UseSlot = (EquipSlot) InventoryActionPkt.RelatedInt;
Item UseItem = Inventory.Equipped.GetItem(UseSlot);
Character Character = CurrSyncBase.GetComponent<Character>();
if (UseItem != null && UseItem.Action != null && Character != null &&
Player.Character != Character) {
// Item exists, it has an action, and the character
// isn't controlled by the client (no double-actions).
UseItem.Action(Character);
}
break;
}
Inventory.ActionHandler.HandleAction(InventoryActionPkt.Action, InventoryActionPkt.RelatedInt);
break;
default:
Debug.LogError("Received an unknown packet, id: " + msg.msgType);

View File

@ -250,27 +250,10 @@ namespace Cyber.Networking.Serverside {
Inventory CurrInventory = Character.GetComponent<Inventory>();
InventoryActionPkt.SyncBaseID = CurrInventory.ID;
switch (InventoryActionPkt.Action) {
case InventoryAction.Equip:
Item Item = ItemDB.Singleton.Get(InventoryActionPkt.RelatedInt);
CurrInventory.Equipped.SetSlot(Item.Slot, Item);
break;
case InventoryAction.Unequip:
EquipSlot Slot = (EquipSlot) InventoryActionPkt.RelatedInt;
CurrInventory.Equipped.ClearSlot(Slot);
break;
case InventoryAction.Use:
EquipSlot UseSlot = (EquipSlot) InventoryActionPkt.RelatedInt;
Item UseItem = CurrInventory.Equipped.GetItem(UseSlot);
if (UseItem != null && UseItem.Action != null && Character != null) {
// Item exists, it has an action, and the character
// isn't controlled by the client (no double-actions).
UseItem.Action(Character);
}
break;
if (CurrInventory.ActionHandler.HandleAction(InventoryActionPkt.Action, InventoryActionPkt.RelatedInt)) {
SendToAll(PktType.InventoryAction, InventoryActionPkt);
}
SendToAll(PktType.InventoryAction, InventoryActionPkt);
break;
default:
Debug.LogError("Received an unknown packet, id: " + msg.msgType);