Selling Functionality, Picking up items, Basic computer
This commit is contained in:
@@ -13,7 +13,7 @@ public class InteractionBehaviour : MonoBehaviour, IObjectParentHolder {
|
||||
|
||||
[SerializeField] private LayerMask _interactionLayer;
|
||||
|
||||
private EmptyCounter _lastInteractedObject = null;
|
||||
private GameObject _lastInteractedObject = null;
|
||||
|
||||
public event EventHandler<OnSelectedCounterArgs> OnSelectedCounter;
|
||||
|
||||
@@ -21,7 +21,7 @@ public class InteractionBehaviour : MonoBehaviour, IObjectParentHolder {
|
||||
private Object _heldObject;
|
||||
|
||||
public class OnSelectedCounterArgs : EventArgs {
|
||||
public EmptyCounter selectedCounter;
|
||||
public GameObject selectedCounter;
|
||||
}
|
||||
|
||||
void Start() {
|
||||
@@ -43,29 +43,32 @@ public class InteractionBehaviour : MonoBehaviour, IObjectParentHolder {
|
||||
RaycastHit hit;
|
||||
Debug.DrawRay(origin, transform.forward * _interactionDistance, Color.red);
|
||||
if (Physics.Raycast(origin, transform.forward, out hit, _interactionDistance, _interactionLayer)) {
|
||||
if (hit.transform.TryGetComponent(out EmptyCounter emptyCounter)) {
|
||||
if (_lastInteractedObject != hit.transform.gameObject) {
|
||||
SetSelectedCounter(emptyCounter);
|
||||
// Debug.Log("Hitting something!");
|
||||
GameObject hitObject = hit.transform.gameObject;
|
||||
//Check if the object has a component that has the IInteractable interface
|
||||
if (hitObject.TryGetComponent(out IInteractable interactable)) {
|
||||
if(_lastInteractedObject != hitObject) {
|
||||
setSelectedObject(hitObject);
|
||||
}
|
||||
}
|
||||
else {
|
||||
SetSelectedCounter(null);
|
||||
setSelectedObject(null);
|
||||
}
|
||||
}
|
||||
else {
|
||||
SetSelectedCounter(null);
|
||||
setSelectedObject(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Interact() {
|
||||
if (_lastInteractedObject == null) return;
|
||||
if (_lastInteractedObject.TryGetComponent(out EmptyCounter emptyCounter)) {
|
||||
emptyCounter.Interact();
|
||||
if (_lastInteractedObject.TryGetComponent(out IInteractable interactable)) {
|
||||
interactable.Interact();
|
||||
}
|
||||
}
|
||||
|
||||
private void SetSelectedCounter(EmptyCounter selectedCounter) {
|
||||
private void setSelectedObject(GameObject selectedCounter) {
|
||||
_lastInteractedObject = selectedCounter;
|
||||
OnSelectedCounter?.Invoke(this, new OnSelectedCounterArgs { selectedCounter = _lastInteractedObject });
|
||||
}
|
||||
|
||||
@@ -150,6 +150,7 @@ GameObject:
|
||||
- component: {fileID: 4625688497782346778}
|
||||
- component: {fileID: 3254711110533748919}
|
||||
- component: {fileID: 1167921562079878720}
|
||||
- component: {fileID: 5484389732031810624}
|
||||
m_Layer: 0
|
||||
m_Name: Player
|
||||
m_TagString: Untagged
|
||||
@@ -209,6 +210,7 @@ MonoBehaviour:
|
||||
type: 3}
|
||||
_interactionAction: {fileID: 3724582617646440488, guid: 0f54988b006d41d4eb3eb26ae11f51f9,
|
||||
type: 3}
|
||||
_holderTransform: {fileID: 2037988965739386903}
|
||||
--- !u!114 &1167921562079878720
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -225,6 +227,26 @@ MonoBehaviour:
|
||||
_interactionLayer:
|
||||
serializedVersion: 2
|
||||
m_Bits: 64
|
||||
_holdItemTransform: {fileID: 2037988965739386903}
|
||||
--- !u!114 &5484389732031810624
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 7365205897944721418}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 1e3fdca004f2d45fe8abbed571a8abd5, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
m_OverrideArea: 0
|
||||
m_Area: 0
|
||||
m_OverrideGenerateLinks: 0
|
||||
m_GenerateLinks: 0
|
||||
m_IgnoreFromBuild: 1
|
||||
m_ApplyToChildren: 1
|
||||
m_AffectedAgents: ffffffff
|
||||
--- !u!1 &9000961490938186306
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -1,30 +1,38 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class PlayerController : BasicCharacter {
|
||||
|
||||
public static PlayerController Instance { get; private set; }
|
||||
|
||||
|
||||
[SerializeField] private InputActionAsset _inputAsset;
|
||||
[SerializeField] private InputActionReference _movementAction;
|
||||
[SerializeField] private InputActionReference _interactionAction;
|
||||
|
||||
[SerializeField] private Transform _holderTransform;
|
||||
private HUDManager _hudManager;
|
||||
|
||||
private Object _object;
|
||||
|
||||
public void Start() {
|
||||
_hudManager = HUDManager.Instance;
|
||||
}
|
||||
|
||||
protected override void Awake() {
|
||||
base.Awake();
|
||||
if (_inputAsset == null || _interactionAction == null) return;
|
||||
|
||||
|
||||
_interactionAction.action.performed += ctx => _interactionBehaviour.Interact();
|
||||
|
||||
|
||||
if (Instance != null) {
|
||||
Debug.LogError("What? 2 players crazy!!!");
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void OnEnable() {
|
||||
if (_inputAsset == null || _interactionAction == null) return;
|
||||
@@ -37,49 +45,33 @@ public class PlayerController : BasicCharacter {
|
||||
}
|
||||
|
||||
private void Update() {
|
||||
HandleMovementInput();
|
||||
HandleInteraction();
|
||||
if (_hudManager == null) {
|
||||
Debug.LogError("No HUDManager found");
|
||||
return;
|
||||
}
|
||||
if (!_hudManager.isUiOpen) {
|
||||
HandleMovementInput();
|
||||
HandleInteraction();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void HandleInteraction() {
|
||||
if (_movementBehaviour == null)
|
||||
return;
|
||||
if(_interactionBehaviour == null)
|
||||
if (_interactionBehaviour == null)
|
||||
return;
|
||||
|
||||
|
||||
_interactionBehaviour.HandleInteraction();
|
||||
}
|
||||
|
||||
|
||||
void HandleMovementInput() {
|
||||
if (_movementBehaviour == null ||
|
||||
_movementAction == null)
|
||||
return;
|
||||
Vector2 movementInput = _movementAction.action.ReadValue<Vector2>();
|
||||
Vector3 desiredDirection = new Vector3(movementInput.x, 0, movementInput.y);
|
||||
|
||||
|
||||
_movementBehaviour.desiredMovementDirection = desiredDirection;
|
||||
_movementBehaviour.desiredLookDirection = desiredDirection;
|
||||
|
||||
}
|
||||
|
||||
//TODO: ask teacher if this is fine
|
||||
public Transform GetHolderTransform() {
|
||||
return _holderTransform;
|
||||
}
|
||||
|
||||
public void SetObject(Object obj) {
|
||||
_object = obj;
|
||||
}
|
||||
|
||||
public Object GetObject() {
|
||||
return _object;
|
||||
}
|
||||
|
||||
public void ClearObject() {
|
||||
_object = null;
|
||||
}
|
||||
|
||||
public bool HasObject() {
|
||||
return _object != null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user