added: item pickups with online persistency
This commit is contained in:
49
Assets/Scripts/Player/InteractionHandler.cs
Normal file
49
Assets/Scripts/Player/InteractionHandler.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using Reset.Items;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace Reset.Units{
|
||||
public class InteractionHandler : MonoBehaviour{
|
||||
private PlayerEnvironmentManager envManager;
|
||||
private Inventory inventory;
|
||||
private EnvironmentObserver observer;
|
||||
|
||||
void Awake(){
|
||||
envManager = GetComponent<PlayerEnvironmentManager>();
|
||||
inventory = GetComponent<Inventory>();
|
||||
|
||||
observer = envManager.FindObserverFromString("itemdrop");
|
||||
|
||||
observer.active = true;
|
||||
}
|
||||
|
||||
public void InteractWith(GameObject target){
|
||||
IInteractable interactable = target.GetComponent<IInteractable>();
|
||||
|
||||
if (interactable == null) {
|
||||
Debug.LogError("This item cannot be interacted with, it has no Interactable interface");
|
||||
return;
|
||||
}
|
||||
|
||||
if (target.GetComponent<ItemDrop>()) {
|
||||
inventory.AddToInventory(target.GetComponent<ItemDrop>().item);
|
||||
}
|
||||
|
||||
interactable.Interact();
|
||||
|
||||
// Don't do any actions that use the same button (hard set to Jump for now)
|
||||
Debug.Log($"Just collected the item, consuming the input @ {Time.time}.");
|
||||
GetComponent<PlayerControls>().SendInputBlock("Jump");
|
||||
}
|
||||
|
||||
void OnInteract(){
|
||||
CheckForInteraction();
|
||||
}
|
||||
|
||||
private void CheckForInteraction(){
|
||||
if (observer.active && observer.Evaluate(gameObject)) {
|
||||
InteractWith(observer.hit.transform.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Player/InteractionHandler.cs.meta
Normal file
11
Assets/Scripts/Player/InteractionHandler.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 96023d711211b9843b01db23192fa84c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: -101
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
31
Assets/Scripts/Player/Inventory.cs
Normal file
31
Assets/Scripts/Player/Inventory.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
using Reset.Items;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Reset.Units{
|
||||
public class Inventory : MonoBehaviour{
|
||||
public Weapon rangedWeapon;
|
||||
public Weapon meleeWeapon;
|
||||
|
||||
public Ability spellAbility1;
|
||||
public Ability spellAbility2;
|
||||
|
||||
public Ability toolAbility1;
|
||||
public Ability toolAbility2;
|
||||
|
||||
public List<Item> storedItems = new List<Item>(15);
|
||||
|
||||
void Start(){
|
||||
|
||||
}
|
||||
|
||||
public void AddToInventory(Item newItem){
|
||||
storedItems.Add(newItem);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update(){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Player/Inventory.cs.meta
Normal file
2
Assets/Scripts/Player/Inventory.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d57ab8ec1cbbdcf46813f268625d3494
|
||||
@@ -10,13 +10,15 @@ using ParadoxNotion;
|
||||
using Reset;
|
||||
using Sirenix.OdinInspector;
|
||||
using Unity.Cinemachine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
public class PlayerControls : MonoBehaviour{
|
||||
// References
|
||||
private Player thisPlayer;
|
||||
|
||||
private PlayerInput input;
|
||||
public SignalDefinition inputSignal;
|
||||
private SignalDefinition inputSignal;
|
||||
private SignalDefinition blockSignal;
|
||||
|
||||
// TODO: Turn these into accessors
|
||||
public Vector2 rawMoveInput;
|
||||
@@ -25,6 +27,21 @@ public class PlayerControls : MonoBehaviour{
|
||||
public GraphOwner graph;
|
||||
|
||||
void Awake(){
|
||||
|
||||
try {
|
||||
inputSignal = Resources.Load<SignalDefinition>("InputSignal");
|
||||
} catch (Exception e) {
|
||||
Debug.LogError($"Error finding the Input Signal defintion: {e.Message}");
|
||||
throw;
|
||||
}
|
||||
|
||||
try {
|
||||
blockSignal = Resources.Load<SignalDefinition>("BlockInputSignal");
|
||||
} catch (Exception e) {
|
||||
Debug.LogError($"Error finding the Input Signal defintion: {e.Message}");
|
||||
throw;
|
||||
}
|
||||
|
||||
thisPlayer = GetComponent<Player>();
|
||||
graph = GetComponent<GraphOwner>();
|
||||
input = GetComponent<PlayerInput>();
|
||||
@@ -34,29 +51,34 @@ public class PlayerControls : MonoBehaviour{
|
||||
|
||||
// Add the delegates for each method
|
||||
foreach (InputAction action in input.actions) {
|
||||
action.started += SendToGraph;
|
||||
action.canceled += SendToGraph;
|
||||
action.performed += SendToGraph;
|
||||
action.started += SendInputSignal;
|
||||
action.canceled += SendInputSignal;
|
||||
action.performed += SendInputSignal;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the delegates for each method
|
||||
void OnDisable(){
|
||||
foreach (InputAction action in input.actions) {
|
||||
action.started -= SendToGraph;
|
||||
action.canceled -= SendToGraph;
|
||||
action.performed -= SendToGraph;
|
||||
action.started -= SendInputSignal;
|
||||
action.canceled -= SendInputSignal;
|
||||
action.performed -= SendInputSignal;
|
||||
}
|
||||
}
|
||||
|
||||
// This will call the OnSignalInvoke for this type of Signal Defintion. CheckInput is the recieving end.
|
||||
public void SendToGraph(InputAction.CallbackContext ctx){
|
||||
// This will call the SignalInvoke for this type of Signal Defintion. CheckInput is the recieving end.
|
||||
public void SendInputSignal(InputAction.CallbackContext ctx){
|
||||
inputSignal.Invoke(transform, transform, false, new object[]{
|
||||
ctx.action,
|
||||
ctx.phase
|
||||
});
|
||||
}
|
||||
|
||||
public void SendInputBlock(string actionName){
|
||||
Debug.Log($"Sending block request...");
|
||||
blockSignal.Invoke(transform, transform, false, new object[]{actionName});
|
||||
}
|
||||
|
||||
public void OnMove(InputValue value){
|
||||
rawMoveInput.x = value.Get<Vector2>().x;
|
||||
rawMoveInput.y = value.Get<Vector2>().y;
|
||||
|
||||
Reference in New Issue
Block a user