Files
TheAuction-GS/Assets/Player/PlayerController.cs

85 lines
2.2 KiB
C#

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 Object _object;
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;
_inputAsset.Enable();
}
private void OnDisable() {
if (_inputAsset == null) return;
_inputAsset.Disable();
}
private void Update() {
HandleMovementInput();
HandleInteraction();
}
private void HandleInteraction() {
if (_movementBehaviour == null)
return;
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;
}
}