Implement item actions, resolve #13

This commit is contained in:
excitedneon 2017-05-15 22:30:12 +03:00
parent c16988d3fe
commit f231252275
11 changed files with 109 additions and 14 deletions

View File

@ -507,6 +507,7 @@ MonoBehaviour:
MovementSpeed: 5 MovementSpeed: 5
InteractionDistance: 2 InteractionDistance: 2
CharacterController: {fileID: 143753897266899886} CharacterController: {fileID: 143753897266899886}
Inventory: {fileID: 114934786006823366}
Head: {fileID: 4900355877646882} Head: {fileID: 4900355877646882}
--- !u!114 &114629890146295716 --- !u!114 &114629890146295716
MonoBehaviour: MonoBehaviour:

View File

@ -5067,6 +5067,7 @@ MonoBehaviour:
MovementSpeed: 5 MovementSpeed: 5
InteractionDistance: 2 InteractionDistance: 2
CharacterController: {fileID: 143869468979164672} CharacterController: {fileID: 143869468979164672}
Inventory: {fileID: 114702404122310282}
Head: {fileID: 4883048021777868} Head: {fileID: 4883048021777868}
--- !u!114 &114599527678381310 --- !u!114 &114599527678381310
MonoBehaviour: MonoBehaviour:

View File

@ -182,13 +182,13 @@ namespace Cyber.Controls {
IconExplainerText.SetTextProperties(Props); IconExplainerText.SetTextProperties(Props);
IconInventory.material.SetColor("_EmissionColor", new Color(IconInventoryColor.r * InvBrightness, IconInventory.material.SetColor("_EmissionColor", new Color(IconInventoryColor.r * InvBrightness,
IconInventoryColor.g * InvBrightness, IconInventoryColor.b * InvBrightness)); IconInventoryColor.g * InvBrightness, IconInventoryColor.b * InvBrightness));
IconStatus.material.SetColor("_EmissionColor", new Color(IconStatusColor.r * StsBrightness, IconStatus.material.SetColor("_EmissionColor", new Color(IconStatusColor.r * StsBrightness,
IconStatusColor.g * StsBrightness, IconStatusColor.b * StsBrightness)); IconStatusColor.g * StsBrightness, IconStatusColor.b * StsBrightness));
IconSocial.material.SetColor("_EmissionColor", new Color(IconSocialColor.r * SclBrightness, IconSocial.material.SetColor("_EmissionColor", new Color(IconSocialColor.r * SclBrightness,
IconSocialColor.g * SclBrightness, IconSocialColor.b * SclBrightness)); IconSocialColor.g * SclBrightness, IconSocialColor.b * SclBrightness));
IconMap.material.SetColor("_EmissionColor", new Color(IconMapColor.r * MapBrightness, IconMap.material.SetColor("_EmissionColor", new Color(IconMapColor.r * MapBrightness,
IconMapColor.g * MapBrightness, IconMapColor.b * MapBrightness)); IconMapColor.g * MapBrightness, IconMapColor.b * MapBrightness));
} }
} else { } else {
@ -243,8 +243,8 @@ namespace Cyber.Controls {
if ((ItemGridSelector.position - ItemGridCells[i].position).magnitude < 0.01f) { if ((ItemGridSelector.position - ItemGridCells[i].position).magnitude < 0.01f) {
ItemGridSelector.position = ItemGridCells[i].position; ItemGridSelector.position = ItemGridCells[i].position;
} else { } else {
ItemGridSelector.position = ItemGridSelector.position =
Vector3.Lerp(ItemGridSelector.position, Vector3.Lerp(ItemGridSelector.position,
ItemGridCells[i].position, 20f * Time.deltaTime); ItemGridCells[i].position, 20f * Time.deltaTime);
} }
Vector3 NewRot = ItemGridSelector.localEulerAngles; Vector3 NewRot = ItemGridSelector.localEulerAngles;
@ -318,4 +318,4 @@ namespace Cyber.Controls {
toFix.transform.localScale = new Vector3(Scale, Scale, Scale); toFix.transform.localScale = new Vector3(Scale, Scale, Scale);
} }
} }
} }

View File

@ -6,6 +6,7 @@ using Cyber.Networking;
using Cyber.Networking.Messages; using Cyber.Networking.Messages;
using Cyber.Entities; using Cyber.Entities;
using Cyber.Util; using Cyber.Util;
using Cyber.Items;
namespace Cyber.Controls { namespace Cyber.Controls {
@ -49,15 +50,18 @@ namespace Cyber.Controls {
} }
// Interactions // Interactions
bool Interacted = false;
GameObject LookedAtObject = CameraUtil.GetLookedAtGameObject(Camera, Character.InteractionDistance); GameObject LookedAtObject = CameraUtil.GetLookedAtGameObject(Camera, Character.InteractionDistance);
if (LookedAtObject != null) { if (LookedAtObject != null) {
Interactable LookingAt = LookedAtObject.GetComponent<Interactable>(); Interactable LookingAt = LookedAtObject.GetComponent<Interactable>();
if (LookingAt != null && (LookingAt.transform.position - Character.GetPosition()).magnitude < Character.InteractionDistance) { if (LookingAt != null && (LookingAt.transform.position - Character.GetPosition()).magnitude < Character.InteractionDistance) {
if (Input.GetButtonDown("Activate")) { if (Input.GetButtonDown("Activate")) {
InteractWith(LookingAt, InteractionType.Activate); InteractWith(LookingAt, InteractionType.Activate);
Interacted = true;
} }
if (Input.GetButtonUp("Activate")) { if (Input.GetButtonUp("Activate")) {
InteractWith(LookingAt, InteractionType.Deactivate); InteractWith(LookingAt, InteractionType.Deactivate);
Interacted = true;
} }
if (LookedAtObject != LastLookedAt) { if (LookedAtObject != LastLookedAt) {
InteractWith(LookingAt, InteractionType.Enter); InteractWith(LookingAt, InteractionType.Enter);
@ -71,6 +75,20 @@ namespace Cyber.Controls {
InteractWith(LastLookedAt.GetComponent<Interactable>(), InteractionType.Exit); InteractWith(LastLookedAt.GetComponent<Interactable>(), InteractionType.Exit);
LastLookedAt = null; LastLookedAt = null;
} }
// Equipment actions
if (!Interacted) {
// 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));
}
if (Input.GetButtonDown("Use Item (L)")) {
Character.UseItemInSlot(EquipSlot.LeftHand);
Client.Send(PktType.InventoryAction, new InventoryActionPkt(InventoryAction.Use, (int) EquipSlot.LeftHand));
}
}
} else if (Character.Moving()) { } else if (Character.Moving()) {
// The debug console is open, stop the player. // The debug console is open, stop the player.
Character.Stop(); Character.Stop();

View File

@ -2,6 +2,7 @@
using UnityEngine; using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;
using Cyber.Networking.Clientside; using Cyber.Networking.Clientside;
using Cyber.Items;
namespace Cyber.Entities.SyncBases { namespace Cyber.Entities.SyncBases {
@ -25,6 +26,11 @@ namespace Cyber.Entities.SyncBases {
/// </summary> /// </summary>
public CharacterController CharacterController; public CharacterController CharacterController;
/// <summary>
/// The inventory of this player.
/// </summary>
public Inventory Inventory;
/// <summary> /// <summary>
/// The head transform for looking around. /// The head transform for looking around.
/// </summary> /// </summary>
@ -55,6 +61,16 @@ 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> /// <summary>
/// Sets the character's rotation. /// Sets the character's rotation.
/// </summary> /// </summary>

View File

@ -33,6 +33,7 @@ namespace Cyber.Entities.SyncBases {
if (Server.IsRunning()) { if (Server.IsRunning()) {
Drive.AddItem(ItemDB.Singleton.Get(0)); Drive.AddItem(ItemDB.Singleton.Get(0));
Drive.AddItem(ItemDB.Singleton.Get(1)); Drive.AddItem(ItemDB.Singleton.Get(1));
Drive.AddItem(ItemDB.Singleton.Get(2));
} }
} }

View File

@ -2,18 +2,23 @@
namespace Cyber.Items { namespace Cyber.Items {
/// <summary> /// <summary>
/// Represents an inventory action /// Represents an inventory action.
/// </summary> /// </summary>
public enum InventoryAction : byte { public enum InventoryAction : byte {
/// <summary> /// <summary>
/// Equip an item, given int is the ID of the item equipping /// Equip an item, given int is the ID of the item equipping.
/// </summary> /// </summary>
Equip, Equip,
/// <summary> /// <summary>
/// Unequip the given slot, removing anything that was inside of it. /// Unequip the given slot, removing anything that was inside of it.
/// </summary> /// </summary>
Unequip Unequip,
/// <summary>
/// Use the item in the given slot.
/// </summary>
Use
} }
} }

View File

@ -1,10 +1,12 @@
 using Cyber.Entities.SyncBases;
namespace Cyber.Items { namespace Cyber.Items {
/// <summary> /// <summary>
/// An item, containing itemmy information. /// An item, containing itemmy information.
/// </summary> /// </summary>
public class Item { public class Item {
public delegate void ItemAction(Character host);
/// <summary> /// <summary>
/// ID of the item, used in <see cref="ItemDB"/>. /// ID of the item, used in <see cref="ItemDB"/>.
@ -36,6 +38,11 @@ namespace Cyber.Items {
/// </summary> /// </summary>
public EquipSlot Slot; public EquipSlot Slot;
/// <summary>
/// The function that is ran when the item is used.
/// </summary>
public ItemAction Action;
/// <summary> /// <summary>
/// Creates an item. This should technically be only called by ItemDB, but it's public because of "reasons". /// Creates an item. This should technically be only called by ItemDB, but it's public because of "reasons".
/// </summary> /// </summary>
@ -45,13 +52,14 @@ namespace Cyber.Items {
/// <param name="weight">The Weight of the item.</param> /// <param name="weight">The Weight of the item.</param>
/// <param name="slot">The equip slot of the item.</param> /// <param name="slot">The equip slot of the item.</param>
/// <param name="description">The description of the item.</param> /// <param name="description">The description of the item.</param>
public Item(int id, int modelId, string name, float weight, EquipSlot slot, string description) { public Item(int id, int modelId, string name, float weight, EquipSlot slot, string description, ItemAction action = null) {
ID = id; ID = id;
ModelID = modelId; ModelID = modelId;
Name = name; Name = name;
Weight = weight; Weight = weight;
Slot = slot; Slot = slot;
Description = description; Description = description;
Action = action;
} }
/// <summary> /// <summary>
@ -59,7 +67,7 @@ namespace Cyber.Items {
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public Item Clone() { public Item Clone() {
return new Item(ID, ModelID, Name, Weight, Slot, Description); return new Item(ID, ModelID, Name, Weight, Slot, Description, Action);
} }
} }
} }

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using Cyber.Console;
namespace Cyber.Items { namespace Cyber.Items {
@ -20,7 +21,8 @@ namespace Cyber.Items {
/// </summary> /// </summary>
public ItemDB() { public ItemDB() {
AddItem(new Item(Counter++, 0, "Very Long Item Name", 1.5f, EquipSlot.Hat, "This item is a rare piece of the \"way too long of a name\" technology, invented by space goblins in ancient times.")); AddItem(new Item(Counter++, 0, "Very Long Item Name", 1.5f, EquipSlot.Hat, "This item is a rare piece of the \"way too long of a name\" technology, invented by space goblins in ancient times."));
AddItem(new Item(Counter++, 1, "Outworldly spherical tube", .5f, EquipSlot.RightHand, "It's so spherical and smooth that it seems like it's not even from this world!")); AddItem(new Item(Counter++, 1, "Outworldly Spherical Tube", .5f, EquipSlot.RightHand, "It's so spherical and smooth that it seems like it's not even from this world!"));
AddItem(new Item(Counter++, 0, "Cube Of Debuggery", 1.5f, EquipSlot.LeftHand, "Does some debuggery when you poke it.", (host) => { Term.Println("*DEBUGGERY*"); }));
} }
/// <summary> /// <summary>

View File

@ -288,6 +288,17 @@ namespace Cyber.Networking.Clientside {
EquipSlot Slot = (EquipSlot) InventoryActionPkt.RelatedInt; EquipSlot Slot = (EquipSlot) InventoryActionPkt.RelatedInt;
Inventory.Equipped.ClearSlot(Slot); Inventory.Equipped.ClearSlot(Slot);
break; 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;
} }
break; break;
default: default:

View File

@ -53,6 +53,38 @@ InputManager:
type: 0 type: 0
axis: 0 axis: 0
joyNum: 0 joyNum: 0
- serializedVersion: 3
m_Name: Use Item (R)
descriptiveName:
descriptiveNegativeName:
negativeButton:
positiveButton: left ctrl
altNegativeButton:
altPositiveButton: mouse 0
gravity: 1000
dead: 0.001
sensitivity: 1000
snap: 0
invert: 0
type: 0
axis: 0
joyNum: 0
- serializedVersion: 3
m_Name: Use Item (L)
descriptiveName:
descriptiveNegativeName:
negativeButton:
positiveButton: left alt
altNegativeButton:
altPositiveButton: mouse 1
gravity: 1000
dead: 0.001
sensitivity: 1000
snap: 0
invert: 0
type: 0
axis: 0
joyNum: 0
- serializedVersion: 3 - serializedVersion: 3
m_Name: Equip m_Name: Equip
descriptiveName: descriptiveName: